Small optimization && refactoring

This commit is contained in:
Insality 2023-07-15 15:56:06 +03:00
parent 337090e74c
commit 556e1a9bae
11 changed files with 33 additions and 46 deletions

View File

@ -60,7 +60,7 @@ end
-- @tparam table action on_input action -- @tparam table action on_input action
-- @local -- @local
function BackHandler.on_input(self, action_id, action) function BackHandler.on_input(self, action_id, action)
if not action[const.RELEASED] then if not action.released then
return false return false
end end

View File

@ -340,10 +340,7 @@ function Button.on_input(self, action_id, action)
local is_pick = true local is_pick = true
local is_key_trigger = (action_id == self.key_trigger) local is_key_trigger = (action_id == self.key_trigger)
if not is_key_trigger then if not is_key_trigger then
is_pick = gui.pick_node(self.node, action.x, action.y) is_pick = helper.pick_node(self.node, action.x, action.y, self.click_zone)
if self.click_zone then
is_pick = is_pick and gui.pick_node(self.click_zone, action.x, action.y)
end
end end
if not is_pick then if not is_pick then

View File

@ -185,7 +185,7 @@ function Drag.init(self, node, on_drag_callback)
self.is_touch = false self.is_touch = false
self.is_drag = false self.is_drag = false
self.touch_start_pos = vmath.vector3(0) self.touch_start_pos = vmath.vector3(0)
self._is_disabled = false self._is_enabled = true
self.can_x = true self.can_x = true
self.can_y = true self.can_y = true
@ -233,14 +233,11 @@ function Drag.on_input(self, action_id, action)
return false return false
end end
if not gui.is_enabled(self.node, true) or self._is_disabled then if not self._is_enabled or not gui.is_enabled(self.node, true) then
return false return false
end end
local is_pick = gui.pick_node(self.node, action.x, action.y) local is_pick = helper.pick_node(self.node, action.x, action.y, self.click_zone)
if self.click_zone then
is_pick = is_pick and gui.pick_node(self.click_zone, action.x, action.y)
end
if not is_pick and not self.is_drag then if not is_pick and not self.is_drag then
end_touch(self) end_touch(self)
return false return false
@ -318,7 +315,7 @@ end
-- @tparam Drag self @{Drag} -- @tparam Drag self @{Drag}
-- @tparam bool is_enabled -- @tparam bool is_enabled
function Drag.set_enabled(self, is_enabled) function Drag.set_enabled(self, is_enabled)
self._is_disabled = not is_enabled self._is_enabled = is_enabled
end end
@ -326,7 +323,7 @@ end
-- @tparam Drag self @{Drag} -- @tparam Drag self @{Drag}
-- @treturn bool -- @treturn bool
function Drag.is_enabled(self) function Drag.is_enabled(self)
return not self._is_disabled return self._is_enabled
end end

View File

@ -64,11 +64,7 @@ function Hover.on_input(self, action_id, action)
return false return false
end end
local is_pick = gui.pick_node(self.node, action.x, action.y) local is_pick = helper.pick_node(self.node, action.x, action.y, self.click_zone)
if self.click_zone then
is_pick = is_pick and gui.pick_node(self.click_zone, action.x, action.y)
end
local hover_function = action_id and self.set_hover or self.set_mouse_hover local hover_function = action_id and self.set_hover or self.set_mouse_hover
if not is_pick then if not is_pick then

View File

@ -99,7 +99,7 @@ end
-- @tparam string template BaseComponent template name -- @tparam string template BaseComponent template name
-- @treturn BaseComponent @{BaseComponent} -- @treturn BaseComponent @{BaseComponent}
function BaseComponent.set_template(self, template) function BaseComponent.set_template(self, template)
template = template or const.EMPTY_STRING template = template or ""
local parent = self:get_parent_component() local parent = self:get_parent_component()
if parent and IS_AUTO_TEMPLATE then if parent and IS_AUTO_TEMPLATE then
@ -183,7 +183,7 @@ end
-- @tparam string|node node_or_name Node name or node itself -- @tparam string|node node_or_name Node name or node itself
-- @treturn node Gui node -- @treturn node Gui node
function BaseComponent.get_node(self, node_or_name) function BaseComponent.get_node(self, node_or_name)
if type(node_or_name) ~= const.STRING then if type(node_or_name) ~= "string" then
-- Assume it's already node from gui.get_node -- Assume it's already node from gui.get_node
return node_or_name return node_or_name
end end

