aboutsummaryrefslogtreecommitdiff
path: root/src_frontend/Components/MainControls/ControlComponents.svelte
blob: 71c522a36cdd49471e31b0a54ecf0780bea53b39 (plain) (blame)
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
<script>
    import { onMount } from "svelte";
    import { fade } from 'svelte/transition';
    import RoundRange from "../../ComponentLib/RoundRange.svelte";
    import FloatingButton from "../../ComponentLib/Button/FloatingButton.svelte";
    import FloatingSelect from "../../ComponentLib/FloatingSelect.svelte";
    import PrettyVar from "../../ComponentLib/PrettyVar.svelte";
    import ControlColors from "./ControlColors.svelte";
    import ControlOthers from "./ControlOthers.svelte";

	import { openSocket } from "../../stores/socketStore";

    let name = "-";
    let brightness = 0;
    let powerIsOn = false;

    let modeSelect;
    let allModes = {};
    let activeMode = "";
    
    let colorVariables = {};
    let variables = {};

    function togglePower() {
        powerIsOn = !powerIsOn;
        openSocket.emit("power:set", powerIsOn);
    }
    function setBrightness() {
        if (!powerIsOn) {
            togglePower();
        }
        openSocket.emit("brightness:set", Math.floor((brightness * 255) / 100));
    }
    function setColor(ev) {
        openSocket.emit("var:set", ev.detail.name, ev.detail.value);
    }
    function setMode(el) {
        openSocket.emit("mode:set", el.target.value, (res) => {
            console.log(res);
        })
    }
    function onVarChange(name, value) {
        if (!name.includes("variable/")) {
            console.log(`Change on unknown globvar "${name}".`);
            return;
        }
        name = name.replace("variable/", "");

        switch (value.type) {
            case "COLOR":
                if (value.value == null) {
                    delete colorVariables[name];
                } else {
                    colorVariables[name] = value.value;
                }
                    colorVariables = colorVariables;
                break;
            default:
                if (value.value == null) {
                    delete variables[name];
                } else {
                    variables[name] = value.value;
                }
                variables = variables;
        }
    }
    openSocket.on("modelist", (modelist) => {
        allModes = [];
        for (let i = 0; i < modelist.length; i++) {
            let modePath = modelist[i].split("/", 1)[0];
            if (!allModes.hasOwnProperty(modePath)) { allModes[modePath] = []; }
            allModes[modePath].push([modelist[i], modelist[i].replace(modePath + "/", "")])
        }
        allModes = allModes;
    })
    openSocket.on("name", (_name) => name = _name);
    openSocket.on("mode", (newMode) => activeMode = newMode);
    openSocket.on("power", (power) => powerIsOn = power);
    openSocket.on("brightness", (value) => brightness = Math.floor((value * 100) / 255));
    openSocket.on("var", onVarChange);
    openSocket.on("vars", (vars) => {
        for (const [name, value] of Object.entries(vars)) {
            onVarChange(`variable/${name}`, value);
        }
    });

    onMount(async () => {
        openSocket.emit("name:get");
        openSocket.emit("modelist:get");
        openSocket.emit("mode:get");
        openSocket.emit("power:get");
        openSocket.emit("brightness:get");
        openSocket.emit("vars:get");
    });

</script>

<style>
    .wrapper {
        box-sizing: border-box;
        padding-bottom: 17px;
        text-align: center;
    }
    h1 {
        color: var(--grey-600);
        margin: 0;
        font-size: 20px;
        font-weight: 100;;
    }
    .row {
        margin-bottom: 25px;
        margin-left: 10%;
        margin-right: 10%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .mode_button {
        margin-left: 10%;
        flex-grow: 1;
    }
</style>

<div class="wrapper">
    <h1>{name}</h1>
    <RoundRange bind:enabled={powerIsOn} on:change={setBrightness} bind:value={brightness} />

    <div class="row">
        <FloatingButton label="POWER" on:click={togglePower} bind:active={powerIsOn} activeBackgroundColor="var(--yellow-500)" activeColor="black" faIcon="fa fa-power-off" />
        <div class="mode_button">
            <FloatingSelect label="MODE" faIcon="fas fa-sliders-h" on:change={setMode} bind:this={modeSelect} bind:value={activeMode} >
                {#each Object.entries(allModes) as [modePath, modes]}
                    <optgroup label={modePath}>
                    {#each modes as mode}
                        <option value={mode[0]}><PrettyVar varText={mode[1]} /></option>
                    {/each}
                    </optgroup>
                {/each}
            </FloatingSelect>
        </div>
    </div>

    {#if Object.keys(colorVariables).length > 0}
        <ControlColors on:change={setColor} bind:colorVariables={colorVariables} />
    {/if}
    <ControlOthers />
</div>