aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/macros/MacroUI.kt
blob: 17fdd0a4a40e0ec9680df619b3cbf151c017ecab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package moe.nea.firmament.features.macros

import io.github.notenoughupdates.moulconfig.gui.CloseEventListener
import io.github.notenoughupdates.moulconfig.observer.ObservableList
import io.github.notenoughupdates.moulconfig.xml.Bind
import moe.nea.firmament.annotations.Subscribe
import moe.nea.firmament.commands.thenExecute
import moe.nea.firmament.events.CommandEvent
import moe.nea.firmament.gui.config.AllConfigsGui.toObservableList
import moe.nea.firmament.gui.config.KeyBindingStateManager
import moe.nea.firmament.keybindings.SavedKeyBinding
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.MoulConfigUtils
import moe.nea.firmament.util.ScreenUtil

class MacroUI {


	companion object {
		@Subscribe
		fun onCommands(event: CommandEvent.SubCommand) {
			// TODO: add button in config
			event.subcommand("macros") {
				thenExecute {
					ScreenUtil.setScreenLater(MoulConfigUtils.loadScreen("config/macros/index", MacroUI(), null))
				}
			}
		}

	}

	@field:Bind("combos")
	val combos = Combos()

	class Combos {
		@field:Bind("actions")
		val actions: ObservableList<ActionEditor> = ObservableList(
			MacroData.DConfig.data.comboActions.mapTo(mutableListOf()) {
				ActionEditor(it, this)
			}
		)

		var dontSave = false

		@Bind
		fun beforeClose(): CloseEventListener.CloseAction {
			if (!dontSave)
				save()
			return CloseEventListener.CloseAction.NO_OBJECTIONS_TO_CLOSE
		}

		@Bind
		fun addCommand() {
			actions.add(
				ActionEditor(
					ComboKeyAction(
						CommandAction("ac Hello from a Firmament Hotkey"),
						listOf()
					),
					this
				)
			)
		}

		@Bind
		fun discard() {
			dontSave = true
			MC.screen?.close()
		}

		@Bind
		fun saveAndClose() {
			save()
			MC.screen?.close()
		}

		@Bind
		fun save() {
			MacroData.DConfig.data.comboActions = actions.map { it.asSaveable() }
			MacroData.DConfig.markDirty()
			ComboProcessor.setActions(MacroData.DConfig.data.comboActions) // TODO: automatically reload those from the config on startup
		}
	}

	class KeyBindingEditor(var binding: SavedKeyBinding, val parent: ActionEditor) {
		val sm = KeyBindingStateManager(
			{ binding },
			{ binding = it },
			::blur,
			::requestFocus
		)

		@field:Bind
		val button = sm.createButton()

		init {
			sm.updateLabel()
		}

		fun blur() {
			button.blur()
		}

		@Bind
		fun delete() {
			parent.combo.removeIf { it === this }
			parent.combo.update()
		}

		fun requestFocus() {
			button.requestFocus()
		}
	}

	class ActionEditor(val action: ComboKeyAction, val parent: MacroUI.Combos) {
		fun asSaveable(): ComboKeyAction {
			return ComboKeyAction(
				CommandAction(command),
				combo.map { it.binding }
			)
		}

		@field:Bind("command")
		var command: String = (action.action as CommandAction).command

		@field:Bind("combo")
		val combo = action.keys.map { KeyBindingEditor(it, this) }.toObservableList()

		@Bind
		fun formattedCombo() =
			combo.joinToString(" > ") { it.binding.toString() }

		@Bind
		fun addStep() {
			combo.add(KeyBindingEditor(SavedKeyBinding.unbound(), this))
		}

		@Bind
		fun back() {
			MC.screen?.close()
		}

		@Bind
		fun delete() {
			parent.actions.removeIf { it === this }
			parent.actions.update()
		}
		@Bind
		fun edit() {
			MC.screen = MoulConfigUtils.loadScreen("config/macros/editor", this, MC.screen)
		}
	}
}

private fun <T> ObservableList<T>.setAll(ts: Collection<T>) {
	val observer = this.observer
	this.clear()
	this.addAll(ts)
	this.observer = observer
	this.update()
}