View File

@ -10,25 +10,16 @@ local M = {}
M.ACTION_TEXT = hash(sys.get_config("druid.input_text", "text")) M.ACTION_TEXT = hash(sys.get_config("druid.input_text", "text"))
M.ACTION_TOUCH = hash(sys.get_config("druid.input_touch", "touch")) M.ACTION_TOUCH = hash(sys.get_config("druid.input_touch", "touch"))
M.ACTION_MARKED_TEXT = hash(sys.get_config("druid.input_marked_text", "marked_text")) M.ACTION_MARKED_TEXT = hash(sys.get_config("druid.input_marked_text", "marked_text"))
M.ACTION_ESC = hash(sys.get_config("druid.input_key_esc", "key_esc")) M.ACTION_ESC = hash(sys.get_config("druid.input_key_esc", "key_esc"))
M.ACTION_BACK = hash(sys.get_config("druid.input_key_back", "key_back")) M.ACTION_BACK = hash(sys.get_config("druid.input_key_back", "key_back"))
M.ACTION_ENTER = hash(sys.get_config("druid.input_key_enter", "key_enter")) M.ACTION_ENTER = hash(sys.get_config("druid.input_key_enter", "key_enter"))
M.ACTION_MULTITOUCH = hash(sys.get_config("druid.input_multitouch", "touch_multi")) M.ACTION_MULTITOUCH = hash(sys.get_config("druid.input_multitouch", "touch_multi"))
M.ACTION_BACKSPACE = hash(sys.get_config("druid.input_key_backspace", "key_backspace")) M.ACTION_BACKSPACE = hash(sys.get_config("druid.input_key_backspace", "key_backspace"))
M.ACTION_SCROLL_UP = hash(sys.get_config("druid.input_scroll_up", "mouse_wheel_up")) M.ACTION_SCROLL_UP = hash(sys.get_config("druid.input_scroll_up", "mouse_wheel_up"))
M.ACTION_SCROLL_DOWN = hash(sys.get_config("druid.input_scroll_down", "mouse_wheel_down")) M.ACTION_SCROLL_DOWN = hash(sys.get_config("druid.input_scroll_down", "mouse_wheel_down"))
M.IS_STENCIL_CHECK = not (sys.get_config("druid.no_stencil_check") == "1") M.IS_STENCIL_CHECK = not (sys.get_config("druid.no_stencil_check") == "1")
M.RELEASED = "released"
M.PRESSED = "pressed"
M.STRING = "string"
M.TABLE = "table"
M.ZERO = "0"
--- Component Interests --- Component Interests
M.ON_INPUT = "on_input" M.ON_INPUT = "on_input"
M.ON_UPDATE = "update" M.ON_UPDATE = "update"
@ -41,12 +32,11 @@ M.ON_MESSAGE_INPUT = "on_message_input"
M.ON_WINDOW_RESIZED = "on_window_resized" M.ON_WINDOW_RESIZED = "on_window_resized"
M.ON_LANGUAGE_CHANGE = "on_language_change" M.ON_LANGUAGE_CHANGE = "on_language_change"
-- Components with higher priority value processed first
M.PRIORITY_INPUT = 10 M.PRIORITY_INPUT = 10
M.PRIORITY_INPUT_HIGH = 20 M.PRIORITY_INPUT_HIGH = 20
M.PRIORITY_INPUT_MAX = 100 M.PRIORITY_INPUT_MAX = 100
M.MESSAGE_INPUT = { M.MESSAGE_INPUT = {
BUTTON_CLICK = "button_click", BUTTON_CLICK = "button_click",
BUTTON_LONG_CLICK = "button_long_click", BUTTON_LONG_CLICK = "button_long_click",
@ -55,7 +45,6 @@ M.MESSAGE_INPUT = {
TEXT_SET = "text_set", TEXT_SET = "text_set",
} }
M.PIVOTS = { M.PIVOTS = {
[gui.PIVOT_CENTER] = vmath.vector3(0), [gui.PIVOT_CENTER] = vmath.vector3(0),
[gui.PIVOT_N] = vmath.vector3(0, 0.5, 0), [gui.PIVOT_N] = vmath.vector3(0, 0.5, 0),
@ -90,7 +79,6 @@ M.LAYOUT_MODE = {
} }
M.VECTOR_ZERO = vmath.vector3(0) M.VECTOR_ZERO = vmath.vector3(0)
M.VECTOR_ONE = vmath.vector3(1)
M.SYS_INFO = sys.get_sys_info() M.SYS_INFO = sys.get_sys_info()
M.CURRENT_SYSTEM_NAME = M.SYS_INFO.system_name M.CURRENT_SYSTEM_NAME = M.SYS_INFO.system_name
@ -135,6 +123,5 @@ M.ERRORS = {
} }
M.EMPTY_FUNCTION = function() end M.EMPTY_FUNCTION = function() end
M.EMPTY_STRING = ""
return M return M

View File

@ -123,7 +123,7 @@ end
function Input.init(self, click_node, text_node, keyboard_type) function Input.init(self, click_node, text_node, keyboard_type)
self.druid = self:get_druid(self) self.druid = self:get_druid(self)
if type(text_node) == const.TABLE then if type(text_node) == "table" then
self.text = text_node self.text = text_node
else else
self.text = self.druid:new_text(text_node) self.text = self.druid:new_text(text_node)

View File

@ -120,11 +120,7 @@ function Swipe.on_input(self, action_id, action)
return false return false
end end
local is_pick = gui.pick_node(self.node, action.x, action.y) local is_pick = helper.pick_node(self.node, action.x, action.y, self.click_zone)
if self.click_zone then
is_pick = is_pick and gui.pick_node(self.click_zone, action.x, action.y)
end
if not is_pick then if not is_pick then
reset_swipe(self, action) reset_swipe(self, action)
return false return false

View File

@ -271,6 +271,23 @@ function M.add_array(target, source)
end end
--- Make a check with gui.pick_node, but with additional node_click_area check.
-- @function helper.pick_node
-- @tparam Node node
-- @tparam number x
-- @tparam number y
-- @tparam[opt] Node node_click_area
-- @local
function M.pick_node(node, x, y, node_click_area)
local is_pick = gui.pick_node(node, x, y)
if node_click_area then
is_pick = is_pick and gui.pick_node(node_click_area, x, y)
end
return is_pick
end
--- Get node size adjusted by scale --- Get node size adjusted by scale
-- @function helper.get_scaled_size -- @function helper.get_scaled_size
-- @tparam node node GUI node -- @tparam node node GUI node
@ -356,7 +373,7 @@ end
-- @treturn string -- @treturn string
function M.table_to_string(t) function M.table_to_string(t)
if not t then if not t then
return const.EMPTY_STRING return ""
end end
local result = "{" local result = "{"

View File

@ -1,3 +0,0 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
return {}

View File

@ -234,8 +234,8 @@ end
--- Druid class constructor --- Druid class constructor
-- @tparam DruidInstance self -- @tparam DruidInstance self
-- @tparam table context Druid context. Usually it is self of script -- @tparam table context Druid context. Usually it is self of gui script
-- @tparam table style Druid style module -- @tparam table style Druid style table
-- @local -- @local
function DruidInstance.initialize(self, context, style) function DruidInstance.initialize(self, context, style)
self._context = context self._context = context