From 3e30fd44172fb9ef617ebfc93eda5747015ea75d Mon Sep 17 00:00:00 2001 From: Insality Date: Sat, 22 Feb 2020 15:09:29 +0300 Subject: [PATCH] Remove match_event from druid, add hover component --- druid/base/back_handler.lua | 6 +-- druid/base/blocker.lua | 5 ++- druid/base/button.lua | 6 ++- druid/base/hover.lua | 76 +++++++++++++++++++++++++++++++++ druid/system/druid_instance.lua | 37 +++++++++------- 5 files changed, 109 insertions(+), 21 deletions(-) create mode 100644 druid/base/hover.lua diff --git a/druid/base/back_handler.lua b/druid/base/back_handler.lua index 67078c5..70cd190 100644 --- a/druid/base/back_handler.lua +++ b/druid/base/back_handler.lua @@ -13,7 +13,6 @@ local M = component.create("back_handler", { const.ON_INPUT }) -- @tparam callback callback On back button -- @tparam[opt] params Callback argument function M.init(self, callback, params) - self.event = const.ACTION_BACK self.callback = callback self.params = params end @@ -24,11 +23,12 @@ end -- @tparam string action_id on_input action id -- @tparam table action on_input action function M.on_input(self, action_id, action) - if action[const.RELEASED] then + if action_id == const.ACTION_BACK and action[const.RELEASED] then self.callback(self:get_context(), self.params) + return true end - return true + return false end diff --git a/druid/base/blocker.lua b/druid/base/blocker.lua index 5c48263..54d41b3 100644 --- a/druid/base/blocker.lua +++ b/druid/base/blocker.lua @@ -10,11 +10,14 @@ local M = component.create("blocker", { const.ON_SWIPE }) function M.init(self, node) self.node = self:get_node(node) - self.event = const.ACTION_TOUCH end function M.on_input(self, action_id, action) + if action_id ~= const.ACTION_TOUCH then + return false + end + if not helper.is_enabled(self.node) then return false end diff --git a/druid/base/button.lua b/druid/base/button.lua index 95add22..85d68dd 100644 --- a/druid/base/button.lua +++ b/druid/base/button.lua @@ -22,8 +22,6 @@ function M.init(self, node, callback, params, anim_node, event) self.style = self:get_style() self.node = self:get_node(node) - -- TODO: match event inside on_input? - self.event = const.ACTION_TOUCH self.anim_node = anim_node and helper:get_node(anim_node) or self.node -- TODO: rename to start_scale self.scale_from = gui.get_scale(self.anim_node) @@ -67,6 +65,10 @@ end function M.on_input(self, action_id, action) + if action_id ~= const.ACTION_TOUCH then + return false + end + if not helper.is_enabled(self.node) then return false end diff --git a/druid/base/hover.lua b/druid/base/hover.lua new file mode 100644 index 0000000..0015f35 --- /dev/null +++ b/druid/base/hover.lua @@ -0,0 +1,76 @@ +--- Component to handle hover node interaction +-- @module druid.input + +local Event = require("druid.event") +local const = require("druid.const") +local helper = require("druid.helper") +local component = require("druid.component") + +local M = component.create("hover", { const.ON_INPUT }) + + +--- Component init function +-- @function hover:init +-- @tparam table self Component instance +-- @tparam node node Gui node +-- @tparam function on_hover_callback Hover callback +function M.init(self, node, on_hover_callback) + self.style = self:get_style() + self.node = self:get_node(node) + + self._is_hovered = false + self.on_hover = Event() + if on_hover_callback then + self.on_hover:subscribe(on_hover_callback) + end +end + + +local function set_hover(self, state) + if self._is_hovered ~= state then + self._is_hovered = state + self.on_hover:trigger(state) + end +end + + +function M.on_input(self, action_id, action) + if action_id ~= const.ACTION_TOUCH then + return + end + + if not helper.is_enabled(self.node) then + return false + end + + local is_pick = gui.pick_node(self.node, action.x, action.y) + + if not is_pick then + set_hover(self, false) + return false + end + + if action.released then + set_hover(self, false) + else + set_hover(self, true) + end +end + + +function M.on_swipe(self) + set_hover(self, false) +end + + +--- Strict button click area. Useful for +-- no click events outside stencil node +-- @function button:set_click_zone +-- @tparam table self Component instance +-- @tparam node zone Gui node +function M.set_click_zone(self, zone) + self.click_zone = self:get_node(zone) +end + + +return M diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index d3800ae..9907edd 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -4,6 +4,7 @@ -- @see druid.button -- @see druid.blocker -- @see druid.back_handler +-- @see druid.input -- @see druid.text -- @see druid.locale -- @see druid.timer @@ -23,6 +24,7 @@ local class = require("druid.system.middleclass") local button = require("druid.base.button") local blocker = require("druid.base.blocker") local back_handler = require("druid.base.back_handler") +local hover = require("druid.base.hover") local text = require("druid.base.text") local locale = require("druid.base.locale") local timer = require("druid.base.timer") @@ -33,7 +35,7 @@ local slider = require("druid.base.slider") local checkbox = require("druid.base.checkbox") local checkbox_group = require("druid.base.checkbox_group") local radio_group = require("druid.base.radio_group") -local input - require("druid.base.input") +local input = require("druid.base.input") -- local infinity_scroll = require("druid.base.infinity_scroll") local progress_rich = require("druid.rich.progress_rich") @@ -94,19 +96,6 @@ local function notify_input_on_swipe(self) end -local function match_event(action_id, events) - if type(events) == const.TABLE then - for i = 1, #events do - if action_id == events[i] then - return true - end - end - else - return action_id == events - end -end - - --- Druid class constructor -- @function druid:initialize -- @tparam context table Druid context. Usually it is self of script @@ -200,7 +189,7 @@ function Druid.on_input(self, action_id, action) if components then for i = #components, 1, -1 do local v = components[i] - if match_event(action_id, v.event) and v:on_input(action_id, action) then + if v:on_input(action_id, action) then return true end end @@ -262,6 +251,15 @@ function Druid.new_back_handler(self, ...) end +--- Create hover basic component +-- @function druid:new_hover +-- @tparam args ... hover init args +-- @treturn Component hover component +function Druid.new_hover(self, ...) + return Druid.create(self, hover, ...) +end + + --- Create text basic component -- @function druid:new_text -- @tparam args ... text init args @@ -334,6 +332,15 @@ function Druid.new_checkbox(self, ...) end +--- Create input basic component +-- @function druid:new_input +-- @tparam args ... input init args +-- @treturn Component input component +function Druid.new_input(self, ...) + return Druid.create(self, input, ...) +end + + --- Create checkbox_group basic component -- @function druid:new_checkbox_group -- @tparam args ... checkbox_group init args