Skip to content

Commit 48a180f

Browse files
committed
Window content layout
1 parent 52483d3 commit 48a180f

File tree

9 files changed

+213
-147
lines changed

9 files changed

+213
-147
lines changed

common/src/main/kotlin/com/lambda/module/modules/client/NewCGui.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.lambda.module.Module
44
import com.lambda.module.tag.ModuleTag
55
import com.lambda.newgui.LambdaScreen.Companion.gui
66
import com.lambda.newgui.LambdaScreen.Companion.toScreen
7-
import com.lambda.newgui.component.window.Window
87
import com.lambda.newgui.component.window.Window.Companion.window
98
import com.lambda.util.math.Vec2d
109

@@ -15,12 +14,15 @@ object NewCGui : Module(
1514
) {
1615
val titleBarHeight by setting("Title Bar Height", 4.0, 0.0..10.0, 0.1)
1716
val padding by setting("Padding", 2.0, 0.0..6.0, 0.1)
17+
val listStep by setting("List Step", 2.0, 0.0..6.0, 0.1)
1818

1919
private val clickGuiLayout =
2020
gui {
2121
window(position = Vec2d.ONE * 20.0, title = "Test window") {
22-
content.window(Vec2d.ONE * 5.0, Vec2d.ONE * 60.0) {
22+
repeat(6) {
23+
window(Vec2d.ONE * 5.0, Vec2d.ONE * 60.0) {
2324

25+
}
2426
}
2527
}
2628
}

common/src/main/kotlin/com/lambda/newgui/component/core/IListEntry.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.

common/src/main/kotlin/com/lambda/newgui/component/core/TextField.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ class TextField(
2121

2222
var offset = initialOffset
2323

24-
// Let user interact through the text
25-
override val interactionPassthrough = true
26-
2724
init {
25+
properties.interactionPassthrough = true
2826
verticalAlignment = VAlign.CENTER
2927
rectUpdate(owner::rect)
3028

common/src/main/kotlin/com/lambda/newgui/component/layout/Layout.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ open class Layout(
4747
/**
4848
* The size of this component
4949
*/
50-
open var size = Vec2d.ZERO
50+
var size = Vec2d.ZERO
5151

5252
/**
5353
* Horizontal alignment
@@ -81,7 +81,7 @@ open class Layout(
8181
*/
8282
var position: Vec2d
8383
get() = ownerRect.leftTop + relativeToAbs(relativePos).let {
84-
if (!clampPosition) it
84+
if (!properties.clampPosition) it
8585
else it.coerceIn(
8686
0.0, ownerRect.size.x - size.x,
8787
0.0, ownerRect.size.y - size.y
@@ -95,6 +95,11 @@ open class Layout(
9595
private fun relativeToAbs(posIn: Vec2d) = posIn + dockingOffset
9696
private fun absToRelative(posIn: Vec2d) = posIn - dockingOffset
9797

98+
/**
99+
* Configurable properties of the component
100+
*/
101+
val properties = LayoutProperties()
102+
98103
// Structure
99104
val children = mutableListOf<Layout>()
100105
private var selectedChild: Layout? = null
@@ -104,7 +109,6 @@ open class Layout(
104109
private val isHovered: Boolean get() = mousePosition in rect && (owner?.isHovered ?: true)
105110

106111
// Graphics
107-
val animation = AnimationTicker()
108112
val renderer: RenderLayer = run {
109113
owner?.let { owner ->
110114
if (!useBatching || !owner.batchChildren) {
@@ -120,9 +124,6 @@ open class Layout(
120124

121125
private var owningRenderer = false
122126

123-
protected open val interactionPassthrough = false
124-
protected open val clampPosition = false
125-
126127
// Actions
127128
private var showActions = mutableListOf<() -> Unit>()
128129
private var hideActions = mutableListOf<() -> Unit>()
@@ -235,15 +236,15 @@ open class Layout(
235236

236237
// Select an element that's on foreground
237238
selectedChild = if (isHovered) children.lastOrNull {
238-
!it.interactionPassthrough && mousePosition in it.rect
239+
!it.properties.interactionPassthrough && mousePosition in it.rect
239240
} else null
240241

241242
// Update children
242243
children.forEach { child ->
243244
if (e is GuiEvent.Render) return@forEach
244245

245246
if (e is GuiEvent.MouseClick) {
246-
val hovered = child == selectedChild || (child.isHovered && child.interactionPassthrough)
247+
val hovered = child == selectedChild || (child.isHovered && child.properties.interactionPassthrough)
247248
val newAction = if (hovered) e.action else Mouse.Action.Release
248249

249250
val newEvent = GuiEvent.MouseClick(e.button, newAction, e.mouse)
@@ -257,7 +258,7 @@ open class Layout(
257258
when (e) {
258259
is GuiEvent.Show -> { mousePosition = Vec2d.ONE * -1000.0; showActions.forEach { it() } }
259260
is GuiEvent.Hide -> { hideActions.forEach { it() } }
260-
is GuiEvent.Tick -> { animation.tick(); tickActions.forEach { it() } }
261+
is GuiEvent.Tick -> { tickActions.forEach { it() } }
261262
is GuiEvent.KeyPress -> { keyPressActions.forEach { it(e.key) } }
262263
is GuiEvent.CharTyped -> { charTypedActions.forEach { it((e.char)) } }
263264
is GuiEvent.MouseMove -> { mousePosition = e.mouse; mouseMoveActions.forEach { it(e.mouse) } }
@@ -283,9 +284,13 @@ open class Layout(
283284
renderer.render()
284285
}
285286

286-
scissor(rect) { // ToDo: merge to ListLayout
287+
val postAction = {
287288
post.forEach { it.onEvent(e) }
288289
}
290+
291+
if (properties.scissorChildren) {
292+
scissor(rect, postAction)
293+
} else postAction()
289294
}
290295
}
291296
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.lambda.newgui.component.layout
2+
3+
class LayoutProperties {
4+
/**
5+
* If true, interactions pass through to elements beneath this one.
6+
*/
7+
var interactionPassthrough = false
8+
9+
/**
10+
* If true, this element's rectangle is clamped within parent's bounds.
11+
*/
12+
var clampPosition = false
13+
14+
/**
15+
* If true, children using their own render layer are clipped within this rect.
16+
*/
17+
var scissorChildren = false
18+
}

common/src/main/kotlin/com/lambda/newgui/component/layout/ListLayout.kt

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.lambda.newgui.component.window
2+
3+
import com.lambda.newgui.component.HAlign
4+
import com.lambda.newgui.component.core.TextField.Companion.textField
5+
import com.lambda.newgui.component.core.UIBuilder
6+
import com.lambda.newgui.component.layout.Layout
7+
import com.lambda.util.Mouse
8+
import com.lambda.util.math.Rect
9+
import com.lambda.util.math.Vec2d
10+
11+
/**
12+
* Represents a titlebar component
13+
*/
14+
class TitleBar(
15+
owner: Window,
16+
title: String,
17+
drag: Boolean
18+
) : Layout(owner, true, true) {
19+
val textField = textField(title) {
20+
horizontalAlignment = HAlign.CENTER
21+
}
22+
23+
private var dragOffset: Vec2d? = null
24+
25+
init {
26+
rectUpdate {
27+
Rect(owner.rect.leftTop, owner.rect.rightTop + Vec2d(0.0, renderer.font.getHeight() * 1.5))
28+
}
29+
30+
if (drag) {
31+
onShow {
32+
dragOffset = null
33+
}
34+
35+
onMouseClick { button: Mouse.Button, action: Mouse.Action ->
36+
dragOffset = if (button == Mouse.Button.Left && action == Mouse.Action.Click) {
37+
mousePosition - owner.position
38+
} else null
39+
}
40+
41+
onMouseMove { mouse ->
42+
dragOffset?.let { drag ->
43+
owner.position = mouse - drag
44+
}
45+
}
46+
}
47+
}
48+
49+
companion object {
50+
@UIBuilder
51+
fun Window.titleBar(
52+
text: String,
53+
drag: Boolean
54+
) = TitleBar(this, text, drag).apply(children::add)
55+
}
56+
}

0 commit comments

Comments
 (0)