feat: Refactor GUI demo to simplify button handling and improve status display

This commit is contained in:
2026-01-05 13:21:07 +00:00
parent e37f9ca0eb
commit cfc658f680

View File

@@ -1,21 +1,7 @@
local Gui = require('gui')
local math3d = require('math3d')
local ctx = Gui.newContext()
local input = Gui.newInputState()
local textState = Gui.newTextState('')
local listState = Gui.newListState()
local items = {
'Dashboard Setup',
'Input Streams',
'Telemetry',
'Power Profile',
'Diagnostics',
'Release Notes',
}
local statusMessage = 'Idle'
local selectedItem = items[1]
local rotationSpeeds = {x = 0.45, y = 0.65}
local function log_trace(fmt, ...)
if not lua_debug or not fmt then
@@ -24,121 +10,105 @@ local function log_trace(fmt, ...)
print(string.format(fmt, ...))
end
local cubeVertices = {
{ position = {-1.0, -1.0, -1.0}, color = {1.0, 0.2, 0.4} },
{ position = {1.0, -1.0, -1.0}, color = {0.2, 0.9, 0.4} },
{ position = {1.0, 1.0, -1.0}, color = {0.3, 0.4, 0.9} },
{ position = {-1.0, 1.0, -1.0}, color = {1.0, 0.8, 0.2} },
{ position = {-1.0, -1.0, 1.0}, color = {0.9, 0.4, 0.5} },
{ position = {1.0, -1.0, 1.0}, color = {0.4, 0.9, 0.5} },
{ position = {1.0, 1.0, 1.0}, color = {0.6, 0.8, 1.0} },
{ position = {-1.0, 1.0, 1.0}, color = {0.4, 0.4, 0.4} },
local buttonStates = {
button1 = false,
button2 = false,
button3 = false,
button4 = false
}
local cubeIndices = {
1, 2, 3, 3, 4, 1,
5, 6, 7, 7, 8, 5,
1, 5, 8, 8, 4, 1,
2, 6, 7, 7, 3, 2,
4, 3, 7, 7, 8, 4,
1, 2, 6, 6, 5, 1,
}
local statusMessage = 'Ready'
local shaderVariants = {
default = {
vertex = 'shaders/cube.vert.spv',
fragment = 'shaders/cube.frag.spv',
},
}
local function drawTestButtons()
-- Background panel
ctx:pushRect({x = 50, y = 50, width = 400, height = 400}, {
color = {0.1, 0.1, 0.1, 0.9},
borderColor = {0.5, 0.5, 0.5, 1.0},
})
local camera = {
eye = {2.0, 2.0, 3.0},
center = {0.0, 0.0, 0.0},
up = {0.0, 1.0, 0.0},
fov = 0.78,
near = 0.1,
far = 10.0,
}
-- Title
Gui.text(ctx, {x = 70, y = 70, width = 360, height = 40}, "2D Test Buttons", {
fontSize = 28,
alignX = "center",
color = {1.0, 1.0, 1.0, 1.0},
})
local function buildModel(time)
local y = math3d.rotation_y(time * rotationSpeeds.y)
local x = math3d.rotation_x(time * rotationSpeeds.x)
return math3d.multiply(y, x)
end
local function createCube(position)
local function computeModel(time)
local base = buildModel(time)
local offset = math3d.translation(position[1], position[2], position[3])
return math3d.multiply(offset, base)
-- Button 1
if Gui.button(ctx, "btn1", {x = 100, y = 130, width = 150, height = 50}, "Button 1") then
buttonStates.button1 = not buttonStates.button1
statusMessage = "Button 1 " .. (buttonStates.button1 and "pressed" or "released")
log_trace("Button 1 clicked: %s", statusMessage)
end
-- Button 2
if Gui.button(ctx, "btn2", {x = 270, y = 130, width = 150, height = 50}, "Button 2") then
buttonStates.button2 = not buttonStates.button2
statusMessage = "Button 2 " .. (buttonStates.button2 and "pressed" or "released")
log_trace("Button 2 clicked: %s", statusMessage)
end
-- Button 3
if Gui.button(ctx, "btn3", {x = 100, y = 200, width = 150, height = 50}, "Button 3") then
buttonStates.button3 = not buttonStates.button3
statusMessage = "Button 3 " .. (buttonStates.button3 and "pressed" or "released")
log_trace("Button 3 clicked: %s", statusMessage)
end
-- Button 4
if Gui.button(ctx, "btn4", {x = 270, y = 200, width = 150, height = 50}, "Button 4") then
buttonStates.button4 = not buttonStates.button4
statusMessage = "Button 4 " .. (buttonStates.button4 and "pressed" or "released")
log_trace("Button 4 clicked: %s", statusMessage)
end
-- Status display
Gui.text(ctx, {x = 70, y = 280, width = 360, height = 30}, "Status: " .. statusMessage, {
fontSize = 18,
alignX = "center",
color = {0.8, 0.8, 1.0, 1.0},
})
-- Button states display
local statesText = string.format("States: 1:%s 2:%s 3:%s 4:%s",
buttonStates.button1 and "ON" or "OFF",
buttonStates.button2 and "ON" or "OFF",
buttonStates.button3 and "ON" or "OFF",
buttonStates.button4 and "ON" or "OFF")
Gui.text(ctx, {x = 70, y = 320, width = 360, height = 30}, statesText, {
fontSize = 16,
alignX = "center",
color = {1.0, 0.8, 0.8, 1.0},
})
-- Reset button
if Gui.button(ctx, "reset", {x = 175, y = 370, width = 100, height = 40}, "Reset") then
buttonStates.button1 = false
buttonStates.button2 = false
buttonStates.button3 = false
buttonStates.button4 = false
statusMessage = "All buttons reset"
log_trace("Reset button clicked")
end
return {
vertices = cubeVertices,
indices = cubeIndices,
compute_model_matrix = computeModel,
shader_key = 'default',
}
end
gui_context = ctx
gui_input = input
function get_scene_objects()
return { createCube({0.0, 0.0, -4.0}) }
return {} -- No 3D objects
end
function get_shader_paths()
return shaderVariants
return {} -- No shaders needed for 2D
end
function get_view_projection(aspect)
local view = math3d.look_at(camera.eye, camera.center, camera.up)
local projection = math3d.perspective(camera.fov, aspect, camera.near, camera.far)
return math3d.multiply(projection, view)
end
local function drawPanel()
ctx:pushRect({x = 10, y = 10, width = 460, height = 520}, {
color = {0.06, 0.07, 0.09, 0.9},
borderColor = {0.35, 0.38, 0.42, 1.0},
})
Gui.text(ctx, {x = 30, y = 30, width = 420, height = 30}, "Command Console", {
fontSize = 24,
alignX = "left",
color = {0.95, 0.95, 0.95, 1.0},
})
Gui.svg(ctx, {x = 320, y = 30, width = 120, height = 120}, "assets/logo.svg")
textState = Gui.textbox(ctx, "search_field", {x = 30, y = 80, width = 420, height = 40}, textState, {
placeholder = 'Filter modules...',
onSubmit = function(text)
statusMessage = 'Searching for: ' .. (text ~= '' and text or 'anything')
log_trace("GuiDemo search submitted: %s", statusMessage)
end,
})
listState = Gui.listView(ctx, "menu_list", {x = 30, y = 140, width = 420, height = 240}, items, listState, {
onSelect = function(idx, item)
selectedItem = item
statusMessage = "Ready to adjust " .. item
log_trace("GuiDemo selection changed: idx=%d item=%s", idx, item)
end,
scrollToSelection = true,
})
if Gui.button(ctx, "apply", {x = 30, y = 400, width = 200, height = 38}, "Apply Settings") then
statusMessage = "Applied configuration for " .. (selectedItem or "-")
log_trace("GuiDemo apply clicked: %s", statusMessage)
end
Gui.text(ctx, {x = 30, y = 448, width = 420, height = 24}, "Status: " .. statusMessage, {
color = {0.6, 0.9, 1.0, 1.0},
alignY = "top",
})
return {} -- No 3D projection needed
end
function get_gui_commands()
ctx:beginFrame(input)
drawPanel()
drawTestButtons()
ctx:endFrame()
return ctx:getCommands()
end