Skip to content

Commit

Permalink
restructured
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Jan 2, 2022
1 parent 413815c commit ce7a347
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 158 deletions.
File renamed without changes.
79 changes: 79 additions & 0 deletions lib/src/main/kotlin/com/github/mnemotechnician/mkui/Buttons.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.github.mnemotechnician.mkui

import arc.scene.*
import arc.scene.ui.*
import arc.scene.ui.layout.*
import arc.scene.style.*
import arc.struct.*
import arc.graphics.*
import arc.graphics.g2d.*
import mindustry.ui.*

/** Adds a custom button constructed by a lambda and returns the created cell */
inline fun Table.customButton(constructor: Button.() -> Unit, style: Button.ButtonStyle = Styles.defaultb, crossinline onclick: Button.() -> Unit): Cell<Button> {
val b = Button(style)
b.clicked { b.onclick() }
b.constructor()
return add(b)
}

/** Adds a text button with an optional onclick listener, returns the created cell */
inline fun Table.textButton(text: String, style: TextButton.TextButtonStyle = Styles.defaultt, crossinline onclick: TextButton.() -> Unit = {}): Cell<TextButton> {
val b = TextButton(text, style)
b.clicked { b.onclick() }
return add(b)
}

/** Adds a text button with a dynamic label and an optional onclick listener, returns the created cell */
inline fun Table.textButton(crossinline provider: () -> String, style: TextButton.TextButtonStyle = Styles.defaultt, crossinline onclick: TextButton.() -> Unit = {}): Cell<TextButton> {
val b = TextButton(provider(), style)
b.clicked { b.onclick() }
b.update { b.setText(provider()) }
return add(b);
}

/** Adds an image button with an optional onclick listener, returns the created cell */
inline fun Table.imageButton(image: Drawable, style: ImageButton.ImageButtonStyle = Styles.defaulti, crossinline onclick: ImageButton.() -> Unit = {}): Cell<ImageButton> {
val b = ImageButton(image, style)
b.clicked { b.onclick() }
return add(b)
}

/** Adds an image button with an optional onclick listener, returns the created cell */
inline fun Table.imageButton(image: TextureRegion, style: ImageButton.ImageButtonStyle = Styles.defaulti, crossinline onclick: ImageButton.() -> Unit = {}): Cell<ImageButton> {
val b = ImageButton(image, style)
b.clicked { b.onclick() }
return add(b)
}

/** Adds an image button with a dynamic image and an optional onclick listener, returns the created cell */
inline fun Table.imageButton(crossinline provider: () -> TextureRegion, style: ImageButton.ImageButtonStyle = Styles.defaulti, crossinline onclick: ImageButton.() -> Unit = {}): Cell<ImageButton> {
val b = ImageButton(provider(), style)
b.update { b.image.setDrawable(provider()) }
b.clicked { b.onclick() }
return add(b)
}

/** 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!")

var toggled = false //funny arc ui stuff
val cell = customButton(constructor, toggleableStyle) {
toggled = !toggled
ontoggle(toggled)
}
cell.update { it.setChecked(toggled) }
return cell;
}

/** Simmilar to toggleButton but adds a constant label */
inline fun Table.textToggle(text: String, toggleableStyle: Button.ButtonStyle = Styles.togglet, crossinline onclick: Button.(Boolean) -> Unit): Cell<Button> {
return toggleButton({ addLabel(text) }, toggleableStyle, onclick)
}

/** Simmilar to toggleButton but adds a constant image */
inline fun Table.imageToggle(text: Drawable, toggleableStyle: Button.ButtonStyle = Styles.clearTogglei, crossinline onclick: Button.(Boolean) -> Unit): Cell<Button> {
return toggleButton({ addImage(text) }, toggleableStyle, onclick)
}
44 changes: 44 additions & 0 deletions lib/src/main/kotlin/com/github/mnemotechnician/mkui/Elements.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.mnemotechnician.mkui

import arc.scene.ui.*
import arc.scene.ui.layout.*
import arc.scene.style.*
import arc.graphics.*
import arc.graphics.g2d.*
import mindustry.ui.*

/** 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)
label.setWrap(wrap)
label.setEllipsis(ellipsis)
return add(label)
}

/** Adds a dynamic label to the table and returns the created cell */
inline fun Table.addLabel(crossinline provider: () -> String, style: Label.LabelStyle = Styles.defaultLabel, wrap: Boolean = true, ellipsis: String? = null): Cell<Label> {
val label = Label("", style)
label.setWrap(wrap)
label.setEllipsis(ellipsis)
label.update { label.setText(provider()) }
return add(label)
}

/** Adds a constant image to the table and returns the created cell */
inline fun Table.addImage(drawable: Drawable): Cell<Image> {
val i = Image(drawable)
return add(i)
}

/** Adds a constant image to the table and returns the created cell */
inline fun Table.addImage(drawable: TextureRegion): Cell<Image> {
val i = Image(drawable)
return add(i)
}

/** Adds a dynamic image to the table and returns the created cell */
inline fun Table.addImage(crossinline provider: () -> TextureRegion): Cell<Image> {
val i = Image(provider())
i.update { provider() }
return add(i)
}
63 changes: 63 additions & 0 deletions lib/src/main/kotlin/com/github/mnemotechnician/mkui/Groups.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Contains some utility layout construction functions
* Function names were made distinct in order to avoid ambiguity
*/
package com.github.mnemotechnician.mkui

import arc.scene.*
import arc.scene.ui.*
import arc.scene.ui.layout.*
import arc.scene.style.*
import arc.struct.*
import arc.graphics.*
import mindustry.ui.*

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

/** 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
} else {
val t = Table(background)
t.constructor()
addChild(t)
t
}
}

/** 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(shown: Boolean = true, background: Drawable = Styles.none, 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(crossinline shown: () -> Boolean = { true }, background: Drawable = Styles.none, animate: Boolean = false, constructor: Table.() -> Unit = {}): Cell<Collapser> {
val cell = addCollapser(shown(), background, constructor)
cell.get().setCollapsed(animate) { !shown() }
return cell;
}

/** Creates a table and a button group, calls the constructor and passes this table to it, adds all created buttons into the same group. Adds the table and returns the created cell. */
inline fun Table.buttonGroup(background: Drawable = Styles.none, constructor: Table.(ButtonGroup<Button>) -> Unit): Cell<Table> {
val group = ButtonGroup<Button>()
val table = Table(background)
table.constructor(group)

//find all buttons and add them to the group
table.children.each {
if (it is Button) group.add(it)
}
return add(table)
}
155 changes: 0 additions & 155 deletions lib/src/main/kotlin/com/github/mnemotechnician/mkui/Layout.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ abstract class Window {
var isDragging = false
internal set

/** Name of this window displayed in the top bar */
/** Name of this window, displayed in the top bar */
open var name = "unnamed window"

/** Called when the window is being created. At this point the window has a Table assigned to it, which should be inflated by this function. */
abstract fun onCreate()

/** The window has already been created, this function is called on every tick. It should avoid modifying the table: that can cause a performance loss. */
/** The window has already been created, this function is called on every tick. You should avoid modifying the table from this function: that can cause a performance loss. */
fun onUpdate() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object WindowManager {

/** Constructs & registers the window */
fun createWindow(window: Window) {
val windowTable = Table(Styles.black5).apply {
val windowTable = Table(Styles.black6).apply {
lateinit var collapser: Collapser

window.rootTable = this
Expand Down

0 comments on commit ce7a347

Please sign in to comment.