Repository navigation
Expand file tree
/
Copy pathappsettings.lua
More file actions
421 lines (366 loc) · 14.6 KB
/
Copy pathappsettings.lua
File metadata and controls
421 lines (366 loc) · 14.6 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
--appsettings
-- Author: SentyFunBall
-- GitHub: https://github.com/SentyFunBall
-- Workshop:
--Code by ST. Do not reuse.--
--- Developed using LifeBoatAPI - Stormworks Lua plugin for VSCode - https://code.visualstudio.com/download (search "Stormworks Lua with LifeboatAPI" extension)
--- If you have any issues, please report them here: https://github.com/nameouschangey/STORMWORKS_VSCodeExtension/issues - by Nameous Changey
--[====[ HOTKEYS ]====]
-- Press F6 to simulate this file
-- Press F7 to build the project, copy the output from /_build/out/ into the game to use
-- Remember to set your Author name etc. in the settings: CTRL+COMMA
--[====[ EDITABLE SIMULATOR CONFIG - *automatically removed from the F7 build output ]====]
---@section __LB_SIMULATOR_ONLY__
do
---@type Simulator -- Set properties and screen sizes here - will run once when the script is loaded
simulator = simulator
simulator:setScreen(1, "3x2")
simulator:setProperty("Theme", 1)
simulator:setProperty("Units", true)
simulator:setProperty("Transmission", true)
-- Runs every tick just before onTick; allows you to simulate the inputs changing
---@param simulator Simulator Use simulator:<function>() to set inputs etc.
---@param ticks number Number of ticks since simulator started
function onLBSimulatorTick(simulator, ticks)
-- touchscreen defaults
local screenConnection = simulator:getTouchScreen(1)
simulator:setInputBool(3, screenConnection.isTouched)
simulator:setInputNumber(1, screenConnection.touchX)
simulator:setInputNumber(2, screenConnection.touchY)
simulator:setInputBool(1, true)
simulator:setInputNumber(4, 0)
simulator:setInputNumber(3, 5)
end;
end
---@endsection
--[====[ IN-GAME CODE ]====]
-- try require("Folder.Filename") to include code from another file in this, so you can store code in libraries
-- the "LifeBoatAPI" is included by default in /_build/libs/ - you can use require("LifeBoatAPI") to get this, and use all the LifeBoatAPI.<functions>!
local _colors = {
{ { 47, 51, 78 }, { 86, 67, 143 }, { 128, 95, 164 } }, --sencar 5 in the micro
{ { 17, 15, 107 }, { 22, 121, 196 }, { 48, 208, 217 } }, --blue
{ { 74, 27, 99 }, { 124, 42, 161 }, { 182, 29, 224 } }, --purple
{ { 35, 54, 41 }, { 29, 87, 36 }, { 12, 133, 26 } }, --green
{ { 69, 1, 10 }, { 122, 1, 1 }, { 160, 9, 9 } }, --TE red
{ { 38, 38, 38 }, { 92, 92, 92 }, { 140, 140, 140 } }, --grey
{ { 92, 50, 1 }, { 158, 92, 16 }, { 201, 119, 24 } } --orange
}
local scrollable = 0
local scrollPixels = 0
local defaultTheme = property.getNumber("Theme")
local open = false
local maxScroll = 0
local beforeRainbow = defaultTheme --which theme it was before setting rainbow mode
local lastRainbowMode = false
local themes = {
"Default",
"Blue",
"purple",
"green",
"TE red",
"Grey",
"Orange"
}
local actions = { --action {"name", state, type (0=toggle,1=dropdown,2=slider), isShow, extra}
{ "Metric", false, 0 },
{ "Manual", false, 0 },
{ "ESC Off", false, 0 },
{ "RGB Mode", false, 0 },
{ "Mp Mode", false, 0 },
{ "Hue adjust", 0, 2, { n = -180, m = 180, v = 0, s = 1 } },
{ "Gradient Res", 0, 2, { n = 1, m = 9, v = 6, s = 0.1 } },
{ "Theme", 0, 1, themes },
{ "Dark Mode", false, 0 },
{ "SenConnect", property.getBool("Enable SenConnect"), 0 },
{ "Auto sleep", true, 0 },
{ "Pwr Bias (F/R)", 0, 2, { n = -1, m = 1, v = 0, s = 0.02 } },
{ "Max Power", 0, 2, { n = 0, m = 100, v = 100, s = 1 } },
}
local actionHeightOffsets = {}
local total = 0
local sleepTicks = 0
local isSleeping = false
for index, action in pairs(actions) do
local height = 0
if action[3] == 0 then
height = 11
total = total + height
elseif action[3] == 1 then
height = 11
total = total + height
elseif action[3] == 2 then
height = 19
total = total + height
end
actionHeightOffsets[index] = total
end
actions[1][2] = not property.getBool("Units")
actions[2][2] = not property.getBool("Transmission")
actions[6][2] = defaultTheme
local theme = _colors[defaultTheme]
local lastIsNight = false
function onTick()
acc = input.getBool(1)
app = input.getNumber(3)
touchX = input.getNumber(1)
touchY = input.getNumber(2)
press = input.getBool(3) and press + 1 or 0
local isNight = input.getBool(6)
if isNight ~= lastIsNight then
lastIsNight = isNight
if isNight then
actions[9][2] = true
else
actions[9][2] = false
end
end
local lock = not input.getBool(4)
local enableSleep = actions[5][2]
-- sleep logic
if enableSleep then
if press > 0 then
sleepTicks = 0
isSleeping = false
else
sleepTicks = sleepTicks + 1
if sleepTicks > 600 then
isSleeping = true
end
end
else
sleepTicks = 0
isSleeping = false
end
if app == 5 and not isSleeping then --die
maxScroll = 204 -- ~~adjust max scroll if dropdown is open~~ no longer needed, const
scrollPixels = math.min(scrollPixels, maxScroll - 64)
--scroll
scrollUp = press > 0 and isPointInRectangle(0, 18, 12, 19)
scrollDown = press > 0 and isPointInRectangle(0, 39, 12, 19)
if scrollUp then
scrollPixels = clamp(scrollPixels - 2, 0, 999)
end
if scrollDown then
scrollPixels = scrollPixels + 2
end
--action inputs
if not lock then
for i, action in pairs(actions) do
scrollable = 15 - scrollPixels + actionHeightOffsets[i]
local toggleScrollable = 15 - scrollPixels + actionHeightOffsets[i]
if action[3] == 0 and scrollable < 15 then
goto continue -- do nothing, out of view
end
if action[3] == 0 and press == 1 and not open and isPointInRectangle(14, toggleScrollable, 80, 8) then --toggle
action[2] = not action[2]
elseif action[3] == 1 and press == 2 then --dropdown
if isPointInRectangle(14, scrollable, 80, 8) then
open = not open
end
--select themes
for j = 1, #themes do
if open and isPointInRectangle(14, scrollable + #themes * j + j, 80, 8) then
theme = _colors[j]
beforeRainbow = j
actions[6][4].v = 0
open = false
end
end
elseif action[3] == 2 and press > 1 and not open then --slider
--down
if isPointInRectangle(14, scrollable, 8, 8) then
action[4].v = clamp(action[4].v - action[4].s, action[4].n, action[4].m)
end
--up
if isPointInRectangle(77, scrollable, 8, 8) then
action[4].v = clamp(action[4].v + action[4].s, action[4].n, action[4].m)
end
end
::continue::
end
end
end
--theme adjustments
tempTheme = rgbToHsv(theme)
hsvTheme = rgbToHsv(_colors[beforeRainbow])
if actions[4][2] then --RGB mode
lastRainbowMode = true
for _, set in pairs(tempTheme) do
set[1] = (set[1] + 0.003) % 1
end
else
if lastRainbowMode then
lastRainbowMode = false
tempTheme = hsvTheme
end
for i, set in ipairs(hsvTheme) do
set[1] = (set[1] + actions[6][4].v / 1200) % 1
tempTheme[i] = set
end
end
theme = hsvToRgb(tempTheme)
--output
for i = 1, 5 do
output.setBool(i, not actions[i][2])
end
output.setBool(6, actions[9][2])
output.setBool(7, actions[10][2])
output.setBool(8, actions[11][2])
output.setNumber(1, math.floor(9 - actions[7][4].v))
output.setNumber(2, actions[12][4].v)
output.setNumber(3, actions[13][4].v / 100)
channel = 24
for i = 1, 3 do
for j = 1, 3 do
output.setNumber(channel, theme[i][j])
channel = channel + 1
end
end
end
function onDraw()
if not acc or app ~= 5 or isSleeping then return end
--[[* MAIN OVERLAY *]] --
c(70, 70, 70)
screen.drawRectF(0, 0, 96, 64)
local hcolor = { theme[2][1] + 25, theme[2][2] + 25, theme[2][3] + 25 }
c(table.unpack(hcolor))
screen.drawText(15, 16 - scrollPixels, "Settings")
c(100, 100, 100)
scrollable = 23 - scrollPixels
screen.drawLine(15, scrollable, 80, scrollable)
-- first pass: draw all non-dropdown elements and closed dropdowns
for i, action in pairs(actions) do
scrollable = 15 - scrollPixels + actionHeightOffsets[i]
if action[3] == 0 then --toggle
drawFullToggle(15, scrollable, action[2], action[1], theme[3], theme[1])
elseif action[3] == 1 and not open then --dropdown (closed only)
drawDropdown(15, scrollable, false, action[1], action[4], theme, theme[3], theme[1])
elseif action[3] == 2 then --slider
drawSlider(15, scrollable - 8, action[1], action[4].v, action[4].n, action[4].m, theme[3], theme[1])
end
end
-- second pass: draw open dropdowns on top
for i, action in pairs(actions) do
if action[3] == 1 and open then --dropdown (open only)
scrollable = 15 - scrollPixels + actionHeightOffsets[i]
drawDropdown(15, scrollable, true, action[1], action[4], theme, theme[3], theme[1])
end
end
--[[* CONTROLS OVERLAY *]] --
c(theme[1][1], theme[1][2], theme[1][3], 250)
screen.drawRectF(0, 15, 13, 64)
if scrollUp then c(150, 150, 150) else c(170, 170, 170) end
drawRoundedRect(1, 19, 10, 18)
if scrollDown then c(150, 150, 150) else c(170, 170, 170) end
drawRoundedRect(1, 40, 10, 18)
c(100, 100, 100)
screen.drawTriangleF(3, 29, 6, 25, 10, 29)
screen.drawTriangleF(2, 48, 6, 53, 11, 48)
end
function c(...)
_ = {...}
for i, v in pairs(_) do
_[i] = v ^ 2.2 / 255 ^ 2.2 * v
end
screen.setColor(table.unpack(_))
end
function clamp(v, l, u)
return math.min(math.max(v, l), u)
end
function isPointInRectangle(rx, ry, rw, rh)
return touchX > rx and touchY > ry and touchX < rx + rw and touchY < ry + rh
end
function drawRoundedRect(x, y, w, h)
screen.drawRectF(x + 1, y + 1, w - 1, h - 1) --body
screen.drawLine(x + 2, y, x + w - 1, y) --top
screen.drawLine(x, y + 2, x, y + h - 1) --left
screen.drawLine(x + w, y + 2, x + w, y + h - 1) --right
screen.drawLine(x + 2, y + h, x + w - 1, y + h) --bottom
end
function drawToggle(x, y, state)
if state then
c(100, 200, 100)
screen.drawLine(x + 1, y, x + 7, y)
screen.drawLine(x, y + 1, x + 6, y + 1)
screen.drawLine(x + 1, y + 2, x + 7, y + 2)
c(200, 200, 200)
screen.drawLine(x + 7, y, x + 7, y + 3)
screen.drawLine(x + 6, y + 1, x + 9, y + 1)
else
c(100, 100, 100)
screen.drawLine(x + 2, y, x + 8, y)
screen.drawLine(x + 3, y + 1, x + 9, y + 1)
screen.drawLine(x + 2, y + 2, x + 8, y + 2)
c(200, 200, 200)
screen.drawLine(x + 1, y, x + 1, y + 3)
screen.drawLine(x, y + 1, x + 3, y + 1)
end
end
function drawFullToggle(x, y, state, text, bgcolor, tcolor)
c(table.unpack(bgcolor))
drawRoundedRect(x, y, #text * 5 + 15, 8)
drawToggle(x + #text * 5 + 5, y + 3, state)
c(table.unpack(tcolor))
screen.drawText(x + 2, y + 2, text)
end
function drawDropdown(x, y, open, title, content, current, bgcolor, tcolor)
width = #title * 5 + 20
c(table.unpack(bgcolor))
drawRoundedRect(x, y, width, open and #content * 9 or 8)
c(table.unpack(tcolor))
screen.drawText(x + 9, y + 2, title)
screen.drawText(x + 2, y + 2, open and "-" or "+")
screen.drawLine(x + 7, y, x + 7, y + 9)
if open then
screen.drawLine(x, y + 8, x + width + 1, y + 8)
for i = 1, #content do
screen.drawText(x + 2, y + 2 + i * 8, content[i])
if current == i then
c(50, 50, 50, 200)
screen.drawRectF(x, y + 1 + i * 8, width + 1, 7)
end
end
end
end
function drawSlider(x, y, title, value, min, max, bgcolor, tcolor)
map = 10 + ((value - min) * ((60 - 10) / (max - min)))
c(table.unpack(bgcolor))
drawRoundedRect(x, y, 70, 16)
c(table.unpack(tcolor))
screen.drawText(x + 2, y + 2, title)
screen.drawText(x + 2, y + 10, "-")
screen.drawText(x + 65, y + 10, "+")
screen.drawLine(x + 7, y + 9, x + 7, y + 16)
screen.drawLine(x + 62, y + 9, x + 62, y + 16)
screen.drawLine(x + 10, y + 12, x + 60, y + 12)
c(100, 200, 100)
screen.drawLine(x + 10, y + 12, x + map, y + 12)
end
function rgbToHsv(theme) --GUESS what this does
converted = {}
for _, set in pairs(theme) do
r, g, b = set[1] / 255, set[2] / 255, set[3] / 255
max, min, d = math.max(r, g, b), math.min(r, g, b), math.max(r, g, b) - math.min(r, g, b)
h, s, v = 0, (max == 0 and 0 or d / max), max
if max ~= min then
h = (max == r and (g - b) / d + (g < b and 6 or 0)) or (max == g and (b - r) / d + 2) or (max == b and (r - g) / d + 4)
h = h / 6
end
converted[#converted+1] = {h, s, v}
end
return converted
end
function hsvToRgb(theme)
converted = {}
for _, set in pairs(theme) do
h, s, v = set[1], set[2], set[3]
i, f = math.floor(h * 6), h * 6 - math.floor(h * 6)
p, q, t = v * (1 - s), v * (1 - f * s), v * (1 - (1 - f) * s)
i = i % 6
r, g, b = (i == 0 and v or i == 1 and q or i == 2 and p or i == 3 and p or i == 4 and t or v),
(i == 0 and t or i == 1 and v or i == 2 and v or i == 3 and q or i == 4 and p or p),
(i == 0 and p or i == 1 and p or i == 2 and t or i == 3 and v or i == 4 and v or q)
converted[#converted+1] = {r*255, g*255, b*255}
end
return converted
end