mirror of
https://github.com/Insality/druid
synced 2025-06-27 18:37:45 +02:00
Code style refactor
This commit is contained in:
parent
32bbddb706
commit
2b534cc205
15
README.md
15
README.md
@ -100,9 +100,9 @@ end
|
|||||||
Basic custom component template looks like this:
|
Basic custom component template looks like this:
|
||||||
```lua
|
```lua
|
||||||
local const = require("druid.const")
|
local const = require("druid.const")
|
||||||
|
local component = require("druid.system.component")
|
||||||
|
|
||||||
local M = {}
|
local M = component.new("amazing_component", { const.ON_INPUT })
|
||||||
M.interest = { const.ON_INPUT }
|
|
||||||
|
|
||||||
function M.init(self, ...)
|
function M.init(self, ...)
|
||||||
-- Component constructor
|
-- Component constructor
|
||||||
@ -137,8 +137,9 @@ On each component recomended describe component schema in next way:
|
|||||||
```lua
|
```lua
|
||||||
-- Component module
|
-- Component module
|
||||||
local helper = require("druid.helper")
|
local helper = require("druid.helper")
|
||||||
|
local component = require("druid.system.component")
|
||||||
|
|
||||||
local M = {}
|
local M = component.new("new_component")
|
||||||
|
|
||||||
local SCHEME = {
|
local SCHEME = {
|
||||||
ROOT = "/root",
|
ROOT = "/root",
|
||||||
@ -146,20 +147,18 @@ local SCHEME = {
|
|||||||
TITLE = "/title"
|
TITLE = "/title"
|
||||||
}
|
}
|
||||||
|
|
||||||
-- TODO: Rework self.template/self.nodes
|
|
||||||
-- Make self._inner_data? { component_name, template, nodes }
|
|
||||||
function M.init(self, template_name, node_table)
|
function M.init(self, template_name, node_table)
|
||||||
-- If component use template, setup it:
|
-- If component use template, setup it:
|
||||||
self.template = template_name
|
self:set_template(template_name)
|
||||||
|
|
||||||
-- If component was cloned with gui.clone_tree, pass his nodes
|
-- If component was cloned with gui.clone_tree, pass his nodes
|
||||||
self.nodes = node_table
|
self:set_nodes(node_table)
|
||||||
|
|
||||||
-- helper can get node from gui/template/table
|
-- helper can get node from gui/template/table
|
||||||
local root = helper.node(self, SCHEME.ROOT)
|
local root = helper.node(self, SCHEME.ROOT)
|
||||||
|
|
||||||
-- This component can spawn another druid components:
|
-- This component can spawn another druid components:
|
||||||
local druid = helper.get_druid(self)
|
local druid = self:get_druid(self)
|
||||||
-- Button self on callback is self of _this_ component
|
-- Button self on callback is self of _this_ component
|
||||||
local button = druid:new_button(...)
|
local button = druid:new_button(...)
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ local log = settings.log
|
|||||||
|
|
||||||
|
|
||||||
--- Basic components
|
--- Basic components
|
||||||
M.comps = {
|
M.components = {
|
||||||
button = require("druid.base.button"),
|
button = require("druid.base.button"),
|
||||||
blocker = require("druid.base.blocker"),
|
blocker = require("druid.base.blocker"),
|
||||||
back_handler = require("druid.base.back_handler"),
|
back_handler = require("druid.base.back_handler"),
|
||||||
@ -34,7 +34,7 @@ M.comps = {
|
|||||||
|
|
||||||
|
|
||||||
local function register_basic_components()
|
local function register_basic_components()
|
||||||
for k, v in pairs(M.comps) do
|
for k, v in pairs(M.components) do
|
||||||
if not druid_instance["new_" .. k] then
|
if not druid_instance["new_" .. k] then
|
||||||
M.register(k, v)
|
M.register(k, v)
|
||||||
else
|
else
|
||||||
@ -60,25 +60,25 @@ end
|
|||||||
|
|
||||||
--- Create Druid instance for creating components
|
--- Create Druid instance for creating components
|
||||||
-- @return instance with all ui components
|
-- @return instance with all ui components
|
||||||
function M.new(component_script, style)
|
function M.new(context, style)
|
||||||
if register_basic_components then
|
if register_basic_components then
|
||||||
register_basic_components()
|
register_basic_components()
|
||||||
register_basic_components = false
|
register_basic_components = false
|
||||||
end
|
end
|
||||||
local self = setmetatable({}, { __index = druid_instance })
|
local druid = setmetatable({}, { __index = druid_instance })
|
||||||
|
|
||||||
-- Druid context here (who created druid)
|
-- Druid context here (who created druid)
|
||||||
-- Usually gui_script, but can be component from self:get_druid()
|
-- Usually gui_script, but can be component from self:get_druid()
|
||||||
self._context = component_script
|
druid._context = context
|
||||||
self._style = style or settings.default_style
|
druid._style = style or settings.default_style
|
||||||
|
|
||||||
-- TODO: Find the better way to handle components
|
-- TODO: Find the better way to handle components
|
||||||
-- All component list
|
-- All component list
|
||||||
self.all_components = {}
|
druid.all_components = {}
|
||||||
-- Map: interest: {components}
|
-- Map: interest: {components}
|
||||||
self.components = {}
|
druid.components = {}
|
||||||
|
|
||||||
return self
|
return druid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
--- Druid helper module
|
--- Druid helper module
|
||||||
-- @module helper
|
-- @module helper
|
||||||
|
|
||||||
|
|
||||||
local const = require("druid.const")
|
local const = require("druid.const")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
@ -71,22 +70,11 @@ function M.centrate_icon_with_text(icon_node, text_node, margin)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- call callback after count calls
|
|
||||||
function M.after(count, callback)
|
|
||||||
local closure = function()
|
|
||||||
count = count - 1
|
|
||||||
if count <= 0 then
|
|
||||||
callback()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return closure
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function M.get_node(node_or_name)
|
function M.get_node(node_or_name)
|
||||||
if type(node_or_name) == const.STRING then
|
if type(node_or_name) == const.STRING then
|
||||||
return gui.get_node(node_or_name)
|
return gui.get_node(node_or_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
return node_or_name
|
return node_or_name
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -97,9 +85,10 @@ end
|
|||||||
-- template или таблица нод после gui.clone_tree)
|
-- template или таблица нод после gui.clone_tree)
|
||||||
function M.node(component, name)
|
function M.node(component, name)
|
||||||
local template_name = component._meta.template or const.EMPTY_STRING
|
local template_name = component._meta.template or const.EMPTY_STRING
|
||||||
|
local nodes = component._meta.nodes
|
||||||
|
|
||||||
if component._meta.nodes then
|
if nodes then
|
||||||
return component._meta.nodes[template_name .. name]
|
return nodes[template_name .. name]
|
||||||
else
|
else
|
||||||
return gui.get_node(template_name .. name)
|
return gui.get_node(template_name .. name)
|
||||||
end
|
end
|
||||||
@ -139,6 +128,7 @@ function M.sign(val)
|
|||||||
if val == 0 then
|
if val == 0 then
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
return (val < 0) and -1 or 1
|
return (val < 0) and -1 or 1
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -156,6 +146,7 @@ function M.is_enabled(node)
|
|||||||
is_enabled = is_enabled and gui.is_enabled(parent)
|
is_enabled = is_enabled and gui.is_enabled(parent)
|
||||||
parent = gui.get_parent(parent)
|
parent = gui.get_parent(parent)
|
||||||
end
|
end
|
||||||
|
|
||||||
return is_enabled
|
return is_enabled
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -80,24 +80,13 @@ function M.remove(self, instance)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Called on_message
|
--- Druid instance update function
|
||||||
function M.on_message(self, message_id, message, sender)
|
-- @function druid:update(dt)
|
||||||
local specific_ui_message = const.SPECIFIC_UI_MESSAGES[message_id]
|
function M.update(self, dt)
|
||||||
if specific_ui_message then
|
local array = self.components[const.ON_UPDATE]
|
||||||
local array = self.components[message_id]
|
|
||||||
if array then
|
|
||||||
local item
|
|
||||||
for i = 1, #array do
|
|
||||||
item = array[i]
|
|
||||||
item[specific_ui_message](item, message, sender)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
local array = self.components[const.ON_MESSAGE]
|
|
||||||
if array then
|
if array then
|
||||||
for i = 1, #array do
|
for i = 1, #array do
|
||||||
array[i]:on_message(message_id, message, sender)
|
array[i]:update(dt)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -129,8 +118,11 @@ local function match_event(action_id, events)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Called ON_INPUT
|
--- Druid instance on_input function
|
||||||
|
-- @function druid:on_input(action_id, action)
|
||||||
function M.on_input(self, action_id, action)
|
function M.on_input(self, action_id, action)
|
||||||
|
-- TODO: расписать отличия ON_SWIPE и ON_INPUT
|
||||||
|
-- Почему-то некоторые используют ON_SWIPE, а логичнее ON_INPUT? (blocker, slider)
|
||||||
local array = self.components[const.ON_SWIPE]
|
local array = self.components[const.ON_SWIPE]
|
||||||
if array then
|
if array then
|
||||||
local v, result
|
local v, result
|
||||||
@ -161,12 +153,25 @@ function M.on_input(self, action_id, action)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Called on_update
|
--- Druid instance on_message function
|
||||||
function M.update(self, dt)
|
-- @function druid:on_message(message_id, message, sender)
|
||||||
local array = self.components[const.ON_UPDATE]
|
function M.on_message(self, message_id, message, sender)
|
||||||
|
local specific_ui_message = const.SPECIFIC_UI_MESSAGES[message_id]
|
||||||
|
if specific_ui_message then
|
||||||
|
local array = self.components[message_id]
|
||||||
|
if array then
|
||||||
|
local item
|
||||||
|
for i = 1, #array do
|
||||||
|
item = array[i]
|
||||||
|
item[specific_ui_message](item, message, sender)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local array = self.components[const.ON_MESSAGE]
|
||||||
if array then
|
if array then
|
||||||
for i = 1, #array do
|
for i = 1, #array do
|
||||||
array[i]:update(dt)
|
array[i]:on_message(message_id, message, sender)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user