Move hover logic from button to hover component

This commit is contained in:
Insality 2020-02-22 15:19:18 +03:00
parent 3e30fd4417
commit 2c0b100ab7
2 changed files with 14 additions and 24 deletions

View File

@ -8,6 +8,14 @@ local component = require("druid.component")
local M = component.create("button", { const.ON_INPUT }) local M = component.create("button", { const.ON_INPUT })
local function on_button_hover(self, hover_state)
if not self.style.on_hover then
return
end
self.style.on_hover(self, self.anim_node, hover_state)
end
--- Component init function --- Component init function
-- @function button:init -- @function button:init
-- @tparam table self Component instance -- @tparam table self Component instance
@ -19,6 +27,7 @@ local M = component.create("button", { const.ON_INPUT })
function M.init(self, node, callback, params, anim_node, event) function M.init(self, node, callback, params, anim_node, event)
assert(callback, "Button should have callback. To block input on zone use blocker component") assert(callback, "Button should have callback. To block input on zone use blocker component")
self.druid = self:get_druid()
self.style = self:get_style() self.style = self:get_style()
self.node = self:get_node(node) self.node = self:get_node(node)
@ -29,20 +38,12 @@ function M.init(self, node, callback, params, anim_node, event)
self.callback = callback self.callback = callback
self.params = params self.params = params
self.hover_anim = self.style.IS_HOVER self.hover_anim = self.style.IS_HOVER
self.hover = self.druid:new_hover(node, self, on_button_hover)
self.click_zone = nil self.click_zone = nil
end end
local function set_hover(self, state)
if self._is_hovered ~= state then
if self.style.on_hover then
self.style.on_hover(self, self.anim_node, state)
end
self._is_hovered = state
end
end
local function on_button_release(self) local function on_button_release(self)
if not self.disabled then if not self.disabled then
if not self.stub and self.can_action then if not self.stub and self.can_action then
@ -51,8 +52,6 @@ local function on_button_release(self)
self.style.on_click(self, self.anim_node) self.style.on_click(self, self.anim_node)
end end
self.callback(self:get_context(), self.params, self) self.callback(self:get_context(), self.params, self)
else
set_hover(self, false)
end end
return true return true
else else
@ -81,7 +80,6 @@ function M.on_input(self, action_id, action)
if not is_pick then if not is_pick then
-- Can't interact, if touch outside of button -- Can't interact, if touch outside of button
self.can_action = false self.can_action = false
set_hover(self, false)
return false return false
end end
@ -93,10 +91,7 @@ function M.on_input(self, action_id, action)
end end
if action.released then if action.released then
set_hover(self, false)
return on_button_release(self) return on_button_release(self)
else
set_hover(self, true)
end end
return not self.disabled return not self.disabled
@ -104,17 +99,11 @@ end
function M.on_swipe(self) function M.on_swipe(self)
-- unhover button if start swipe
self.can_action = false self.can_action = false
set_hover(self, false)
end end
function M.set_enabled(self, state) function M.set_enabled(self, state)
-- if self.disabled == state then
-- return
-- end
self.disabled = not state self.disabled = not state
if self.style.on_set_enabled then if self.style.on_set_enabled then
self.style.on_set_enabled(self, self.node, state) self.style.on_set_enabled(self, self.node, state)

View File

@ -14,11 +14,12 @@ local M = component.create("hover", { const.ON_INPUT })
-- @tparam table self Component instance -- @tparam table self Component instance
-- @tparam node node Gui node -- @tparam node node Gui node
-- @tparam function on_hover_callback Hover callback -- @tparam function on_hover_callback Hover callback
function M.init(self, node, on_hover_callback) function M.init(self, node, context, on_hover_callback)
self.style = self:get_style() self.style = self:get_style()
self.node = self:get_node(node) self.node = self:get_node(node)
self._is_hovered = false self._is_hovered = false
self.context = context
self.on_hover = Event() self.on_hover = Event()
if on_hover_callback then if on_hover_callback then
self.on_hover:subscribe(on_hover_callback) self.on_hover:subscribe(on_hover_callback)
@ -29,7 +30,7 @@ end
local function set_hover(self, state) local function set_hover(self, state)
if self._is_hovered ~= state then if self._is_hovered ~= state then
self._is_hovered = state self._is_hovered = state
self.on_hover:trigger(state) self.on_hover:trigger(self.context, state)
end end
end end