Difference between revisions of "ButtonDisabled class"
From GiderosMobile
(Created page with "__TOC__ This button takes three bitmaps as argument. 1 bitmap for each state: ''Up'', ''Down'', ''Disabled''. === ButtonDisabled '''Class''' === <syntaxhighlight lang="lua">...") |
|||
Line 136: | Line 136: | ||
=== ButtonDisabled '''Demo''' === | === ButtonDisabled '''Demo''' === | ||
+ | {{#widget:GApp|app=ButtonDisabledDemo1.GApp|width=480|height=320}} | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- ButtonDisabled DEMO | -- ButtonDisabled DEMO |
Latest revision as of 04:13, 30 November 2023
This button takes three bitmaps as argument. 1 bitmap for each state: Up, Down, Disabled.
ButtonDisabled Class
--[[
-- ButtonDisabled
-- A Button class with: an Image, Up state, Down state, Disabled state (IUDD)
v 0.1.1: 2023-11-30 changed addChild to setVisible
v 0.1.0: 2011 init (based on the initial generic Gideros Button class)
]]
ButtonDisabled = Core.class(Sprite)
function ButtonDisabled:init(upState, downState, disabledState)
self.upState = upState
self.downState = downState
self.disabledState = disabledState
self:addChild(self.upState)
if self.downState then self:addChild(self.downState) end
if self.disabledState then self:addChild(self.disabledState) end
self.focus = false
self.disabled = false
-- set the visual state as "up"
self:updateVisualState(false)
-- register to all mouse and touch events
self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
self:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self)
self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)
self:addEventListener(Event.TOUCHES_CANCEL, self.onTouchesCancel, self)
end
function ButtonDisabled:onMouseDown(ev)
if self:hitTestPoint(ev.x, ev.y) then
self.focus = true
self:updateVisualState(true)
ev:stopPropagation()
end
end
function ButtonDisabled:onMouseMove(ev)
if self.focus then
if not self:hitTestPoint(ev.x, ev.y) then
self.focus = false
self:updateVisualState(false)
end
ev:stopPropagation()
end
end
function ButtonDisabled:onMouseUp(ev)
if self.focus then
self.focus = false
self:updateVisualState(false)
if not self.disabled then
self:dispatchEvent(Event.new("clicked")) -- button is clicked, dispatch "clicked" event
end
ev:stopPropagation()
end
end
-- if button is on focus, stop propagation of touch events
function ButtonDisabled:onTouchesBegin(ev)
if self.focus then
ev:stopPropagation()
end
end
-- if button is on focus, stop propagation of touch events
function ButtonDisabled:onTouchesMove(ev)
if self.focus then
ev:stopPropagation()
end
end
-- if button is on focus, stop propagation of touch events
function ButtonDisabled:onTouchesEnd(ev)
if self.focus then
ev:stopPropagation()
end
end
-- if touches are cancelled, reset the state of the button
function ButtonDisabled:onTouchesCancel(ev)
if self.focus then
self.focus = false
self:updateVisualState(false)
end
end
-- if state is true show downState else show upState
function ButtonDisabled:updateVisualState(state)
self.upState:setVisible(false)
if self.downState then self.downState:setVisible(false) end
if self.disabledState then self.disabledState:setVisible(false) end
if self.disabled then -- button is Disabled
if self.disabledState then
self.disabledState:setVisible(true)
else
self.upState:setVisible(true)
end
else
if state then -- button is Down
if self.downState then
self.downState:setVisible(true)
else
self.upState:setVisible(true)
end
else -- button is Up
self.upState:setVisible(true)
end
end
end
function ButtonDisabled:setDisabled(disabled)
if self.disabled == disabled then return end
self.disabled = disabled
self.focus = false
self:updateVisualState(false)
end
function ButtonDisabled:getDisabled()
return self.disabled
end
ButtonDisabled Demo
-- ButtonDisabled DEMO
-- bg
application:setBackgroundColor(0x6c6c6c)
-- textures
local texbtnup = Texture.new("gfx/ui/btn_01_up.png")
local texbtndown = Texture.new("gfx/ui/btn_01_down.png")
local texbtndisable = Texture.new("gfx/ui/btn_01_disabled.png")
-- buttons
local btniudd = ButtonDisabled.new(Bitmap.new(texbtnup), Bitmap.new(texbtndown), Bitmap.new(texbtndisable))
local btniudd2 = ButtonDisabled.new(Bitmap.new(texbtnup), Bitmap.new(texbtndown), Bitmap.new(texbtndisable))
local btniudd3 = ButtonDisabled.new(Bitmap.new(texbtnup), Bitmap.new(texbtndown), Bitmap.new(texbtndisable))
-- params
btniudd:setScale(0.5)
btniudd2:setScale(0.5)
btniudd3:setScale(0.5)
-- position
btniudd:setPosition(16*4, 16*1)
btniudd2:setPosition(16*9, 16*1)
btniudd3:setPosition(16*14, 16*1)
-- order
stage:addChild(btniudd)
stage:addChild(btniudd2)
stage:addChild(btniudd3)
-- listeners
btniudd:addEventListener("clicked", function()
print("button is disabled", btniudd:getDisabled())
btniudd2:setDisabled(not btniudd2:getDisabled())
end)
btniudd2:addEventListener("clicked", function()
print("button2 is disabled", btniudd2:getDisabled())
btniudd3:setDisabled(not btniudd3:getDisabled())
end)
btniudd3:addEventListener("clicked", function()
print("button3 is disabled", btniudd3:getDisabled())
end)