image.stretch

button.image
groupIndex for button and checkbox
This commit is contained in:
linux-man 2016-11-13 04:11:22 +00:00
parent 0e69a908d5
commit d22aa28502
5 changed files with 125 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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