mirror of
https://github.com/airstruck/luigi.git
synced 2025-11-18 12:25:06 +00:00
getRectangle returns x,y,w,h instead of x1,y1,x2,y2
This commit is contained in:
@@ -57,53 +57,53 @@ function Renderer:renderSlices (widget)
|
||||
local path = widget.slices
|
||||
if not path then return end
|
||||
|
||||
local x1, y1, x2, y2 = widget:getRectangle(true)
|
||||
local x, y, w, h = widget:getRectangle(true)
|
||||
|
||||
local slices = self:loadSlices(path)
|
||||
|
||||
local batch = Backend.SpriteBatch(slices.image)
|
||||
|
||||
local xScale = ((x2 - x1) - slices.width * 2) / slices.width
|
||||
local yScale = ((y2 - y1) - slices.height * 2) / slices.height
|
||||
local xScale = (w - slices.width * 2) / slices.width
|
||||
local yScale = (h - slices.height * 2) / slices.height
|
||||
|
||||
batch:add(slices.middleCenter, x1 + slices.width, y1 + slices.height, 0,
|
||||
batch:add(slices.middleCenter, x + slices.width, y + slices.height, 0,
|
||||
xScale, yScale)
|
||||
|
||||
batch:add(slices.topCenter, x1 + slices.width, y1, 0,
|
||||
batch:add(slices.topCenter, x + slices.width, y, 0,
|
||||
xScale, 1)
|
||||
batch:add(slices.bottomCenter, x1 + slices.width, y2 - slices.height, 0,
|
||||
batch:add(slices.bottomCenter, x + slices.width, y + h - slices.height, 0,
|
||||
xScale, 1)
|
||||
|
||||
batch:add(slices.middleLeft, x1, y1 + slices.height, 0,
|
||||
batch:add(slices.middleLeft, x, y + slices.height, 0,
|
||||
1, yScale)
|
||||
batch:add(slices.middleRight, x2 - slices.width, y1 + slices.height, 0,
|
||||
batch:add(slices.middleRight, x + w - slices.width, y + slices.height, 0,
|
||||
1, yScale)
|
||||
|
||||
batch:add(slices.topLeft, x1, y1)
|
||||
batch:add(slices.topRight, x2 - slices.width, y1)
|
||||
batch:add(slices.bottomLeft, x1, y2 - slices.height)
|
||||
batch:add(slices.bottomRight, x2 - slices.width, y2 - slices.height)
|
||||
batch:add(slices.topLeft, x, y)
|
||||
batch:add(slices.topRight, x + w - slices.width, y)
|
||||
batch:add(slices.bottomLeft, x, y + h - slices.height)
|
||||
batch:add(slices.bottomRight, x + w - slices.width, y + h - slices.height)
|
||||
|
||||
Backend.draw(batch)
|
||||
end
|
||||
|
||||
function Renderer:renderBackground (widget)
|
||||
if not widget.background then return end
|
||||
local x1, y1, x2, y2 = widget:getRectangle(true)
|
||||
local x, y, w, h = widget:getRectangle(true)
|
||||
|
||||
Backend.push()
|
||||
Backend.setColor(widget.background)
|
||||
Backend.drawRectangle('fill', x1, y1, x2 - x1, y2 - y1)
|
||||
Backend.drawRectangle('fill', x, y, w, h)
|
||||
Backend.pop()
|
||||
end
|
||||
|
||||
function Renderer:renderOutline (widget)
|
||||
if not widget.outline then return end
|
||||
local x1, y1, x2, y2 = widget:getRectangle(true)
|
||||
local x, y, w, h = widget:getRectangle(true)
|
||||
|
||||
Backend.push()
|
||||
Backend.setColor(widget.outline)
|
||||
Backend.drawRectangle('line', x1 + 0.5, y1 + 0.5, x2 - x1, y2 - y1)
|
||||
Backend.drawRectangle('line', x + 0.5, y + 0.5, w, h)
|
||||
Backend.pop()
|
||||
end
|
||||
|
||||
@@ -187,22 +187,22 @@ function Renderer:positionText (widget, x1, y1, x2, y2)
|
||||
end
|
||||
|
||||
function Renderer:renderIconAndText (widget)
|
||||
local x1, y1, x2, y2 = widget:getRectangle(true, true)
|
||||
local x, y, w, h = widget:getRectangle(true, true)
|
||||
|
||||
-- if the drawable area has no width or height, don't render
|
||||
if x2 <= x1 or y2 <= y1 then
|
||||
if w < 1 or h < 1 then
|
||||
return
|
||||
end
|
||||
|
||||
Backend.push()
|
||||
|
||||
Backend.setScissor(x1, y1, x2 - x1, y2 - y1)
|
||||
|
||||
local iconX, iconY, textX, textY, font
|
||||
Backend.setScissor(x, y, w, h)
|
||||
|
||||
-- calculate position for icon and text based on alignment and padding
|
||||
iconX, iconY, x1, y1, x2, y2 = self:positionIcon(widget, x1, y1, x2, y2)
|
||||
font, textX, textY = self:positionText(widget, x1, y1, x2, y2)
|
||||
local iconX, iconY, x1, y1, x2, y2 = self:positionIcon(
|
||||
widget, x, y, x + w, y + h)
|
||||
local font, textX, textY = self:positionText(
|
||||
widget, x1, y1, x2, y2)
|
||||
|
||||
local icon = widget.icon and self:loadImage(widget.icon)
|
||||
local text = widget.text
|
||||
@@ -217,15 +217,15 @@ function Renderer:renderIconAndText (widget)
|
||||
if align:find 'middle' then
|
||||
local textHeight = widget.textData:getHeight()
|
||||
local contentHeight = textHeight + padding + iconHeight
|
||||
local offset = ((y2 - y1) - contentHeight) / 2
|
||||
iconY = y1 + offset
|
||||
textY = y1 + offset + padding + iconHeight
|
||||
local offset = (h - contentHeight) / 2
|
||||
iconY = y + offset
|
||||
textY = y + offset + padding + iconHeight
|
||||
elseif align:find 'top' then
|
||||
iconY = y1
|
||||
textY = y1 + padding + iconHeight
|
||||
iconY = y
|
||||
textY = y + padding + iconHeight
|
||||
else -- if align:find 'bottom'
|
||||
local textHeight = widget.textData:getHeight()
|
||||
textY = y2 - textHeight
|
||||
textY = y + h - textHeight
|
||||
iconY = textY - padding - iconHeight
|
||||
end
|
||||
end
|
||||
@@ -234,9 +234,9 @@ function Renderer:renderIconAndText (widget)
|
||||
-- TODO: handle this in Backend.Text
|
||||
if text and not widget.wrap then
|
||||
if align:find 'right' then
|
||||
textX = textX + ((x2 - x1) - widget.textData:getWidth())
|
||||
textX = textX + (w - widget.textData:getWidth())
|
||||
elseif align:find 'center' then
|
||||
textX = textX + ((x2 - x1) - widget.textData:getWidth()) / 2
|
||||
textX = textX + (w - widget.textData:getWidth()) / 2
|
||||
end
|
||||
end
|
||||
|
||||
@@ -250,7 +250,7 @@ function Renderer:renderIconAndText (widget)
|
||||
end
|
||||
|
||||
-- draw the text
|
||||
if text and x2 > x1 then
|
||||
if text and w > 1 then
|
||||
textX, textY = math.floor(textX), math.floor(textY)
|
||||
Backend.draw(widget.textData, textX, textY)
|
||||
end
|
||||
|
||||
@@ -487,7 +487,7 @@ function Widget:getHeight ()
|
||||
end
|
||||
|
||||
--[[--
|
||||
Get two points describing a rectangle within the widget.
|
||||
Get x/y/width/height values describing a rectangle within the widget.
|
||||
|
||||
@tparam boolean useMargin
|
||||
Whether to adjust the rectangle based on the widget's margin.
|
||||
@@ -502,19 +502,19 @@ The upper left corner's X position.
|
||||
The upper left corner's Y position.
|
||||
|
||||
@treturn number
|
||||
The lower right corner's X position.
|
||||
The rectangle's width
|
||||
|
||||
@treturn number
|
||||
The lower right corner's Y position.
|
||||
The rectangle's height
|
||||
--]]--
|
||||
function Widget:getRectangle (useMargin, usePadding)
|
||||
local x1, y1 = self:getX(), self:getY()
|
||||
local x2, y2 = self:getWidth() + x1, self:getHeight() + y1
|
||||
local x, y = self:getX(), self:getY()
|
||||
local w, h = self:getWidth(), self:getHeight()
|
||||
local function shrink(amount)
|
||||
x1 = x1 + amount
|
||||
y1 = y1 + amount
|
||||
x2 = x2 - amount
|
||||
y2 = y2 - amount
|
||||
x = x + amount
|
||||
y = y + amount
|
||||
w = w - amount * 2
|
||||
h = h - amount * 2
|
||||
end
|
||||
if useMargin then
|
||||
shrink(self.margin or 0)
|
||||
@@ -522,7 +522,7 @@ function Widget:getRectangle (useMargin, usePadding)
|
||||
if usePadding then
|
||||
shrink(self.padding or 0)
|
||||
end
|
||||
return math.floor(x1), math.floor(y1), math.floor(x2), math.floor(y2)
|
||||
return math.floor(x), math.floor(y), math.floor(w), math.floor(h)
|
||||
end
|
||||
|
||||
--[[--
|
||||
@@ -540,7 +540,8 @@ true if the point is within the widget, else false.
|
||||
function Widget:isAt (x, y)
|
||||
checkReshape(self)
|
||||
|
||||
local x1, y1, x2, y2 = self:getRectangle()
|
||||
local x1, y1, w, h = self:getRectangle()
|
||||
local x2, y2 = x1 + w, y1 + h
|
||||
return (x1 <= x) and (x2 >= x) and (y1 <= y) and (y2 >= y)
|
||||
end
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ return function (self)
|
||||
end)
|
||||
|
||||
self:onReshape(function ()
|
||||
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
||||
local x, y, w, h = self:getRectangle(true, true)
|
||||
local min = bar.minwidth
|
||||
x1 = x1 + min
|
||||
bar.width = self.value * (x2 - x1) + min
|
||||
x = x + min
|
||||
bar.width = self.value * w + min
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -35,7 +35,8 @@ return function (self)
|
||||
end)
|
||||
|
||||
local function press (event)
|
||||
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
||||
local x1, y1, w, h = self:getRectangle(true, true)
|
||||
local x2, y2 = x1 + w, y1 + h
|
||||
local halfThumb = thumb:getWidth() / 2
|
||||
x1, x2 = x1 + halfThumb, x2 - halfThumb
|
||||
self.value = clamp((event.x - x1) / (x2 - x1))
|
||||
@@ -58,7 +59,9 @@ return function (self)
|
||||
end)
|
||||
|
||||
self:onReshape(function (event)
|
||||
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
||||
-- TODO: eliminate redundancy with `press`
|
||||
local x1, y1, w, h = self:getRectangle(true, true)
|
||||
local x2, y2 = x1 + w, y1 + h
|
||||
local halfThumb = thumb:getWidth() / 2
|
||||
x1, x2 = x1 + halfThumb, x2 - halfThumb
|
||||
spacer.width = self.value * (x2 - x1)
|
||||
|
||||
@@ -4,7 +4,8 @@ local utf8 = require(ROOT .. 'utf8')
|
||||
local Backend = require(ROOT .. 'backend')
|
||||
|
||||
local function scrollToCaret (self)
|
||||
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
||||
local x1, y1, w, h = self:getRectangle(true, true)
|
||||
local x2, y2 = x1 + w, y1 + h
|
||||
local oldX = self.endX
|
||||
local newX
|
||||
|
||||
@@ -24,8 +25,8 @@ end
|
||||
|
||||
local function findCaretFromText (self, text)
|
||||
local font = self.fontData
|
||||
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
||||
return #text, font:getAdvance(text) + x1 - self.scrollX
|
||||
local x = self:getRectangle(true, true)
|
||||
return #text, font:getAdvance(text) + x - self.scrollX
|
||||
end
|
||||
|
||||
local function setCaretFromText (self, text, mode)
|
||||
@@ -43,7 +44,7 @@ end
|
||||
|
||||
-- return caret index and x position
|
||||
local function findCaretFromPoint (self, x, y)
|
||||
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
||||
local x1 = self:getRectangle(true, true)
|
||||
|
||||
local font = self.fontData
|
||||
local width, lastWidth = 0
|
||||
@@ -227,27 +228,27 @@ return function (self)
|
||||
|
||||
self:onDisplay(function (event)
|
||||
local startX, endX = self.startX or 0, self.endX or 0
|
||||
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
||||
local width, height = endX - startX, y2 - y1
|
||||
local x, y, w, h = self:getRectangle(true, true)
|
||||
local width, height = endX - startX, h
|
||||
local font = self.fontData
|
||||
local textColor = self.textColor or { 0, 0, 0, 255 }
|
||||
local textTop = math.floor(y1 + ((y2 - y1) - font:getLineHeight()) / 2)
|
||||
local textTop = math.floor(y + (h - font:getLineHeight()) / 2)
|
||||
|
||||
Backend.push()
|
||||
Backend.setScissor(x1, y1, x2 - x1, y2 - y1)
|
||||
Backend.setScissor(x, y, w, h)
|
||||
Backend.setFont(font)
|
||||
|
||||
-- draw highlight
|
||||
Backend.setColor(self.highlight)
|
||||
Backend.drawRectangle('fill', startX, y1, width, height)
|
||||
Backend.drawRectangle('fill', startX, y, width, height)
|
||||
if Backend.getTime() % 2 < 1.75 then
|
||||
Backend.setColor(textColor)
|
||||
Backend.drawRectangle('fill', endX, y1, 1, height)
|
||||
Backend.drawRectangle('fill', endX, y, 1, height)
|
||||
end
|
||||
|
||||
-- draw text
|
||||
Backend.setColor(textColor)
|
||||
Backend.print(self.value, x1 - self.scrollX, textTop)
|
||||
Backend.print(self.value, x - self.scrollX, textTop)
|
||||
if not self.focused then
|
||||
Backend.pop()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user