mirror of
https://github.com/airstruck/luigi.git
synced 2026-01-10 08:18:22 +00:00
Renderer -> Painter
This commit is contained in:
@@ -3,7 +3,6 @@ local Backend = require 'luigi.backend'
|
|||||||
|
|
||||||
local window = Layout {
|
local window = Layout {
|
||||||
type = 'window',
|
type = 'window',
|
||||||
background = { 255, 255, 255 },
|
|
||||||
icon = 'logo.png',
|
icon = 'logo.png',
|
||||||
title = 'Test window',
|
title = 'Test window',
|
||||||
width = 300,
|
width = 300,
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ local ROOT = (...):gsub('[^.]*$', '')
|
|||||||
local Backend = require(ROOT .. 'backend')
|
local Backend = require(ROOT .. 'backend')
|
||||||
local Base = require(ROOT .. 'base')
|
local Base = require(ROOT .. 'base')
|
||||||
local Event = require(ROOT .. 'event')
|
local Event = require(ROOT .. 'event')
|
||||||
local Renderer = require(ROOT .. 'renderer')
|
|
||||||
|
|
||||||
local Input = Base:extend()
|
local Input = Base:extend()
|
||||||
|
|
||||||
@@ -17,7 +16,7 @@ end
|
|||||||
|
|
||||||
function Input:handleDisplay (layout)
|
function Input:handleDisplay (layout)
|
||||||
local root = layout.root
|
local root = layout.root
|
||||||
if root then Renderer:render(root) end
|
if root then root:paint() end
|
||||||
Event.Display:emit(layout)
|
Event.Display:emit(layout)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -215,7 +214,7 @@ function Input:handleReshape (layout, width, height)
|
|||||||
|
|
||||||
root:reshape()
|
root:reshape()
|
||||||
|
|
||||||
if root.type ~= 'window' then
|
if root.type ~= 'window' then -- FIXME: move stuff below to a Widget method
|
||||||
if not root.width then
|
if not root.width then
|
||||||
root.dimensions.width = width
|
root.dimensions.width = width
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ local Event = require(ROOT .. 'event')
|
|||||||
local Font = Backend.Font
|
local Font = Backend.Font
|
||||||
local Text = Backend.Text
|
local Text = Backend.Text
|
||||||
|
|
||||||
local Renderer = Base:extend()
|
local Painter = Base:extend()
|
||||||
|
|
||||||
local imageCache = {}
|
local imageCache = {}
|
||||||
local sliceCache = {}
|
local sliceCache = {}
|
||||||
@@ -28,7 +28,11 @@ local function intersectScissor (x, y, w, h)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Renderer:loadImage (path)
|
function Painter:constructor (widget)
|
||||||
|
self.widget = widget
|
||||||
|
end
|
||||||
|
|
||||||
|
function Painter:loadImage (path)
|
||||||
if not imageCache[path] then
|
if not imageCache[path] then
|
||||||
imageCache[path] = Backend.Image(path)
|
imageCache[path] = Backend.Image(path)
|
||||||
end
|
end
|
||||||
@@ -38,7 +42,7 @@ end
|
|||||||
|
|
||||||
-- TODO: make slices a seperate drawable
|
-- TODO: make slices a seperate drawable
|
||||||
|
|
||||||
function Renderer:loadSlices (path)
|
function Painter:loadSlices (path)
|
||||||
local slices = sliceCache[path]
|
local slices = sliceCache[path]
|
||||||
|
|
||||||
if not slices then
|
if not slices then
|
||||||
@@ -69,8 +73,8 @@ function Renderer:loadSlices (path)
|
|||||||
return slices
|
return slices
|
||||||
end
|
end
|
||||||
|
|
||||||
function Renderer:renderSlices (widget)
|
function Painter:paintSlices ()
|
||||||
|
local widget = self.widget
|
||||||
local path = widget.slices
|
local path = widget.slices
|
||||||
if not path then return end
|
if not path then return end
|
||||||
|
|
||||||
@@ -104,7 +108,8 @@ function Renderer:renderSlices (widget)
|
|||||||
Backend.draw(batch)
|
Backend.draw(batch)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Renderer:renderBackground (widget)
|
function Painter:paintBackground ()
|
||||||
|
local widget = self.widget
|
||||||
if not widget.background then return end
|
if not widget.background then return end
|
||||||
local x, y, w, h = widget:getRectangle(true)
|
local x, y, w, h = widget:getRectangle(true)
|
||||||
|
|
||||||
@@ -114,7 +119,8 @@ function Renderer:renderBackground (widget)
|
|||||||
Backend.pop()
|
Backend.pop()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Renderer:renderOutline (widget)
|
function Painter:paintOutline ()
|
||||||
|
local widget = self.widget
|
||||||
if not widget.outline then return end
|
if not widget.outline then return end
|
||||||
local x, y, w, h = widget:getRectangle(true)
|
local x, y, w, h = widget:getRectangle(true)
|
||||||
|
|
||||||
@@ -125,7 +131,8 @@ function Renderer:renderOutline (widget)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- returns icon coordinates and rectangle with remaining space
|
-- returns icon coordinates and rectangle with remaining space
|
||||||
function Renderer:positionIcon (widget, x1, y1, x2, y2)
|
function Painter:positionIcon (x1, y1, x2, y2)
|
||||||
|
local widget = self.widget
|
||||||
if not widget.icon then
|
if not widget.icon then
|
||||||
return nil, nil, x1, y1, x2, y2
|
return nil, nil, x1, y1, x2, y2
|
||||||
end
|
end
|
||||||
@@ -160,7 +167,8 @@ function Renderer:positionIcon (widget, x1, y1, x2, y2)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- returns text coordinates
|
-- returns text coordinates
|
||||||
function Renderer:positionText (widget, x1, y1, x2, y2)
|
function Painter:positionText (x1, y1, x2, y2)
|
||||||
|
local widget = self.widget
|
||||||
if not widget.text or x1 >= x2 then
|
if not widget.text or x1 >= x2 then
|
||||||
return nil, nil, x1, y1, x2, y2
|
return nil, nil, x1, y1, x2, y2
|
||||||
end
|
end
|
||||||
@@ -199,16 +207,17 @@ function Renderer:positionText (widget, x1, y1, x2, y2)
|
|||||||
return font, x1, y
|
return font, x1, y
|
||||||
end
|
end
|
||||||
|
|
||||||
function Renderer:renderIconAndText (widget)
|
function Painter:paintIconAndText ()
|
||||||
|
local widget = self.widget
|
||||||
if not (widget.icon or widget.text) then return end
|
if not (widget.icon or widget.text) then return end
|
||||||
local x, y, w, h = widget:getRectangle(true, true)
|
local x, y, w, h = widget:getRectangle(true, true)
|
||||||
if w < 1 or h < 1 then return end
|
if w < 1 or h < 1 then return end
|
||||||
|
|
||||||
-- calculate position for icon and text based on alignment and padding
|
-- calculate position for icon and text based on alignment and padding
|
||||||
local iconX, iconY, x1, y1, x2, y2 = self:positionIcon(
|
local iconX, iconY, x1, y1, x2, y2 = self:positionIcon(
|
||||||
widget, x, y, x + w, y + h)
|
x, y, x + w, y + h)
|
||||||
local font, textX, textY = self:positionText(
|
local font, textX, textY = self:positionText(
|
||||||
widget, x1, y1, x2, y2)
|
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
|
||||||
@@ -265,36 +274,37 @@ function Renderer:renderIconAndText (widget)
|
|||||||
Backend.pop()
|
Backend.pop()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Renderer:renderChildren (widget)
|
function Painter:paintChildren ()
|
||||||
|
local widget = self.widget
|
||||||
for i, child in ipairs(widget) do
|
for i, child in ipairs(widget) do
|
||||||
self:render(child)
|
child:paint()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Renderer:render (widget)
|
function Painter:paint ()
|
||||||
|
local widget = self.widget
|
||||||
Event.PreDisplay:emit(widget, { target = widget }, function()
|
Event.PreDisplay:emit(widget, { target = widget }, function()
|
||||||
|
|
||||||
local x, y, w, h = widget:getRectangle()
|
local x, y, w, h = widget:getRectangle()
|
||||||
|
|
||||||
-- if the drawable area has no width or height, don't render
|
-- if the drawable area has no width or height, don't paint
|
||||||
if w < 1 or h < 1 then
|
if w < 1 or h < 1 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
Backend.push()
|
Backend.push()
|
||||||
|
|
||||||
local parent = widget.parent
|
if widget.parent then
|
||||||
if parent then
|
|
||||||
intersectScissor(x, y, w, h)
|
intersectScissor(x, y, w, h)
|
||||||
else
|
else
|
||||||
Backend.setScissor()
|
Backend.setScissor()
|
||||||
end
|
end
|
||||||
|
|
||||||
self:renderBackground(widget)
|
self:paintBackground()
|
||||||
self:renderOutline(widget)
|
self:paintOutline()
|
||||||
self:renderSlices(widget)
|
self:paintSlices()
|
||||||
self:renderIconAndText(widget)
|
self:paintIconAndText()
|
||||||
self:renderChildren(widget)
|
self:paintChildren()
|
||||||
|
|
||||||
Backend.pop()
|
Backend.pop()
|
||||||
|
|
||||||
@@ -302,4 +312,4 @@ function Renderer:render (widget)
|
|||||||
Event.Display:emit(widget, { target = widget })
|
Event.Display:emit(widget, { target = widget })
|
||||||
end
|
end
|
||||||
|
|
||||||
return Renderer
|
return Painter
|
||||||
@@ -10,6 +10,7 @@ local ROOT = (...):gsub('[^.]*$', '')
|
|||||||
local Backend = require(ROOT .. 'backend')
|
local Backend = require(ROOT .. 'backend')
|
||||||
local Event = require(ROOT .. 'event')
|
local Event = require(ROOT .. 'event')
|
||||||
local Attribute = require(ROOT .. 'attribute')
|
local Attribute = require(ROOT .. 'attribute')
|
||||||
|
local Painter = require(ROOT .. 'painter')
|
||||||
local Font = Backend.Font
|
local Font = Backend.Font
|
||||||
|
|
||||||
local Widget = {}
|
local Widget = {}
|
||||||
@@ -215,6 +216,7 @@ local function metaCall (Widget, layout, self)
|
|||||||
self.dimensions = { width = nil, height = nil }
|
self.dimensions = { width = nil, height = nil }
|
||||||
self.attributes = {}
|
self.attributes = {}
|
||||||
self.attributeDescriptors = {}
|
self.attributeDescriptors = {}
|
||||||
|
self.painter = Painter(self)
|
||||||
|
|
||||||
setmetatable(self, { __index = metaIndex, __newindex = metaNewIndex })
|
setmetatable(self, { __index = metaIndex, __newindex = metaNewIndex })
|
||||||
|
|
||||||
@@ -483,7 +485,7 @@ function Widget:calculateDimension (name)
|
|||||||
return size
|
return size
|
||||||
end
|
end
|
||||||
|
|
||||||
local function calculateRootPosition (self, axis)
|
function Widget:calculateRootPosition (axis)
|
||||||
local value = (axis == 'x' and self.left) or (axis ~= 'x' and self.top)
|
local value = (axis == 'x' and self.left) or (axis ~= 'x' and self.top)
|
||||||
|
|
||||||
if value then
|
if value then
|
||||||
@@ -512,7 +514,7 @@ function Widget:calculatePosition (axis)
|
|||||||
local parent = self.parent
|
local parent = self.parent
|
||||||
local scroll = 0
|
local scroll = 0
|
||||||
if not parent then
|
if not parent then
|
||||||
return calculateRootPosition(self, axis)
|
return self:calculateRootPosition(axis)
|
||||||
else
|
else
|
||||||
scroll = axis == 'x' and (parent.scrollX or 0)
|
scroll = axis == 'x' and (parent.scrollX or 0)
|
||||||
or axis ~= 'x' and (parent.scrollY or 0)
|
or axis ~= 'x' and (parent.scrollY or 0)
|
||||||
@@ -640,7 +642,7 @@ function Widget:getFont ()
|
|||||||
end
|
end
|
||||||
return self.fontData
|
return self.fontData
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[--
|
--[[--
|
||||||
Get x/y/width/height values describing a rectangle within the widget.
|
Get x/y/width/height values describing a rectangle within the widget.
|
||||||
|
|
||||||
@@ -722,6 +724,10 @@ function Widget:eachAncestor (includeSelf)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Widget:paint ()
|
||||||
|
return self.painter:paint()
|
||||||
|
end
|
||||||
|
|
||||||
--[[--
|
--[[--
|
||||||
Reshape the widget.
|
Reshape the widget.
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,14 @@ local ROOT = (...):gsub('[^.]*.[^.]*$', '')
|
|||||||
local Backend = require(ROOT .. 'backend')
|
local Backend = require(ROOT .. 'backend')
|
||||||
|
|
||||||
return function (self)
|
return function (self)
|
||||||
|
|
||||||
|
function self:calculateRootPosition (axis)
|
||||||
|
self.position[axis] = 0
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
function self.painter:paintIconAndText () end
|
||||||
|
|
||||||
--[[--
|
--[[--
|
||||||
Special Attributes
|
Special Attributes
|
||||||
|
|
||||||
@@ -130,7 +138,7 @@ Window icon. Should be a string containing a path to an image.
|
|||||||
icon = value
|
icon = value
|
||||||
Backend.setWindowIcon(value)
|
Backend.setWindowIcon(value)
|
||||||
end,
|
end,
|
||||||
-- get = function () return false end
|
get = function () return icon end
|
||||||
})
|
})
|
||||||
--[[--
|
--[[--
|
||||||
Maximum width of the window's client area.
|
Maximum width of the window's client area.
|
||||||
@@ -194,7 +202,7 @@ Position of the window's top edge.
|
|||||||
if value == nil then return end
|
if value == nil then return end
|
||||||
Backend.setWindowTop(value)
|
Backend.setWindowTop(value)
|
||||||
end,
|
end,
|
||||||
-- get = Backend.getWindowTop
|
get = Backend.getWindowTop
|
||||||
})
|
})
|
||||||
|
|
||||||
--[[--
|
--[[--
|
||||||
@@ -207,7 +215,7 @@ Position of the window's left edge.
|
|||||||
if value == nil then return end
|
if value == nil then return end
|
||||||
Backend.setWindowLeft(value)
|
Backend.setWindowLeft(value)
|
||||||
end,
|
end,
|
||||||
-- get = Backend.getWindowLeft
|
get = Backend.getWindowLeft
|
||||||
})
|
})
|
||||||
|
|
||||||
--[[--
|
--[[--
|
||||||
|
|||||||
Reference in New Issue
Block a user