-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcompositor
182 lines (175 loc) · 5.86 KB
/
compositor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
local unpack = unpack or table.unpack
local Compositor = {
newBuffer = function(self, pid)
local _redirect = framebuffer.new(self.x, self.y, self.target.isColor())
local redirect = {}
for k, v in pairs(_redirect) do
if type(k) == "string" and type(v) == "function" then
redirect[k] = function(...)
local result = {_redirect[k](...)}
if not self.deferDraw then self:draw() end
return unpack(result)
end
else
redirect[k] = _redirect[k]
end
end
redirect.buffer.pid = pid
local bufferNum = #self.bufferStack - self.locked.top + 1
table.insert(self.bufferStack, bufferNum, redirect.buffer)
return redirect, bufferNum
end,
draw = function(self, mode)
if #self.bufferStack > 0 then
local sizeX, sizeY = self.target.getSize()
local redirect = framebuffer.new(sizeX, sizeY, self.target.isColor())
local finalBuffer = redirect.buffer
if mode ~= "cursor" then
for i=1, self.y do
finalBuffer.text[i] = string.rep(" ", self.x)
finalBuffer.textColor[i] = string.rep("0", self.x)
finalBuffer.backColor[i] = string.rep("f", self.x)
end
for i=1, #self.bufferStack do
local layer = self.bufferStack[i]
if layer.minY > 0 then
local preStop = layer.minX - 1
local preStart = math.min(1, preStop)
local postStart = layer.maxX + 1
local postStop = self.x
local j = layer.minY
repeat
finalBuffer.text[j] = string.sub(string.sub(finalBuffer.text[j], preStart, preStop)..string.sub(layer.text[j], layer.minX, layer.maxX)..string.sub(finalBuffer.text[j], postStart, postStop), 1, self.x)
finalBuffer.textColor[j] = string.sub(string.sub(finalBuffer.textColor[j], preStart, preStop)..string.sub(layer.textColor[j], layer.minX, layer.maxX)..string.sub(finalBuffer.textColor[j], postStart, postStop), 1, self.x)
finalBuffer.backColor[j] = string.sub(string.sub(finalBuffer.backColor[j], preStart, preStop)..string.sub(layer.backColor[j], layer.minX, layer.maxX)..string.sub(finalBuffer.backColor[j], postStart, postStop), 1, self.x)
j = j + 1
until j > layer.maxY
end
end
end
local top = self.bufferStack[#self.bufferStack]
finalBuffer.cursorX = top.cursorX
finalBuffer.cursorY = top.cursorY
finalBuffer.curTextColor = top.curTextColor
finalBuffer.curBackColor = top.curBackColor
--ensure cursor is inside opaque area before setting blink.
if top.cursorX >= top.minX and top.cursorX <= top.maxX and top.cursorY >= top.minY and top.cursorY <= top.maxY then
finalBuffer.cursorBlink = top.cursorBlink
else
finalBuffer.cursorBlink = false
end
--farm out actual drawing procedures to framebuffer API.
local _old = term.redirect(self.target)
term.setCursorBlink(false)
self.currentBuffer = framebuffer.draw(finalBuffer, self.currentBuffer)
if _old then
term.redirect(_old)
else
term.restore()
end
end
end,
toFront = function(self, pos)
if pos > self.locked.bottom and pos <= #self.bufferStack - self.locked.top then
local newPos = #self.bufferStack - self.locked.top
table.insert(self.bufferStack, newPos, (table.remove(self.bufferStack, pos)))
if not self.deferDraw then self:draw() end
return true
end
return false
end,
toBack = function(self, pos)
if pos > self.locked.bottom and pos <= #self.bufferStack - self.locked.top then
table.insert(self.bufferStack, self.locked.bottom + 1, (table.remove(self.bufferStack, pos)))
if not self.deferDraw then self:draw() end
return true
end
return false
end,
lockToFront = function(self, pos)
if self:toFront(pos) then
self.locked.top = self.locked.top + 1
return true
end
return false
end,
lockToBack = function(self, pos)
if self:toBack(pos) then
self.locked.bottom = self.locked.bottom + 1
return true
end
return false
end,
unlock = function(self, pos)
if pos <= self.locked.bottom then
table.insert(self.bufferStack, self.locked.bottom, (table.remove(self.bufferStack, pos)))
self.locked.bottom = self.locked.bottom - 1
if not self.deferDraw then self:draw() end
return true
elseif pos > #self.bufferStack - self.locked.top then
table.insert(self.bufferStack, #self.bufferStack - self.locked.top + 1, (table.remove(self.bufferStack, pos)))
self.locked.top = self.locked.top - 1
if not self.deferDraw then self:draw() end
return true
end
return false
end,
removeBuffer = function(self, buffer)
local pos
for i = 1, #self.bufferStack do
if self.bufferStack[i] == buffer then
pos = i
break
end
end
if pos then
if self.locked.bottom > 0 and pos <= self.locked.bottom then
self.locked.bottom = self.locked.bottom - 1
elseif self.locked.top > 0 and pos >= #self.bufferStack - self.locked.top + 1 then
self.locked.top = self.locked.top - 1
end
table.remove(self.bufferStack, pos)
end
if not self.deferDraw then self:draw() end
end,
removePid = function(self, pid)
for i = #self.bufferStack, 1, -1 do
local buf = self.bufferStack[i]
if buf.pid == pid then
if self.locked.bottom > 0 and i <= self.locked.bottom then
self.locked.bottom = self.locked.bottom - 1
elseif self.locked.top > 0 and i >= #self.bufferStack - self.locked.top + 1 then
self.locked.top = self.locked.top - 1
end
table.remove(self.bufferStack, i)
end
end
end,
}
local cmetatable = {
__index = Compositor,
}
function new(target)
local comp = {
bufferStack = {},
target = target or term.native,
currentBuffer = {text = {}, textColor = {}, backColor = {}},
deferDraw = false,
locked = {
top = 0,
bottom = 0,
},
}
setmetatable( comp, cmetatable )
if comp.target.getSize() then
comp.x, comp.y = comp.target.getSize()
else
return nil
end
for i=1, comp.y do
comp.currentBuffer.text[i] = string.rep(" ", comp.x)
comp.currentBuffer.textColor[i] = string.rep("0", comp.x)
comp.currentBuffer.backColor[i] = string.rep("f", comp.x)
end
return comp
end