LoveFrames/objects/button.lua

250 lines
5.5 KiB
Lua
Raw Normal View History

2012-05-06 00:24:42 +00:00
--[[------------------------------------------------
-- Love Frames - A GUI library for LOVE --
2014-01-14 11:29:11 +00:00
-- Copyright (c) 2012-2014 Kenny Shields --
2012-05-06 00:24:42 +00:00
--]]------------------------------------------------
2013-11-25 13:06:41 +00:00
-- button object
local newobject = loveframes.NewObject("button", "loveframes_object_button", true)
2012-05-06 00:24:42 +00:00
--[[---------------------------------------------------------
- func: initialize()
- desc: initializes the object
--]]---------------------------------------------------------
function newobject:initialize()
self.type = "button"
self.text = "Button"
self.width = 80
self.height = 25
self.internal = false
self.down = false
self.clickable = true
self.enabled = true
self.OnClick = nil
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: update(deltatime)
- desc: updates the object
--]]---------------------------------------------------------
function newobject:update(dt)
2012-05-06 00:24:42 +00:00
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
local alwaysupdate = self.alwaysupdate
if not visible then
if not alwaysupdate then
2012-05-06 00:24:42 +00:00
return
end
end
self:CheckHover()
local hover = self.hover
local down = self.down
local downobject = loveframes.downobject
local parent = self.parent
local base = loveframes.base
local update = self.Update
if not hover then
self.down = false
if downobject == self then
self.hover = true
end
else
if downobject == self then
self.down = true
end
end
2012-05-06 00:24:42 +00:00
-- move to parent if there is a parent
if parent ~= base then
2012-05-06 00:24:42 +00:00
self.x = self.parent.x + self.staticx
self.y = self.parent.y + self.staticy
end
if update then
update(self, dt)
2012-05-06 00:24:42 +00:00
end
end
--[[---------------------------------------------------------
- func: draw()
- desc: draws the object
--]]---------------------------------------------------------
function newobject:draw()
2012-05-06 00:24:42 +00:00
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
if not visible then
2012-05-06 00:24:42 +00:00
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.DrawButton or skins[defaultskin].DrawButton
local draw = self.Draw
local drawcount = loveframes.drawcount
2012-05-06 00:24:42 +00:00
-- set the object's draw order
self:SetDrawOrder()
if draw then
draw(self)
2012-05-06 00:24:42 +00:00
else
drawfunc(self)
2012-05-06 00:24:42 +00:00
end
end
--[[---------------------------------------------------------
- func: mousepressed(x, y, button)
- desc: called when the player presses a mouse button
--]]---------------------------------------------------------
function newobject:mousepressed(x, y, button)
2012-05-06 00:24:42 +00:00
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
if not visible then
2012-05-06 00:24:42 +00:00
return
end
local hover = self.hover
if hover and button == "l" then
2012-05-06 00:24:42 +00:00
local baseparent = self:GetBaseParent()
if baseparent and baseparent.type == "frame" then
baseparent:MakeTop()
end
self.down = true
loveframes.downobject = self
2012-05-06 00:24:42 +00:00
end
end
--[[---------------------------------------------------------
- func: mousereleased(x, y, button)
- desc: called when the player releases a mouse button
--]]---------------------------------------------------------
function newobject:mousereleased(x, y, button)
2012-05-06 00:24:42 +00:00
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
if not visible then
2012-05-06 00:24:42 +00:00
return
end
local hover = self.hover
local down = self.down
2012-05-06 00:24:42 +00:00
local clickable = self.clickable
local enabled = self.enabled
local onclick = self.OnClick
2012-05-06 00:24:42 +00:00
if hover and down and clickable and button == "l" then
if enabled then
if onclick then
onclick(self, x, y)
end
2012-05-06 00:24:42 +00:00
end
end
self.down = false
end
--[[---------------------------------------------------------
- func: SetText(text)
- desc: sets the object's text
--]]---------------------------------------------------------
function newobject:SetText(text)
2012-05-06 00:24:42 +00:00
self.text = text
2014-02-11 12:34:07 +00:00
return self
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: GetText()
- desc: gets the object's text
--]]---------------------------------------------------------
function newobject:GetText()
2012-05-06 00:24:42 +00:00
return self.text
end
--[[---------------------------------------------------------
- func: SetClickable(bool)
- desc: sets whether the object can be clicked or not
--]]---------------------------------------------------------
function newobject:SetClickable(bool)
2012-05-06 00:24:42 +00:00
self.clickable = bool
2014-02-11 12:34:07 +00:00
return self
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: GetClickable(bool)
- desc: gets whether the object can be clicked or not
--]]---------------------------------------------------------
function newobject:GetClickable()
2012-05-06 00:24:42 +00:00
return self.clickable
end
--[[---------------------------------------------------------
- func: SetClickable(bool)
- desc: sets whether or not the object is enabled
2012-05-06 00:24:42 +00:00
--]]---------------------------------------------------------
function newobject:SetEnabled(bool)
2012-05-06 00:24:42 +00:00
self.enabled = bool
2014-02-11 12:34:07 +00:00
return self
2012-05-06 00:24:42 +00:00
end
--[[---------------------------------------------------------
- func: GetEnabled()
- desc: gets whether or not the object is enabled
2012-05-06 00:24:42 +00:00
--]]---------------------------------------------------------
function newobject:GetEnabled()
2012-05-06 00:24:42 +00:00
return self.enabled
end