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
|
|
|
--]]------------------------------------------------
|
|
|
|
|
2019-03-06 23:19:58 +00:00
|
|
|
return function(loveframes)
|
|
|
|
---------- module start ----------
|
2014-10-23 22:36:16 +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
|
2014-08-17 23:28:47 +00:00
|
|
|
self.toggleable = false
|
|
|
|
self.toggle = false
|
2012-12-28 12:19:02 +00:00
|
|
|
self.OnClick = nil
|
2021-09-05 17:46:49 +00:00
|
|
|
self.OnMouseOver = nil
|
|
|
|
self.OnMouseOut = nil
|
2016-11-13 04:11:22 +00:00
|
|
|
self.groupIndex = 0
|
|
|
|
self.checked = false
|
2021-09-05 17:46:49 +00:00
|
|
|
self.mouseover = false
|
2012-05-06 00:24:42 +00:00
|
|
|
|
2019-03-06 23:19:58 +00:00
|
|
|
self:SetDrawFunc()
|
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
|
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
|
2021-09-05 17:46:49 +00:00
|
|
|
local onmouseover = self.OnMouseOver
|
|
|
|
local onmouseout = self.OnMouseOut
|
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
|
2021-09-05 17:46:49 +00:00
|
|
|
if onmouseout and self.mouseover then
|
|
|
|
onmouseout(self)
|
|
|
|
self.mouseover = false
|
|
|
|
end
|
2012-09-29 23:20:41 +00:00
|
|
|
else
|
2021-09-05 17:46:49 +00:00
|
|
|
if onmouseover and not self.mouseover then
|
|
|
|
onmouseover(self)
|
|
|
|
self.mouseover = true
|
|
|
|
end
|
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
|
2021-09-05 17:46:49 +00:00
|
|
|
self.x = self.parent.x + self.staticx - (parent.offsetx or 0)
|
|
|
|
self.y = self.parent.y + self.staticy - (parent.offsety or 0)
|
2012-05-06 00:24:42 +00:00
|
|
|
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: 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
|
|
|
|
|
2021-09-05 17:46:49 +00:00
|
|
|
if hover 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
|
|
|
|
|
2021-09-05 17:46:49 +00:00
|
|
|
self.pressed_button = button
|
2012-05-06 00:24:42 +00:00
|
|
|
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
|
|
|
|
2021-09-05 17:46:49 +00:00
|
|
|
if hover and down and clickable and self.pressed_button == button then
|
2012-09-29 23:20:41 +00:00
|
|
|
if enabled then
|
2016-11-13 04:11:22 +00:00
|
|
|
if self.groupIndex ~= 0 then
|
|
|
|
local baseparent = self.parent
|
|
|
|
if baseparent then
|
|
|
|
for k, v in ipairs(baseparent.children) do
|
|
|
|
if v.groupIndex then
|
|
|
|
if v.groupIndex == self.groupIndex then
|
|
|
|
v.checked = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.checked = true
|
|
|
|
end
|
2012-09-29 23:20:41 +00:00
|
|
|
if onclick then
|
2021-09-05 17:46:49 +00:00
|
|
|
onclick(self, x, y, self.pressed_button)
|
2012-05-06 03:35:10 +00:00
|
|
|
end
|
2014-08-17 23:28:47 +00:00
|
|
|
if self.toggleable then
|
|
|
|
local ontoggle = self.OnToggle
|
|
|
|
self.toggle = not self.toggle
|
|
|
|
if ontoggle then
|
|
|
|
ontoggle(self, self.toggle)
|
|
|
|
end
|
|
|
|
end
|
2012-05-06 00:24:42 +00:00
|
|
|
end
|
|
|
|
end
|
2021-09-05 17:46:49 +00:00
|
|
|
|
2012-05-06 00:24:42 +00:00
|
|
|
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
|
|
|
|
|
2021-02-04 21:01:56 +00:00
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetFont(font)
|
|
|
|
- desc: sets the object's font, nil uses the skin's default
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function newobject:SetFont(font)
|
|
|
|
|
|
|
|
self.font = font
|
|
|
|
return self
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: GetFont()
|
|
|
|
- desc: gets the object's font
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function newobject:GetFont()
|
|
|
|
|
|
|
|
return self.font
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-03-06 23:19:58 +00:00
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetImage(image)
|
|
|
|
- desc: adds an image to the object
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function newobject:SetImage(image)
|
|
|
|
|
|
|
|
if type(image) == "string" then
|
|
|
|
self.image = love.graphics.newImage(image)
|
|
|
|
self.image:setFilter("nearest", "nearest")
|
|
|
|
else
|
|
|
|
self.image = image
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: GetImage()
|
|
|
|
- desc: gets the object's image
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function newobject:GetImage()
|
|
|
|
|
|
|
|
return self.image
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-05-06 00:24:42 +00:00
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- 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
|
|
|
|
|
2014-06-29 16:58:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: GetDown()
|
|
|
|
- desc: gets whether or not the object is currently
|
|
|
|
being pressed
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function newobject:GetDown()
|
|
|
|
|
|
|
|
return self.down
|
|
|
|
|
2014-08-17 23:28:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: SetToggleable(bool)
|
|
|
|
- desc: sets whether or not the object is toggleable
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function newobject:SetToggleable(bool)
|
|
|
|
|
|
|
|
self.toggleable = bool
|
|
|
|
return self
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
|
|
- func: GetToggleable()
|
|
|
|
- desc: gets whether or not the object is toggleable
|
|
|
|
--]]---------------------------------------------------------
|
|
|
|
function newobject:GetToggleable()
|
|
|
|
|
|
|
|
return self.toggleable
|
|
|
|
|
2014-11-09 21:24:10 +00:00
|
|
|
end
|
2019-03-06 23:19:58 +00:00
|
|
|
|
|
|
|
---------- module end ----------
|
|
|
|
end
|