Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
137 lines (122 loc) · 3.78 KB
/
Copy pathmain.lua
File metadata and controls
137 lines (122 loc) · 3.78 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
-- Multi-File Kit: demonstrates require(), bsx.json, sandboxed bsx.fs, bsx.ui v3 tabs/color, and bsx.draw v2 font_scale.
local fmt = require('helpers.format')
local THEME_DIR = 'presets'
local THEME_FILE = 'presets/theme.json'
local state = {
title = 'BSX Multi-File Kit',
accent = '#64E6FFFF',
scale = 1.35,
show_banner = false,
status = 'Ready',
files_summary = '(none)',
}
local function refresh_files()
local ok, list = pcall(bsx.fs.list, THEME_DIR)
state.files_summary = ok and fmt.summarize_files(list) or '(unavailable)'
end
local function load_theme()
if not bsx.fs.exists(THEME_FILE) then
refresh_files()
return false, 'no saved theme yet'
end
local ok_read, raw = pcall(bsx.fs.read, THEME_FILE)
if not ok_read or type(raw) ~= 'string' then
refresh_files()
return false, tostring(raw)
end
local decoded, err = bsx.json.decode(raw)
if type(decoded) ~= 'table' then
refresh_files()
return false, tostring(err)
end
if type(decoded.title) == 'string' and decoded.title ~= '' then
state.title = decoded.title
end
state.accent = fmt.sanitize_hex(decoded.accent, state.accent)
if type(decoded.scale) == 'number' then
state.scale = math.max(0.5, math.min(4.0, decoded.scale))
end
refresh_files()
return true, 'loaded presets/theme.json'
end
local function save_theme()
if not bsx.fs.exists(THEME_DIR) then
pcall(bsx.fs.mkdir, THEME_DIR)
end
local encoded, err = bsx.json.encode({
title = state.title,
accent = state.accent,
scale = state.scale,
}, { pretty = true })
if not encoded then
return false, tostring(err)
end
local ok_write, werr = pcall(bsx.fs.write, THEME_FILE, encoded)
refresh_files()
if not ok_write then
return false, tostring(werr)
end
return true, 'saved presets/theme.json'
end
load_theme()
if not bsx.fs.exists(THEME_FILE) then
save_theme()
end
local panel = bsx.ui.panel('Multi-File Kit')
local status_lbl
local files_lbl
local function update_labels(msg)
state.status = msg or state.status
if panel and status_lbl then
panel:set_text(status_lbl, 'Status: ' .. state.status)
end
if panel and files_lbl then
panel:set_text(files_lbl, 'data/' .. THEME_DIR .. ': ' .. state.files_summary)
end
end
if panel then
panel:begin_tab_bar('kit_tabs')
panel:tab('Theme & Draw')
panel:input_text('Banner Title', state.title, function(text)
if text ~= '' then
state.title = text
end
end)
panel:color('Accent RGBA', state.accent, function(_, hex)
state.accent = fmt.sanitize_hex(hex, state.accent)
end)
panel:slider('Draw Font Scale', 0.5, 3.0, state.scale, function(v)
state.scale = math.max(0.5, math.min(4.0, tonumber(v) or 1.0))
end)
panel:checkbox('Show Foreground Scaled Banner', state.show_banner, function(v)
state.show_banner = (v == true)
if not state.show_banner then
pcall(bsx.draw.clear)
end
end)
panel:end_tab()
panel:tab('Files (bsx.fs + JSON)')
files_lbl = panel:text('data/' .. THEME_DIR .. ': ' .. state.files_summary)
status_lbl = panel:text('Status: ' .. state.status)
panel:button('Save Theme JSON', function()
local ok, msg = save_theme()
update_labels(ok and msg or ('Save failed: ' .. msg))
end)
panel:same_line()
panel:button('Reload Theme JSON', function()
local ok, msg = load_theme()
update_labels(ok and msg or ('Load failed: ' .. msg))
end)
panel:end_tab()
panel:end_tab_bar()
end
bsx.on('frame', function()
if state.show_banner then
pcall(bsx.draw.clear)
pcall(bsx.draw.text, 28, 86, fmt.format_banner(state), state.accent, state.scale)
end
end)
bsx.log('MULTI_FILE_KIT_READY files=' .. state.files_summary .. ' scale=' .. string.format('%.2f', state.scale))
bsx.on('world_ready', function()
bsx.log('MULTI_FILE_KIT_WORLD_READY files=' .. state.files_summary)
end)