Skip to content

Commit

Permalink
yeah i can do that
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Jan 2, 2022
1 parent 0c9bfc9 commit a9827bc
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 9 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Mindustry Kotlin Ui Lib
An utility library for mindustry mods.
An ui library for mindustry mods.

Provides a set of inline extension functions that allow you to build mindustry ui in an idiomatic & optimized way.
Also provides some utility extension functions that arc lacks.
Provides some useful stuff, such as inline ui construction functions, utility functions, new ui classes, etc.

May not be compatible with other (non-mindustry) arc projects.

Expand All @@ -14,6 +13,8 @@ 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

Note: you can go to the releases tab and use the name of the latest release as the tag. These names usually start with `snapshot-`.

example of a `build.gradle` file:
```groovy
//... some other code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ import arc.scene.*
import arc.scene.ui.*
import arc.scene.ui.layout.*

/** 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;
inline fun <reified T> Element.asOrNull() = if (this is T) this else null;
6 changes: 4 additions & 2 deletions lib/src/main/kotlin/com/github/mnemotechnician/mkui/Layout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ 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.addLabel(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, 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): Cell<Label> {
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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.mnemotechnician.mkui.windows

import arc.scene.ui.layout.*
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 table this window is assigned to. Initialized when the window is created. */
lateinit var table: Table
internal set

/** Whether this window is collapsed by the user */
var isCollapsed = false
internal set

/** 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. */
fun onUpdate() {
}

/** Called whenever the window is being dragged by the user */
fun onDrag() {
}

/** Called whenever this window is being toggled by the user */
fun onToggle(collapsed: Boolean) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.github.mnemotechnician.mkui.windows

import arc.*
import arc.util.*
import arc.struct.*
import arc.scene.event.*
import arc.scene.ui.*
import arc.scene.ui.layout.*
import mindustry.*
import mindustry.ui.*
import mindustry.game.*
import com.github.mnemotechnician.mkui.*

/** Manages all windows displayed on the screen. Lazily initialized. */
object WindowManager {

internal val windowGroup = WidgetGroup()
val windows = Seq<Window>()

init {
Events.run(EventType.ClientLoadEvent::class.java) {
windowGroup.setFillParent(true)
windowGroup.touchable = Touchable.childrenOnly;
Core.scene.add(windowGroup)

Log.info("[blue]Initialized the window manager")
}

Events.run(EventType.Trigger.update) {
windows.each { 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
addTable(Styles.black3) {
addLabel({ window.name }, ellipsis = "...").fillX()
//collapse/show
textToggle("-", Styles.togglet) {
childAs<Label>(0).setText(if (it) "[accent]=" else "[accent]-")

window.isCollapsed = it
window.onToggle(it)

TODO("collapsing is not yet implemented")
}

dragged { x, y ->
window.onDrag()
TODO("dragging is not yet implemented")
}
}.fillX().marginBottom(5f)

row()

//main container
addTable {
window.table = this
window.onCreate()
}
}

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

windows.add(window)
}

/** Creates an anonymous window. Such a window won't be able to receive onUpdate, onToggle, onDrag events. */
inline fun createWindow(name: String, crossinline constructor: Table.() -> Unit) {
createWindow(object : Window() {
override var name = name

override fun onCreate() {
table.constructor()
}
})
}

}

0 comments on commit a9827bc

Please sign in to comment.