cc/test.lua

143 lines
3.8 KiB
Lua

local config = require "config"
local tabview = require "tabview"
local winhlp = require "winhlp"
local header_lines = 2
-- display working state
local currently_working = true
-- schedule immediate reload
local immediate_reload = false
-- window for tabview
local tabview_window = nil
local function display()
local tw,th = term.getSize()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.yellow)
term.setTextColor(colors.black)
term.clearLine()
term.write(" " .. string.upper(tabview.currentTab().name) .. " ")
term.setCursorPos(tw-8,1)
if currently_working then
term.write(" w ")
else
term.write(" R ")
end
term.write(" ^ v ")
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.setCursorPos(1,2)
term.clearLine()
for name, tab in pairs(tabview.allTabs()) do
tab.displayTab(tab)
end
end
local function on_touch(touch_x, touch_y)
local tw,th = term.getSize()
if touch_y == 1 then
if touch_x > tw-3 and touch_x <= tw then
tabview.currentTab().pageDown()
return true
elseif touch_x > tw-6 and touch_x < tw-2 then
tabview.currentTab().pageUp()
return true
elseif currently_working == false and touch_x > tw-9 and touch_x < tw-5 then
-- reload button
immediate_reload = true
return true
elseif touch_x < (#tabview.currentTab().name + 2) then
tabview.switchTab()
return true
end
elseif tabview_window ~= nil and
winhlp.contains(tabview_window, touch_x, touch_y) and
tabview.onTouch(winhlp.translate(tabview_window, touch_x, touch_y)) then
return true
end
return false
end
local function update_windows()
if tabview_window then
local tw, th = term.getSize()
tabview_window.reposition(1, 1+header_lines, tw, th-header_lines)
tabview.updateSize()
end
end
local function wait_for(time_secs)
display()
local timer_id = os.startTimer(time_secs)
while true do
local event_data = {os.pullEvent()}
if event_data[1] == "monitor_touch" or (event_data[1] == "mouse_click" and event_data[2] == 1) then
if on_touch(event_data[3], event_data[4]) then
display()
end
if immediate_reload == true then
os.cancelTimer(timer_id)
immediate_reload = false
break
end
elseif event_data[1] == "timer" then
if event_data[2] == timer_id then
break
end
elseif event_data[1] == "term_resize" then
update_windows()
display()
end
end
end
local function init()
if peripheral.isPresent(config.monitor_side) then
local monitor = peripheral.wrap(config.monitor_side)
monitor.setTextScale(config.monitor_scale)
term.redirect(monitor)
end
term.setCursorBlink(false)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(1,2)
term.write("Booting test.lua")
local tw, th = term.getSize()
tabview_window = window.create(term.current(), 1, 1+header_lines, tw, th-header_lines)
tabview.setTerm(tabview_window)
-- to display requests and answer with an ME interface
tabview.addTab(require "requests", 1)
-- to display colony work orders
tabview.addTab(require "orders", 2)
tabview.showTab(config.initial_tab)
end
local function main()
init()
while true do
currently_working = true
for name, tab in pairs(tabview.allTabs()) do
tab.fetch(wait_for)
end
currently_working = false
wait_for(config.round_sleep_time)
end
end
main()