mirror of
https://github.com/Insality/druid
synced 2025-06-27 10:27:48 +02:00
Merge pull request #27 from AGulev/feature/D16-checkbox
Feature/d16 checkbox
This commit is contained in:
commit
26d80828a1
50
druid/base/checkbox.lua
Normal file
50
druid/base/checkbox.lua
Normal file
@ -0,0 +1,50 @@
|
||||
--- Druid checkbox component
|
||||
-- @module base.checkbox
|
||||
|
||||
local helper = require("druid.helper")
|
||||
|
||||
local M = {}
|
||||
|
||||
|
||||
local function state_animate(node, state)
|
||||
local target = state and 1 or 0
|
||||
gui.animate(node, "color.w", target, gui.EASING_OUTSINE, 0.1)
|
||||
end
|
||||
|
||||
|
||||
function M.set_state(self, state, is_silence)
|
||||
if self.state == state then
|
||||
return
|
||||
end
|
||||
|
||||
self.state = state
|
||||
state_animate(self.node, state)
|
||||
|
||||
if not is_silence and self.callback then
|
||||
self.callback(self.parent.parent, state)
|
||||
end
|
||||
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)
|
||||
end
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
return M
|
49
druid/base/checkbox_group.lua
Normal file
49
druid/base/checkbox_group.lua
Normal file
@ -0,0 +1,49 @@
|
||||
--- Checkboux group module
|
||||
-- @module base.checkbox_group
|
||||
|
||||
local M = {}
|
||||
|
||||
|
||||
local function on_checkbox_click(self, index)
|
||||
if self.callback then
|
||||
self.callback(self.parent.parent, index)
|
||||
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, click_nodes)
|
||||
self.checkboxes = {}
|
||||
self.callback = callback
|
||||
|
||||
for i = 1, #nodes do
|
||||
local click_node = click_nodes and click_nodes[i] or nil
|
||||
local checkbox = self.parent:new_checkbox(nodes[i], function()
|
||||
on_checkbox_click(self, i)
|
||||
end, click_node)
|
||||
|
||||
table.insert(self.checkboxes, checkbox)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return M
|
@ -23,6 +23,8 @@ M.comps = {
|
||||
progress = require("druid.base.progress"),
|
||||
grid = require("druid.base.grid"),
|
||||
scroll = require("druid.base.scroll"),
|
||||
checkbox = require("druid.base.checkbox"),
|
||||
checkbox_group = require("druid.base.checkbox_group"),
|
||||
slider = require("druid.base.slider"),
|
||||
|
||||
progress_rich = require("druid.rich.progress_rich"),
|
||||
|
Loading…
x
Reference in New Issue
Block a user