aboutsummaryrefslogtreecommitdiff
path: root/features/settings/settingThings/settingBase.js
blob: ae924ca1c5825ed0cd2ef2ce755e86e5a0524186 (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
import SoopyContentChangeEvent from "../../../../guimanager/EventListener/SoopyContentChangeEvent";
import SoopyMouseClickEvent from "../../../../guimanager/EventListener/SoopyMouseClickEvent";
import BoxWithText from "../../../../guimanager/GuiElement/BoxWithText";
import BoxWithTextAndDescription from "../../../../guimanager/GuiElement/BoxWithTextAndDescription"
import SoopyGuiElement from "../../../../guimanager/GuiElement/SoopyGuiElement";
import renderLibs from "../../../../guimanager/renderLibs";
import helpDataLoader from "../helpDataLoader";
import settingsCommunicator from "../settingsCommunicator";
import SoopyMarkdownElement from "../../../../guimanager/GuiElement/SoopyMarkdownElement";

class SettingBase {
    constructor(name, description, defaultVal, settingId, module) {
        this.name = name;
        this.description = description;
        this.defaultVal = defaultVal;
        this.settingId = settingId
        this.module = module
        this.moduleId = module.getId()

        this.val = defaultVal;

        this.guiObject = new BoxWithTextAndDescription().setDesc("§0" + this.description.replace(/\n/g, "\n§0")).setText("§0" + this.name).setLocation(0, 0, 1, 0.175)

        this.settingObject = new SoopyGuiElement().setLocation(0.8, 0, 0.2, 1)

        this.guiObject.addChild(this.settingObject)

        this.helpButton = new BoxWithText().setText("§0?").setLocation(3, 3, 0.05, 0.5)
        this.helpButton.location.location.setRelative(false, false)

        this.helpButton.addEvent(new SoopyMouseClickEvent().setHandler(() => {
            module.FeatureManager.features.soopyGui.class.openSidebarPage(new SoopyGuiElement().setLocation(0.05, 0.05, 0.9, 0.9).setScrollable(true).addChild(new SoopyMarkdownElement().setLocation(0, 0, 1, 1).setText("Loading...")))

            this.getHelp(helpText => {
                module.FeatureManager.features.soopyGui.class.openSidebarPage(new SoopyGuiElement().setLocation(0.05, 0.05, 0.9, 0.9).setScrollable(true).addChild(new SoopyMarkdownElement().setLocation(0, 0, 1, 1).setText(helpText)))
            })
        }))

        this.helpButton.setLore(["Click for more information about this setting"])

        settingsCommunicator.addSetting(this.moduleId, settingId, this)

        if (!module.FeatureManager.featureSettingsData[this.moduleId]) {
            module.FeatureManager.featureSettingsData[this.moduleId] = {}
        }
        if (!module.FeatureManager.featureSettingsData[this.moduleId].subSettings) module.FeatureManager.featureSettingsData[this.moduleId].subSettings = {}
        if (!module.FeatureManager.featureSettingsData[this.moduleId].subSettings[settingId]) {
            module.FeatureManager.featureSettingsData[this.moduleId].subSettings[settingId] = {
                value: this.getDefaultValue(),
                temp_val: this.getDefaultValue()
            }

            module.FeatureManager.featureSettingsDataLastUpdated = true
        }
        let temp_val_temp = module.FeatureManager.featureSettingsData[this.moduleId].subSettings[settingId].temp_val
        this.setValue(module.FeatureManager.featureSettingsData[this.moduleId].subSettings[settingId].value)
        this.temp_val = temp_val_temp

        this.requiresO = undefined

        this.onchangethings = []

        this.initTime = Date.now()
    }

    update() {
        if (this.hasHelp()) {
            this.guiObject.addChild(this.helpButton)

            this.guiObject.text.setLocation(0.075, 0, 0.8 - 0.075, 0.6)
        } else {
            this.guiObject.text.setLocation(0, 0, 0.8, 0.6)
        }
    }

    hasHelp() {
        return helpDataLoader.hasData(this.moduleId, this.settingId)
    }

    getHelp(callback) {
        helpDataLoader.getData(this.moduleId, this.settingId, callback)
    }

    getValue() {
        return this.val;
    }

    setValue(val) {
        if (this.val === val) return
        this.val = val;

        if (!this.requiresO || this.requiresO.getValue()) {
            this.temp_val = val
        }

        if (this.module.FeatureManager.featureSettingsData[this.moduleId].subSettings[this.settingId].value !== val) {
            this.module.FeatureManager.featureSettingsData[this.moduleId].subSettings[this.settingId].value = val

            this.module.FeatureManager.featureSettingsDataLastUpdated = true
        }
        if (this.module.FeatureManager.featureSettingsData[this.moduleId].subSettings[this.settingId].temp_val !== this.temp_val) {
            this.module.FeatureManager.featureSettingsData[this.moduleId].subSettings[this.settingId].temp_val = this.temp_val

            this.module.FeatureManager.featureSettingsDataLastUpdated = true
        }

        if (this.onchangethings && Date.now() - this.initTime > 1000) this.onchangethings.forEach(([fun, context]) => { fun.call(context) })
    }

    getName() {
        return this.name;
    }

    getDescription() {
        return this.description;
    }

    getDefaultValue() {
        return this.defaultVal;
    }

    getGuiObject() {
        return this.guiObject;
    }

    requires(toggleSetting) {
        this.requiresO = toggleSetting

        toggleSetting.toggleObject.addEvent(new SoopyContentChangeEvent().setHandler((newVal, oldVal, resetFun) => {
            if (newVal) {
                this.guiObject.location.size.y.set(0.2, 500)
            } else {
                this.guiObject.location.size.y.set(0, 500)
            }
        }))
        let newVal = this.requiresO.getValue()
        if (!newVal) {
            this.guiObject.location.size.y.set(0, 0)
        }

        return this
    }

    contributor(name) {
        //TODO: this entire function
        return this
    }

    delete() {
        settingsCommunicator.removeSetting(this.module, this.settingId)
    }

    onchange(context, fun) {
        this.onchangethings.push([fun, context])
        return this
    }
}

export default SettingBase