LoveFrames/objects/imagebutton.lua

259 lines
6.3 KiB
Lua
Raw Normal View History

--[[------------------------------------------------
-- L<>VE Frames --
-- By Kenny Shields --
--]]------------------------------------------------
-- imagebutton clas
imagebutton = class("imagebutton", base)
imagebutton:include(loveframes.templates.default)
--[[---------------------------------------------------------
- func: initialize()
- desc: initializes the object
--]]---------------------------------------------------------
function imagebutton:initialize()
self.type = "imagebutton"
self.text = "Image Button"
self.width = 50
self.height = 50
self.internal = false
self.down = false
self.clickable = true
self.enabled = true
self.image = nil
self.OnClick = nil
end
--[[---------------------------------------------------------
- func: update(deltatime)
- desc: updates the object
--]]---------------------------------------------------------
function imagebutton:update(dt)
local visible = self.visible
local alwaysupdate = self.alwaysupdate
if visible == false then
if alwaysupdate == false then
return
end
end
self:CheckHover()
local hover = self.hover
local hoverobject = loveframes.hoverobject
local down = self.down
if hover == false then
self.down = false
elseif hover == true then
if hoverobject == self then
self.down = true
end
end
if down == false and hoverobject == self then
self.hover = true
end
-- move to parent if there is a parent
if self.parent ~= loveframes.base then
self.x = self.parent.x + self.staticx
self.y = self.parent.y + self.staticy
end
if self.Update then
self.Update(self, dt)
end
end
--[[---------------------------------------------------------
- func: draw()
- desc: draws the object
--]]---------------------------------------------------------
function imagebutton:draw()
local visible = self.visible
if visible == false then
return
end
local skins = loveframes.skins.available
local skinindex = loveframes.config["ACTIVESKIN"]
local defaultskin = loveframes.config["DEFAULTSKIN"]
local selfskin = self.skin
local skin = skins[selfskin] or skins[skinindex]
local drawfunc = skin.DrawImageButton or skins[defaultskin].DrawImageButton
loveframes.drawcount = loveframes.drawcount + 1
self.draworder = loveframes.drawcount
if self.Draw ~= nil then
self.Draw(self)
else
drawfunc(self)
end
end
--[[---------------------------------------------------------
- func: mousepressed(x, y, button)
- desc: called when the player presses a mouse button
--]]---------------------------------------------------------
function imagebutton:mousepressed(x, y, button)
local visible = self.visible
if visible == false then
return
end
local hover = self.hover
if hover == true and button == "l" then
local baseparent = self:GetBaseParent()
if baseparent and baseparent.type == "frame" then
baseparent:MakeTop()
end
self.down = true
loveframes.hoverobject = self
end
end
--[[---------------------------------------------------------
- func: mousereleased(x, y, button)
- desc: called when the player releases a mouse button
--]]---------------------------------------------------------
function imagebutton:mousereleased(x, y, button)
local visible = self.visible
if visible == false then
return
end
local hover = self.hover
local down = self.down
local clickable = self.clickable
local enabled = self.enabled
if hover == true and down == true and button == "l" and clickable == true then
if enabled == true then
if self.OnClick then
self.OnClick(self, x, y)
end
end
end
self.down = false
end
--[[---------------------------------------------------------
- func: SetText(text)
- desc: sets the object's text
--]]---------------------------------------------------------
function imagebutton:SetText(text)
self.text = text
end
--[[---------------------------------------------------------
- func: GetText()
- desc: gets the object's text
--]]---------------------------------------------------------
function imagebutton:GetText()
return self.text
end
--[[---------------------------------------------------------
- func: SetClickable(bool)
- desc: sets whether the object can be clicked or not
--]]---------------------------------------------------------
function imagebutton:SetClickable(bool)
self.clickable = bool
end
--[[---------------------------------------------------------
- func: GetClickable(bool)
- desc: gets whether the object can be clicked or not
--]]---------------------------------------------------------
function imagebutton:GetClickable()
return self.clickable
end
--[[---------------------------------------------------------
- func: SetClickable(bool)
- desc: sets whether the object is enabled or not
--]]---------------------------------------------------------
function imagebutton:SetEnabled(bool)
self.enabled = bool
end
--[[---------------------------------------------------------
- func: GetEnabled()
- desc: gets whether the object is enabled or not
--]]---------------------------------------------------------
function imagebutton:GetEnabled()
return self.enabled
end
--[[---------------------------------------------------------
- func: SetImage(image)
- desc: sets the object's image
--]]---------------------------------------------------------
function imagebutton:SetImage(image)
if type(image) == "string" then
self.image = love.graphics.newImage(image)
else
self.image = image
end
end
--[[---------------------------------------------------------
- func: GetImage()
- desc: gets whether the object is enabled or not
--]]---------------------------------------------------------
function imagebutton:GetImage()
return self.image
end
--[[---------------------------------------------------------
- func: SizeToImage()
- desc: makes the object the same size as it's image
--]]---------------------------------------------------------
function imagebutton:SizeToImage()
local image = self.image
if image then
self.width = image:getWidth()
self.height = image:getHeight()
end
end