mirror of
https://github.com/Insality/druid
synced 2025-09-27 18:12:21 +02:00
Remove checkbox, checkbox group, radio group and old default templates
This commit is contained in:
@@ -1,97 +0,0 @@
|
||||
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
|
||||
|
||||
--- Druid checkbox component
|
||||
--
|
||||
-- <a href="https://insality.github.io/druid/druid/index.html?example=general_checkboxes" target="_blank"><b>Example Link</b></a>
|
||||
-- @module Checkbox
|
||||
-- @within BaseComponent
|
||||
-- @alias druid.checkbox
|
||||
|
||||
--- On change state callback(self, state)
|
||||
-- @tfield DruidEvent on_change_state @{DruidEvent}
|
||||
|
||||
--- Visual node
|
||||
-- @tfield node node
|
||||
|
||||
--- Button trigger node
|
||||
-- @tfield node|nil click_node
|
||||
|
||||
--- Button component from click_node
|
||||
-- @tfield Button button @{Button}
|
||||
|
||||
---
|
||||
|
||||
local Event = require("druid.event")
|
||||
local component = require("druid.component")
|
||||
|
||||
local Checkbox = component.create("checkbox")
|
||||
|
||||
|
||||
local function on_click(self)
|
||||
self:set_state(not self.state)
|
||||
end
|
||||
|
||||
|
||||
--- Component style params.
|
||||
-- You can override this component styles params in druid styles table
|
||||
-- or create your own style
|
||||
-- @table style
|
||||
-- @tfield function on_change_state (self, node, state)
|
||||
function Checkbox.on_style_change(self, style)
|
||||
self.style = {}
|
||||
|
||||
self.style.on_change_state = style.on_change_state or function(_, node, state)
|
||||
gui.set_enabled(node, state)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--- The @{Checkbox} constructor
|
||||
-- @tparam Checkbox self @{Checkbox}
|
||||
-- @tparam node node Gui node
|
||||
-- @tparam function callback Checkbox callback
|
||||
-- @tparam node|nil click_node Trigger node, by default equals to node. Default: node
|
||||
-- @tparam boolean|nil initial_state The initial state of checkbox, default - false
|
||||
function Checkbox.init(self, node, callback, click_node, initial_state)
|
||||
self.druid = self:get_druid()
|
||||
self.node = self:get_node(node)
|
||||
self.click_node = self:get_node(click_node or node)
|
||||
|
||||
self.button = self.druid:new_button(self.click_node or self.node, on_click)
|
||||
self:set_state(initial_state, true, true)
|
||||
|
||||
self.on_change_state = Event(callback)
|
||||
end
|
||||
|
||||
|
||||
function Checkbox.on_layout_change(self)
|
||||
self:set_state(self.state, true)
|
||||
end
|
||||
|
||||
|
||||
--- Set checkbox state
|
||||
-- @tparam Checkbox self @{Checkbox}
|
||||
-- @tparam boolean|nil state Checkbox state
|
||||
-- @tparam boolean|nil is_silent Don't trigger on_change_state if true
|
||||
-- @tparam boolean|nil is_instant If instant checkbox change
|
||||
function Checkbox.set_state(self, state, is_silent, is_instant)
|
||||
self.state = state
|
||||
self.style.on_change_state(self, self.node, state, is_instant)
|
||||
|
||||
if not is_silent then
|
||||
self.on_change_state:trigger(self:get_context(), state)
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- Return checkbox state
|
||||
-- @tparam Checkbox self @{Checkbox}
|
||||
-- @treturn boolean Checkbox state
|
||||
function Checkbox.get_state(self)
|
||||
return self.state
|
||||
end
|
||||
|
||||
|
||||
return Checkbox
|
@@ -1,73 +0,0 @@
|
||||
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
|
||||
|
||||
--- Checkbox group module
|
||||
--
|
||||
-- <a href="https://insality.github.io/druid/druid/index.html?example=general_checkboxes" target="_blank"><b>Example Link</b></a>
|
||||
-- @module CheckboxGroup
|
||||
-- @within BaseComponent
|
||||
-- @alias druid.checkbox_group
|
||||
|
||||
--- On any checkbox click callback(self, index)
|
||||
-- @tfield DruidEvent on_checkbox_click @{DruidEvent}
|
||||
|
||||
--- Array of checkbox components
|
||||
-- @tfield table checkboxes @{Checkbox}
|
||||
|
||||
---
|
||||
|
||||
local Event = require("druid.event")
|
||||
local component = require("druid.component")
|
||||
|
||||
local CheckboxGroup = component.create("checkbox_group")
|
||||
|
||||
|
||||
--- The @{CheckboxGroup} constructor
|
||||
-- @tparam CheckboxGroup self @{CheckboxGroup}
|
||||
-- @tparam node[] nodes Array of gui node
|
||||
-- @tparam function callback Checkbox callback
|
||||
-- @tparam node[]|nil click_nodes Array of trigger nodes, by default equals to nodes
|
||||
function CheckboxGroup.init(self, nodes, callback, click_nodes)
|
||||
self.druid = self:get_druid()
|
||||
self.checkboxes = {}
|
||||
|
||||
self.on_checkbox_click = Event(callback)
|
||||
|
||||
for i = 1, #nodes do
|
||||
local click_node = click_nodes and click_nodes[i] or nil
|
||||
local checkbox = self.druid:new_checkbox(nodes[i], function()
|
||||
self.on_checkbox_click:trigger(self:get_context(), i)
|
||||
end, click_node)
|
||||
|
||||
table.insert(self.checkboxes, checkbox)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--- Set checkbox group state
|
||||
-- @tparam CheckboxGroup self @{CheckboxGroup}
|
||||
-- @tparam boolean[] indexes Array of checkbox state
|
||||
-- @tparam boolean|nil is_instant If instant state change
|
||||
function CheckboxGroup.set_state(self, indexes, is_instant)
|
||||
for i = 1, #indexes do
|
||||
if self.checkboxes[i] then
|
||||
self.checkboxes[i]:set_state(indexes[i], true, is_instant)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--- Return checkbox group state
|
||||
-- @tparam CheckboxGroup self @{CheckboxGroup}
|
||||
-- @treturn boolean[] Array if checkboxes state
|
||||
function CheckboxGroup.get_state(self)
|
||||
local result = {}
|
||||
|
||||
for i = 1, #self.checkboxes do
|
||||
table.insert(result, self.checkboxes[i]:get_state())
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
return CheckboxGroup
|
@@ -1,81 +0,0 @@
|
||||
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
|
||||
|
||||
--- Radio group module
|
||||
--
|
||||
-- <a href="https://insality.github.io/druid/druid/index.html?example=general_checkboxes" target="_blank"><b>Example Link</b></a>
|
||||
-- @module RadioGroup
|
||||
-- @within BaseComponent
|
||||
-- @alias druid.radio_group
|
||||
|
||||
--- On any checkbox click
|
||||
-- @tfield DruidEvent on_radio_click @{DruidEvent}
|
||||
|
||||
--- Array of checkbox components
|
||||
-- @tfield Checkbox[] checkboxes
|
||||
|
||||
---
|
||||
|
||||
local Event = require("druid.event")
|
||||
local component = require("druid.component")
|
||||
|
||||
local RadioGroup = component.create("radio_group")
|
||||
|
||||
|
||||
local function on_checkbox_click(self, index, is_instant)
|
||||
for i = 1, #self.checkboxes do
|
||||
self.checkboxes[i]:set_state(i == index, true, is_instant)
|
||||
end
|
||||
|
||||
self.on_radio_click:trigger(self:get_context(), index)
|
||||
end
|
||||
|
||||
|
||||
--- The @{RadioGroup} constructor
|
||||
-- @tparam RadioGroup self @{RadioGroup}
|
||||
-- @tparam node[] nodes Array of gui node
|
||||
-- @tparam function callback Radio callback
|
||||
-- @tparam node[]|nil click_nodes Array of trigger nodes, by default equals to nodes. Default - nodes
|
||||
function RadioGroup.init(self, nodes, callback, click_nodes)
|
||||
self.druid = self:get_druid()
|
||||
self.checkboxes = {}
|
||||
|
||||
self.on_radio_click = Event(callback)
|
||||
|
||||
for i = 1, #nodes do
|
||||
local click_node = click_nodes and click_nodes[i] or nil
|
||||
local checkbox = self.druid:new_checkbox(nodes[i], function()
|
||||
on_checkbox_click(self, i)
|
||||
end, click_node)
|
||||
|
||||
table.insert(self.checkboxes, checkbox)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--- Set radio group state
|
||||
-- @tparam RadioGroup self @{RadioGroup}
|
||||
-- @tparam number index Index in radio group
|
||||
-- @tparam boolean|nil is_instant If is instant state change
|
||||
function RadioGroup.set_state(self, index, is_instant)
|
||||
on_checkbox_click(self, index, is_instant)
|
||||
end
|
||||
|
||||
|
||||
--- Return radio group state
|
||||
-- @tparam RadioGroup self @{RadioGroup}
|
||||
-- @treturn number Index in radio group
|
||||
function RadioGroup.get_state(self)
|
||||
local result = -1
|
||||
|
||||
for i = 1, #self.checkboxes do
|
||||
if self.checkboxes[i]:get_state() then
|
||||
result = i
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
return RadioGroup
|
Reference in New Issue
Block a user