1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-14 20:54:50 +00:00
opus/sys/apis/ui/canvas.lua

367 lines
7.8 KiB
Lua
Raw Normal View History

2017-10-05 17:07:48 +00:00
local class = require('class')
local Region = require('ui.region')
local Util = require('util')
2017-10-06 07:07:24 +00:00
local _rep = string.rep
local _sub = string.sub
local _gsub = string.gsub
local Canvas = class()
2017-10-05 17:07:48 +00:00
2017-10-06 07:07:24 +00:00
Canvas.colorPalette = { }
Canvas.darkPalette = { }
Canvas.grayscalePalette = { }
for n = 1, 16 do
Canvas.colorPalette[2 ^ (n - 1)] = _sub("0123456789abcdef", n, n)
Canvas.grayscalePalette[2 ^ (n - 1)] = _sub("088888878877787f", n, n)
Canvas.darkPalette[2 ^ (n - 1)] = _sub("8777777f77fff77f", n, n)
2017-10-05 17:07:48 +00:00
end
function Canvas:init(args)
self.x = 1
self.y = 1
2017-10-06 07:07:24 +00:00
self.bg = colors.black
self.fg = colors.white
2017-10-05 17:07:48 +00:00
self.layers = { }
Util.merge(self, args)
2017-10-06 07:07:24 +00:00
self.ex = self.x + self.width - 1
self.ey = self.y + self.height - 1
if not self.palette then
if self.isColor then
self.palette = Canvas.colorPalette
else
self.palette = Canvas.grayscalePalette
end
end
2017-10-05 17:07:48 +00:00
self.lines = { }
for i = 1, self.height do
self.lines[i] = { }
end
end
function Canvas:resize(w, h)
for i = self.height, h do
self.lines[i] = { }
end
while #self.lines > h do
table.remove(self.lines, #self.lines)
end
if w ~= self.width then
for i = 1, self.height do
2017-10-06 07:07:24 +00:00
self.lines[i] = { dirty = true }
2017-10-05 17:07:48 +00:00
end
end
self.ex = self.x + w - 1
self.ey = self.y + h - 1
self.width = w
self.height = h
end
function Canvas:copy()
2017-10-06 07:07:24 +00:00
local b = Canvas({
x = self.x,
y = self.y,
width = self.width,
height = self.height,
isColor = self.isColor,
})
for i = 1, self.height do
2017-10-05 17:07:48 +00:00
b.lines[i].text = self.lines[i].text
b.lines[i].fg = self.lines[i].fg
b.lines[i].bg = self.lines[i].bg
end
return b
end
function Canvas:addLayer(layer, bg, fg)
local canvas = Canvas({
2017-10-06 07:07:24 +00:00
x = layer.x,
y = layer.y,
width = layer.width,
height = layer.height,
2017-10-05 17:07:48 +00:00
isColor = self.isColor,
})
canvas:clear(bg, fg)
canvas.parent = self
table.insert(self.layers, canvas)
return canvas
end
function Canvas:removeLayer()
for k, layer in pairs(self.parent.layers) do
if layer == self then
self:setVisible(false)
table.remove(self.parent.layers, k)
break
end
end
end
function Canvas:setVisible(visible)
self.visible = visible
if not visible then
self.parent:dirty()
-- set parent's lines to dirty for each line in self
end
end
function Canvas:write(x, y, text, bg, fg)
if bg then
2017-10-06 07:07:24 +00:00
bg = _rep(self.palette[bg], #text)
2017-10-05 17:07:48 +00:00
end
if fg then
2017-10-06 07:07:24 +00:00
fg = _rep(self.palette[fg], #text)
2017-10-05 17:07:48 +00:00
end
self:writeBlit(x, y, text, bg, fg)
end
function Canvas:writeBlit(x, y, text, bg, fg)
if y > 0 and y <= self.height and x <= self.width then
local width = #text
-- fix ffs
if x < 1 then
2017-10-06 07:07:24 +00:00
text = _sub(text, 2 - x)
2017-10-05 17:07:48 +00:00
if bg then
2017-10-06 07:07:24 +00:00
bg = _sub(bg, 2 - x)
2017-10-05 17:07:48 +00:00
end
if bg then
2017-10-06 07:07:24 +00:00
fg = _sub(fg, 2 - x)
2017-10-05 17:07:48 +00:00
end
width = width + x - 1
x = 1
end
if x + width - 1 > self.width then
2017-10-06 07:07:24 +00:00
text = _sub(text, 1, self.width - x + 1)
2017-10-05 17:07:48 +00:00
if bg then
2017-10-06 07:07:24 +00:00
bg = _sub(bg, 1, self.width - x + 1)
2017-10-05 17:07:48 +00:00
end
if bg then
2017-10-06 07:07:24 +00:00
fg = _sub(fg, 1, self.width - x + 1)
2017-10-05 17:07:48 +00:00
end
width = #text
end
if width > 0 then
local function replace(sstr, pos, rstr, width)
if pos == 1 and width == self.width then
return rstr
elseif pos == 1 then
2017-10-06 07:07:24 +00:00
return rstr .. _sub(sstr, pos+width)
2017-10-05 17:07:48 +00:00
elseif pos + width > self.width then
2017-10-06 07:07:24 +00:00
return _sub(sstr, 1, pos-1) .. rstr
2017-10-05 17:07:48 +00:00
end
2017-10-06 07:07:24 +00:00
return _sub(sstr, 1, pos-1) .. rstr .. _sub(sstr, pos+width)
2017-10-05 17:07:48 +00:00
end
local line = self.lines[y]
line.dirty = true
line.text = replace(line.text, x, text, width)
if fg then
line.fg = replace(line.fg, x, fg, width)
end
if bg then
line.bg = replace(line.bg, x, bg, width)
end
end
end
end
function Canvas:writeLine(y, text, fg, bg)
self.lines[y].dirty = true
self.lines[y].text = text
self.lines[y].fg = fg
self.lines[y].bg = bg
end
function Canvas:reset()
self.regions = nil
end
function Canvas:clear(bg, fg)
2017-10-06 07:07:24 +00:00
local text = _rep(' ', self.width)
fg = _rep(self.palette[fg or self.fg], self.width)
bg = _rep(self.palette[bg or self.bg], self.width)
for i = 1, self.height do
2017-10-05 17:07:48 +00:00
self:writeLine(i, text, fg, bg)
end
end
function Canvas:punch(rect)
if not self.regions then
self.regions = Region.new(self.x, self.y, self.ex, self.ey)
end
self.regions:subRect(rect.x, rect.y, rect.ex, rect.ey)
end
function Canvas:blitClipped(device)
for _,region in ipairs(self.regions.region) do
self:blit(device,
{ x = region[1] - self.x + 1,
y = region[2] - self.y + 1,
ex = region[3]- self.x + 1,
ey = region[4] - self.y + 1 },
{ x = region[1], y = region[2] })
end
end
function Canvas:redraw(device)
self:reset()
if #self.layers > 0 then
for _,layer in pairs(self.layers) do
self:punch(layer)
end
self:blitClipped(device)
else
self:blit(device)
end
self:clean()
end
function Canvas:isDirty()
for _, line in pairs(self.lines) do
if line.dirty then
return true
end
end
end
function Canvas:dirty()
for _, line in pairs(self.lines) do
line.dirty = true
end
end
function Canvas:clean()
for y, line in pairs(self.lines) do
line.dirty = false
end
end
function Canvas:render(device, layers) --- redrawAll ?
layers = layers or self.layers
if #layers > 0 then
self.regions = Region.new(self.x, self.y, self.ex, self.ey)
local l = Util.shallowCopy(layers)
for _, canvas in ipairs(layers) do
table.remove(l, 1)
if canvas.visible then
self:punch(canvas)
canvas:render(device, l)
end
end
self:blitClipped(device)
self:reset()
else
self:blit(device)
end
self:clean()
end
function Canvas:blit(device, src, tgt)
src = src or { x = 1, y = 1, ex = self.ex - self.x + 1, ey = self.ey - self.y + 1 }
tgt = tgt or self
for i = 0, src.ey - src.y do
local line = self.lines[src.y + i]
if line and line.dirty then
local t, fg, bg = line.text, line.fg, line.bg
if src.x > 1 or src.ex < self.ex then
2017-10-06 07:07:24 +00:00
t = _sub(t, src.x, src.ex)
fg = _sub(fg, src.x, src.ex)
bg = _sub(bg, src.x, src.ex)
2017-10-05 17:07:48 +00:00
end
--if tgt.y + i > self.ey then -- wrong place to do clipping ??
-- break
--end
device.setCursorPos(tgt.x, tgt.y + i)
device.blit(t, fg, bg)
end
end
end
2017-10-06 07:07:24 +00:00
function Canvas:applyPalette(palette)
local lookup = { }
for n = 1, 16 do
lookup[self.palette[2 ^ (n - 1)]] = palette[2 ^ (n - 1)]
end
for _, l in pairs(self.lines) do
l.fg = _gsub(l.fg, '%w', lookup)
l.bg = _gsub(l.bg, '%w', lookup)
l.dirty = true
end
self.palette = palette
end
2017-10-05 17:07:48 +00:00
function Canvas.convertWindow(win, parent, x, y)
local w, h = win.getSize()
win.canvas = Canvas({
2017-10-06 07:07:24 +00:00
x = x,
y = y,
width = w,
height = h,
2017-10-05 17:07:48 +00:00
isColor = win.isColor(),
})
function win.clear()
win.canvas:clear(win.getBackgroundColor(), win.getTextColor())
end
function win.clearLine()
local x, y = win.getCursorPos()
win.canvas:write(1,
y,
2017-10-06 07:07:24 +00:00
_rep(' ', win.canvas.width),
2017-10-05 17:07:48 +00:00
win.getBackgroundColor(),
win.getTextColor())
end
function win.write(str)
local x, y = win.getCursorPos()
win.canvas:write(x,
y,
str,
win.getBackgroundColor(),
win.getTextColor())
end
function win.blit(text, fg, bg)
local x, y = win.getCursorPos()
win.canvas:writeBlit(x, y, text, bg, fg)
end
function win.redraw()
win.canvas:redraw(parent)
end
function win.scroll()
error('CWin:scroll: not implemented')
end
function win.reposition(x, y, width, height)
win.canvas.x, win.canvas.y = x, y
win.canvas:resize(width or win.canvas.width, height or win.canvas.height)
end
win.clear()
end
return Canvas