getRectangle returns x,y,w,h instead of x1,y1,x2,y2

This commit is contained in:
airstruck
2015-11-27 06:40:02 -05:00
parent 29dac8a611
commit 290f6333b7
5 changed files with 64 additions and 59 deletions

View File

@@ -57,53 +57,53 @@ function Renderer:renderSlices (widget)
local path = widget.slices local path = widget.slices
if not path then return end 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 slices = self:loadSlices(path)
local batch = Backend.SpriteBatch(slices.image) local batch = Backend.SpriteBatch(slices.image)
local xScale = ((x2 - x1) - slices.width * 2) / slices.width local xScale = (w - slices.width * 2) / slices.width
local yScale = ((y2 - y1) - slices.height * 2) / slices.height 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) xScale, yScale)
batch:add(slices.topCenter, x1 + slices.width, y1, 0, batch:add(slices.topCenter, x + slices.width, y, 0,
xScale, 1) 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) xScale, 1)
batch:add(slices.middleLeft, x1, y1 + slices.height, 0, batch:add(slices.middleLeft, x, y + slices.height, 0,
1, yScale) 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) 1, yScale)
batch:add(slices.topLeft, x1, y1) batch:add(slices.topLeft, x, y)
batch:add(slices.topRight, x2 - slices.width, y1) batch:add(slices.topRight, x + w - slices.width, y)
batch:add(slices.bottomLeft, x1, y2 - slices.height) batch:add(slices.bottomLeft, x, y + h - slices.height)
batch:add(slices.bottomRight, x2 - slices.width, y2 - slices.height) batch:add(slices.bottomRight, x + w - slices.width, y + h - slices.height)
Backend.draw(batch) Backend.draw(batch)
end end
function Renderer:renderBackground (widget) function Renderer:renderBackground (widget)
if not widget.background then return end 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.push()
Backend.setColor(widget.background) Backend.setColor(widget.background)
Backend.drawRectangle('fill', x1, y1, x2 - x1, y2 - y1) Backend.drawRectangle('fill', x, y, w, h)
Backend.pop() Backend.pop()
end end
function Renderer:renderOutline (widget) function Renderer:renderOutline (widget)
if not widget.outline then return end 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.push()
Backend.setColor(widget.outline) 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() Backend.pop()
end end
@@ -187,22 +187,22 @@ function Renderer:positionText (widget, x1, y1, x2, y2)
end end
function Renderer:renderIconAndText (widget) 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 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 return
end end
Backend.push() Backend.push()
Backend.setScissor(x1, y1, x2 - x1, y2 - y1) Backend.setScissor(x, y, w, h)
local iconX, iconY, textX, textY, font
-- calculate position for icon and text based on alignment and padding -- calculate position for icon and text based on alignment and padding
iconX, iconY, x1, y1, x2, y2 = self:positionIcon(widget, x1, y1, x2, y2) local iconX, iconY, x1, y1, x2, y2 = self:positionIcon(
font, textX, textY = self:positionText(widget, x1, y1, x2, y2) 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 icon = widget.icon and self:loadImage(widget.icon)
local text = widget.text local text = widget.text
@@ -217,15 +217,15 @@ function Renderer:renderIconAndText (widget)
if align:find 'middle' then if align:find 'middle' then
local textHeight = widget.textData:getHeight() local textHeight = widget.textData:getHeight()
local contentHeight = textHeight + padding + iconHeight local contentHeight = textHeight + padding + iconHeight
local offset = ((y2 - y1) - contentHeight) / 2 local offset = (h - contentHeight) / 2
iconY = y1 + offset iconY = y + offset
textY = y1 + offset + padding + iconHeight textY = y + offset + padding + iconHeight
elseif align:find 'top' then elseif align:find 'top' then
iconY = y1 iconY = y
textY = y1 + padding + iconHeight textY = y + padding + iconHeight
else -- if align:find 'bottom' else -- if align:find 'bottom'
local textHeight = widget.textData:getHeight() local textHeight = widget.textData:getHeight()
textY = y2 - textHeight textY = y + h - textHeight
iconY = textY - padding - iconHeight iconY = textY - padding - iconHeight
end end
end end
@@ -234,9 +234,9 @@ function Renderer:renderIconAndText (widget)
-- TODO: handle this in Backend.Text -- TODO: handle this in Backend.Text
if text and not widget.wrap then if text and not widget.wrap then
if align:find 'right' then if align:find 'right' then
textX = textX + ((x2 - x1) - widget.textData:getWidth()) textX = textX + (w - widget.textData:getWidth())
elseif align:find 'center' then elseif align:find 'center' then
textX = textX + ((x2 - x1) - widget.textData:getWidth()) / 2 textX = textX + (w - widget.textData:getWidth()) / 2
end end
end end
@@ -250,7 +250,7 @@ function Renderer:renderIconAndText (widget)
end end
-- draw the text -- draw the text
if text and x2 > x1 then if text and w > 1 then
textX, textY = math.floor(textX), math.floor(textY) textX, textY = math.floor(textX), math.floor(textY)
Backend.draw(widget.textData, textX, textY) Backend.draw(widget.textData, textX, textY)
end end

View File

@@ -487,7 +487,7 @@ function Widget:getHeight ()
end 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 @tparam boolean useMargin
Whether to adjust the rectangle based on the widget's margin. 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. The upper left corner's Y position.
@treturn number @treturn number
The lower right corner's X position. The rectangle's width
@treturn number @treturn number
The lower right corner's Y position. The rectangle's height
--]]-- --]]--
function Widget:getRectangle (useMargin, usePadding) function Widget:getRectangle (useMargin, usePadding)
local x1, y1 = self:getX(), self:getY() local x, y = self:getX(), self:getY()
local x2, y2 = self:getWidth() + x1, self:getHeight() + y1 local w, h = self:getWidth(), self:getHeight()
local function shrink(amount) local function shrink(amount)
x1 = x1 + amount x = x + amount
y1 = y1 + amount y = y + amount
x2 = x2 - amount w = w - amount * 2
y2 = y2 - amount h = h - amount * 2
end end
if useMargin then if useMargin then
shrink(self.margin or 0) shrink(self.margin or 0)
@@ -522,7 +522,7 @@ function Widget:getRectangle (useMargin, usePadding)
if usePadding then if usePadding then
shrink(self.padding or 0) shrink(self.padding or 0)
end 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 end
--[[-- --[[--
@@ -540,7 +540,8 @@ true if the point is within the widget, else false.
function Widget:isAt (x, y) function Widget:isAt (x, y)
checkReshape(self) 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) return (x1 <= x) and (x2 >= x) and (y1 <= y) and (y2 >= y)
end end

View File

@@ -12,9 +12,9 @@ return function (self)
end) end)
self:onReshape(function () 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 local min = bar.minwidth
x1 = x1 + min x = x + min
bar.width = self.value * (x2 - x1) + min bar.width = self.value * w + min
end) end)
end end

