From d22aa285028f2d85dedcefc4ac979094d17f5b47 Mon Sep 17 00:00:00 2001 From: linux-man Date: Sun, 13 Nov 2016 04:11:22 +0000 Subject: [PATCH] image.stretch button.image groupIndex for button and checkbox --- objects/button.lua | 15 +++++++++ objects/checkbox.lua | 15 ++++++++- objects/imagebutton.lua | 15 +++++++++ readme.md | 21 +++++++++++++ skins/Blue/skin.lua | 68 ++++++++++++++++++++++++++++++++++++----- 5 files changed, 125 insertions(+), 9 deletions(-) diff --git a/objects/button.lua b/objects/button.lua index fd69c4d..27cb763 100644 --- a/objects/button.lua +++ b/objects/button.lua @@ -27,6 +27,8 @@ function newobject:initialize() self.toggleable = false self.toggle = false self.OnClick = nil + self.groupIndex = 0 + self.checked = false end @@ -182,6 +184,19 @@ function newobject:mousereleased(x, y, button) if hover and down and clickable and button == 1 then if enabled then + 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 if onclick then onclick(self, x, y) end diff --git a/objects/checkbox.lua b/objects/checkbox.lua index 1fac7d9..371ed74 100644 --- a/objects/checkbox.lua +++ b/objects/checkbox.lua @@ -29,6 +29,7 @@ function newobject:initialize() self.enabled = true self.internals = {} self.OnChanged = nil + self.groupIndex = 0 end @@ -211,8 +212,20 @@ function newobject:mousereleased(x, y, button) if hover and down and enabled and button == 1 then if checked then - self.checked = false + if self.groupIndex == 0 then self.checked = false end else + 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 + end self.checked = true end if onchanged then diff --git a/objects/imagebutton.lua b/objects/imagebutton.lua index 17c32db..99f3b45 100644 --- a/objects/imagebutton.lua +++ b/objects/imagebutton.lua @@ -26,6 +26,8 @@ function newobject:initialize() self.image = nil self.imagecolor = {255, 255, 255, 255} self.OnClick = nil + self.groupIndex = 0 + self.checked = false end @@ -182,6 +184,19 @@ function newobject:mousereleased(x, y, button) if hover and down and clickable and button == 1 then if enabled then + 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 if onclick then onclick(self, x, y) end diff --git a/readme.md b/readme.md index aa6b379..68c169e 100644 --- a/readme.md +++ b/readme.md @@ -16,3 +16,24 @@ Created by Kenny Shields **Third-Party Libraries** - middleclass by kikito - https://github.com/kikito/middleclass + +What's new: + +textinput - Unicode support. + +Properties: + +image.stretch (boolean) + +buttom.image (image) -- left aligned image + +button.groupIndex (int) +button.checked (boolean) + +imagebutton.groupIndex (int) +imagebutton.checked (boolean) + +checkbox.groupIndex + +-- Buttons/checkboxes with the same groupIndex behave as group buttons + diff --git a/skins/Blue/skin.lua b/skins/Blue/skin.lua index 8d6140d..80bda28 100644 --- a/skins/Blue/skin.lua +++ b/skins/Blue/skin.lua @@ -314,6 +314,7 @@ function skin.DrawButton(object) local twidth = font:getWidth(object.text) local theight = font:getHeight(object.text) local down = object:GetDown() + local checked = object.checked local enabled = object:GetEnabled() local clickable = object:GetClickable() local textdowncolor = skin.controls.button_text_down_color @@ -388,14 +389,27 @@ function skin.DrawButton(object) end end else - if down then + if down or checked 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) + if object.image then + love.graphics.setColor(255, 255, 255) + love.graphics.draw(object.image, x + 2, y + height/2 - object.image:getHeight()/2) + love.graphics.setColor(textdowncolor) + local text = object.text + local font = skin.controls.button_text_font + while font:getWidth(text) > width - object.image:getWidth() - 10 do + text =text:sub(2) + while text:byte(1, 1) > 127 do text = text:sub(2) end + end + love.graphics.print(text, x + object.image:getWidth() + 4, y + height/2 - theight/2) + else + love.graphics.setColor(textdowncolor) + love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) + end -- button border love.graphics.setColor(bordercolor) skin.OutlinedRectangle(x, y, width, height) @@ -405,8 +419,21 @@ function skin.DrawButton(object) 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) + if object.image then + love.graphics.setColor(255, 255, 255) + love.graphics.draw(object.image, x + 2, y + height/2 - object.image:getHeight()/2) + love.graphics.setColor(texthovercolor) + local text = object.text + local font = skin.controls.button_text_font + while font:getWidth(text) > width - object.image:getWidth() - 10 do + text =text:sub(2) + while text:byte(1, 1) > 127 do text = text:sub(2) end + end + love.graphics.print(text, x + object.image:getWidth() + 4, y + height/2 - theight/2) + else + love.graphics.setColor(texthovercolor) + love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) + end -- button border love.graphics.setColor(bordercolor) skin.OutlinedRectangle(x, y, width, height) @@ -416,8 +443,21 @@ function skin.DrawButton(object) 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) + if object.image then + love.graphics.setColor(255, 255, 255) + love.graphics.draw(object.image, object:GetX() + 2, object:GetY() + object:GetHeight()/2 - object.image:getHeight()/2) + love.graphics.setColor(textnohovercolor) + local text = object.text + local font = skin.controls.button_text_font + while font:getWidth(text) > width - object.image:getWidth() - 10 do + text =text:sub(2) + while text:byte(1, 1) > 127 do text = text:sub(2) end + end + love.graphics.print(text, x + object.image:getWidth() + 4, y + height/2 - theight/2) + else + love.graphics.setColor(textnohovercolor) + love.graphics.print(text, x + width/2 - twidth/2, y + height/2 - theight/2) + end -- button border love.graphics.setColor(bordercolor) skin.OutlinedRectangle(x, y, width, height) @@ -483,7 +523,12 @@ function skin.DrawImage(object) local sheary = object:GetShearY() local image = object.image local color = object.imagecolor + local stretch = object.stretch + if stretch then + scalex, scaley = object:GetWidth() / image:getWidth(), object:GetHeight() / image:getHeight() + end + if color then love.graphics.setColor(color) love.graphics.draw(image, x, y, orientation, scalex, scaley, offsetx, offsety, shearx, sheary) @@ -516,7 +561,8 @@ function skin.DrawImageButton(object) local textdowncolor = skin.controls.imagebutton_text_down_color local texthovercolor = skin.controls.imagebutton_text_hover_color local textnohovercolor = skin.controls.imagebutton_text_nohover_color - + local checked = object.checked + if down then if image then love.graphics.setColor(imagecolor) @@ -548,6 +594,12 @@ function skin.DrawImageButton(object) love.graphics.setColor(textnohovercolor) love.graphics.print(text, x + width/2 - twidth/2, y + height - theight - 6) end + if checked == true then + love.graphics.setColor(bordercolor) + love.graphics.setLineWidth(3) + love.graphics.setLineStyle("smooth") + love.graphics.rectangle("line", x+1, y+1, width-2, height-2) + end end