aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/raeids/stratus/gui/ChatShortcutEditGui.kt
blob: fd04f26040f499aaf91340b1f7fde758500c1dd3 (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
package com.raeids.stratus.gui

import com.raeids.stratus.hook.ChatShortcuts
import gg.essential.api.EssentialAPI
import gg.essential.api.gui.buildConfirmationModal
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.childOf
import gg.essential.elementa.dsl.constrain
import gg.essential.elementa.dsl.percent
import gg.essential.elementa.dsl.pixels
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() {

    private val initialAlias = alias
    private val initialCommand = command

    override fun initScreen(width: Int, height: Int) {
        super.initScreen(width, height)
        val block = 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") {
            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(var alias: String, var command: String): WindowScreen(restoreCurrentGuiOnClose = true) {
        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
        }
    }
}