Skip to content

Commit

Permalink
[imgui] dsl stuff and organizing in one style
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaiR committed Dec 3, 2019
1 parent 839f53d commit e8175c1
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 5,5 @@ insert_final_newline = true

[*.{kt,kts}]
indent_size = 4
max_line_length = 140
max_line_length = 160
trim_trailing_whitespace = true
2 changes: 1 addition & 1 deletion src/main/kotlin/strongdmm/StrongDmm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 32,7 @@ class StrongDmm(title: String) : ImGuiWindow(title) {
// UIs
menuBarUi.process()
coordsPanelUi.process(windowWidth, windowHeight)
openedMapsPanelUi.process(windowWidth, windowHeight)
openedMapsPanelUi.process(windowWidth)
availableMapsDialogUi.process()
tilePopupUi.process()
editVarsDialogUi.process(windowWidth, windowHeight)
Expand Down
7 changes: 3 additions & 4 deletions src/main/kotlin/strongdmm/ui/AvailableMapsDialogUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 8,14 @@ import imgui.ImGui.openPopup
import imgui.ImGui.sameLine
import imgui.ImGui.setNextWindowSize
import imgui.ImGui.text
import imgui.WindowFlag
import imgui.dsl_.*
import strongdmm.controller.canvas.CanvasBlockStatus
import strongdmm.event.Event
import strongdmm.event.EventConsumer
import strongdmm.event.EventSender
import strongdmm.util.inline.AbsPath
import strongdmm.util.inline.RelPath
import imgui.WindowFlag as Wf

class AvailableMapsDialogUi : EventSender, EventConsumer {
private var isOpen: Boolean = false
Expand All @@ -33,10 33,10 @@ class AvailableMapsDialogUi : EventSender, EventConsumer {

setNextWindowSize(Vec2(600, 275), Cond.Once)

popupModal("Available Maps", null, Wf.NoResize.i) {
popupModal("Available Maps", null, WindowFlag.NoResize.i) {
text("Selected: ${selectionStatus.value}")

child("available_maps_list", Vec2(580, 200), true, Wf.HorizontalScrollbar.i) {
child("available_maps_list", Vec2(580, 200), true, WindowFlag.HorizontalScrollbar.i) {
sendEvent(Event.MapController.FetchAllAvailable { availableMaps ->
availableMaps.forEach { (abs, rel) ->
bullet()
Expand All @@ -53,7 53,6 @@ class AvailableMapsDialogUi : EventSender, EventConsumer {
sendEvent(Event.MapController.Open(selectedMapPath!!))
closePopup()
}

sameLine()
button("Cancel", ::closePopup)
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/strongdmm/ui/CoordsPanelUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 6,13 @@ import imgui.Cond
import imgui.ImGui.setNextWindowPos
import imgui.ImGui.setNextWindowSize
import imgui.ImGui.text
import imgui.WindowFlag
import imgui.dsl.window
import strongdmm.byond.dmm.Dmm
import strongdmm.byond.dmm.MapId
import strongdmm.event.Event
import strongdmm.event.EventConsumer
import strongdmm.util.OUT_OF_BOUNDS
import imgui.WindowFlag as Wf

class CoordsPanelUi : EventConsumer {
private var isHasMap: Boolean = false
Expand All @@ -37,7 37,7 @@ class CoordsPanelUi : EventConsumer {
setNextWindowPos(Vec2(windowWidth - 110, windowHeight - 40), Cond.Once)
setNextWindowSize(Vec2(100, 10))

window("coords_panel", flags = Wf.NoResize or Wf.NoTitleBar) {
window("coords_panel", flags = WindowFlag.NoResize or WindowFlag.NoTitleBar) {
if (xMapMousePos == OUT_OF_BOUNDS || yMapMousePos == OUT_OF_BOUNDS) {
text("out of bound")
} else {
Expand Down
26 changes: 12 additions & 14 deletions src/main/kotlin/strongdmm/ui/EnvironmentTreePanelUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 6,10 @@ import imgui.ImGui
import imgui.ImGui.sameLine
import imgui.ImGui.setNextWindowPos
import imgui.ImGui.setNextWindowSize
import imgui.ImGui.treeNodeEx
import imgui.ImGui.treePop
import imgui.MutableProperty0
import imgui.TreeNodeFlag
import imgui.dsl_.menuBar
import imgui.dsl_.window
import strongdmm.byond.*
import strongdmm.byond.dme.Dme
Expand All @@ -17,7 18,7 @@ import strongdmm.byond.dmi.GlobalDmiHolder
import strongdmm.byond.dmi.IconSprite
import strongdmm.event.Event
import strongdmm.event.EventConsumer
import strongdmm.util.LMB
import strongdmm.util.imgui.itemClicked

class EnvironmentTreePanelUi : EventConsumer {
private var currentEnv: Dme? = null
Expand All @@ -43,10 44,6 @@ class EnvironmentTreePanelUi : EventConsumer {
return@window
}

menuBar {
ImGui.checkbox("Show icons", isShowIcons)
}

createTreeNodes(currentEnv!!.getItem(TYPE_AREA)!!)
createTreeNodes(currentEnv!!.getItem(TYPE_TURF)!!)
createTreeNodes(currentEnv!!.getItem(TYPE_OBJ)!!)
Expand All @@ -64,24 61,25 @@ class EnvironmentTreePanelUi : EventConsumer {
}

if (dmeItem.children.isEmpty()) {
ImGui.treeNodeEx(treeNode.name, flags = TreeNodeFlag.Leaf or TreeNodeFlag.NoTreePushOnOpen or selectedFlag)
treeNodeEx(treeNode.name, flags = TreeNodeFlag.Leaf or TreeNodeFlag.NoTreePushOnOpen or selectedFlag)
} else {
if (ImGui.treeNodeEx(treeNode.name, flags = TreeNodeFlag.OpenOnArrow or TreeNodeFlag.OpenOnDoubleClick or selectedFlag)) {
if (ImGui.isItemClicked(LMB) && !isSelectedInCycle) {
selectedType = dmeItem.type
isSelectedInCycle = true
}
itemClicked { selectType(dmeItem.type) }

dmeItem.children.forEach { child ->
createTreeNodes(currentEnv!!.getItem(child)!!)
}

ImGui.treePop()
treePop()
}
}

if (ImGui.isItemClicked(LMB) && !isSelectedInCycle) {
selectedType = dmeItem.type
itemClicked { selectType(dmeItem.type) }
}

private fun selectType(type: String) {
if (!isSelectedInCycle) {
selectedType = type
isSelectedInCycle = true
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/strongdmm/ui/MenuBarUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 22,7 @@ class MenuBarUi : EventSender, EventConsumer {

init {
consumeEvent(Event.Global.ResetEnvironment::class.java, ::handleResetEnvironment)
consumeEvent(Event.Global.ActionStatusChanged::class.java, ::handleActionStatusChagnged)
consumeEvent(Event.Global.ActionStatusChanged::class.java, ::handleActionStatusChanged)
}

fun process() {
Expand Down Expand Up @@ -53,7 53,7 @@ class MenuBarUi : EventSender, EventConsumer {

private fun openEnvironment() {
NfdUtil.selectFile("dme")?.let { path ->
progressText = "Loading " path.value.substringAfterLast("\\")
progressText = "Loading " path.value.replace('\\', '/').substringAfterLast("/")
sendEvent(Event.EnvironmentController.Open(path) {
progressText = null
isEnvironmentOpen = it.isOpen()
Expand Down Expand Up @@ -89,7 89,7 @@ class MenuBarUi : EventSender, EventConsumer {
isEnvironmentOpen = false
}

private fun handleActionStatusChagnged(event: Event<ActionStatus, Unit>) {
private fun handleActionStatusChanged(event: Event<ActionStatus, Unit>) {
isUndoEnabled = event.body.hasUndoAction
isRedoEnabled = event.body.hasRedoAction
}
Expand Down
19 changes: 7 additions & 12 deletions src/main/kotlin/strongdmm/ui/OpenedMapsPanelUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 3,6 @@ package strongdmm.ui
import glm_.vec2.Vec2
import imgui.Col
import imgui.Cond
import imgui.ImGui.isItemHovered
import imgui.ImGui.sameLine
import imgui.ImGui.selectable
import imgui.ImGui.setNextWindowCollapsed
Expand All @@ -16,10 15,12 @@ import imgui.dsl.window
import imgui.dsl.withStyleColor
import strongdmm.event.Event
import strongdmm.event.EventSender
import strongdmm.util.RED32
import strongdmm.util.imgui.RED32
import strongdmm.util.imgui.itemAction
import strongdmm.util.imgui.itemHovered

class OpenedMapsPanelUi : EventSender {
fun process(windowWidth: Int, windowHeight: Int) {
fun process(windowWidth: Int) {
sendEvent(Event.MapController.FetchAllOpened { openedMaps ->
if (openedMaps.isEmpty()) {
return@FetchAllOpened
Expand All @@ -37,17 38,11 @@ class OpenedMapsPanelUi : EventSender {
sendEvent(Event.MapController.Close(map.id))
}
}

sameLine()

if (selectable(map.mapName, selectedMap == map) && selectedMap != map) {
(selectable(map.mapName, selectedMap == map) && selectedMap != map).itemAction {
sendEvent(Event.MapController.Switch(map.id))
}

if (isItemHovered()) {
tooltip {
text(map.relMapPath.value)
}
}.itemHovered {
tooltip { text(map.relMapPath.value) }
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/main/kotlin/strongdmm/ui/TilePopupUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 57,7 @@ class TilePopupUi : EventConsumer, EventSender {

image(sprite.textureId, ICON_SIZE, Vec2(sprite.u1, sprite.v1), Vec2(sprite.u2, sprite.v2))
sameLine()

menu("$name##$index") {
showTileItemOptions(tile, tileItem, index)
}

menu("$name##$index") { showTileItemOptions(tile, tileItem, index) }
sameLine()
text("[${tileItem.type}] ") // Two spaces in the end to make text not to overlap over the menu arrow.
}
Expand Down
31 changes: 11 additions & 20 deletions src/main/kotlin/strongdmm/ui/vars/EditVarsDialogUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 30,7 @@ import strongdmm.controller.canvas.CanvasBlockStatus
import strongdmm.event.Event
import strongdmm.event.EventConsumer
import strongdmm.event.EventSender
import strongdmm.util.GREEN32
import strongdmm.util.LMB
import strongdmm.util.imgui.*

class EditVarsDialogUi : EventSender, EventConsumer {
companion object {
Expand Down Expand Up @@ -76,9 75,7 @@ class EditVarsDialogUi : EventSender, EventConsumer {

window("Edit Variables: ${tileItem.type}##$WINDOW_ID") {
drawControls()

separator()

child("vars_table") {
drawVariables()
}
Expand All @@ -87,22 84,18 @@ class EditVarsDialogUi : EventSender, EventConsumer {
}

private fun drawControls() {
checkbox("##isShowModifiedVars", isShowModifiedVars)
if (ImGui.isItemHovered()) {
checkbox("##isShowModifiedVars", isShowModifiedVars).itemHovered {
tooltip { text("Show modified variables") }
}

sameLine()
if (inputText("##varsFilter", varsFilterRaw)) {
inputText("##varsFilter", varsFilterRaw).itemAction {
varsFilter = String(varsFilterRaw, 0, varsFilterRaw.strlen)
}

sameLine()
button("OK") {
applyModifiedVars()
dispose()
}

sameLine()
button("Cancel", block = ::dispose)
}
Expand Down Expand Up @@ -132,22 125,20 @@ class EditVarsDialogUi : EventSender, EventConsumer {
setNextItemWidth(ImGui.getColumnWidth(-1))

if (variable === currentEditVar) {
if (inputText("##${variable.name}", variable.buffer!!, InputTextFlag.EnterReturnsTrue.i)) {
inputText("##${variable.name}", variable.buffer!!, InputTextFlag.EnterReturnsTrue.i).itemAction {
currentEditVar?.stopEdit()
currentEditVar = null
}
} else {
alignTextToFramePadding()
text(variable.displayValue)

if (ImGui.isItemHovered()) {
ImGui.mouseCursor = MouseCursor.Hand
}

if (ImGui.isItemClicked(LMB)) {
currentEditVar?.stopEdit()
currentEditVar = variable
variable.startEdit()
text(variable.displayValue).with {
itemHovered { ImGui.mouseCursor = MouseCursor.Hand }
itemClicked {
currentEditVar?.stopEdit()
currentEditVar = variable
variable.startEdit()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
package strongdmm.util
package strongdmm.util.imgui

import imgui.COL32

Expand Down
26 changes: 26 additions & 0 deletions src/main/kotlin/strongdmm/util/imgui/dsl.kt
Original file line number Diff line number Diff line change
@@ -0,0 1,26 @@
package strongdmm.util.imgui

import imgui.ImGui
import strongdmm.util.LMB

inline fun Any.itemHovered(block: () -> Unit) {
if (ImGui.isItemHovered()) {
block()
}
}

inline fun Boolean.itemAction(block: () -> Unit) {
if (this) {
block()
}
}

inline fun Any.itemClicked(mouse: Int = LMB, block: () -> Unit) {
if (ImGui.isItemClicked(mouse)) {
block()
}
}

inline fun Any.with(block: () -> Unit) {
block()
}

0 comments on commit e8175c1

Please sign in to comment.