Skip to content

Commit cf4f15e

Browse files
committed
added multimonitor support
- cursor fix @input - now you are able to add bigger monitors - still in beta Some features are still missing: checking if monitors are connected/disconnected, resizing by destroying the blocks
1 parent a329154 commit cf4f15e

5 files changed

Lines changed: 290 additions & 27 deletions

File tree

Basalt/Frame.lua

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local _OBJECTS = require("loadObjects")
33
local BasaltDraw = require("basaltDraw")
44
local utils = require("utils")
55
local layout = require("layout")
6+
local basaltMon = require("basaltMon")
67
local uuid = utils.uuid
78
local rpairs = utils.rpairs
89
local xmlValue = utils.getValueFromXML
@@ -27,6 +28,7 @@ return function(name, parent, pTerm, basalt)
2728

2829
local monSide = ""
2930
local isMonitor = false
31+
local isGroupedMonitor = false
3032
local monitorAttached = false
3133
local dragXOffset = 0
3234
local dragYOffset = 0
@@ -522,8 +524,10 @@ return function(name, parent, pTerm, basalt)
522524
base.show(self)
523525
if(self.parent==nil)then
524526
basalt.setActiveFrame(self)
525-
if(isMonitor)then
527+
if(isMonitor)and not(isGroupedMonitor)then
526528
basalt.setMonitorFrame(monSide, self)
529+
elseif(isGroupedMonitor)then
530+
basalt.setMonitorFrame(self:getName(), self, monSide)
527531
else
528532
basalt.setMainFrame(self)
529533
end
@@ -534,11 +538,15 @@ return function(name, parent, pTerm, basalt)
534538
hide = function (self)
535539
base.hide(self)
536540
if(self.parent==nil)then
537-
if(activeFrame == self)then activeFrame = nil end
538-
if(isMonitor)then
541+
if(activeFrame == self)then activeFrame = nil end -- bug activeFrame always nil
542+
if(isMonitor)and not(isGroupedMonitor)then
539543
if(basalt.getMonitorFrame(monSide) == self)then
540544
basalt.setActiveFrame(nil)
541545
end
546+
elseif(isGroupedMonitor)then
547+
if(basalt.getMonitorFrame(self:getName()) == self)then
548+
basalt.setActiveFrame(nil)
549+
end
542550
else
543551
if(basalt.getMainFrame() == self)then
544552
basalt.setMainFrame(nil)
@@ -642,25 +650,47 @@ return function(name, parent, pTerm, basalt)
642650
return self
643651
end,
644652

645-
setMonitor = function(self, side)
653+
setMonitorScale = function(self, scale)
654+
if(isMonitor)then
655+
termObject.setTextScale(scale)
656+
end
657+
return self
658+
end,
659+
660+
setMonitor = function(self, side, scale)
646661
if(side~=nil)and(side~=false)then
647-
if(peripheral.getType(side)=="monitor")then
648-
termObject = peripheral.wrap(side)
662+
if(type(side)=="string")then
663+
if(peripheral.getType(side)=="monitor")then
664+
termObject = peripheral.wrap(side)
665+
monitorAttached = true
666+
end
667+
if(self.parent~=nil)then
668+
self.parent:removeObject(self)
669+
end
670+
isMonitor = true
671+
basalt.setMonitorFrame(side, self)
672+
elseif(type(side)=="table")then
673+
termObject = basaltMon(side)
649674
monitorAttached = true
650-
651-
end
652-
if(self.parent~=nil)then
653-
self.parent:removeObject(self)
675+
isMonitor = true
676+
isGroupedMonitor = true
677+
basalt.setMonitorFrame(self:getName(), self, true)
654678
end
655-
isMonitor = true
656-
basalt.setMonitorFrame(side, self)
657679
else
658680
termObject = parentTerminal
659681
isMonitor = false
660-
if(basalt.getMonitorFrame(monSide)==self)then
661-
basalt.setMonitorFrame(monSide, nil)
682+
isGroupedMonitor = false
683+
if(type(monSide)=="string")then
684+
if(basalt.getMonitorFrame(monSide)==self)then
685+
basalt.setMonitorFrame(monSide, nil)
686+
end
687+
else
688+
if(basalt.getMonitorFrame(self:getName())==self)then
689+
basalt.setMonitorFrame(self:getName(), nil)
690+
end
662691
end
663692
end
693+
if(scale~=nil)then termObject.setTextScale(scale) end
664694
basaltDraw = BasaltDraw(termObject)
665695
self:setSize(termObject.getSize())
666696
autoSize = true
@@ -717,8 +747,18 @@ return function(name, parent, pTerm, basalt)
717747
end
718748
if(isMonitor)then
719749
if(autoSize)then
720-
if(event=="monitor_resize")and(p1==monSide)then
721-
self:setSize(termObject.getSize())
750+
if(event=="monitor_resize")then
751+
if(type(monSide)=="string")then
752+
self:setSize(termObject.getSize())
753+
elseif(type(monSide)=="table")then
754+
for k,v in pairs(monSide)do
755+
for a,b in pairs(v)do
756+
if(p1==b)then
757+
self:setSize(termObject.getSize())
758+
end
759+
end
760+
end
761+
end
722762
autoSize = true
723763
self:updateDraw()
724764
end
@@ -752,7 +792,12 @@ return function(name, parent, pTerm, basalt)
752792
end
753793
end,
754794

