Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Jan 2, 2022
1 parent a9827bc commit 8eda34b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
23 changes: 20 additions & 3 deletions lib/src/main/kotlin/com/github/mnemotechnician/mkui/Layout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import mindustry.ui.*

private val tmpButtons = Seq<Button>(10); //used by buttonGroup

/** Adds a table to the group, passes it to the lamda and returns the created table. */
/** Adds a table constructed by a lambda to the group, passes it to the lamda and returns the created table. */
inline fun Group.addTable(background: Drawable = Styles.none, constructor: Table.() -> Unit = {}): Table {
return if (this is Table) {
this.addTable(background, constructor).get() //tables work differently
Expand All @@ -26,13 +26,29 @@ inline fun Group.addTable(background: Drawable = Styles.none, constructor: Table
}
}

/** Adds a table to the existing table and returns the created table cell */
/** Adds a table constructed by a lambda to the existing table and returns the created table cell */
inline fun Table.addTable(background: Drawable = Styles.none, constructor: Table.() -> Unit = {}): Cell<Table> {
val t = Table(background)
t.constructor()
return add(t)
}

/** Adds a collapser constructed by a lambda to the existing table and returns the created cell */
inline fun Table.addCollapser(background: Drawable = Styles.none, shown: Boolean = true, constructor: Table.() -> Unit = {}): Cell<Collapser> {
val table = Table(background)
table.constructor()

val col = Collapser(table, !shown)
return add(col)
}

/** Adds a collapser constructed by a lambda to the existing table and returns the created cell. Whether it's shown is determined by the lambda. */
inline fun Table.addCollapser(background: Drawable = Styles.none, crossinline shown: () -> Boolean = { true }, animate: Boolean = false, constructor: Table.() -> Unit = {}): Cell<Collapser> {
val cell = addCollapser(background, shown(), constructor)
cell.get().setCollapsed(animate) { !shown() }
return cell;
}

/** Adds a constant label to the table and returns the created cell */
inline fun Table.addLabel(text: String, style: Label.LabelStyle = Styles.defaultLabel, wrap: Boolean = false, ellipsis: String? = null): Cell<Label> {
val label = Label(text, style)
Expand Down Expand Up @@ -101,7 +117,8 @@ inline fun Table.addImage(crossinline provider: () -> Drawable): Cell<Image> {
return add(i)
}

/** Creates a toggle button constructed by a lambda and returns the created cell. The style MUST support checked state. Ontoggle is called whenever the button is toggled. */
/** Creates a toggle button constructed by a lambda and returns the created cell. Ontoggle is called whenever the button is toggled.
* @throws IllegalArgumentException when the providen style doesn't support checked state */
inline fun Table.toggleButton(constructor: Button.() -> Unit, toggleableStyle: Button.ButtonStyle = Styles.togglet, crossinline ontoggle: Button.(Boolean) -> Unit): Cell<Button> {
if (toggleableStyle.checked == null) throw IllegalArgumentException("This style does not support checked state!")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import com.github.mnemotechnician.mkui.*
/** A class that represents a floating on-screen window that the user can drag and interact with. */
abstract class Window {

/** The root of the window */
lateinit internal var rootTable: Table

/** The table this window is assigned to. Initialized when the window is created. */
lateinit var table: Table
internal set
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.mnemotechnician.mkui.windows

import arc.*
import arc.math.*
import arc.util.*
import arc.struct.*
import arc.scene.event.*
Expand All @@ -27,43 +28,62 @@ object WindowManager {
}

Events.run(EventType.Trigger.update) {
windows.each { it.onUpdate() }
windows.each {
//keep in stage
val root = it.rootTable
val pos = root.localToParentCoordinates(Tmp.v1.set(0f, 0f));

root.setPosition(
Mathf.clamp(pos.x, root.getPrefWidth() / 2, windowGroup.width - root.getPrefWidth() / 2),
Mathf.clamp(pos.y, root.getPrefHeight() / 2, windowGroup.height - root.getPrefHeight() / 2)
);

it.onUpdate()
}
}
}

/** Constructs & registers the window */
fun createWindow(window: Window) {
val windowTable = Table(Styles.black5).apply {
//tob bar — name, buttons and also a way to drag the table
lateinit var collapser: Collapser

window.rootTable = this

//top bar — name, buttons and also a way to drag the table
addTable(Styles.black3) {
addLabel({ window.name }, ellipsis = "...").fillX()

//collapse/show
textToggle("-", Styles.togglet) {
childAs<Label>(0).setText(if (it) "[accent]=" else "[accent]-")

collapser.toggle()
window.isCollapsed = it
window.onToggle(it)

TODO("collapsing is not yet implemented")
}

dragged { x, y ->
val oldPos = window.rootTable.localToParentCoordinates(Tmp.v1.set(x, y))
window.rootTable.setPosition(oldPos.x, oldPos.y)

window.onDrag()
TODO("dragging is not yet implemented")
}
}.fillX().marginBottom(5f)

row()

//main container
addTable {
collapser = addCollapser(animate = true) {
setClip(true)

window.table = this
window.onCreate()
}
}.pad(5f).get()
}

windowGroup.addChild(windowTable)
windowTable.setPosition(windowGroup.width / 2, windowGroup.height / 2)
windowTable.setPosition(Core.scene.width / 2, Core.scene.height / 2)

windows.add(window)
}
Expand Down

0 comments on commit 8eda34b

Please sign in to comment.