SHOW:
|
|
- or go back to the newest paste.
1 | local playerEvents = { | |
2 | "key", "char", "mouse_click", "mouse_drag", "mouse_up" | |
3 | } | |
4 | local running = true | |
5 | local program = "rom/programs/shell.lua" | |
6 | local split = "vertical" | |
7 | local args = {...} | |
8 | if args[1] then | |
9 | if args[1] == "1" then args[1] = "horizontal" end | |
10 | if args[1] == "2" then args[1] = "vertical" end | |
11 | split = args[1] | |
12 | if args[2] then | |
13 | program = args[2] | |
14 | if args[3] then | |
15 | for i = 3, #args do | |
16 | program = program.." "..args[i] | |
17 | end | |
18 | end | |
19 | end | |
20 | end | |
21 | ||
22 | local width, height = term.getSize() | |
23 | local halfwidth = math.floor(width/2) | |
24 | local halfheight = math.floor(height/2) | |
25 | local splits = { | |
26 | vertical = { | |
27 | window1 = window.create(term.current(), 1, 1, halfwidth-1, height), | |
28 | window2 = window.create(term.current(), halfwidth+1, 1, halfwidth+1, height) | |
29 | }, | |
30 | horizontal = { | |
31 | window1 = window.create(term.current(), 1, 1, width, halfheight), | |
32 | window2 = window.create(term.current(), 1, halfheight+1, width, halfheight+1) | |
33 | } | |
34 | } | |
35 | local cx, cy | |
36 | local selected = 1 | |
37 | ||
38 | local co1 = coroutine.create(function() | |
39 | shell.run("rom/programs/shell.lua") | |
40 | end) | |
41 | ||
42 | local co2 = coroutine.create(function() | |
43 | shell.run(program) | |
44 | end) | |
45 | ||
46 | local resumeProgram = function(co, window, eventData, num) | |
47 | if coroutine.status(co) == "dead" then | |
48 | running = false | |
49 | end | |
50 | local window = splits[split][window] | |
51 | local oldTerm = term.redirect(window) | |
52 | local x, y = window.getPosition() | |
53 | if string.sub(eventData[1], 1, #"mouse") == "mouse" then | |
54 | eventData[3] = eventData[3]-x+1 | |
55 | eventData[4] = eventData[4]-y+1 | |
56 | end | |
57 | local isPlayerEvent = false | |
58 | for i = 1, #playerEvents do | |
59 | if playerEvents[i] == eventData[1] then isPlayerEvent = true end | |
60 | end | |
61 | if selected == num then | |
62 | coroutine.resume(co, table.unpack(eventData)) | |
63 | local wcx, wcy = term.getCursorPos() | |
64 | cx, cy = wcx+x-1, wcy+y-1 | |
65 | elseif not isPlayerEvent then | |
66 | coroutine.resume(co, table.unpack(eventData)) | |
67 | else | |
68 | coroutine.resume(co, "") | |
69 | end | |
70 | term.redirect(oldTerm) | |
71 | end | |
72 | ||
73 | local isTermClicked = function(mx, my, te) | |
74 | local x, y = te.getPosition() | |
75 | local w, h = te.getSize() | |
76 | if mx>=x and my>y then | |
77 | if mx<x+w and my<y+h then | |
78 | return true | |
79 | end | |
80 | end | |
81 | end | |
82 | ||
83 | while running do | |
84 | local timer = os.startTimer(0.1) | |
85 | local eventData = {os.pullEventRaw()} | |
86 | os.cancelTimer(timer) | |
87 | ||
88 | if eventData[1] == "mouse_click" then | |
89 | local mx, my = eventData[3], eventData[4] | |
90 | if isTermClicked(mx, my, splits[split]["window1"]) then | |
91 | selected = 1 | |
92 | elseif isTermClicked(mx, my, splits[split]["window2"]) then | |
93 | selected = 2 | |
94 | end | |
95 | end | |
96 | ||
97 | resumeProgram(co1, "window1", eventData, 1) | |
98 | resumeProgram(co2, "window2", eventData, 2) | |
99 | ||
100 | if split == "vertical" then | |
101 | paintutils.drawLine(halfwidth, 1, halfwidth, height, colors.lightGray) | |
102 | else | |
103 | paintutils.drawLine(1, halfheight, width, halfheight, colors.lightGray) | |
104 | end | |
105 | if cx and cy then | |
106 | term.setCursorPos(cx, cy) | |
107 | end | |
108 | end | |
109 | ||
110 | term.setBackgroundColour(colors.black) | |
111 | term.clear() | |
112 | term.setCursorPos(1, 1) |