View File

@@ -35,7 +35,8 @@ return function (self)
end) end)
local function press (event) 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 local halfThumb = thumb:getWidth() / 2
x1, x2 = x1 + halfThumb, x2 - halfThumb x1, x2 = x1 + halfThumb, x2 - halfThumb
self.value = clamp((event.x - x1) / (x2 - x1)) self.value = clamp((event.x - x1) / (x2 - x1))
@@ -58,7 +59,9 @@ return function (self)
end) end)
self:onReshape(function (event) 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 local halfThumb = thumb:getWidth() / 2
x1, x2 = x1 + halfThumb, x2 - halfThumb x1, x2 = x1 + halfThumb, x2 - halfThumb
spacer.width = self.value * (x2 - x1) spacer.width = self.value * (x2 - x1)

View File

@@ -4,7 +4,8 @@ local utf8 = require(ROOT .. 'utf8')
local Backend = require(ROOT .. 'backend') local Backend = require(ROOT .. 'backend')
local function scrollToCaret (self) 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 oldX = self.endX
local newX local newX
@@ -24,8 +25,8 @@ end
local function findCaretFromText (self, text) local function findCaretFromText (self, text)
local font = self.fontData local font = self.fontData
local x1, y1, x2, y2 = self:getRectangle(true, true) local x = self:getRectangle(true, true)
return #text, font:getAdvance(text) + x1 - self.scrollX return #text, font:getAdvance(text) + x - self.scrollX
end end
local function setCaretFromText (self, text, mode) local function setCaretFromText (self, text, mode)
@@ -43,7 +44,7 @@ end
-- return caret index and x position -- return caret index and x position
local function findCaretFromPoint (self, x, y) 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 font = self.fontData
local width, lastWidth = 0 local width, lastWidth = 0
@@ -227,27 +228,27 @@ return function (self)
self:onDisplay(function (event) self:onDisplay(function (event)
local startX, endX = self.startX or 0, self.endX or 0 local startX, endX = self.startX or 0, self.endX or 0
local x1, y1, x2, y2 = self:getRectangle(true, true) local x, y, w, h = self:getRectangle(true, true)
local width, height = endX - startX, y2 - y1 local width, height = endX - startX, h
local font = self.fontData local font = self.fontData
local textColor = self.textColor or { 0, 0, 0, 255 } 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.push()
Backend.setScissor(x1, y1, x2 - x1, y2 - y1) Backend.setScissor(x, y, w, h)
Backend.setFont(font) Backend.setFont(font)
-- draw highlight -- draw highlight
Backend.setColor(self.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 if Backend.getTime() % 2 < 1.75 then
Backend.setColor(textColor) Backend.setColor(textColor)
Backend.drawRectangle('fill', endX, y1, 1, height) Backend.drawRectangle('fill', endX, y, 1, height)
end end
-- draw text -- draw text
Backend.setColor(textColor) Backend.setColor(textColor)
Backend.print(self.value, x1 - self.scrollX, textTop) Backend.print(self.value, x - self.scrollX, textTop)
if not self.focused then if not self.focused then
Backend.pop() Backend.pop()
return return