-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c9bfc9
commit a9827bc
Showing
5 changed files
with
126 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
lib/src/main/kotlin/com/github/mnemotechnician/mkui/windows/Window.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
lib/src/main/kotlin/com/github/mnemotechnician/mkui/windows/WindowManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
}) | ||
} | ||
|
||
} |