aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/macros/MacroUI.kt
blob: 88f20a1c097e73c3bca772909587bf56ec38db5b (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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()

	@field:Bind("wheels")
	val wheels = Wheels()
	var dontSave = false

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

	fun save() {
		MacroData.DConfig.data.comboActions = combos.actions.map { it.asSaveable() }
		MacroData.DConfig.data.wheels = wheels.wheels.map { it.asSaveable() }
		MacroData.DConfig.markDirty()
		RadialMacros.setWheels(MacroData.DConfig.data.wheels)
		ComboProcessor.setActions(MacroData.DConfig.data.comboActions)
	}

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

	class Command(
		@field:Bind("text")
		var text: String,
		val parent: Wheel,
	) {
		@Bind
		fun delete() {
			parent.editableCommands.removeIf { it === this }
			parent.editableCommands.update()
			parent.commands.update()
		}

		fun asCommandAction() = CommandAction(text)
	}

	inner class Wheel(
		val parent: Wheels,
		var binding: SavedKeyBinding,
		commands: List<CommandAction>,
	) {

		fun asSaveable(): MacroWheel {
			return MacroWheel(binding, commands.map { it.asCommandAction() })
		}

		@Bind("keyCombo")
		fun text() = binding.format().string

		@field:Bind("commands")
		val commands = commands.mapTo(ObservableList(mutableListOf())) { Command(it.command, this) }

		@field:Bind("editableCommands")
		val editableCommands = this.commands.toObservableList()

		@Bind
		fun addOption() {
			editableCommands.add(Command("", this))
		}

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

		@Bind
		fun edit() {
			MC.screen = MoulConfigUtils.loadScreen("config/macros/editor_wheel", this, MC.screen)
		}

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

		val sm = KeyBindingStateManager(
			{ binding },
			{ binding = it },
			::blur,
			::requestFocus
		)

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

		init {
			sm.updateLabel()
		}

		fun blur() {
			button.blur()
		}


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

	inner class Wheels {
		@field:Bind("wheels")
		val wheels: ObservableList<Wheel> = MacroData.DConfig.data.wheels.mapTo(ObservableList(mutableListOf())) {
			Wheel(this, it.key, it.options.map { CommandAction((it as CommandAction).command) })
		}

		@Bind
		fun discard() {
			this@MacroUI.discard()
		}

		@Bind
		fun saveAndClose() {
			this@MacroUI.saveAndClose()
		}

		@Bind
		fun save() {
			this@MacroUI.save()
		}

		@Bind
		fun addWheel() {
			wheels.add(Wheel(this, SavedKeyBinding.unbound(), listOf()))
		}
	}

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

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

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

		@Bind
		fun discard() {
			this@MacroUI.discard()
		}

		@Bind
		fun saveAndClose() {
			this@MacroUI.discard()
		}

		@Bind
		fun save() {
			this@MacroUI.save()
		}
	}

	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()
		}


		fun requestFocus() {
			button.requestFocus()
		}

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

	class ActionEditor(val action: ComboKeyAction, val parent: 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_combo", 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()
}