755-
mouseHandler = function(self, button, x, y)
795+
mouseHandler = function(self, button, x, y, _, side)
796+
if(isGroupedMonitor)then
797+
if(termObject.calculateClick~=nil)then
798+
x, y = termObject.calculateClick(side, x, y)
799+
end
800+
end
756801
if(base.mouseHandler(self, button, x, y))then
757802
if(events["mouse_click"]~=nil)then
758803
self:setCursor(false)

Basalt/libraries/basaltDraw.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ return function(drawTerm)
181181
isBlinking = terminal.getCursorBlink()
182182
end
183183
terminal.setCursorBlink(false)
184-
if(mirrorTerm~=nil)then terminal.setCursorBlink(false) end
184+
if(mirrorTerm~=nil)then mirrorTerm.setCursorBlink(false) end
185185
for n = 1, height do
186186
terminal.setCursorPos(1, n)
187187
terminal.blit(cacheT[n], cacheFG[n], cacheBG[n])

Basalt/libraries/basaltMon.lua

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
-- Right now this doesn't support scroll(n)
2+
-- Because this lbirary is mainly made for basalt - it doesn't need scroll support, maybe i will add it in the future
3+
4+
local tHex = {
5+
[colors.white] = "0",
6+
[colors.orange] = "1",
7+
[colors.magenta] = "2",
8+
[colors.lightBlue] = "3",
9+
[colors.yellow] = "4",
10+
[colors.lime] = "5",
11+
[colors.pink] = "6",
12+
[colors.gray] = "7",
13+
[colors.lightGray] = "8",
14+
[colors.cyan] = "9",
15+
[colors.purple] = "a",
16+
[colors.blue] = "b",
17+
[colors.brown] = "c",
18+
[colors.green] = "d",
19+
[colors.red] = "e",
20+
[colors.black] = "f",
21+
}
22+
23+
local type,len,rep,sub = type,string.len,string.rep,string.sub
24+
25+
26+
return function (monitorNames)
27+
local monitors = {}
28+
for k,v in pairs(monitorNames)do
29+
monitors[k] = {}
30+
for a,b in pairs(v)do
31+
local mon = peripheral.wrap(b)
32+
if(mon==nil)then
33+
error("Unable to find monitor "..b)
34+
end
35+
monitors[k][a] = mon
36+
monitors[k][a].name = b
37+
end
38+
end
39+
40+
41+
local x,y,monX,monY,monW,monH,w,h = 1,1,1,1,0,0,0,0
42+
local blink,scale = false,1
43+
local fg,bg = colors.white,colors.black
44+
45+
46+
local function calcSize()
47+
local maxW,maxH = 0,0
48+
for k,v in pairs(monitors)do
49+
local _maxW,_maxH = 0,0
50+
for a,b in pairs(v)do
51+
local nw,nh = b.getSize()
52+
_maxW = _maxW + nw
53+
_maxH = nh > _maxH and nh or _maxH
54+
end
55+
maxW = maxW > _maxW and maxW or _maxW
56+
maxH = maxH + _maxH
57+
end
58+
w,h = maxW,maxH
59+
end
60+
calcSize()
61+
62+
local function calcPosition()
63+
local relY = 0
64+
local mX,mY = 0,0
65+
for k,v in pairs(monitors)do
66+
local relX = 0
67+
local _mh = 0
68+
for a,b in pairs(v)do
69+
local mw,mh = b.getSize()
70+
if(x-relX>=1)and(x-relX<=mw)then
71+
mX = a
72+
end
73+
b.setCursorPos(x-relX, y-relY)
74+
relX = relX + mw
75+
if(_mh<mh)then _mh = mh end
76+
end
77+
if(y-relY>=1)and(y-relY<=_mh)then
78+
mY = k
79+
end
80+
relY = relY + _mh
81+
end
82+
monX,monY = mX,mY
83+
end
84+
calcPosition()
85+
86+
local function call(f, ...)
87+
local t = {...}
88+
return function()
89+
for k,v in pairs(monitors)do
90+
for a,b in pairs(v)do
91+
b[f](table.unpack(t))
92+
end
93+
end
94+
end
95+
end
96+
97+
local function cursorBlink()
98+
call("setCursorBlink", false)()
99+
if not(blink)then return end
100+
if(monitors[monY]==nil)then return end
101+
local mon = monitors[monY][monX]
102+
if(mon==nil)then return end
103+
mon.setCursorBlink(blink)
104+
end
105+
106+
local function blit(text, tCol, bCol)
107+
if(monitors[monY]==nil)then return end
108+
local mon = monitors[monY][monX]
109+
if(mon==nil)then return end
110+
mon.blit(text, tCol, bCol)
111+
local mW, mH = mon.getSize()
112+
if(len(text)+x>mW)then
113+
local monRight = monitors[monY][monX+1]
114+
if(monRight~=nil)then
115+
monRight.blit(text, tCol, bCol)
116+
monX = monX + 1
117+
x = x + len(text)
118+
end
119+
end
120+
calcPosition()
121+
end
122+
123+
return {
124+
clear = call("clear"),
125+
126+
setCursorBlink = function(_blink)
127+
blink = _blink
128+
cursorBlink()
129+
end,
130+
131+
getCursorBlink = function()
132+
return blink
133+
end,
134+
135+
getCursorPos = function()
136+
return x, y
137+
end,
138+
139+
setCursorPos = function(newX,newY)
140+
x, y = newX, newY
141+
calcPosition()
142+
cursorBlink()
143+
end,
144+
145+
setTextScale = function(_scale)
146+
call("setTextScale", _scale)()
147+
calcSize()
148+
calcPosition()
149+
scale = _scale
150+
end,
151+
152+
getTextScale = function()
153+
return scale
154+
end,
155+
156+
blit = function(text,fgCol,bgCol)
157+
blit(text,fgCol,bgCol)
158+
end,
159+
160+
write = function(text)
161+
text = tostring(text)
162+
local l = len(text)
163+
blit(text, rep(tHex[fg], l), rep(tHex[bg], l))
164+
end,
165+
166+
getSize = function()
167+
return w,h
168+
end,
169+
170+
setBackgroundColor = function(col)
171+
call("setBackgroundColor", col)()
172+
bg = col
173+
end,
174+
175+
setTextColor = function(col)
176+
call("setTextColor", col)()
177+
fg = col
178+
end,
179+
180+
calculateClick = function(name, xClick, yClick)
181+
local relY = 0
182+
for k,v in pairs(monitors)do
183+
local relX = 0
184+
local maxY = 0
185+
for a,b in pairs(v)do
186+
local wM,hM = b.getSize()
187+
if(b.name==name)then
188+
return xClick + relX, yClick + relY
189+
end
190+
relX = relX + wM
191+
if(hM > maxY)then maxY = hM end
192+
end
193+
relY = relY + maxY
194+
end
195+
return xClick, yClick
196+
end,
197+
198+
}
199+
end

0 commit comments

Comments
 (0)