aboutsummaryrefslogtreecommitdiff
path: root/features/settings/settingThings/toggle.js
blob: d839b8f843866bb9a228e199d95692ed1aa8a40d (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
import Enum from "../../../../guimanager/Enum";
import SoopyContentChangeEvent from "../../../../guimanager/EventListener/SoopyContentChangeEvent";
import Toggle from "../../../../guimanager/GuiElement/Toggle";
import SettingBase from "./settingBase";

class ToggleSetting extends SettingBase {
    constructor(name, description, defaultVal, settingId, module){
        super(name, description, defaultVal, settingId, module)

        this.toggleObject = new Toggle().setLocation(0, 0.3, 0.8, 0.4).setValue(this.getValue())
        this.settingObject.addChild(this.toggleObject)

        this.toggleObject.addEvent(new SoopyContentChangeEvent().setHandler((newVal, oldVal, resetFun)=>{
            this.setValue(newVal)
        }))

    }

    setValue(newVal){
        super.setValue(newVal)

        this.toggleObject.setValue(newVal)

        return this
    }

    requires(toggleSetting){
        this.requiresO = toggleSetting

        toggleSetting.toggleObject.addEvent(new SoopyContentChangeEvent().setHandler((newVal, oldVal, resetFun)=>{
            if(newVal){
                this.setValue(this.temp_val)

                this.toggleObject.triggerEvent(Enum.EVENT.CONTENT_CHANGE, [this.temp_val, false, ()=>{}])

                this.guiObject.location.size.y.set(0.2, 500)
            }else{
                this.temp_val = this.getValue()
                this.setValue(false)

                this.toggleObject.triggerEvent(Enum.EVENT.CONTENT_CHANGE, [false, this.temp_val, ()=>{}])

                this.guiObject.location.size.y.set(0, 500)
            }
        }))
        let newVal = this.requiresO.getValue()
        if(!newVal){
            let temp_val = this.temp_val
            this.setValue(false)
            this.temp_val = temp_val
            this.guiObject.location.size.y.set(0, 0)
        }

        return this
    }
}

export default ToggleSetting