Advertisement
Omsigames

init.lua

Dec 25th, 2024 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.49 KB | None | 0 0
  1. local GUI = require("grapes.GUI")
  2. local term = require("term")
  3. local config = require("config")
  4. local utils = require("utils")
  5. local text = require("text")
  6. local controllers = require("controllers")
  7. local thread = require("thread")
  8. local screen = require("grapes.Screen")
  9.  
  10. local SwitchTexts = {}
  11. local SignalTexts = {}
  12. local settingsWindow = nil
  13.  
  14. local workspace = GUI.workspace()
  15.  
  16. workspace:addChild(GUI.panel(1, 1, workspace.width, workspace.height, 0x000000))
  17.  
  18. workspace:addChild(GUI.label(1, 1, workspace.width, workspace.height, 0xFFFFFF, "Open Rail Management System"):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_TOP))
  19. workspace:addChild(GUI.label(1, 1, workspace.width, workspace.height, 0xFFFFFF, "By Petsox and tpeterka1"):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_BOTTOM))
  20.  
  21. -- Draw exit button
  22. local exitBtn = workspace:addChild(GUI.label(155, 50, 6, 1, 0xFFFFFF, "[Exit]"))
  23. exitBtn.eventHandler = function(workspace, object, event)
  24.     if event == "touch" then
  25.         utils.resetLayout()
  26.         screen.flush()
  27.         term.clear()
  28.         os.exit()
  29.     end
  30. end
  31.  
  32. -- Draw settings
  33. local settBtn = workspace:addChild(GUI.label(1, 50, 10, 1, 0xFFFFFF, "[Settings]"))
  34. settBtn.eventHandler = function(workspace, object, event)
  35.     if event == "touch" then
  36.         if not settingsWindow then
  37.             settingsWindow = workspace:addChild(GUI.titledWindow(workspace.width/2, workspace.height/2, 21, 20, "Settings", true))
  38.  
  39.             -- Switch number toggle
  40.             local switchNumberStg = settingsWindow:addChild(GUI.button(3, 3, 16, 3, 0x19ED15, 0x000000, 0xED1515, 0x000000, "Switch Numbers"))
  41.             switchNumberStg.switchMode = true
  42.             switchNumberStg.animated = false
  43.             if workspace.children[SwitchTexts[1]].hidden then switchNumberStg.pressed = true end
  44.             switchNumberStg.onTouch = function()
  45.                 for _, switchText in pairs(SwitchTexts) do
  46.                     workspace.children[switchText].hidden = not workspace.children[switchText].hidden
  47.                 end
  48.                 workspace:draw()
  49.             end
  50.  
  51.             -- Signal description toggle
  52.             local sigDescSetting = settingsWindow:addChild(GUI.button(3, 7, 16, 3, 0x19ED15, 0x000000, 0xED1515, 0x000000, "Signal Names"))
  53.             sigDescSetting.switchMode = true
  54.             sigDescSetting.animated = false
  55.             if workspace.children[SignalTexts[1]].hidden then sigDescSetting.pressed = true end
  56.             sigDescSetting.onTouch = function()
  57.                 for _, sigDesc in pairs(SignalTexts) do
  58.                     workspace.children[sigDesc].hidden = not workspace.children[sigDesc].hidden
  59.                 end
  60.                 workspace:draw()
  61.             end
  62.  
  63.  
  64.             workspace:draw()
  65.             settingsWindow.actionButtons.close.onTouch = function()
  66.                 settingsWindow:remove()
  67.                 settingsWindow = nil
  68.             end
  69.         end
  70.     end
  71. end
  72.  
  73. -- Import tracks
  74. for _, track in pairs(config.Tracks) do
  75.     local newTrack = workspace:addChild(GUI.text(track[1], track[2], 0xB2B2B2, text.trim(track[3]) or ""))
  76.     local text = newTrack.text
  77.     if text == "⦗" or text == "⦘" or text == "︹" or text == "︺" then
  78.         newTrack.color = 0x0000FF
  79.     end
  80. end
  81.  
  82. -- Import switches
  83. for _, switch in pairs(config.Switches) do
  84.     -- Create switch button in layout
  85.     local newSwitch = workspace:addChild(GUI.text(switch[1], switch[2], 0xB2B2B2, text.trim(switch[3]) or ""))
  86.     newSwitch.state = false
  87.     newSwitch.eventHandler = function(workspace, object, event)
  88.         if event == "touch" then
  89.             -- When switch is clicked, we toggle the switch in the GUI and send the state to the controller
  90.             object.state = not object.state
  91.             object.text = object.state and switch[4] or switch[3]
  92.             utils.toggleSwitch(switch[5])
  93.             workspace:draw()
  94.         end
  95.     end
  96.  
  97.     -- Create switch description
  98.     local newSwitchTbl = table.clone(switch)
  99.     newSwitchTbl[5] = (string.lower(string.sub(switch[5], 1, 2)) == "vy" and string.sub(switch[5], 3, -3)) or text.trim(switch[5])
  100.     local calculatedTextPos = utils.calcSwitchTextPos(newSwitchTbl)
  101.     local switchName = newSwitchTbl[5]
  102.     table.insert(SwitchTexts, workspace:addChild(GUI.text(calculatedTextPos.x, calculatedTextPos.y, 0xFFFFFF, switchName)):indexOf())
  103. end
  104.  
  105. -- Import crossings
  106. for _, crossing in pairs(config.Crossings) do
  107.     -- Create crossing button in layout
  108.     local newcrossing = workspace:addChild(GUI.text(crossing[1], crossing[2], 0xB2B2B2, text.trim(crossing[3]) or ""))
  109.     newcrossing.state = false
  110.     newcrossing.eventHandler = function(workspace, object, event)
  111.         if event == "touch" then
  112.             -- When crossing is clicked, we toggle the crossing in the GUI and send the state to the controller
  113.             object.state = not object.state
  114.             if object.state then
  115.                 object.color = 0xFF0000
  116.             else
  117.                 object.color = 0xB2B2B2
  118.             end
  119.             object.text = object.state and crossing[4] or crossing[3]
  120.             utils.toggleCrossing(crossing[5])
  121.             workspace:draw()
  122.         end
  123.     end
  124. end
  125.  
  126. -- Import signals
  127. local signalMenus = {}
  128.  
  129. local function startPN(signal, signalTbl)
  130.     local t
  131.     t = thread.create(function()
  132.         while true do
  133.             if not (controllers.Signals.getState(signalTbl[3]) == "PN") then t:kill() end
  134.             signal.colors.default.text = 0xFFFFFF
  135.             signal.colors.pressed.text = 0xFFFFFF
  136.             workspace:draw()
  137.             if not (controllers.Signals.getState(signalTbl[3]) == "PN") then t:kill() end
  138.             os.sleep(0.5)
  139.             signal.colors.default.text = 0xB2B2B2
  140.             signal.colors.pressed.text = 0xB2B2B2
  141.             workspace:draw()
  142.             if not (controllers.Signals.getState(signalTbl[3]) == "PN") then t:kill() end
  143.             os.sleep(0.5)
  144.         end
  145.     end):resume()
  146. end
  147.  
  148. local function setSignalStateGUI(signal, state, signalTbl)
  149.     signal.colors.default.text = 0xB2B2B2
  150.     signal.colors.pressed.text = 0xB2B2B2
  151.     ::signal::
  152.     if state == nil then return end
  153.     if state == "Stuj" then
  154.         signal.colors.default.text = 0xB2B2B2
  155.         signal.colors.pressed.text = 0xB2B2B2
  156.     elseif state == "Vystraha" then
  157.         signal.colors.default.text = 0x00FF00
  158.         signal.colors.pressed.text = 0x00FF00
  159.     elseif state == "Volno" then
  160.         signal.colors.default.text = 0x00FF00
  161.         signal.colors.pressed.text = 0x00FF00
  162.     elseif state == "PosunDov" then
  163.         signal.colors.default.text = 0xFFFFFF
  164.         signal.colors.pressed.text = 0xFFFFFF
  165.     elseif state == "PosunZak" then
  166.         signal.colors.default.text = 0xB2B2B2
  167.         signal.colors.pressed.text = 0xB2B2B2
  168.     elseif state == "PN" then
  169.         startPN(signal, signalTbl)
  170.     elseif string.sub(state, 1, 3) == "R40" or string.sub(state, 1, 3) == "R60" or string.sub(state, 1, 3) == "R80" then
  171.         signal.colors.default.text = 0xFFFF00
  172.         signal.colors.pressed.text = 0xFFFF00
  173.     elseif string.sub(state, 1, 4) == "Opak" then
  174.         state = string.sub(state, 5)
  175.         goto signal
  176.     end
  177. end
  178.  
  179. for _, signal in pairs(config.Signals) do
  180.     -- Create signal button in layout
  181.     local newSignal = workspace:addChild(GUI.button(signal[1], signal[2], 1, 1, 0x000000, 0xB2B2B2, 0x000000, 0xB2B2B2, signal[4]))
  182.     signalMenus[signal[3]] = false
  183.     newSignal.onTouch = function()
  184.         -- When signal is clicked, we first check if the menu is already open
  185.         if signalMenus[signal[3]] == false then
  186.             -- If not, we create the menu
  187.             signalMenus[signal[3]] = true
  188.             local signalMenu = workspace:addChild(GUI.titledWindow(workspace.width - 30, workspace.height - 25, 30, 25, signal[3], true))
  189.             signalMenu.actionButtons.close.onTouch = function()
  190.                 -- When the close button is clicked, we remove the menu and set it's existence to false
  191.                 signalMenu:remove()
  192.                 signalMenus[signal[3]] = false
  193.                 setSignalStateGUI(newSignal, controllers.Signals.getState(signal[3]), signal)
  194.             end
  195.  
  196.             -- We then add all the possible states to the menu
  197.             for i, state in pairs(controllers.Signals.getValidStatesForSignal(signal[3])) do
  198.                 if state == "Stuj" and string.sub(signal[3], 1, 2) == "Se" then goto continue end
  199.                 local signalMenuState = signalMenu:addChild(GUI.button(5, i+1, 20, 1, 0x555555, 0x000000, 0x19ED15, 0x000000, state))
  200.                 signalMenuState.switchMode = true
  201.                 signalMenuState.animated = false
  202.                 -- PN state is highlited red (for safety reasons)
  203.                 if state == "PN" then signalMenuState.colors.default.text = 0xFC0303 end
  204.                 -- We highlight the current state that the signal is in
  205.                 local currentState = controllers.Signals.getState(signal[3])
  206.                 if currentState == state then
  207.                     signalMenuState.pressed = true
  208.                 end
  209.                 if currentState == "Stuj" and state == "PosunZak" then signalMenuState.pressed = true end
  210.                 signalMenuState.onTouch = function()
  211.                     -- When a state is clicked, we check if the signal is a normal signal or an expect signal
  212.                     if not (string.sub(signal[3], 1, 2) == "Pr") then
  213.                         -- If it's a normal signal, we set the state of the signal to the state that was clicked and we send the state to the expect signal
  214.                         for _, signalState in pairs(signalMenu.children) do
  215.                             signalState.pressed = false
  216.                         end
  217.                         signalMenuState.pressed = true
  218.                         controllers.Signals.setState(signal[3], state)
  219.                         utils.sendStateToExpectSig(signal[3], state)
  220.                         setSignalStateGUI(newSignal, state, signal)
  221.                         workspace:draw()
  222.                     else
  223.                         -- If it's an expect signal, we alert the user that the expect signal is controlled automatically
  224.                         signalMenuState.pressed = false
  225.                         if controllers.Signals.getState(signal[3]) == state then
  226.                             signalMenuState.pressed = true
  227.                         end
  228.                         workspace:draw()
  229.                         GUI.alert("Předvěsti jsou ovládány automaticky / Expect signals are controlled automatically")
  230.                     end
  231.                 end
  232.                 ::continue::
  233.             end
  234.             workspace:draw()
  235.         end
  236.     end
  237.     setSignalStateGUI(newSignal, controllers.Signals.getState(signal[3]), signal)
  238.  
  239.     -- Create signal description
  240.     local newSigTbl = table.clone(signal)
  241.     newSigTbl[3] = string.sub(signal[3], 1, -3)
  242.     local calculatedTextPos = utils.calcSignalTextPos(newSigTbl)
  243.     local sigName = newSigTbl[3]
  244.     table.insert(SignalTexts, workspace:addChild(GUI.text(calculatedTextPos.x, calculatedTextPos.y, 0xFFFFFF, sigName)):indexOf())
  245. end
  246.  
  247. -- Import labels
  248. for _, label in pairs(config.Labels) do
  249.     workspace:addChild(GUI.button(math.ceil(label[1] - (string.len(label[3]) / 2)), label[2], string.len(label[3]) + 2, 1, 0x0000FF, 0xFFFFFF, 0x0000FF, 0xFFFFFF, label[3]))
  250. end
  251.  
  252. -- Reset layout
  253. utils.resetLayout()
  254.  
  255. screen.flush()
  256. workspace:draw()
  257. workspace:start()
  258.  
Tags: ORMS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement