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

    getValue(){
        return this.val;
    }

    setValue(val){
        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
        }
    }

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

export default SettingBase