Renderer -> Painter

This commit is contained in:
airstruck
2015-12-18 12:58:10 -05:00
parent 8dd46e2bbc
commit 0281944066
5 changed files with 56 additions and 34 deletions

View File

@@ -3,7 +3,6 @@ local Backend = require 'luigi.backend'
local window = Layout {
type = 'window',
background = { 255, 255, 255 },
icon = 'logo.png',
title = 'Test window',
width = 300,

View File

@@ -3,7 +3,6 @@ local ROOT = (...):gsub('[^.]*$', '')
local Backend = require(ROOT .. 'backend')
local Base = require(ROOT .. 'base')
local Event = require(ROOT .. 'event')
local Renderer = require(ROOT .. 'renderer')
local Input = Base:extend()
@@ -17,7 +16,7 @@ end
function Input:handleDisplay (layout)
local root = layout.root
if root then Renderer:render(root) end
if root then root:paint() end
Event.Display:emit(layout)
end
@@ -215,7 +214,7 @@ function Input:handleReshape (layout, width, height)
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
root.dimensions.width = width
end

View File

@@ -6,7 +6,7 @@ local Event = require(ROOT .. 'event')
local Font = Backend.Font
local Text = Backend.Text
local Renderer = Base:extend()
local Painter = Base:extend()
local imageCache = {}
local sliceCache = {}
@@ -28,7 +28,11 @@ local function intersectScissor (x, y, w, h)
end
end
function Renderer:loadImage (path)
function Painter:constructor (widget)
self.widget = widget
end
function Painter:loadImage (path)
if not imageCache[path] then
imageCache[path] = Backend.Image(path)
end
@@ -38,7 +42,7 @@ end
-- TODO: make slices a seperate drawable
function Renderer:loadSlices (path)
function Painter:loadSlices (path)
local slices = sliceCache[path]
if not slices then
@@ -69,8 +73,8 @@ function Renderer:loadSlices (path)
return slices
end
function Renderer:renderSlices (widget)
function Painter:paintSlices ()
local widget = self.widget
local path = widget.slices
if not path then return end
@@ -104,7 +108,8 @@ function Renderer:renderSlices (widget)
Backend.draw(batch)
end
function Renderer:renderBackground (widget)
function Painter:paintBackground ()
local widget = self.widget
if not widget.background then return end
local x, y, w, h = widget:getRectangle(true)
@@ -114,7 +119,8 @@ function Renderer:renderBackground (widget)
Backend.pop()
end
function Renderer:renderOutline (widget)
function Painter:paintOutline ()
local widget = self.widget
if not widget.outline then return end
local x, y, w, h = widget:getRectangle(true)
@@ -125,7 +131,8 @@ function Renderer:renderOutline (widget)
end
-- 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
return nil, nil, x1, y1, x2, y2
end
@@ -160,7 +167,8 @@ function Renderer:positionIcon (widget, x1, y1, x2, y2)
end
-- 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
return nil, nil, x1, y1, x2, y2
end
@@ -199,16 +207,17 @@ function Renderer:positionText (widget, x1, y1, x2, y2)
return font, x1, y
end
function Renderer:renderIconAndText (widget)
function Painter:paintIconAndText ()
local widget = self.widget
if not (widget.icon or widget.text) then return end
local x, y, w, h = widget:getRectangle(true, true)
if w < 1 or h < 1 then return end
-- calculate position for icon and text based on alignment and padding
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(
widget, x1, y1, x2, y2)
x1, y1, x2, y2)
local icon = widget.icon and self:loadImage(widget.icon)
local text = widget.text
@@ -265,36 +274,37 @@ function Renderer:renderIconAndText (widget)
Backend.pop()
end
function Renderer:renderChildren (widget)
function Painter:paintChildren ()
local widget = self.widget
for i, child in ipairs(widget) do
self:render(child)
child:paint()
end
end
function Renderer:render (widget)
function Painter:paint ()
local widget = self.widget
Event.PreDisplay:emit(widget, { target = widget }, function()
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
return
end
Backend.push()
local parent = widget.parent
if parent then
if widget.parent then
intersectScissor(x, y, w, h)
else
Backend.setScissor()
end
self:renderBackground(widget)
self:renderOutline(widget)
self:renderSlices(widget)
self:renderIconAndText(widget)
self:renderChildren(widget)
self:paintBackground()
self:paintOutline()
self:paintSlices()
self:paintIconAndText()
self:paintChildren()
Backend.pop()
@@ -302,4 +312,4 @@ function Renderer:render (widget)
Event.Display:emit(widget, { target = widget })
end
return Renderer
return Painter

View File

@@ -10,6 +10,7 @@ local ROOT = (...):gsub('[^.]*$', '')
local Backend = require(ROOT .. 'backend')
local Event = require(ROOT .. 'event')
local Attribute = require(ROOT .. 'attribute')
local Painter = require(ROOT .. 'painter')
local Font = Backend.Font
local Widget = {}
@@ -215,6 +216,7 @@ local function metaCall (Widget, layout, self)
self.dimensions = { width = nil, height = nil }
self.attributes = {}
self.attributeDescriptors = {}
self.painter = Painter(self)
setmetatable(self, { __index = metaIndex, __newindex = metaNewIndex })
@@ -483,7 +485,7 @@ function Widget:calculateDimension (name)
return size
end
local function calculateRootPosition (self, axis)
function Widget:calculateRootPosition (axis)
local value = (axis == 'x' and self.left) or (axis ~= 'x' and self.top)
if value then
@@ -512,7 +514,7 @@ function Widget:calculatePosition (axis)
local parent = self.parent
local scroll = 0
if not parent then
return calculateRootPosition(self, axis)
return self:calculateRootPosition(axis)
else
scroll = axis == 'x' and (parent.scrollX or 0)
or axis ~= 'x' and (parent.scrollY or 0)
@@ -640,7 +642,7 @@ function Widget:getFont ()
end
return self.fontData
end
--[[--
Get x/y/width/height values describing a rectangle within the widget.
@@ -722,6 +724,10 @@ function Widget:eachAncestor (includeSelf)
end
end
function Widget:paint ()
return self.painter:paint()
end
--[[--
Reshape the widget.

View File

@@ -35,6 +35,14 @@ local ROOT = (...):gsub('[^.]*.[^.]*$', '')
local Backend = require(ROOT .. 'backend')
return function (self)
function self:calculateRootPosition (axis)
self.position[axis] = 0
return 0
end
function self.painter:paintIconAndText () end
--[[--
Special Attributes
@@ -130,7 +138,7 @@ Window icon. Should be a string containing a path to an image.
icon = value
Backend.setWindowIcon(value)
end,
-- get = function () return false end
get = function () return icon end
})
--[[--
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
Backend.setWindowTop(value)
end,
-- get = Backend.getWindowTop
get = Backend.getWindowTop
})
--[[--
@@ -207,7 +215,7 @@ Position of the window's left edge.
if value == nil then return end
Backend.setWindowLeft(value)
end,
-- get = Backend.getWindowLeft
get = Backend.getWindowLeft
})
--[[--