aboutsummaryrefslogtreecommitdiff
path: root/features/settings/index.js
blob: 6888f8a61952b5cd2ab977ba83693673f70cd61a (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/// <reference types="../../../CTAutocomplete" />
/// <reference lib="es2015" />
import Feature from "../../featureClass/class";
import SoopyGuiElement from "../../../guimanager/GuiElement/SoopyGuiElement";
import SoopyTextElement from "../../../guimanager/GuiElement/SoopyTextElement";
import SoopyBoxElement from "../../../guimanager/GuiElement/SoopyBoxElement";
import BoxWithToggleAndDescription from "../../../guimanager/GuiElement/BoxWithToggleAndDescription";
import ButtonWithArrowAndDescription from "../../../guimanager/GuiElement/ButtonWithArrowAndDescription";
import SoopyMouseClickEvent from "../../../guimanager/EventListener/SoopyMouseClickEvent";
import SoopyContentChangeEvent from "../../../guimanager/EventListener/SoopyContentChangeEvent";
import settingsCommunicator from "./settingsCommunicator";
import GuiPage from "../soopyGui/GuiPage"
import { SoopyGui, SoopyRenderEvent } from "../../../guimanager";
import TextBox from "../../../guimanager/GuiElement/TextBox";
import locationSettingHolder from "./locationSettingHolder";


class SettingsRenderer extends Feature {
    constructor() {
        super()
    }

    onEnable(){
        this.SettingPage = new SettingPage()
        this.EditLocationsPage = new EditLocationsPage()
        this.SettingPage.FeatureManager = this.FeatureManager

        this.registerStep(true, 1, ()=>{ 
            if(!this.EditLocationsPage) return
            
            if(this.EditLocationsPage.needsExitPage){
                this.EditLocationsPage.goToPage(0, 500)
                this.EditLocationsPage.needsExitPage = false
            }
        })

    }

    onDisable(){
        this.EditLocationsPage = undefined
        this.SettingPage = undefined
    }
}

class EditLocationsPage extends GuiPage {
    
    constructor(){
        super(9)
        
        this.name = "Edit GUI Locations"

        this.needsExitPage = false

        this.soopyGui = new SoopyGui()
        this.soopyGui._renderBackground = ()=>{} //remove background darkening


        
        this.soopyGui.ctGui.registerDraw((mouseX, mouseY, partialTicks)=>{
            this.renderGui(mouseX, mouseY)
            this.soopyGui._render(mouseX, mouseY, partialTicks)
        })
        this.soopyGui.ctGui.registerClicked((mouseX, mouseY, button)=>{
            this.clicked(mouseX, mouseY)
            this.soopyGui._onClick(mouseX, mouseY, button)
        })
        this.soopyGui.ctGui.registerMouseReleased((mouseX, mouseY)=>{
            this.released(mouseX, mouseY)
        })

        this.finaliseLoading()
    }
    
    renderGui(mouseX, mouseY){
        for(let setting of locationSettingHolder.getData()){
            if(setting.parent){
                if(setting.parent.isEnabled()){
                    setting.renderGui(mouseX, mouseY)
                }
            }else{
                setting.renderGui(mouseX, mouseY)
            }
        }
    }

    clicked(mouseX, mouseY){
        for(let setting of locationSettingHolder.getData()){
            if(setting.clicked(mouseX, mouseY)) return //dont allow the user to drag 2 locations at once
        }
    }

    released(mouseX, mouseY){
        for(let setting of locationSettingHolder.getData()){
            setting.released(mouseX, mouseY)
        }
    }

    onOpen(){
        this.needsExitPage = true

        this.soopyGui.open()
    }
}

class SettingPage extends GuiPage {
    constructor(){
        super(10)
        
        this.name = "Settings"

        this.pages = [this.newPage(), this.newPage()]

        this.settingsCategoryArea = undefined
        this.settingsTitle = undefined
        this.settingsArea = undefined
        this.modifyingFeature = false
        this.featureLoadedTextBox = undefined

        this.SettingPage = undefined

        this.pages[0].addEvent(new SoopyRenderEvent().setHandler(()=>{this.updateLocs()}))

        //###############################################################################################
        //                                     Settings Category Page
        //###############################################################################################

        
        let settingsCategoryTitle = new SoopyTextElement().setText("§0Settings").setMaxTextScale(3).setLocation(0.1, 0.05, 0.5, 0.1)
        this.pages[0].addChild(settingsCategoryTitle)

        this.settingsCategorySearch = new TextBox().setPlaceholder("Search...").setLocation(0.6, 0.05, 0.3, 0.1)
        this.pages[0].addChild(this.settingsCategorySearch)

        this.settingsCategorySearch.text.addEvent(new SoopyContentChangeEvent().setHandler(()=>{
            this.updateSettingCategories()
        }))

        this.settingsCategoryArea = new SoopyGuiElement().setLocation(0.1, 0.2, 0.8, 0.8).setScrollable(true)
        this.pages[0].addChild(this.settingsCategoryArea)


        //###############################################################################################
        //                                     Settings Page
        //###############################################################################################

        this.settingsTitle = new SoopyTextElement().setMaxTextScale(3).setLocation(0.1, 0.05, 0.8, 0.1)
        this.pages[1].addChild(this.settingsTitle)

        this.settingsArea = new SoopyGuiElement().setLocation(0.1, 0.2, 0.8, 0.8).setScrollable(true)
        this.pages[1].addChild(this.settingsArea)


        this.finaliseLoading()
    }

    onOpen(){
        this.updateSettingCategories()
        this.settingsCategoryArea.location.scroll.x.set(0, 0)
        this.settingsCategoryArea.location.scroll.y.set(0, 0)
        this.settingsCategoryArea._scrollAmount = 0

        this.settingsCategorySearch.setText("")
        this.updateSettingCategories()
    }

    onOpenPage(p){
        if(p===1) this.updateSettingCategories()

        this.closeSidebarPage()
    }

    updateSettingCategories(){

        let search = this.settingsCategorySearch.text.text

        this.settingsCategoryArea.children = []

        let height = 0

        Object.keys(this.FeatureManager.featureMetas).sort((a, b)=>{return a.sortA-b.sortA}).forEach((f)=>{

            let meta = this.FeatureManager.featureMetas[f]

            let showing = search.length === 0

            if(!showing){
                showing = meta.name.toLowerCase().includes(search.toLowerCase()) || meta.description.toLowerCase().includes(search.toLowerCase())
            }

            if(!showing){
                settingsCommunicator.getModuleSettings(f).forEach(setting=>{

                    if(setting && (setting.name.toLowerCase().includes(search.toLowerCase()) || setting.description.toLowerCase().includes(search.toLowerCase()))){
                        showing = true
                    }
                })
            }

            if(!showing) return

            let isHidden = meta.isHidden
            if(typeof isHidden === "string"){
                isHidden = require("../" + f + "/" + isHidden).hidden(this.FeatureManager)
            }
            if(isHidden) return

            let category = new ButtonWithArrowAndDescription().setText("&0" + meta.name).setDesc("&0" +meta.description).setLocation(0, height, 1, 0.2)
            category.addEvent(new SoopyMouseClickEvent().setHandler(()=>{this.clickedOpenCategory(f)}))

            this.settingsCategoryArea.addChild(category)

            height+=0.225

            
            if(search.length > 0){
                settingsCommunicator.getModuleSettings(f).forEach(setting=>{

                    if(setting && (setting.name.toLowerCase().includes(search.toLowerCase()) || setting.description.toLowerCase().includes(search.toLowerCase()))){

                        setting.getGuiObject().location.location.y.set(height, 0)
                        this.settingsCategoryArea.addChild(setting.getGuiObject())
            
                        height += 0.025+setting.getGuiObject().location.size.y.get()

                        setting.update()
                    }
                })
            }
        })


        // this.FeatureManager.features = {}; enabled features
    }

    updateSettings(category){
        let meta = this.FeatureManager.featureMetas[category]

        this.settingsArea.children = []

        this.settingsTitle.setText("&0" + this.FeatureManager.featureMetas[category].name)

        let height = 0

        if(meta.isTogglable){
            let toggle = new BoxWithToggleAndDescription().setLocation(0, height, 1, 0.2).setText("&0Main toggle").setDesc("&0This is the main toggle for the whole category")
            this.settingsArea.addChild(toggle)

            toggle.toggle.setValue(this.FeatureManager.isFeatureLoaded(category), 0)

            toggle.toggle.addEvent(new SoopyContentChangeEvent().setHandler((newVal, oldVal, resetFun)=>{
                //dont allow editing if currenately loading/unloading it
                if(this.modifyingFeature){
                    resetFun()
                    return
                }

                //toggle the feature
                this.modifyingFeature = true
                new Thread(()=>{
                    this.FeatureManager.featureSettingsData[category].enabled = newVal
                    this.FeatureManager.featureSettingsDataLastUpdated = true

                    if(!newVal && this.FeatureManager.isFeatureLoaded(category)){
                        this.FeatureManager.unloadFeature(category)
                    }
                    if(newVal && !this.FeatureManager.isFeatureLoaded(category)){
                        this.FeatureManager.loadFeature(category)
                    }

                    Thread.sleep(100)

                    this.modifyingFeature = false
                    
                    this.updateSettings(category)
                }).start()
            }))
            height += toggle.location.size.y.get()+0.045
        }

        if(!this.FeatureManager.isFeatureLoaded(category)){

            //only show if feature issnt loaded

            let textBox = new SoopyBoxElement().setLocation(0, height, 1, 0.2)
            .addChild(new SoopyTextElement().setText("&0Feature not loaded").setLocation(0, 0, 1, 0.5))
            .addChild(new SoopyTextElement().setText("&0Load feature to access settings").setLocation(0, 0.5, 1, 0.5))
            this.settingsArea.addChild(textBox)
            return;
        }

        settingsCommunicator.getModuleSettings(category).forEach(setting=>{
            setting.getGuiObject().location.location.y.set(height, 0)
            this.settingsArea.addChild(setting.getGuiObject())

            height += 0.045+setting.getGuiObject().location.size.y.get()
            
            setting.update()
        })
    }

    updateLocs(){
        let totalHeight = 0

        this.settingsArea.children.forEach(e=>{
            e.location.location.y.set(totalHeight, 0)

            totalHeight += e.location.size.y.get()+Math.min(0.045,e.location.size.y.get())
        })

        totalHeight = 0

        this.settingsCategoryArea.children.forEach(e=>{
            e.location.location.y.set(totalHeight, 0)

            totalHeight += e.location.size.y.get()+Math.min(0.025,e.location.size.y.get())
        })
    }

    clickedOpenCategory(category){
        this.updateSettings(category)
        this.goToPage(2)

        this.settingsArea.location.scroll.x.set(0, 0)
        this.settingsArea.location.scroll.y.set(0, 0)
        this.settingsArea._scrollAmount = 0
    }
}

module.exports = {
    class: new SettingsRenderer()
}