Skip to content

Commit

Permalink
use splitters
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Jan 5, 2022
1 parent 17ce310 commit 2b0d8b0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 49 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Mindustry Kotlin Ui Lib
An ui library for mindustry mods.
An ui library for mindustry kotlin mods.

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.

***Note: this library will only work with mods written in kotlin!***

Messy documentation: [mnemotechnician.github.io/mkui](https://mnemotechnician.github.io/mkui/)

# Note
Most of features provided by this library are supposed to be used in kotlin mods, as they have little to no usage in java (it lacks many features providen by kotlin)

If you (somewhy) want to use this library in a java mod, you will have to include the whole kotlin stdlib as an implementation dependency.

# Adding this dependency
In your `build.gradle` file:
* add `maven { url("https://jitpack.io") }` to the `repositories {}` block
Expand Down
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
Expand Up @@ -6,6 +6,7 @@ import arc.scene.style.*
import arc.graphics.*
import arc.graphics.g2d.*
import mindustry.ui.*
import mindustry.gen.*

/** 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> {
Expand Down Expand Up @@ -53,4 +54,18 @@ inline fun Table.textArea(text: String = "", style: TextField.TextFieldStyle = S
val area = TextArea(text, style)
area.changed { area.onchange(area.getText()) }
return add(area)
}

/** Creates a horizontal splitter and returns the created cell. This method automatically creates two rows. */
fun Table.hsplitter(color: Color = Color.white, padding: Float = 5f): Cell<Image> {
row()
val cell = addImage(Tex.whiteui)
row()
return cell.color(color).fillX().padTop(padding).padBottom(padding)
}

/** Creates a vertical splitter and returns the created cell. */
fun Table.vsplitter(color: Color = Color.white, padding: Float = 5f): Cell<Image> {
val cell = addImage(Tex.whiteui)
return cell.color(color).fillY().padLeft(padding).padRight(padding)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import arc.math.*
import arc.util.*
import arc.struct.*
import arc.input.*
import arc.graphics.*
import arc.scene.*
import arc.scene.actions.*
import arc.scene.event.*
import arc.scene.ui.*
import arc.scene.ui.layout.*
import mindustry.*
import mindustry.ui.*
import mindustry.gen.*
import mindustry.game.*
import com.github.mnemotechnician.mkui.*

Expand All @@ -26,6 +28,7 @@ object WindowManager {
internal var initialized = false

init {
//TODO: this will probably not be called if the mod hasn't accessed the manager before the game was loaded
Events.run(EventType.ClientLoadEvent::class.java) {
windowGroup.setFillParent(true)
windowGroup.touchable = Touchable.childrenOnly;
Expand Down Expand Up @@ -63,59 +66,77 @@ object WindowManager {

window.rootTable = this

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

//collapse/show
textToggle("[accent]-", Styles.togglet) {
childAs<Label>(0).setText(if (it) "[accent]=" else "[accent]-")
//top border
addImage(Tex.whiteui).fillX().colspan(3).row()

//left border
addImage(Tex.whiteui).fillY()

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

collapser.setCollapsed(it, true)
window.isCollapsed = it
window.onToggle(it)
}.size(40f)

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

//making it draggable
addListener(object : InputListener() {
var dragx = 0f
var dragy = 0f;
vsplitter(Color.black)

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

collapser.setCollapsed(it, true)
window.isCollapsed = it
window.onToggle(it)
}.size(40f)

override fun touchDown(event: InputEvent, x: Float, y: Float, pointer: Int, button: KeyCode): Boolean {
dragx = x; dragy = y;
window.isDragging = true;
return true;
}
//hide button
textButton("[red]X", Styles.togglet) {
this@apply.fadeRemove()
window.onDestroy()
}.size(40f).visible { window.closeable }

override fun touchDragged(event: InputEvent, x: Float, y: Float, pointer: Int) {
val pos = window.rootTable.localToParentCoordinates(Tmp.v1.set(x - dragx, y - dragy))
window.rootTable.setPosition(pos.x, pos.y)
//making it draggable
addListener(object : InputListener() {
var dragx = 0f
var dragy = 0f;

override fun touchDown(event: InputEvent, x: Float, y: Float, pointer: Int, button: KeyCode): Boolean {
dragx = x; dragy = y;
window.isDragging = true;
return true;
}

window.onDrag()
}
override fun touchDragged(event: InputEvent, x: Float, y: Float, pointer: Int) {
val pos = window.rootTable.localToParentCoordinates(Tmp.v1.set(x - dragx, y - dragy))
window.rootTable.setPosition(pos.x, pos.y)

window.onDrag()
}

override fun touchUp(e: InputEvent, x: Float, y: Float, pointer: Int, button: KeyCode) {
window.isDragging = false;
}
})
}.fillX()

hsplitter()

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

override fun touchUp(e: InputEvent, x: Float, y: Float, pointer: Int, button: KeyCode) {
window.isDragging = false;
}
})
}.fillX().padLeft(5f).padRight(5f)
window.table = this
}.grow().pad(5f).get()
}

row()
//right border
image(Tex.whiteui).fillY()

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

window.table = this
}.grow().pad(5f).get()
//bottom border
row()
image(Tex.whiteui).fillX().colspan(3)
}

window.onCreate()
Expand Down

0 comments on commit 2b0d8b0

Please sign in to comment.