Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Better Button Class? — Gideros Forum

Better Button Class?

antixantix Member
edited February 2017 in Code snippets
Here is my take on a generic button class..
--[[
 
name:   ImageButton
vers:   1.0.0
desc:   A simple image button
auth:   by Cliff Earl, Antix Development
date:   Feb 2017
legal:  Distribute freely but include my credits please
 
--]]
 
ImageButton = Core.class(Bitmap)
 
function ImageButton:init(upTexture, downTexture, options)
 
  local props = { -- Default properties
    name            = "Button",
    x               = 0,
    y               = 0,
    handleByCenter  = true,
    scale           = 1,
    alpha           = 1,
    rotation        = 0,
    enabled         = true,
    onPressed       = nil,
    onDragged       = nil,
    onReleased      = nil,
  }
 
	if options then -- Superimpose supplied properties
		for key, value in pairs(options) do
			props[key]= value
		end
	end
 
  self.props = props
 
  self.parent = self -- For when using as a standalone button
 
  self.enabled = props.enabled
  self.name = props.name
 
  self.upTexture = upTexture
  self.downTexture = downTexture
 
  self.focus = false
  self.id = -1
 
  if props.handleByCenter then self:setAnchorPoint(0.5, 0.5) end
 
  self:setPosition(props.x, props.y)
  self:setAlpha(props.alpha)
  self:setRotation(props.rotation)
 
  self:addEventListener(Event.TOUCHES_BEGIN, self.pressed, self) -- ADD EVENT HANDLERS
  self:addEventListener(Event.TOUCHES_MOVE, self.dragged, self)
  self:addEventListener(Event.TOUCHES_END, self.released, self)
end
 
function ImageButton:dispose()
  self.enabled = false
  self:removeEventListener(Event.TOUCHES_BEGIN, self.pressed, self) -- REMOVE EVENT HANDLERS
  self:removeEventListener(Event.TOUCHES_MOVE, self.dragged, self)
  self:removeEventListener(Event.TOUCHES_END, self.released, self)
 
  self.props = nil
  self.parent = nil
  self.enabled = nil
  self.name = nil
  self.upTexture = nil
  self.downTexture = nil
  self.focus = nil
  self.id = nil
end
 
function ImageButton:enable(state) -- Enable / Disable
  self.enabled = state
end
 
function ImageButton:pressed(e) -- Handle touch events
  if not self.parent.enabled or not self.enabled then return end
  if self:hitTestPoint(e.touch.x, e.touch.y) then
    e:stopPropagation()
    self.id = e.touch.id
    self.focus = true
    local touchX, touchY = e.touch.x - self:getX(), e.touch.y - self:getY()
    self.touchX, self.touchY = touchX, touchY
 
    if self.downTexture then self:setTextureRegion(self.downTexture) end
 
    local props = self.props
    if props.onPressed then
      props.onPressed(self) -- Perform action
    end
  end
end
 
function ImageButton:dragged(e) -- Handle move events
  if not self.parent.enabled or not self.enabled then return end
  if e.touch.id == self.id then
    self.focus = true
    e:stopPropagation()
 
    local touchX, touchY = e.touch.x - self:getX(), e.touch.y - self:getY()
    self.touchX, self.touchY = touchX, touchY
 
    local props = self.props
    if props.onDragged then
      props.onDragged(self) -- Perform action
    end
   end
end
 
function ImageButton:released(e) -- Handle touch end events
  if not self.parent.enabled or not self.enabled then return end
  self:setTextureRegion(self.upTexture)
  if e.touch.id == self.id and self.focus and self:hitTestPoint(e.touch.x, e.touch.y) then
    e:stopPropagation()
    self.focus = false
    self.id = -1
 
    local props = self.props
    if props.onReleased then
      props.onReleased(self) -- Perform action
    end
  end
end
Example use..
local pack   = TexturePack.new("images/gui.txt", "images/gui.png")
 
local button = ImageButton.new(
  pack:getTextureRegion("button_u.png"),
  pack:getTextureRegion("button_d.png"),
  {
    x               = 32,
    y               = 16,
    name            = "TEST_BUTTON",
    onPressed       = function(e)
      print(e.props.name.." Pressed")
    end,
    onDragged       = function(e)
      print(e.props.name.." Dragged")
    end,
    onReleased      = function(e)
      print(e.props.name.." Released")
    end,
  })
 
stage:addChild(button)
Maybe you will find it useful in simple projects..
zip
zip
aButton.zip
6K
+1 -1 (+5 / -0 )Share on Facebook
Sign In or Register to comment.