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

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)

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

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

    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
    }

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

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

export default SettingBase