From 0db6ac9dda8c6cc9c79f7649d119038d2784d881 Mon Sep 17 00:00:00 2001 From: Kenny Shields Date: Sun, 17 Aug 2014 19:28:47 -0400 Subject: [PATCH] Add toggle functionality to the button object --- objects/button.lua | 30 ++++++++++ skins/Blue/skin.lua | 134 +++++++++++++++++++++++++++++--------------- 2 files changed, 118 insertions(+), 46 deletions(-) diff --git a/objects/button.lua b/objects/button.lua index e5b8be3..70d6c18 100644 --- a/objects/button.lua +++ b/objects/button.lua @@ -20,6 +20,8 @@ function newobject:initialize() self.down = false self.clickable = true self.enabled = true + self.toggleable = false + self.toggle = false self.OnClick = nil end @@ -179,6 +181,13 @@ function newobject:mousereleased(x, y, button) if onclick then onclick(self, x, y) end + if self.toggleable then + local ontoggle = self.OnToggle + self.toggle = not self.toggle + if ontoggle then + ontoggle(self, self.toggle) + end + end end end @@ -258,4 +267,25 @@ function newobject:GetDown() return self.down +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 + end \ No newline at end of file diff --git a/skins/Blue/skin.lua b/skins/Blue/skin.lua index d5b79ec..c3e749c 100644 --- a/skins/Blue/skin.lua +++ b/skins/Blue/skin.lua @@ -276,14 +276,13 @@ function skin.DrawButton(object) local texthovercolor = skin.controls.button_text_hover_color local textnohovercolor = skin.controls.button_text_nohover_color local textnonclickablecolor = skin.controls.button_text_nonclickable_color + local image_hover = skin.images["button-hover.png"] + local scaley = height/image_hover:getHeight() if not enabled or not clickable then - local image = skin.images["button-unclickable.png"] - local imageheight = image:getHeight() - local scaley = height/imageheight -- button body love.graphics.setColor(255, 255, 255, 255) - love.graphics.draw(image, x, y, 0, width, scaley) + love.graphics.draw(skin.images["button-unclickable.png"], x, y, 0, width, scaley) -- button text love.graphics.setFont(font) love.graphics.setColor(textnonclickablecolor) @@ -294,48 +293,91 @@ function skin.DrawButton(object) return end - if down then - local image = skin.images["button-down.png"] - local imageheight = image:getHeight() - local scaley = height/imageheight - -- button body - love.graphics.setColor(255, 255, 255, 255) - love.graphics.draw(image, x, y, 0, width, scaley) - -- button text - love.graphics.setFont(font) - love.graphics.setColor(textdowncolor) - love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) - -- button border - love.graphics.setColor(bordercolor) - skin.OutlinedRectangle(x, y, width, height) - elseif hover then - local image = skin.images["button-hover.png"] - local imageheight = image:getHeight() - local scaley = height/imageheight - -- button body - love.graphics.setColor(255, 255, 255, 255) - love.graphics.draw(image, x, y, 0, width, scaley) - -- button text - love.graphics.setFont(font) - love.graphics.setColor(texthovercolor) - love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) - -- button border - love.graphics.setColor(bordercolor) - skin.OutlinedRectangle(x, y, width, height) - else - local image = skin.images["button-nohover.png"] - local imageheight = image:getHeight() - local scaley = height/imageheight - -- button body - love.graphics.setColor(255, 255, 255, 255) - love.graphics.draw(image, x, y, 0, width, scaley) - -- button text - love.graphics.setFont(font) - love.graphics.setColor(textnohovercolor) - love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) - -- button border - love.graphics.setColor(bordercolor) - skin.OutlinedRectangle(x, y, width, height) + if object.toggleable then + if hover then + if down then + -- button body + love.graphics.setColor(255, 255, 255, 255) + love.graphics.draw(skin.images["button-down.png"], x, y, 0, width, scaley) + -- button text + love.graphics.setFont(font) + love.graphics.setColor(textdowncolor) + love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) + -- button border + love.graphics.setColor(bordercolor) + skin.OutlinedRectangle(x, y, width, height) + else + -- button body + love.graphics.setColor(255, 255, 255, 255) + love.graphics.draw(image_hover, x, y, 0, width, scaley) + -- button text + love.graphics.setFont(font) + love.graphics.setColor(texthovercolor) + love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) + -- button border + love.graphics.setColor(bordercolor) + skin.OutlinedRectangle(x, y, width, height) + end + else + if object.toggle then + -- button body + love.graphics.setColor(255, 255, 255, 255) + love.graphics.draw(skin.images["button-down.png"], x, y, 0, width, scaley) + -- button text + love.graphics.setFont(font) + love.graphics.setColor(textdowncolor) + love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) + -- button border + love.graphics.setColor(bordercolor) + skin.OutlinedRectangle(x, y, width, height) + else + -- button body + love.graphics.setColor(255, 255, 255, 255) + love.graphics.draw(skin.images["button-nohover.png"], x, y, 0, width, scaley) + -- button text + love.graphics.setFont(font) + love.graphics.setColor(textnohovercolor) + love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) + -- button border + love.graphics.setColor(bordercolor) + skin.OutlinedRectangle(x, y, width, height) + end + end + else + if down then + -- button body + love.graphics.setColor(255, 255, 255, 255) + love.graphics.draw(skin.images["button-down.png"], x, y, 0, width, scaley) + -- button text + love.graphics.setFont(font) + love.graphics.setColor(textdowncolor) + love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) + -- button border + love.graphics.setColor(bordercolor) + skin.OutlinedRectangle(x, y, width, height) + elseif hover then + -- button body + love.graphics.setColor(255, 255, 255, 255) + love.graphics.draw(image_hover, x, y, 0, width, scaley) + -- button text + love.graphics.setFont(font) + love.graphics.setColor(texthovercolor) + love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) + -- button border + love.graphics.setColor(bordercolor) + skin.OutlinedRectangle(x, y, width, height) + else + -- button body + love.graphics.setColor(255, 255, 255, 255) + love.graphics.draw(skin.images["button-nohover.png"], x, y, 0, width, scaley) + -- button text + love.graphics.setFont(font) + love.graphics.setColor(textnohovercolor) + love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) + -- button border + love.graphics.setColor(bordercolor) + skin.OutlinedRectangle(x, y, width, height) + end end love.graphics.setColor(255, 255, 255, 150)