mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-01 17:38:41 +00:00
Add button, toggle, and option elements
This commit is contained in:
parent
c7fca1b956
commit
68612c6900
9 changed files with 407 additions and 0 deletions
106
builtin/ui/clickable_elems.lua
Normal file
106
builtin/ui/clickable_elems.lua
Normal file
|
@ -0,0 +1,106 @@
|
|||
-- Luanti
|
||||
-- SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
-- Copyright (C) 2024 v-rob, Vincent Robinson <robinsonvincent89@gmail.com>
|
||||
|
||||
ui.Button = ui._new_type(ui.Elem, "button", 0x02, true)
|
||||
|
||||
function ui.Button:_init(props)
|
||||
ui.Elem._init(self, props)
|
||||
|
||||
self._disabled = ui._opt(props.disabled, "boolean")
|
||||
self._on_press = ui._opt(props.on_press, "function")
|
||||
end
|
||||
|
||||
function ui.Button:_encode_fields()
|
||||
local fl = ui._make_flags()
|
||||
|
||||
ui._shift_flag(fl, self._disabled)
|
||||
ui._shift_flag(fl, self._on_press)
|
||||
|
||||
return ui._encode("SZ", ui.Elem._encode_fields(self), ui._encode_flags(fl))
|
||||
end
|
||||
|
||||
ui.Button._handlers[0x00] = function(self, ev, data)
|
||||
return self._on_press
|
||||
end
|
||||
|
||||
ui.Toggle = ui._new_type(ui.Elem, "toggle", 0x03, true)
|
||||
|
||||
ui.Check = ui.derive_elem(ui.Toggle, "check")
|
||||
ui.Switch = ui.derive_elem(ui.Toggle, "switch")
|
||||
|
||||
function ui.Toggle:_init(props)
|
||||
ui.Elem._init(self, props)
|
||||
|
||||
self._disabled = ui._opt(props.disabled, "boolean")
|
||||
self._selected = ui._opt(props.selected, "boolean")
|
||||
|
||||
self._on_press = ui._opt(props.on_press, "function")
|
||||
self._on_change = ui._opt(props.on_change, "function")
|
||||
end
|
||||
|
||||
function ui.Toggle:_encode_fields()
|
||||
local fl = ui._make_flags()
|
||||
|
||||
ui._shift_flag(fl, self._disabled)
|
||||
ui._shift_flag_bool(fl, self._selected)
|
||||
|
||||
ui._shift_flag(fl, self._on_press)
|
||||
ui._shift_flag(fl, self._on_change)
|
||||
|
||||
return ui._encode("SZ", ui.Elem._encode_fields(self), ui._encode_flags(fl))
|
||||
end
|
||||
|
||||
ui.Toggle._handlers[0x00] = function(self, ev, data)
|
||||
return self._on_press
|
||||
end
|
||||
|
||||
ui.Toggle._handlers[0x01] = function(self, ev, data)
|
||||
local selected = ui._decode("B", data)
|
||||
ev.selected = selected ~= 0
|
||||
|
||||
return self._on_change
|
||||
end
|
||||
|
||||
ui.Option = ui._new_type(ui.Elem, "option", 0x04, true)
|
||||
|
||||
ui.Radio = ui.derive_elem(ui.Option, "radio")
|
||||
|
||||
function ui.Option:_init(props)
|
||||
ui.Elem._init(self, props)
|
||||
|
||||
self._disabled = ui._opt(props.disabled, "boolean")
|
||||
self._selected = ui._opt(props.selected, "boolean")
|
||||
|
||||
self._family = ui._opt(props.family, "id")
|
||||
|
||||
self._on_press = ui._opt(props.on_press, "function")
|
||||
self._on_change = ui._opt(props.on_change, "function")
|
||||
end
|
||||
|
||||
function ui.Option:_encode_fields()
|
||||
local fl = ui._make_flags()
|
||||
|
||||
ui._shift_flag(fl, self._disabled)
|
||||
ui._shift_flag_bool(fl, self._selected)
|
||||
|
||||
if ui._shift_flag(fl, self._family) then
|
||||
ui._encode_flag(fl, "z", self._family)
|
||||
end
|
||||
|
||||
ui._shift_flag(fl, self._on_press)
|
||||
ui._shift_flag(fl, self._on_change)
|
||||
|
||||
return ui._encode("SZ", ui.Elem._encode_fields(self), ui._encode_flags(fl))
|
||||
end
|
||||
|
||||
ui.Option._handlers[0x00] = function(self, ev, data)
|
||||
return self._on_press
|
||||
end
|
||||
|
||||
ui.Option._handlers[0x01] = function(self, ev, data)
|
||||
local selected = ui._decode("B", data)
|
||||
ev.selected = selected ~= 0
|
||||
|
||||
return self._on_change
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue