Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Horizontal Mining Offset UI with Toggle & Status Monitoring Only
- -- 1) Screen wrappers (computer + monitors)
- local screens = {}
- -- wrap the terminal itself
- table.insert(screens, {
- clear = function() term.clear(); term.setCursorPos(1,1) end,
- getSize = term.getSize,
- setTextColor = term.setTextColor,
- setBackgroundColor = term.setBackgroundColor,
- setCursorPos = term.setCursorPos,
- write = term.write,
- })
- -- wrap any attached monitors
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "monitor" then
- local m = peripheral.wrap(side)
- table.insert(screens, {
- clear = function() m.clear(); m.setCursorPos(1,1) end,
- getSize = m.getSize,
- setTextColor = m.setTextColor,
- setBackgroundColor = m.setBackgroundColor,
- setCursorPos = m.setCursorPos,
- write = m.write,
- monitor = m,
- })
- end
- end
- -- 2) Auto-scale monitors so at least 7 lines fit
- for _, s in ipairs(screens) do
- if s.monitor then
- local best = 0.5
- for _, scale in ipairs({1, 0.5}) do
- s.monitor.setTextScale(scale)
- local w,h = s.getSize()
- if w >= 40 and h >= 7 then
- best = scale
- break
- end
- end
- s.monitor.setTextScale(best)
- end
- end
- -- 3) Drawing helpers
- local function forAll(fn) for _,s in ipairs(screens) do fn(s) end end
- local function clearAll()
- forAll(function(s)
- s.setTextColor(colors.black)
- s.setBackgroundColor(colors.lightGray)
- s.clear()
- end)
- end
- -- 4) Static UI texts
- local titleText = "Horizontal Mining System"
- local subtitleText = "Guided Mining Offset Only"
- local offsetText = "Offset Mode (T): "
- local promptH = "Press H for Main Menu"
- local promptT = "Press T to Toggle Offset"
- -- offset state
- local isOffsetOn = false
- -- 5) Draw header + offset-state line
- local function drawStatic()
- forAll(function(s)
- local w,_ = s.getSize()
- -- Title (line 1)
- s.setTextColor(colors.white); s.setBackgroundColor(colors.black)
- s.setCursorPos(math.floor((w-#titleText)/2)+1,1); s.write(titleText)
- -- Subtitle (line 3)
- s.setTextColor(colors.black); s.setBackgroundColor(colors.lightGray)
- s.setCursorPos(math.floor((w-#subtitleText)/2)+1,3); s.write(subtitleText)
- -- Blank line 4
- -- Offset-state (line 5)
- local status = isOffsetOn and "ON" or "OFF"
- local full = offsetText..status
- s.setTextColor(colors.black); s.setBackgroundColor(colors.lightGray)
- s.setCursorPos(1,5); s.write(string.rep(" ", w))
- s.setCursorPos(math.floor((w-#full)/2)+1,5); s.write(offsetText)
- s.setTextColor(colors.white)
- s.setBackgroundColor(isOffsetOn and colors.lime or colors.red)
- s.write(status)
- end)
- end
- -- 6) Draw bottom prompt lines (lines h-1 and h)
- local function drawPrompt()
- forAll(function(s)
- local w,h = s.getSize()
- -- line h-1: Toggle (T)
- s.setTextColor(colors.white); s.setBackgroundColor(colors.blue)
- s.setCursorPos(1,h-1); s.write(string.rep(" ",w))
- s.setCursorPos(math.floor((w-#promptT)/2)+1,h-1); s.write(promptT)
- -- line h: Main menu (H)
- s.setTextColor(colors.white); s.setBackgroundColor(colors.blue)
- s.setCursorPos(1,h); s.write(string.rep(" ",w))
- s.setCursorPos(math.floor((w-#promptH)/2)+1,h); s.write(promptH)
- end)
- end
- -- 7) Open all modems for Rednet and send initial check
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- end
- end
- rednet.broadcast("check", "SMiner")
- -- 8) Initial draw
- clearAll()
- drawStatic()
- drawPrompt()
- -- 9) Listen for SMiner status updates so we can refresh UI if another controller toggles
- local function statusListener()
- while true do
- local _, msg = rednet.receive("SMiner")
- if msg == "StatON" then
- isOffsetOn = true
- clearAll(); drawStatic(); drawPrompt()
- elseif msg == "StatOFF" then
- isOffsetOn = false
- clearAll(); drawStatic(); drawPrompt()
- end
- end
- end
- -- 10) Main event loop: handle key presses to toggle or reboot
- local function mainLoop()
- while true do
- local ev, key = os.pullEvent("key")
- if key == keys.t then
- -- flip local state, broadcast to others, and redraw
- isOffsetOn = not isOffsetOn
- rednet.broadcast(isOffsetOn and "StatON" or "StatOFF", "SMiner")
- clearAll(); drawStatic(); drawPrompt()
- elseif key == keys.h then
- os.reboot()
- end
- end
- end
- -- 11) Run both loops in parallel
- parallel.waitForAny(statusListener, mainLoop)
Advertisement
Add Comment
Please, Sign In to add comment