mirror of
https://github.com/Insality/druid
synced 2025-06-27 18:37:45 +02:00
Refactoring: step 1
druid.lua: - `factory` replaced with `self` - set new name for metatable - replaced `ipairs` with `for `i loop data.lua: - renamed custom messages
This commit is contained in:
parent
fdbacd033d
commit
4ce8e85d15
@ -4,7 +4,7 @@ local helper = require("druid.helper")
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
M.interest = {
|
M.interest = {
|
||||||
data.TRANSLATABLE,
|
data.ON_CHANGE_LANGUAGE,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,20 +1,37 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
-- actions
|
||||||
|
|
||||||
M.A_TOUCH = hash("touch")
|
M.A_TOUCH = hash("touch")
|
||||||
M.A_TEXT = hash("text")
|
M.A_TEXT = hash("text")
|
||||||
M.A_BACKSPACE = hash("backspace")
|
M.A_BACKSPACE = hash("backspace")
|
||||||
M.A_ENTER = hash("enter")
|
M.A_ENTER = hash("enter")
|
||||||
M.A_ANDR_BACK = hash("back")
|
M.A_ANDR_BACK = hash("back")
|
||||||
|
|
||||||
-- interest
|
|
||||||
M.LAYOUT_CHANGED = hash("layout_changed")
|
|
||||||
M.ON_MESSAGE = hash("on_message")
|
|
||||||
M.ON_INPUT = hash("on_input")
|
|
||||||
M.ON_SWIPE = hash("on_swipe")
|
|
||||||
M.ON_UPDATE = hash("on_update")
|
|
||||||
M.TRANSLATABLE = hash("TRANSLATABLE")
|
|
||||||
|
|
||||||
M.RELEASED = "released"
|
M.RELEASED = "released"
|
||||||
M.PRESSED = "pressed"
|
M.PRESSED = "pressed"
|
||||||
|
|
||||||
|
--- interests
|
||||||
|
|
||||||
|
M.ON_MESSAGE = hash("on_message")
|
||||||
|
M.ON_UPDATE = hash("on_update")
|
||||||
|
|
||||||
|
-- input
|
||||||
|
M.ON_SWIPE = hash("on_swipe")
|
||||||
|
M.ON_INPUT = hash("on_input")
|
||||||
|
|
||||||
|
M.ui_input = {
|
||||||
|
[M.ON_SWIPE] = true,
|
||||||
|
[M.ON_INPUT] = true
|
||||||
|
|
||||||
|
}
|
||||||
|
-- ui messages
|
||||||
|
M.ON_CHANGE_LANGUAGE = hash("on_change_language")
|
||||||
|
M.ON_LAYOUT_CHANGED = hash("on_layout_changed")
|
||||||
|
|
||||||
|
M.specific_ui_messages = {
|
||||||
|
[M.ON_CHANGE_LANGUAGE] = "on_change_language",
|
||||||
|
[M.ON_LAYOUT_CHANGED] = "on_layout_changed"
|
||||||
|
}
|
||||||
|
|
||||||
return M
|
return M
|
128
druid/druid.lua
128
druid/druid.lua
@ -5,7 +5,7 @@ local settings = require("druid.settings")
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local log = settings.log
|
local log = settings.log
|
||||||
local _factory = {}
|
local _fct_metatable = {}
|
||||||
|
|
||||||
M.comps = {
|
M.comps = {
|
||||||
button = require("druid.base.button"),
|
button = require("druid.base.button"),
|
||||||
@ -22,7 +22,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.comps) do
|
||||||
if not _factory["new_" .. k] then
|
if not _fct_metatable["new_" .. k] then
|
||||||
M.register(k, v)
|
M.register(k, v)
|
||||||
else
|
else
|
||||||
log("Basic component", k, "already registered")
|
log("Basic component", k, "already registered")
|
||||||
@ -33,8 +33,8 @@ end
|
|||||||
|
|
||||||
function M.register(name, module)
|
function M.register(name, module)
|
||||||
-- TODO: Find better solution to creating elements?
|
-- TODO: Find better solution to creating elements?
|
||||||
_factory["new_" .. name] = function(factory, ...)
|
_fct_metatable["new_" .. name] = function(self, ...)
|
||||||
return _factory.new(factory, module, ...)
|
return _fct_metatable.new(self, module, ...)
|
||||||
end
|
end
|
||||||
log("Register component", name)
|
log("Register component", name)
|
||||||
end
|
end
|
||||||
@ -42,57 +42,64 @@ end
|
|||||||
|
|
||||||
--- Create UI instance for ui elements
|
--- Create UI instance for ui elements
|
||||||
-- @return instance with all ui components
|
-- @return instance with all ui components
|
||||||
function M.new(self)
|
function M.new(component_script)
|
||||||
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 factory = setmetatable({}, {__index = _factory})
|
local self = setmetatable({}, {__index = _fct_metatable})
|
||||||
factory.parent = self
|
self.parent = component_script
|
||||||
return factory
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function input_init(factory)
|
local function input_init(self)
|
||||||
if not factory.input_inited then
|
if not self.input_inited then
|
||||||
factory.input_inited = true
|
self.input_inited = true
|
||||||
druid_input.focus()
|
druid_input.focus()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function create(module, factory)
|
local function create(self, module)
|
||||||
local instance = setmetatable({}, {__index = module})
|
local instance = setmetatable({}, {__index = module})
|
||||||
instance.parent = factory
|
instance.parent = self
|
||||||
factory[#factory + 1] = instance
|
self[#self + 1] = instance
|
||||||
|
|
||||||
local register_to = module.interest or {}
|
local register_to = module.interest
|
||||||
for i, v in ipairs(register_to) do
|
if register_to then
|
||||||
if not factory[v] then
|
local v
|
||||||
factory[v] = {}
|
for i = 1, #register_to do
|
||||||
|
v = register_to[i]
|
||||||
|
if not self[v] then
|
||||||
|
self[v] = {}
|
||||||
end
|
end
|
||||||
factory[v][#factory[v] + 1] = instance
|
self[v][#self[v] + 1] = instance
|
||||||
|
|
||||||
if v == data.ON_INPUT or v == data.ON_SWIPE then
|
if data.ui_input[v] then
|
||||||
input_init(factory)
|
input_init(self)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function _factory.remove(factory, instance)
|
function _fct_metatable.remove(self, instance)
|
||||||
for i = #factory, 1, -1 do
|
for i = #self, 1, -1 do
|
||||||
if factory[i] == instance then
|
if self[i] == instance then
|
||||||
table.remove(factory, i)
|
table.remove(self, i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local interest = instance.interest
|
local interest = instance.interest
|
||||||
if interest then
|
if interest then
|
||||||
for i, v in ipairs(interest) do
|
local v
|
||||||
for j = #factory[v], 1, -1 do
|
for i = 1, #interest do
|
||||||
if factory[v][j] == instance then
|
v = interest[i]
|
||||||
table.remove(factory[v], j)
|
local array = self[v]
|
||||||
|
for j = #array, 1, -1 do
|
||||||
|
if array[j] == instance then
|
||||||
|
table.remove(array, j)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -100,8 +107,8 @@ function _factory.remove(factory, instance)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function _factory.new(factory, module, ...)
|
function _fct_metatable.new(self, module, ...)
|
||||||
local instance = create(module, factory)
|
local instance = create(self, module)
|
||||||
|
|
||||||
if instance.init then
|
if instance.init then
|
||||||
instance:init(...)
|
instance:init(...)
|
||||||
@ -112,31 +119,33 @@ end
|
|||||||
|
|
||||||
|
|
||||||
--- Called on_message
|
--- Called on_message
|
||||||
function _factory.on_message(factory, message_id, message, sender)
|
function _fct_metatable.on_message(self, message_id, message, sender)
|
||||||
if message_id == data.LAYOUT_CHANGED then
|
local specific_ui_message = data.specific_ui_messages[message_id]
|
||||||
if factory[data.LAYOUT_CHANGED] then
|
if specific_ui_message then
|
||||||
M.translate(factory)
|
local array = self[message_id]
|
||||||
for i, v in ipairs(factory[data.LAYOUT_CHANGED]) do
|
if array then
|
||||||
v:on_layout_updated(message)
|
local item
|
||||||
|
for i = 1, #array do
|
||||||
|
item = array[i]
|
||||||
|
item[specific_ui_message](item, message, sender)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif message_id == data.TRANSLATABLE then
|
|
||||||
M.translate(factory)
|
|
||||||
else
|
else
|
||||||
if factory[data.ON_MESSAGE] then
|
local array = self[data.ON_MESSAGE]
|
||||||
for i, v in ipairs(factory[data.ON_MESSAGE]) do
|
if array then
|
||||||
v:on_message(message_id, message, sender)
|
for i = 1, #array do
|
||||||
|
array[i]:on_message(message_id, message, sender)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function notify_input_on_swipe(factory)
|
local function notify_input_on_swipe(self)
|
||||||
if factory[data.ON_INPUT] then
|
if self[data.ON_INPUT] then
|
||||||
local len = #factory[data.ON_INPUT]
|
local len = #self[data.ON_INPUT]
|
||||||
for i = len, 1, -1 do
|
for i = len, 1, -1 do
|
||||||
local comp = factory[data.ON_INPUT][i]
|
local comp = self[data.ON_INPUT][i]
|
||||||
if comp.on_swipe then
|
if comp.on_swipe then
|
||||||
comp:on_swipe()
|
comp:on_swipe()
|
||||||
end
|
end
|
||||||
@ -159,24 +168,26 @@ end
|
|||||||
|
|
||||||
|
|
||||||
--- Called ON_INPUT
|
--- Called ON_INPUT
|
||||||
function _factory.on_input(factory, action_id, action)
|
function _fct_metatable.on_input(self, action_id, action)
|
||||||
if factory[data.ON_SWIPE] then
|
local array = self[data.ON_SWIPE]
|
||||||
|
if array then
|
||||||
local v, result
|
local v, result
|
||||||
local len = #factory[data.ON_SWIPE]
|
local len = #array
|
||||||
for i = len, 1, -1 do
|
for i = len, 1, -1 do
|
||||||
v = factory[data.ON_SWIPE][i]
|
v = array[i]
|
||||||
result = result or v:on_input(action_id, action)
|
result = result or v:on_input(action_id, action)
|
||||||
end
|
end
|
||||||
if result then
|
if result then
|
||||||
notify_input_on_swipe(factory)
|
notify_input_on_swipe(self)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if factory[data.ON_INPUT] then
|
array = self[data.ON_INPUT]
|
||||||
|
if array then
|
||||||
local v
|
local v
|
||||||
local len = #factory[data.ON_INPUT]
|
local len = #array
|
||||||
for i = len, 1, -1 do
|
for i = len, 1, -1 do
|
||||||
v = factory[data.ON_INPUT][i]
|
v = array[i]
|
||||||
if match_event(action_id, v.event) and v:on_input(action_id, action) then
|
if match_event(action_id, v.event) and v:on_input(action_id, action) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -188,10 +199,11 @@ end
|
|||||||
|
|
||||||
|
|
||||||
--- Called on_update
|
--- Called on_update
|
||||||
function _factory.update(factory, dt)
|
function _fct_metatable.update(self, dt)
|
||||||
if factory[data.ON_UPDATE] then
|
local array = self[data.ON_UPDATE]
|
||||||
for i, v in ipairs(factory[data.ON_UPDATE]) do
|
if array then
|
||||||
v:update(dt)
|
for i = 1, #array do
|
||||||
|
array[i]:update(dt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user