aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/cc/woverflow/chatting/gui/ChatShortcutEditGui.kt
blob: 983e2ed9bbc62906c677d871409b9c917903a938 (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
package cc.woverflow.chatting.gui

import cc.woverflow.chatting.chat.ChatShortcuts
import gg.essential.api.EssentialAPI
import gg.essential.api.gui.buildConfirmationModal
import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.WindowScreen
import gg.essential.elementa.components.UIBlock
import gg.essential.elementa.constraints.CenterConstraint
import gg.essential.elementa.constraints.SiblingConstraint
import gg.essential.elementa.dsl.*
import gg.essential.vigilance.gui.VigilancePalette
import gg.essential.vigilance.gui.settings.ButtonComponent
import gg.essential.vigilance.gui.settings.TextComponent

class ChatShortcutEditGui(private var alias: String, private var command: String, private val editing: Boolean) :
    WindowScreen(restoreCurrentGuiOnClose = true, version = ElementaVersion.V1) {

    private val initialAlias = alias
    private val initialCommand = command

    override fun initScreen(width: Int, height: Int) {
        super.initScreen(width, height)
        window.clearChildren()  // make sure everything is cleared, sometimes the shortcuts duplicated
        val block by UIBlock(VigilancePalette.getBackground()).constrain {
            this.x = CenterConstraint()
            this.y = CenterConstraint()
            this.width = 100.pixels()
            this.height = 100.pixels()
        } childOf window
        TextComponent(initialAlias, "Alias", wrap = false, protected = false).constrain {
            x = CenterConstraint()
            y = 10.percent()
        }.childOf(block).onValueChange {
            if (it is String) alias = it
        }
        TextComponent(initialCommand, "Command", wrap = false, protected = false).constrain {
            x = CenterConstraint()
            y = SiblingConstraint()
        }.childOf(block).onValueChange {
            if (it is String) command = it
        }
        if (editing) {
            ButtonComponent("Reset") {
                EssentialAPI.getGuiUtil().openScreen(ChatShortcutEditGui(initialAlias, initialCommand, editing))
            } constrain {
                x = CenterConstraint()
                y = 70.percent()
            } childOf window
        }
        ButtonComponent("Save") {
            alias = alias.substringAfter("/")
            command = command.substringAfter("/")
            if (editing) {
                ChatShortcuts.removeShortcut(initialAlias)
            }
            if (alias.isBlank() || command.isBlank()) {
                return@ButtonComponent
            }
            if (ChatShortcuts.shortcuts.any { it.first == alias }) {
                EssentialAPI.getGuiUtil().openScreen(ChatShortcutConfirmGui(alias, command))
                return@ButtonComponent
            }
            ChatShortcuts.writeShortcut(alias, command)
            restorePreviousScreen()
        } constrain {
            x = CenterConstraint()
            y = 80.percent()
        } childOf window
    }

    inner class ChatShortcutConfirmGui(private var alias: String, private var command: String) :
        WindowScreen(restoreCurrentGuiOnClose = true, version = ElementaVersion.V1) {
        override fun initScreen(width: Int, height: Int) {
            super.initScreen(width, height)
            EssentialAPI.getEssentialComponentFactory().buildConfirmationModal {
                text = "An alias with this name already exists, are you sure you want to overwrite it?"
                onConfirm = {
                    ChatShortcuts.writeShortcut(alias, command)
                    EssentialAPI.getGuiUtil().openScreen(null)
                }
                onDeny = {
                    restorePreviousScreen()
                }
            } childOf this@ChatShortcutConfirmGui.window
        }
    }
}