Skip to content

Commit

Permalink
new methods & improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Jan 3, 2022
1 parent 40add3b commit 8839daf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
26 changes: 17 additions & 9 deletions lib/src/main/kotlin/com/github/mnemotechnician/mkui/Buttons.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Contains utility button creation functions
*/
package com.github.mnemotechnician.mkui

import arc.scene.*
Expand All @@ -10,24 +13,26 @@ 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> {
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> {
inline fun Table.textButton(text: String, style: TextButton.TextButtonStyle = Styles.defaultt, wrap: Boolean = false, crossinline onclick: TextButton.() -> Unit = {}): Cell<TextButton> {
val b = TextButton(text, style)
b.clicked { b.onclick() }
b.label.setWrap(wrap)
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> {
inline fun Table.textButton(crossinline provider: () -> String, style: TextButton.TextButtonStyle = Styles.defaultt, wrap: Boolean = false, crossinline onclick: TextButton.() -> Unit = {}): Cell<TextButton> {
val b = TextButton(provider(), style)
b.clicked { b.onclick() }
b.label.setWrap(wrap)
b.update { b.setText(provider()) }
return add(b);
}
Expand All @@ -49,14 +54,14 @@ inline fun Table.imageButton(image: TextureRegion, style: ImageButton.ImageButto
/** 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() }
b.update { b.image.setDrawable(provider()) }
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> {
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
Expand All @@ -69,11 +74,14 @@ inline fun Table.toggleButton(constructor: Button.() -> Unit, toggleableStyle: B
}

/** 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)
inline fun Table.textToggle(text: String, toggleableStyle: Button.ButtonStyle = Styles.togglet, wrap: Boolean = false, crossinline ontoggle: Button.(Boolean) -> Unit = {}): Cell<Button> {
val cell = toggleButton({ addLabel(text) }, toggleableStyle, ontoggle)

cell.get().childAs<Label>(0).setWrap(wrap)
return cell
}

/** 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)
inline fun Table.imageToggle(text: Drawable, toggleableStyle: Button.ButtonStyle = Styles.clearTogglei, crossinline ontoggle: Button.(Boolean) -> Unit = {}): Cell<Button> {
return toggleButton({ addImage(text) }, toggleableStyle, ontoggle)
}
15 changes: 15 additions & 0 deletions lib/src/main/kotlin/com/github/mnemotechnician/mkui/Elements.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Contains utility element creation functions
*/
package com.github.mnemotechnician.mkui

import arc.scene.ui.*
Expand Down Expand Up @@ -42,3 +45,15 @@ inline fun Table.addImage(crossinline provider: () -> TextureRegion): Cell<Image
i.update { provider() }
return add(i)
}

inline fun Table.textField(text: String = "", style: TextField.TextFieldStyle = Styles.defaultField, crossinline onchange: TextField.(String) -> Unit = {}): Cell<TextField> {
val field = TextField(text, style)
field.changed { field.onchange(field.getText()) }
return add(field)
}

inline fun Table.textArea(text: String = "", style: TextField.TextFieldStyle = Styles.areaField, crossinline onchange: TextArea.(String) -> Unit = {}): Cell<TextArea> {
val area = TextArea(text, style)
area.changed { area.onchange(area.getText()) }
return add(area)
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ object WindowManager {
collapser.setCollapsed(it, true)
window.isCollapsed = it
window.onToggle(it)
}.size(30f)
}.size(40f)

//hide button
textButton("[red]X", Styles.togglet) {
this@apply.fadeRemove()
window.onDestroy()
}.size(30f).visible { window.closeable }
}.size(40f).visible { window.closeable }

//making it draggable
addListener(object : InputListener() {
Expand All @@ -105,19 +105,21 @@ object WindowManager {
window.isDragging = false;
}
})
}.fillX().pad(5f)
}.fillX().padLeft(5f).padRight(5f)

row()

//main container
collapser = addCollapser(true) {
setClip(true)
setBackground(Styles.black3)

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

window.onCreate()

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

0 comments on commit 8839daf

Please sign in to comment.