mirror of
https://github.com/Insality/druid
synced 2025-06-27 10:27:48 +02:00
add checkbox group component
This commit is contained in:
parent
6cebc9d0c9
commit
c048224a1f
@ -26,6 +26,11 @@ function M.set_state(self, state, is_silence)
|
||||
end
|
||||
|
||||
|
||||
function M.get_state(self)
|
||||
return self.state
|
||||
end
|
||||
|
||||
|
||||
-- TODO: pass self as first parameter
|
||||
local function on_click(context, self)
|
||||
M.set_state(self, not self.state)
|
||||
@ -36,6 +41,7 @@ function M.init(self, node, callback, click_node)
|
||||
self.node = helper.get_node(node)
|
||||
self.click_node = helper.get_node(click_node)
|
||||
self.callback = callback
|
||||
|
||||
self.button = self.parent:new_button(self.click_node or self.node, on_click, self)
|
||||
M.set_state(self, false, true)
|
||||
end
|
||||
|
47
druid/base/checkbox_group.lua
Normal file
47
druid/base/checkbox_group.lua
Normal file
@ -0,0 +1,47 @@
|
||||
local M = {}
|
||||
|
||||
|
||||
local function on_checkbox_click(self, index)
|
||||
if self.is_radio_mode then
|
||||
for i = 1, #self.checkboxes do
|
||||
self.checkboxes[i]:set_state(i == index, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function M.set_state(self, indexes)
|
||||
for i = 1, #indexes do
|
||||
if self.checkbox[indexes[i]] then
|
||||
self.checkbox[indexes[i]]:set_state(true, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function M.get_state(self)
|
||||
local result = {}
|
||||
|
||||
for i = 1, #self.checkboxes do
|
||||
table.insert(result, self.checkboxes[i]:get_state())
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
function M.init(self, nodes, callback, is_radio_mode, anim_nodes)
|
||||
self.is_radio_mode = is_radio_mode
|
||||
self.checkboxes = {}
|
||||
|
||||
for i = 1, #nodes do
|
||||
local anim_node = anim_nodes and anim_nodes[i] or nil
|
||||
local checkbox = self.parent:new_checkbox(nodes[i], function()
|
||||
on_checkbox_click(self, i)
|
||||
end, anim_node)
|
||||
table.insert(self.checkboxes, checkbox)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return M
|
@ -24,6 +24,7 @@ M.comps = {
|
||||
scroll = require("druid.base.scroll"),
|
||||
slider = require("druid.base.slider"),
|
||||
checkbox = require("druid.base.checkbox"),
|
||||
checkbox_group = require("druid.base.checkbox_group"),
|
||||
|
||||
progress_rich = require("druid.rich.progress_rich"),
|
||||
}
|
||||
|
@ -70,12 +70,15 @@ end
|
||||
|
||||
|
||||
local function setup_checkbox(self)
|
||||
self.druid:new_checkbox("radio1/check", nil, "radio1/back")
|
||||
self.druid:new_checkbox("radio2/check", nil, "radio2/back")
|
||||
self.druid:new_checkbox("radio3/check", nil, "radio3/back")
|
||||
self.druid:new_checkbox("checkbox1/check", nil, "checkbox1/back")
|
||||
self.druid:new_checkbox("checkbox2/check", nil, "checkbox2/back")
|
||||
self.druid:new_checkbox("checkbox3/check", nil, "checkbox3/back")
|
||||
self.druid:new_checkbox_group(
|
||||
{"radio1/check", "radio2/check", "radio3/check" },
|
||||
nil, true,
|
||||
{"radio1/back", "radio2/back", "radio3/back"})
|
||||
|
||||
self.druid:new_checkbox_group(
|
||||
{"checkbox1/check", "checkbox2/check", "checkbox3/check" },
|
||||
nil, false,
|
||||
{"checkbox1/back", "checkbox2/back", "checkbox3/back"})
|
||||
end
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user