2012-05-06 00:24:42 +00:00
|
|
|
--[[------------------------------------------------
|
2012-09-29 23:20:41 +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
|
2012-11-24 22:42:16 +00:00
|
|
|
local newobject = loveframes.NewObject("button", "loveframes_object_button", true)
|
2012-05-06 00:24:42 +00:00
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: initialize()
|
|
|
|
- desc: initializes the object
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:initialize()
|
2012-10-22 14:19:02 +00:00
|
|
|
|
2012-12-28 12:19:02 +00:00
|
|
|
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
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:update(dt)
|
2012-05-06 00:24:42 +00:00
|
|
|
|
2013-02-11 21:15:40 +00:00
|
|
|
local state = loveframes.state
|
|
|
|
local selfstate = self.state
|
|
|
|
|
|
|
|
if state ~= selfstate then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-12-28 12:19:02 +00:00
|
|
|
local visible = self.visible
|
2012-05-22 07:10:18 +00:00
|
|
|
local alwaysupdate = self.alwaysupdate
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not visible then
|
|
|
|
if not alwaysupdate then
|
2012-05-06 00:24:42 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self:CheckHover()
|
|
|
|
|
2012-12-28 12:19:02 +00:00
|
|
|
local hover = self.hover
|
|
|
|
local down = self.down
|
2013-10-30 23:05:10 +00:00
|
|
|
local downobject = loveframes.downobject
|
2012-12-28 12:19:02 +00:00
|
|
|
local parent = self.parent
|
|
|
|
local base = loveframes.base
|
|
|
|
local update = self.Update
|
2012-05-22 07:10:18 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not hover then
|
2012-05-13 03:57:27 +00:00
|
|
|
self.down = false
|
2013-10-30 23:05:10 +00:00
|
|
|
if downobject == self then
|
2012-11-24 22:42:16 +00:00
|
|
|
self.hover = true
|
|
|
|
end
|
2012-09-29 23:20:41 +00:00
|
|
|
else
|
2013-10-30 23:05:10 +00:00
|
|
|
if downobject == self then
|
2012-05-13 03:57:27 +00:00
|
|
|
self.down = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-06 00:24:42 +00:00
|
|
|
-- move to parent if there is a parent
|
2012-09-29 23:20:41 +00:00
|
|
|
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
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if update then
|
|
|
|
update(self, dt)
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: draw()
|
|
|
|
- desc: draws the object
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:draw()
|
2012-05-06 00:24:42 +00:00
|
|
|
|
2013-02-11 21:15:40 +00:00
|
|
|
local state = loveframes.state
|
|
|
|
local selfstate = self.state
|
|
|
|
|
|
|
|
if state ~= selfstate then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-05-22 07:10:18 +00:00
|
|
|
local visible = self.visible
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not visible then
|
2012-05-06 00:24:42 +00:00
|
|
|
return
|
|
|
|
end
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-12-28 12:19:02 +00:00
|
|
|
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
|
|
|
|
2012-10-22 14:19:02 +00:00
|
|
|
-- set the object's draw order
|
|
|
|
self:SetDrawOrder()
|
2012-09-02 00:56:32 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if draw then
|
|
|
|
draw(self)
|
2012-05-06 00:24:42 +00:00
|
|
|
else
|
2012-09-02 00:56:32 +00:00
|
|
|
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
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:mousepressed(x, y, button)
|
2012-05-06 00:24:42 +00:00
|
|
|
|
2013-02-11 21:15:40 +00:00
|
|
|
local state = loveframes.state
|
|
|
|
local selfstate = self.state
|
|
|
|
|
|
|
|
if state ~= selfstate then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-05-22 07:10:18 +00:00
|
|
|
local visible = self.visible
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not visible then
|
2012-05-06 00:24:42 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-05-22 07:10:18 +00:00
|
|
|
local hover = self.hover
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
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
|
2013-10-30 23:05:10 +00:00
|
|
|
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
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:mousereleased(x, y, button)
|
2012-05-06 00:24:42 +00:00
|
|
|
|
2013-02-11 21:15:40 +00:00
|
|
|
local state = loveframes.state
|
|
|
|
local selfstate = self.state
|
|
|
|
|
|
|
|
if state ~= selfstate then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-05-22 07:10:18 +00:00
|
|
|
local visible = self.visible
|
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if not visible then
|
2012-05-06 00:24:42 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-12-28 12:19:02 +00:00
|
|
|
local hover = self.hover
|
|
|
|
local down = self.down
|
2012-05-06 00:24:42 +00:00
|
|
|
local clickable = self.clickable
|
2012-12-28 12:19:02 +00:00
|
|
|
local enabled = self.enabled
|
|
|
|
local onclick = self.OnClick
|
2012-05-06 00:24:42 +00:00
|
|
|
|
2012-09-29 23:20:41 +00:00
|
|
|
if hover and down and clickable and button == "l" then
|
|
|
|
if enabled then
|
|
|
|
if onclick then
|
|
|
|
onclick(self, x, y)
|
2012-05-06 03:35:10 +00:00
|
|
|
end
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.down = false
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetText(text)
|
|
|
|
- desc: sets the object's text
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
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
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
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
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
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
|
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:GetClickable()
|
2012-05-06 00:24:42 +00:00
|
|
|
|
|
|
|
return self.clickable
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetClickable(bool)
|
2013-05-15 18:02:55 +00:00
|
|
|
- desc: sets whether or not the object is enabled
|
2012-05-06 00:24:42 +00:00
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +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()
|
2013-05-15 18:02:55 +00:00
|
|
|
- desc: gets whether or not the object is enabled
|
2012-05-06 00:24:42 +00:00
|
|
|
--]]---------------------------------------------------------
|
2012-11-24 22:42:16 +00:00
|
|
|
function newobject:GetEnabled()
|
2012-05-06 00:24:42 +00:00
|
|
|
|
|
|
|
return self.enabled
|
|
|
|
|
|
|
|
end
|