-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathCommandAliasesGui.kt
More file actions
169 lines (150 loc) · 6.71 KB
/
Copy pathCommandAliasesGui.kt
File metadata and controls
169 lines (150 loc) · 6.71 KB
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
/*
* Skytils - Hypixel Skyblock Quality of Life Mod
* Copyright (C) 2020-2024 Skytils
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package gg.skytils.skytilsmod.gui.features
import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.WindowScreen
import gg.essential.elementa.components.ScrollComponent
import gg.essential.elementa.components.UIContainer
import gg.essential.elementa.components.UIText
import gg.essential.elementa.components.input.UITextInput
import gg.essential.elementa.constraints.CenterConstraint
import gg.essential.elementa.constraints.ChildBasedSizeConstraint
import gg.essential.elementa.constraints.RelativeConstraint
import gg.essential.elementa.constraints.SiblingConstraint
import gg.essential.elementa.dsl.*
import gg.essential.elementa.effects.OutlineEffect
import gg.essential.universal.UChat
import gg.essential.universal.UKeyboard
import gg.essential.vigilance.utils.onLeftClick
import gg.skytils.skytilsmod.Skytils.prefix
import gg.skytils.skytilsmod.core.PersistentSave
import gg.skytils.skytilsmod.features.impl.handlers.CommandAliases
import gg.skytils.skytilsmod.gui.ReopenableGUI
import gg.skytils.skytilsmod.gui.components.HelpComponent
import gg.skytils.skytilsmod.gui.components.SimpleButton
import gg.skytils.skytilsmod.utils.multiplatform.isValidChar
import java.awt.Color
class CommandAliasesGui : WindowScreen(ElementaVersion.V2, newGuiScale = 2), ReopenableGUI {
private val scrollComponent: ScrollComponent
init {
UIText("Command Aliases").childOf(window).constrain {
x = CenterConstraint()
y = RelativeConstraint(0.075f)
height = 14.pixels()
}
scrollComponent = ScrollComponent(
innerPadding = 4f,
).childOf(window).constrain {
x = CenterConstraint()
y = 15.percent()
width = 90.percent()
height = 70.percent() + 2.pixels()
}
val bottomButtons = UIContainer().childOf(window).constrain {
x = CenterConstraint()
y = 90.percent()
width = ChildBasedSizeConstraint()
height = ChildBasedSizeConstraint()
}
SimpleButton("Save and Exit").childOf(bottomButtons).constrain {
x = 0.pixels()
y = 0.pixels()
}.onLeftClick {
displayScreen(null)
}
SimpleButton("Add Alias").childOf(bottomButtons).constrain {
x = SiblingConstraint(5f)
y = 0.pixels()
}.onLeftClick {
addNewAlias()
}
HelpComponent(
window,
"What are aliases? Aliases are little shortcuts for commands. For example, /slay could be short for /skytilsslayer. Want to give it a try? Click 'Add Alias', enter 'slay' for 'Alias Name' and 'skytilsslayer' as the executed command! Then, run the command /slay Sychic! Aliases will also append any arguments that you supply."
)
CommandAliases.sanityCheck()
CommandAliases.aliases.entries.sortedBy { it.key }.forEach { name ->
addNewAlias(name.key, name.value)
}
}
private fun addNewAlias(alias: String = "", replacement: String = "") {
val container = UIContainer().childOf(scrollComponent).constrain {
x = CenterConstraint()
y = SiblingConstraint(5f)
width = 80.percent()
height = 9.5.percent()
}.effect(OutlineEffect(Color(0, 243, 255), 1f))
val aliasName = UITextInput("Alias Name").childOf(container).constrain {
x = 5.pixels()
y = CenterConstraint()
width = 30.percent()
}.apply {
onLeftClick {
grabWindowFocus()
}
setText(alias)
}
val commandInput = UITextInput("Executed Command").childOf(container).constrain {
x = SiblingConstraint(5f)
y = CenterConstraint()
width = 50.percent()
}.apply {
onLeftClick {
grabWindowFocus()
}
setText(replacement)
}
aliasName.apply {
onKeyType { _, keyCode ->
if (keyCode == UKeyboard.KEY_TAB) commandInput.grabWindowFocus()
setText(getText().filter(::isValidChar).take(255))
}
}
commandInput.apply {
onKeyType { _, keyCode ->
if (keyCode == UKeyboard.KEY_TAB) aliasName.grabWindowFocus()
setText(getText().filter(::isValidChar).take(255))
}
}
SimpleButton("Remove").childOf(container).constrain {
x = 85.percent()
y = CenterConstraint()
height = 75.percent()
}.onLeftClick {
scrollComponent.removeChild(container)
}
}
override fun onScreenClose() {
val aliasKeys = CommandAliases.aliases.keys.toSet() // Make sure to copy with toSet()
super.onScreenClose()
CommandAliases.aliases.clear()
for (container in scrollComponent.allChildren) {
val text = container.childrenOfType<UITextInput>()
if (text.size != 2) throw IllegalStateException("${container.componentName} does not have 2 UITextInput's! Available children ${container.children.map { it.componentName }}")
val alias = (text.find { it.placeholder == "Alias Name" }
?: throw IllegalStateException("${container.componentName} does not have the alias UITextInput! Available children ${container.children.map { it.componentName }}")).getText()
val replacement = (text.find { it.placeholder == "Executed Command" }
?: throw IllegalStateException("${container.componentName} does not have the command UITextInput! Available children ${container.children.map { it.componentName }}")).getText()
if (alias.isBlank() || replacement.isBlank()) continue
CommandAliases.aliases[alias] = replacement
}
PersistentSave.markDirty<CommandAliases>()
CommandAliases.refresh(customRemovalKeys = aliasKeys)
UChat.chat("$prefix §6You may need to switch servers to apply changes to aliases.")
}
}