Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Jan 1, 2022
1 parent c042435 commit 0c9bfc9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ In your `build.gradle` file:
(it should already be here if you're using the official mod template)
* add `implementation "com.github.mnemotechnician:MKUI:TAG"` (replace `TAG` with the the latest tag in this repo) to the `dependencies {}` block

example of a `build.gradle` file:
```groovy
//... some other code
repositories {
mavenCentral()
maven { url("https://jitpack.io") }
}
dependencies {
compileOnly "com.github.Anuken.Arc:arc-core:$mindustryVersion" //these two lines should
compileOnly "com.github.Anuken.Mindustry:core:$mindustryVersion" //already be here
implementation "com.github.mnemotechnician:mkui:snapshot-3"
}
//... other code
```

## in case you're using kotlin dls:
Ignore this paragraph if you're using the official kotlin mod template
* use `maven("https://jitpack.io")` in the first step
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ import arc.scene.*
import arc.scene.ui.*
import arc.scene.ui.layout.*

//todo
/** casts the element to the specified class */
inline fun <reified T> Element.to() = this as T;

/** casts the element to the specified class or returns null if it's not an instance of this class */
inline fun <reified T> Element.toOrNull() = if (this is T) this else null;
51 changes: 47 additions & 4 deletions lib/src/main/kotlin/com/github/mnemotechnician/mkui/Layout.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/**
* 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 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) {
Expand All @@ -27,22 +34,22 @@ inline fun Table.addTable(background: Drawable = Styles.none, constructor: Table
}

/** Adds a constant label to the table and returns the created cell */
inline fun Table.label(text: String, style: Label.LabelStyle = Styles.defaultLabel, wrap: Boolean = false): Cell<Label> {
inline fun Table.addLabel(text: String, style: Label.LabelStyle = Styles.defaultLabel, wrap: Boolean = false): Cell<Label> {
val label = Label(text, style)
label.setWrap(wrap)
return add(label)
}

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

/** Adds a custom button constructed by a lambda and returns the created cell */
inline fun Table.customButton(style: Button.ButtonStyle = Styles.defaultb, crossinline onclick: Button.() -> Unit, constructor: Button.() -> Unit): Cell<Button> {
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()
Expand Down Expand Up @@ -90,4 +97,40 @@ inline fun Table.addImage(crossinline provider: () -> Drawable): Cell<Image> {
val i = Image(provider())
i.update { provider() }
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. */
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)
}

/** 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)
}

0 comments on commit 0c9bfc9

Please sign in to comment.