Skip to content

Commit

Permalink
group widget and stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Hobbyshop committed Apr 26, 2024
1 parent 830ec05 commit 59e0bc4
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/framework/Widget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,55 @@ abstract class LeafWidget : Widget() {
voidUI.eventHandler.unregister(this)
}

}

/**
* A special type of widget which contains multiple child widgets instead of just one.
*/
abstract class GroupWidget : Widget() {

/**
* The children of the group widget.
*/
protected lateinit var roots: Array<Widget>

override fun init(screen: Screen, parent: Widget) {
this.voidUI = screen.voidUI
this.screen = screen

roots = buildGroup()
roots.forEach { it.init(screen, this) }
}

/**
* Since it is not clear how group widgets position their children, they need to define the layout themselves.
*/
abstract override fun layout(constraints: BoxConstraints)

override fun postLayoutInit(parentOffset: Offset, parent: Widget) {
offset = parentOffset

val renderable = createRenderObject()
if (renderable != null)
screen.renderStack.push(renderable)

roots.forEach { it.postLayoutInit(parentOffset, this) }
}

override fun remove() {
roots.forEach { it.remove() }
}

/**
* Returns an array of widgets, which build the widget tree.
*/
abstract fun buildGroup(): Array<Widget>

/**
* The build method from the widget is not called on group widgets.
*/
override fun build(): Widget {
return Placeholder()
}

}
32 changes: 32 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/widgets/Stack.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.neptuneclient.voidui.widgets

import com.neptuneclient.voidui.framework.BoxConstraints
import com.neptuneclient.voidui.framework.GroupWidget
import com.neptuneclient.voidui.framework.Size
import com.neptuneclient.voidui.framework.Widget
import kotlin.math.max

/**
* A widget which layers multiple widgets on top of each other.
*
* @param children The array of children in the stack.
*/
class Stack(private val children: Array<Widget>) : GroupWidget() {

override fun layout(constraints: BoxConstraints) {
var childWidth = 0f
var childHeight = 0f

children.forEach {
it.layout(constraints)
childWidth = max(childWidth, it.size.width)
childHeight = max(childHeight, it.size.height)
}
size = constraints.constrain(Size(childWidth, childHeight))
}

override fun buildGroup(): Array<Widget> {
return children
}

}
25 changes: 24 additions & 1 deletion src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import com.neptuneclient.voidui.VoidUI
import com.neptuneclient.voidui.event.MouseClickedEvent
import com.neptuneclient.voidui.event.MouseReleasedEvent
import com.neptuneclient.voidui.framework.Screen
import com.neptuneclient.voidui.framework.Size
import com.neptuneclient.voidui.framework.Widget
import com.neptuneclient.voidui.objects.Border
import com.neptuneclient.voidui.objects.EdgeInsets
import com.neptuneclient.voidui.utils.image
import com.neptuneclient.voidui.widgets.*
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable
import org.lwjgl.glfw.GLFW
Expand All @@ -15,7 +19,26 @@ import java.awt.Color
class TestScreen(voidUI: VoidUI) : Screen(voidUI) {

override fun build(): Widget {
return Placeholder()
return Container(
margin = EdgeInsets.all(100f),
border = Border(2f, Color.RED),
child = Stack(
children = arrayOf(
Image(
src = image("images/hampter.png"),
imageSize = Size(
width = 200f,
height = 200f
)
),
Container(
color = Color.GREEN,
padding = EdgeInsets.symmetric(horizontal = 120f),
child = Text("Hello World!")
)
)
)
)
}

}
Expand Down

0 comments on commit 59e0bc4

Please sign in to comment.