2020-11-28 15:27:25 +01:00
|
|
|
-- widgets/text.lua :: basic text widget object
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Copyright © 2019 Kazhnuz
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
]]
|
2022-08-13 12:43:49 +02:00
|
|
|
local BaseWidget = require "framework.scenes.gui.menus.widgets.base"
|
2020-11-28 15:27:25 +01:00
|
|
|
local TextWidget = BaseWidget:extend()
|
|
|
|
|
|
|
|
-- TEXT WIDGET
|
|
|
|
-- Simple text widget
|
|
|
|
|
2020-12-05 21:20:20 +01:00
|
|
|
function TextWidget:new(menuName, font, label, position, padding)
|
2020-12-05 09:02:22 +01:00
|
|
|
TextWidget.super.new(self, menuName)
|
2020-11-28 15:27:25 +01:00
|
|
|
self.font = font
|
2020-12-05 09:27:40 +01:00
|
|
|
self.labels = {}
|
2020-12-05 21:20:20 +01:00
|
|
|
self.padding = padding or 0
|
2020-12-05 09:27:40 +01:00
|
|
|
|
|
|
|
-- We add the first label
|
|
|
|
local position = position or "center"
|
|
|
|
self:addLabel(label, position)
|
2021-12-04 13:18:54 +01:00
|
|
|
self:setColor(1, 1, 1)
|
2020-12-04 12:54:57 +01:00
|
|
|
end
|
|
|
|
|
2020-12-05 09:27:40 +01:00
|
|
|
function TextWidget:addLabel(label, position)
|
|
|
|
local complexLabel = {}
|
2022-07-27 21:57:34 +02:00
|
|
|
assert(label ~= nil, "Label can't be nil")
|
2020-12-05 09:27:40 +01:00
|
|
|
complexLabel.label = label
|
2022-07-27 21:57:34 +02:00
|
|
|
complexLabel.position = position or "left"
|
2020-12-05 09:27:40 +01:00
|
|
|
table.insert(self.labels, complexLabel)
|
|
|
|
end
|
|
|
|
|
2021-01-24 15:44:24 +01:00
|
|
|
function TextWidget:replaceLabel(id, newLabel)
|
|
|
|
self.labels[id].label = newLabel
|
|
|
|
end
|
|
|
|
|
2021-12-04 13:18:54 +01:00
|
|
|
function TextWidget:setColor(r, g, b)
|
|
|
|
self.color = {}
|
|
|
|
self.color.r = r
|
|
|
|
self.color.g = g
|
|
|
|
self.color.b = b
|
|
|
|
end
|
|
|
|
|
2020-12-04 12:54:57 +01:00
|
|
|
function TextWidget:setSelectedColor(r, g, b)
|
|
|
|
self.selectedColor = {}
|
|
|
|
self.selectedColor.r = r
|
|
|
|
self.selectedColor.g = g
|
|
|
|
self.selectedColor.b = b
|
|
|
|
end
|
|
|
|
|
2021-01-23 18:30:30 +01:00
|
|
|
function TextWidget:getFont()
|
|
|
|
return self.assets:getFont(self.font)
|
|
|
|
end
|
|
|
|
|
2020-12-04 12:54:57 +01:00
|
|
|
function TextWidget:getSelectedColor()
|
2021-12-04 13:18:54 +01:00
|
|
|
if (self.selectedColor ~= nil) then
|
|
|
|
return self.selectedColor.r, self.selectedColor.g, self.selectedColor.b
|
|
|
|
else
|
|
|
|
return self:getColor()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextWidget:getColor()
|
|
|
|
return self.color.r, self.color.g, self.color.b
|
2020-11-28 15:27:25 +01:00
|
|
|
end
|
|
|
|
|
2021-01-23 18:30:30 +01:00
|
|
|
function TextWidget:getPadding()
|
|
|
|
return self.padding
|
|
|
|
end
|
|
|
|
|
2021-12-04 13:18:54 +01:00
|
|
|
function TextWidget:getPaddingLeft()
|
|
|
|
return self.paddingLeft or self:getPadding()
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextWidget:getPaddingRight()
|
|
|
|
return self.paddingRight or self:getPadding()
|
|
|
|
end
|
|
|
|
|
2020-11-28 15:27:25 +01:00
|
|
|
function TextWidget:drawCanvas()
|
|
|
|
local w, h
|
2021-01-23 18:30:30 +01:00
|
|
|
local font = self:getFont()
|
2020-12-05 09:27:40 +01:00
|
|
|
h = math.floor(self.height / 2) - (font:getHeight() / 2)
|
|
|
|
|
|
|
|
for _, complexLabel in pairs(self.labels) do
|
|
|
|
if (complexLabel.position == "center") then
|
|
|
|
w = math.floor(self.width / 2)
|
|
|
|
elseif (complexLabel.position == "left") then
|
2021-12-04 13:18:54 +01:00
|
|
|
w = self:getPaddingLeft()
|
2020-12-05 09:27:40 +01:00
|
|
|
elseif (complexLabel.position == "right") then
|
2021-12-04 13:18:54 +01:00
|
|
|
w = math.floor(self.width - self:getPaddingRight())
|
|
|
|
else
|
|
|
|
error("Position " .. complexLabel.position .. " is unknown for label " .. complexLabel.label)
|
2020-12-05 09:27:40 +01:00
|
|
|
end
|
|
|
|
font:draw(complexLabel.label, w, h, -1, complexLabel.position)
|
|
|
|
end
|
2020-11-28 15:27:25 +01:00
|
|
|
end
|
|
|
|
|
2021-12-04 13:18:54 +01:00
|
|
|
function TextWidget:draw(x, y, w, h)
|
|
|
|
local r, g, b = self:getColor()
|
|
|
|
love.graphics.setColor(r, g, b, 1)
|
|
|
|
if self.canvas.texture ~= nil then
|
|
|
|
love.graphics.draw(self.canvas.texture, x, y)
|
|
|
|
end
|
|
|
|
utils.graphics.resetColor()
|
|
|
|
end
|
|
|
|
|
2020-12-04 12:54:57 +01:00
|
|
|
function TextWidget:drawSelected(x, y, w, h)
|
|
|
|
local r, g, b = self:getSelectedColor()
|
|
|
|
love.graphics.setColor(r, g, b, 1)
|
2021-12-04 13:18:54 +01:00
|
|
|
if self.canvas.texture ~= nil then
|
|
|
|
love.graphics.draw(self.canvas.texture, x, y)
|
|
|
|
end
|
2020-12-04 12:54:57 +01:00
|
|
|
utils.graphics.resetColor()
|
|
|
|
end
|
|
|
|
|
2020-11-28 15:27:25 +01:00
|
|
|
return TextWidget
|