Advertisement
Downfire

elevatorHost

Jun 17th, 2024 (edited)
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 31.96 KB | None | 0 0
  1. local colorList = {
  2.     [0] = "white",
  3.     [1] = "orange",
  4.     [2] = "magenta",
  5.     [3] = "lightBlue",
  6.     [4] = "yellow",
  7.     [5] = "lime",
  8.     [6] = "pink",
  9.     [7] = "gray",
  10.     [8] = "lightGray",
  11.     [9] = "cyan",
  12.     [10] = "purple",
  13.     [11] = "blue",
  14.     [12] = "brown",
  15.     [13] = "green",
  16.     [14] = "red",
  17.     [15] = "black"
  18. }
  19.  
  20. local colorIndexTable = {
  21.     [0] = colors.white,
  22.     [1] = colors.orange,
  23.     [2] = colors.magenta,
  24.     [3] = colors.lightBlue,
  25.     [4] = colors.yellow,
  26.     [5] = colors.lime,
  27.     [6] = colors.pink,
  28.     [7] = colors.gray,
  29.     [8] = colors.lightGray,
  30.     [9] = colors.cyan,
  31.     [10] = colors.purple,
  32.     [11] = colors.blue,
  33.     [12] = colors.brown,
  34.     [13] = colors.green,
  35.     [14] = colors.red,
  36.     [15] = colors.black
  37. }
  38.  
  39. local indexColorTable = {
  40.     [colors.white] = 0,
  41.     [colors.orange] = 1,
  42.     [colors.magenta] = 2,
  43.     [colors.lightBlue] = 3,
  44.     [colors.yellow] = 4,
  45.     [colors.lime] = 5,
  46.     [colors.pink] = 6,
  47.     [colors.gray] = 7,
  48.     [colors.lightGray] = 8,
  49.     [colors.cyan] = 9,
  50.     [colors.purple] = 10,
  51.     [colors.blue] = 11,
  52.     [colors.brown] = 12,
  53.     [colors.green] = 13,
  54.     [colors.red] = 14,
  55.     [colors.black] = 15
  56. }
  57.  
  58. local colorToNameTable = {
  59.     [colors.white] = "white",
  60.     [colors.orange] = "orange",
  61.     [colors.magenta] = "magenta",
  62.     [colors.lightBlue] = "lightBlue",
  63.     [colors.yellow] = "yellow",
  64.     [colors.lime] = "lime",
  65.     [colors.pink] = "pink",
  66.     [colors.gray] = "gray",
  67.     [colors.lightGray] = "lightGray",
  68.     [colors.cyan] = "cyan",
  69.     [colors.purple] = "purple",
  70.     [colors.blue] = "blue",
  71.     [colors.brown] = "brown",
  72.     [colors.green] = "green",
  73.     [colors.red] = "red",
  74.     [colors.black] = "black"
  75. }
  76.  
  77. local nameToColorTable = {
  78.     ["white"] = colors.white,
  79.     ["orange"] = colors.orange,
  80.     ["magenta"] = colors.magenta,
  81.     ["lightBlue"] = colors.lightBlue,
  82.     ["yellow"] = colors.yellow,
  83.     ["lime"] = colors.lime,
  84.     ["pink"] = colors.pink,
  85.     ["gray"] = colors.gray,
  86.     ["lightGray"] = colors.lightGray,
  87.     ["cyan"] = colors.cyan,
  88.     ["purple"] = colors.purple,
  89.     ["blue"] = colors.blue,
  90.     ["brown"] = colors.brown,
  91.     ["green"] = colors.green,
  92.     ["red"] = colors.red,
  93.     ["black"] = colors.black
  94. }
  95.  
  96. local hostConfig = {
  97.     ["id"] = nil,
  98.     ["elevatorID"] = nil,
  99.     ["elevatorName"] = "",
  100.     ["rsBcInputSide"] = nil,
  101.     ["rsBcOutputSide"] = nil,
  102.     ["theme"] = {
  103.         ["displayBackground"] = colors.black,
  104.         ["displayForeground"] = colors.white,
  105.         ["frameBackground"] = colors.lightGray,
  106.         ["frameForeground"] = colors.black,
  107.         ["frameBorder"] = colors.gray,
  108.         ["buttonBackground"] = colors.gray,
  109.         ["buttonForeground"] = colors.white,
  110.         ["buttonBorder"] = colors.lightGray
  111.     },
  112. }
  113.  
  114. local floors = {
  115.     [0] = {},
  116.     [1] = {},
  117.     [2] = {},
  118.     [3] = {},
  119.     [4] = {},
  120.     [5] = {},
  121.     [6] = {},
  122.     [7] = {},
  123.     [8] = {},
  124.     [9] = {},
  125.     [10] = {},
  126.     [11] = {},
  127.     [12] = {},
  128.     [13] = {},
  129.     [14] = {},
  130.     [15] = {}
  131. }
  132.  
  133. local basalt = require("basalt")
  134. rednet.open("top")
  135. local protocol = ""
  136.  
  137. local main = basalt.createFrame()
  138.  
  139. local mainFrame = main:addFrame()
  140. mainFrame:setSize("parent.w","parent.h")
  141. mainFrame:setBackground(hostConfig.theme.displayBackground)
  142.  
  143. local mainThread = main:addThread()
  144. local secondThread = main:addThread()
  145. local thirdThread = main:addThread()
  146.  
  147. local scrollAbleFrame = nil
  148. local settingsFrame = nil
  149. local sidebar = nil
  150.  
  151. local function getNewFloorTemplate()
  152.     local newFloor = {
  153.         ["name"] = nil,
  154.         ["id"] = nil,
  155.         ["elevatorID"] = nil,
  156.         ["elevatorName"] = "",
  157.         ["isElevatorOnFloor"] = false,
  158.         ["isFloorCalled"] = false,
  159.         ["state"] = false,
  160.         ["theme"] = {
  161.             ["displayBackground"] = colors.lightGray,
  162.             ["displayForeground"] = colors.black,
  163.             ["frameBackground"] = colors.gray,
  164.             ["frameForeground"] = colors.black,
  165.             ["buttonBackground"] = colors.lightGray,
  166.             ["buttonForeground"] = colors.black,
  167.             ["buttonBorder"] = colors.black
  168.         },
  169.         ["basaltConfigFrame"] = nil,
  170.         ["basaltConfigSideBarButton"] = nil
  171.     }
  172.     return newFloor
  173. end
  174.  
  175. -- Load config if present
  176.  
  177.  
  178. local function SaveConfig()
  179.     local hConfig = {
  180.         ["id"] = hostConfig.id,
  181.         ["elevatorID"] = hostConfig.elevatorID,
  182.         ["elevatorName"] = hostConfig.elevatorName,
  183.         ["rsBcInputSide"] = hostConfig.rsBcInputSide,
  184.         ["rsBcOutputSide"] = hostConfig.rsBcOutputSide,
  185.         ["theme"] = {
  186.             ["displayBackground"] = hostConfig.theme.displayBackground,
  187.             ["displayForeground"] = hostConfig.theme.displayForeground,
  188.             ["frameBackground"] = hostConfig.theme.frameBackground,
  189.             ["frameForeground"] = hostConfig.theme.frameForeground,
  190.             ["frameBorder"] = hostConfig.theme.frameBorder,
  191.             ["buttonBackground"] = hostConfig.theme.buttonBackground,
  192.             ["buttonForeground"] = hostConfig.theme.buttonForeground,
  193.             ["buttonBorder"] = hostConfig.theme.buttonBorder
  194.         },
  195.     }
  196.  
  197.     local fConfigs = {
  198.         [0] = getNewFloorTemplate(),
  199.         [1] = getNewFloorTemplate(),
  200.         [2] = getNewFloorTemplate(),
  201.         [3] = getNewFloorTemplate(),
  202.         [4] = getNewFloorTemplate(),
  203.         [5] = getNewFloorTemplate(),
  204.         [6] = getNewFloorTemplate(),
  205.         [7] = getNewFloorTemplate(),
  206.         [8] = getNewFloorTemplate(),
  207.         [9] = getNewFloorTemplate(),
  208.         [10] = getNewFloorTemplate(),
  209.         [11] = getNewFloorTemplate(),
  210.         [12] = getNewFloorTemplate(),
  211.         [13] = getNewFloorTemplate(),
  212.         [14] = getNewFloorTemplate(),
  213.         [15] = getNewFloorTemplate()
  214.     }
  215.  
  216.     for index, floor in pairs(floors) do
  217.         fConfigs[index].name = floors[index].name
  218.         fConfigs[index].id = floors[index].id
  219.         fConfigs[index].elevatorID = floors[index].elevatorID
  220.         fConfigs[index].elevatorName = floors[index].elevatorName
  221.         fConfigs[index].state = floors[index].state
  222.         fConfigs[index].isElevatorOnFloor = floors[index].isElevatorOnFloor
  223.         fConfigs[index].isFloorCalled = floors[index].isFloorCalled
  224.         fConfigs[index].theme.displayBackground = floors[index].theme.displayBackground
  225.         fConfigs[index].theme.displayForeground = floors[index].theme.displayForeground
  226.         fConfigs[index].theme.frameBackground = floors[index].theme.frameBackground
  227.         fConfigs[index].theme.frameForeground = floors[index].theme.frameForeground
  228.         fConfigs[index].theme.buttonBackground = floors[index].theme.buttonBackground
  229.         fConfigs[index].theme.buttonForeground = floors[index].theme.buttonForeground
  230.         fConfigs[index].theme.buttonBorder = floors[index].theme.buttonBorder
  231.     end
  232.  
  233.     local file = fs.open("elevatorHostConfig.txt", "w")
  234.     local serConfig = textutils.serialise({ hConfig, fConfigs})
  235.     file.write(serConfig)
  236.     file.close()
  237.     hConfig = nil
  238.     fConfigs = nil
  239. end
  240.  
  241. local function LoadConfig()
  242.     local file = fs.open("elevatorHostConfig.txt", "r")
  243.     if type(file) == "table" then
  244.         local serFile = file.readAll()
  245.         hostConfig = textutils.unserialise(serFile)[1]
  246.  
  247.         floors[0] = textutils.unserialise(serFile)[2][0]
  248.         floors[1] = textutils.unserialise(serFile)[2][1]
  249.         floors[2] = textutils.unserialise(serFile)[2][2]
  250.         floors[3] = textutils.unserialise(serFile)[2][3]
  251.         floors[4] = textutils.unserialise(serFile)[2][4]
  252.         floors[5] = textutils.unserialise(serFile)[2][5]
  253.         floors[6] = textutils.unserialise(serFile)[2][6]
  254.         floors[7] = textutils.unserialise(serFile)[2][7]
  255.         floors[8] = textutils.unserialise(serFile)[2][8]
  256.         floors[9] = textutils.unserialise(serFile)[2][9]
  257.         floors[10] = textutils.unserialise(serFile)[2][10]
  258.         floors[11] = textutils.unserialise(serFile)[2][11]
  259.         floors[12] = textutils.unserialise(serFile)[2][12]
  260.         floors[13] = textutils.unserialise(serFile)[2][13]
  261.         floors[14] = textutils.unserialise(serFile)[2][14]
  262.         floors[15] = textutils.unserialise(serFile)[2][15]
  263.     end
  264.     if file ~= nil then
  265.         file.close()
  266.     end
  267. end
  268.  
  269. if fs.exists("elevatorHostConfig.txt") == true then
  270.     LoadConfig()
  271. else
  272.     for index, floor in pairs(floors) do
  273.         floors[index] = getNewFloorTemplate()
  274.         floors[index].id = index
  275.     end
  276. end
  277.  
  278.  
  279. local function CreateFloorFrameAndButton(index)
  280.     local floor = getNewFloorTemplate()
  281.     local frame = mainFrame:addFrame()
  282.     floors[index].basaltConfigFrame = frame
  283.     frame:setPosition(1,1)
  284.     frame:setBackground(hostConfig.theme.displayBackground)
  285.     frame:setSize("parent.w - 1", "parent.h - 1")
  286.     frame:hide()
  287.  
  288.     local header = frame:addLabel()
  289.     header:setPosition(2,2)
  290.     header:setSize("parent.w - 2", 5)
  291.     header:setForeground(hostConfig.theme.displayForeground)
  292.     header:setTextAlign("center")
  293.     header:setText("Floor No" .. tostring(floors[index].id))
  294.     --add button to scrollAbleFrame and add function for show and hide frame
  295.  
  296.     local leftFrame = frame:addFrame()
  297.     leftFrame:setSize("parent.w / 2", "parent.h -11")
  298.     leftFrame:setPosition(1,7)
  299.     leftFrame:setBackground(hostConfig.theme.frameBackground)
  300.     leftFrame:setBorder(hostConfig.theme.frameBorder)
  301.  
  302.     local rightFrame = frame:addFrame()
  303.     rightFrame:setSize("parent.w / 2", "parent.h -11")
  304.     rightFrame:setPosition("parent.w / 2", 7)
  305.     leftFrame:setBackground(floors[index].theme.displayBackground)
  306.     rightFrame:setBorder(hostConfig.theme.frameBorder)
  307.  
  308.     local previewFrame = rightFrame:addFrame()
  309.     previewFrame:setSize("(parent.w / 2) - 2", "parent.h")
  310.     previewFrame:setPosition("(parent.w / 2) - (self.w /2), 1")
  311.    
  312.  
  313.     local previewButton = previewFrame:addButton()
  314.     previewButton:setSize("parent.w - 2", 5)
  315.     previewButton:setPosition(2,"(parent.h /2) - (self.h /2)")
  316.     previewButton:setText(tostring(floors[index].name))
  317.     previewButton:setForeground(floors[index].theme.buttonForeground)
  318.     previewButton:setBackground(floors[index].theme.buttonBackground)
  319.     previewButton:setBorder(floors[index].theme.buttonBorder)
  320.  
  321.     local floorNameLabel = leftFrame:addLabel()
  322.     floorNameLabel:setSize("(parent.w / 2) - 2",1)
  323.     floorNameLabel:setPosition(2, 6)
  324.     floorNameLabel:setForeground(hostConfig.theme.frameForeground)
  325.     floorNameLabel:setText("Floor Name : ")
  326.  
  327.     local floorNameInput = leftFrame:addInput()
  328.     floorNameInput:setSize("(parent.w / 2) - 10",1)
  329.     floorNameInput:setPosition("(parent.w /2) + 5", 6)
  330.     floorNameInput:setForeground(hostConfig.theme.frameForeground)
  331.     floorNameInput:setBackground(colors.lightGray)
  332.     floorNameInput:setValue(tostring(floors[index].name))
  333.     floorNameInput:onChange(function(self, event, char)
  334.         previewButton:setText(tostring(floorNameInput:getValue()))
  335.     end)
  336.    
  337.    
  338.     local themeDisplayForegroundLabel = leftFrame:addLabel()
  339.     themeDisplayForegroundLabel:setSize("(parent.w / 2) - 2",1)
  340.     themeDisplayForegroundLabel:setPosition(2, 8)
  341.     themeDisplayForegroundLabel:setForeground(hostConfig.theme.frameForeground)
  342.     themeDisplayForegroundLabel:setText("Display Text Color : ")
  343.  
  344.     local themeDisplayFgDropDown = leftFrame:addDropdown()
  345.     themeDisplayFgDropDown:setPosition("(parent.w / 2) + 5",8)
  346.     themeDisplayFgDropDown:setDropdownSize(12, 5)
  347.     for i = 0, 15 do
  348.         themeDisplayFgDropDown:addItem(tostring(colorList[i]))
  349.         if floors[index].theme.displayForeground == colorIndexTable[i] then
  350.             themeDisplayFgDropDown:selectItem(i+1)
  351.             rightFrame:setForeground(floors[index].theme.displayForeground)
  352.         end
  353.     end
  354.     themeDisplayFgDropDown:onChange(function(self, event, item)
  355.         rightFrame:setForeground(nameToColorTable[item.text])
  356.         floor.theme.displayForeground = nameToColorTable[item.text]
  357.     end)
  358.  
  359.     local themeDisplayBackgroundLabel = leftFrame:addLabel()
  360.     themeDisplayBackgroundLabel:setSize("(parent.w / 2) - 2",1)
  361.     themeDisplayBackgroundLabel:setPosition(2, 10)
  362.     themeDisplayBackgroundLabel:setForeground(hostConfig.theme.frameForeground)
  363.     themeDisplayBackgroundLabel:setText("Display Background Color : ")
  364.  
  365.     local themeDisplayBgDropDown = leftFrame:addDropdown()
  366.     themeDisplayBgDropDown:setPosition("(parent.w / 2) + 5",10)
  367.     themeDisplayBgDropDown:setDropdownSize(12, 5)
  368.     for i = 0, 15 do
  369.         themeDisplayBgDropDown:addItem(tostring(colorList[i]))
  370.         if floors[index].theme.displayBackground == colorIndexTable[i] then
  371.             themeDisplayBgDropDown:selectItem(i+1)
  372.             rightFrame:setBackground(floors[index].theme.displayBackground)
  373.         end
  374.     end
  375.     themeDisplayBgDropDown:onChange(function(self, event, item)
  376.         rightFrame:setBackground(nameToColorTable[item.text])
  377.         floor.theme.displayBackground = nameToColorTable[item.text]
  378.     end)
  379.  
  380.     local themeFrameForegroundLabel = leftFrame:addLabel()
  381.     themeFrameForegroundLabel:setSize("(parent.w / 2) - 2",1)
  382.     themeFrameForegroundLabel:setPosition(2, 12)
  383.     themeFrameForegroundLabel:setForeground(hostConfig.theme.frameForeground)
  384.     themeFrameForegroundLabel:setText("Frame Text Color : ")
  385.  
  386.     local themeFrameFgDropDown = leftFrame:addDropdown()
  387.     themeFrameFgDropDown:setPosition("(parent.w / 2) + 5",12)
  388.     themeFrameFgDropDown:setDropdownSize(12, 5)
  389.     for i = 0, 15 do
  390.         themeFrameFgDropDown:addItem(tostring(colorList[i]))
  391.         if floors[index].theme.frameForeground == colorIndexTable[i] then
  392.             themeFrameFgDropDown:selectItem(i+1)
  393.             previewFrame:setForeground(floors[index].theme.frameForeground)
  394.         end
  395.     end
  396.     themeFrameFgDropDown:onChange(function(self, event, item)
  397.         previewFrame:setForeground(nameToColorTable[item.text])
  398.         floor.theme.frameForeground = nameToColorTable[item.text]
  399.     end)
  400.  
  401.     local themeFrameBackgroundLabel = leftFrame:addLabel()
  402.     themeFrameBackgroundLabel:setSize("(parent.w / 2) - 2",1)
  403.     themeFrameBackgroundLabel:setPosition(2, 14)
  404.     themeFrameBackgroundLabel:setForeground(hostConfig.theme.frameForeground)
  405.     themeFrameBackgroundLabel:setText("Frame Background Color : ")
  406.  
  407.     local themeFrameBgDropDown = leftFrame:addDropdown()
  408.     themeFrameBgDropDown:setPosition("(parent.w / 2) + 5", 14)
  409.     themeFrameBgDropDown:setDropdownSize(12, 5)
  410.     for i = 0, 15 do
  411.         themeFrameBgDropDown:addItem(tostring(colorList[i]))
  412.         if floors[index].theme.frameBackground == colorIndexTable[i] then
  413.             themeFrameBgDropDown:selectItem(i+1)
  414.             previewFrame:setBackground(floors[index].theme.frameBackground)
  415.         end
  416.     end
  417.     themeFrameBgDropDown:onChange(function(self, event, item)
  418.         previewFrame:setBackground(nameToColorTable[item.text])
  419.         floor.theme.frameBackground = nameToColorTable[item.text]
  420.     end)
  421.  
  422.     local themeButtonForegroundLabel = leftFrame:addLabel()
  423.     themeButtonForegroundLabel:setSize("(parent.w / 2) - 2",1)
  424.     themeButtonForegroundLabel:setPosition(2, 16)
  425.     themeButtonForegroundLabel:setForeground(hostConfig.theme.frameForeground)
  426.     themeButtonForegroundLabel:setText("Button Text Color : ")
  427.  
  428.     local themeButtonFgDropDown = leftFrame:addDropdown()
  429.     themeButtonFgDropDown:setPosition("(parent.w / 2) + 5",16)
  430.     themeButtonFgDropDown:setDropdownSize(12, 5)
  431.     for i = 0, 15 do
  432.         themeButtonFgDropDown:addItem(tostring(colorList[i]))
  433.         if floors[index].theme.buttonForeground == colorIndexTable[i] then
  434.             themeButtonFgDropDown:selectItem(i+1)
  435.             previewButton:setForeground(floors[index].theme.buttonForeground)
  436.         end
  437.     end
  438.     themeButtonFgDropDown:onChange(function(self, event, item)
  439.         previewButton:setForeground(nameToColorTable[item.text])
  440.         floor.theme.buttonForeground = nameToColorTable[item.text]
  441.     end)
  442.  
  443.     local themeButtonBackgroundLabel = leftFrame:addLabel()
  444.     themeButtonBackgroundLabel:setSize("(parent.w / 2) - 2",1)
  445.     themeButtonBackgroundLabel:setPosition(2, 18)
  446.     themeButtonBackgroundLabel:setForeground(hostConfig.theme.frameForeground)
  447.     themeButtonBackgroundLabel:setText("Button Background Color : ")
  448.  
  449.     local themeButtonBgDropDown = leftFrame:addDropdown()
  450.     themeButtonBgDropDown:setPosition("(parent.w / 2) + 5", 18)
  451.     themeButtonBgDropDown:setDropdownSize(12, 5)
  452.     for i = 0, 15 do
  453.         themeButtonBgDropDown:addItem(tostring(colorList[i]))
  454.         if floors[index].theme.buttonBackground == colorIndexTable[i] then
  455.             themeButtonBgDropDown:selectItem(i+1)
  456.             previewButton:setBackground(floors[index].theme.buttonBackground)
  457.         end
  458.     end
  459.     themeButtonBgDropDown:onChange(function(self, event, item)
  460.         previewButton:setBackground(nameToColorTable[item.text])
  461.         floor.theme.buttonBackground = nameToColorTable[item.text]
  462.     end)
  463.  
  464.     local themeButtonBorderLabel = leftFrame:addLabel()
  465.     themeButtonBorderLabel:setSize("(parent.w / 2) - 2",1)
  466.     themeButtonBorderLabel:setPosition(2, 20)
  467.     themeButtonBorderLabel:setForeground(hostConfig.theme.frameForeground)
  468.     themeButtonBorderLabel:setText("Button Border Color : ")
  469.  
  470.     local themeButtonBorderDropDown = leftFrame:addDropdown()
  471.     themeButtonBorderDropDown:setPosition("(parent.w / 2) + 5", 20)
  472.     themeButtonBorderDropDown:setDropdownSize(12, 5)
  473.     for i = 0, 15 do
  474.         themeButtonBorderDropDown:addItem(tostring(colorList[i]))
  475.         if floors[index].theme.buttonBorder == colorIndexTable[i] then
  476.             themeButtonBorderDropDown:selectItem(i+1)
  477.             previewButton:setBorder(floors[index].theme.buttonBorder)
  478.         end
  479.     end
  480.     themeButtonBorderDropDown:onChange(function(self, event, item)
  481.         previewButton:setBorder(nameToColorTable[item.text])
  482.         floor.theme.buttonBorder = nameToColorTable[item.text]
  483.     end)
  484.  
  485.  
  486.  
  487.  
  488.     local saveButton = frame:addButton()
  489.     saveButton:setSize(10,3)
  490.     saveButton:setPosition("parent.w - 11", "parent.h - 2")
  491.     saveButton:setText("Save")
  492.     saveButton:onClick(function(self,event,button,x,y)
  493.         if(event=="mouse_click")and(button==1)then
  494.             if floorNameInput.getValue() ~= "" then
  495.                 floors[index].name = tostring(floorNameInput.getValue())
  496.                 floors[index].elevatorID = hostConfig.elevatorID
  497.                 floors[index].theme.displayBackground = floor.theme.displayBackground
  498.                 floors[index].theme.displayForeground = floor.theme.displayForeground
  499.                 floors[index].theme.frameBackground = floor.theme.frameBackground
  500.                 floors[index].theme.frameBackground = floor.theme.frameBackground
  501.                 floors[index].theme.buttonBackground = floor.theme.buttonBackground
  502.                 floors[index].theme.buttonForeground = floor.theme.buttonForeground
  503.                 floors[index].theme.buttonBorder = floor.theme.buttonBorder
  504.                 SaveConfig()
  505.             else
  506.                 basalt.debug("[Error] : Please fill out all fields !!!")
  507.             end
  508.         end
  509.     end)
  510.  
  511.     --SidebarButton
  512.     if scrollAbleFrame ~= nil then
  513.         local sideBarButton = scrollAbleFrame:addButton()
  514.         floors[index].basaltConfigSideBarButton = sideBarButton
  515.         sideBarButton:setSize("parent.w - 2", 3)
  516.         sideBarButton:setText("Floor No" .. tostring(floors[index].id))
  517.         if floors[index].id == 0 then
  518.             sideBarButton:setPosition(2, 2)
  519.         else
  520.             sideBarButton:setPosition(2, (floors[index].id * 4) + 2)
  521.         end
  522.  
  523.         if floors[index].state == true then
  524.             sideBarButton:show()
  525.         else
  526.             sideBarButton:hide()
  527.         end
  528.  
  529.         sideBarButton:onClick(function(self,event,button,x,y)
  530.             if(event=="mouse_click")and(button==1)then
  531.                 local isVisible = floors[index].basaltConfigFrame:isVisible()
  532.                 for indexLoop, floorLoop in pairs(floors) do
  533.                     if floors[indexLoop].basaltConfigFrame ~= nil then
  534.                         floors[indexLoop].basaltConfigFrame:hide()
  535.                     end
  536.                 end
  537.                 if isVisible == false then
  538.                     floors[index].basaltConfigFrame:show()
  539.                 end
  540.                 if settingsFrame ~= nil  then
  541.                     if settingsFrame:isVisible() == true then settingsFrame:hide() end
  542.                 end
  543.             end
  544.         end)
  545.     end
  546. end
  547.  
  548.  
  549. local function ReadRednet()
  550.     local counter = 0
  551.     while true do
  552.         local id, message = rednet.receive(protocol, 5)
  553.         if message ~= nil then
  554.             local channel = message[1]
  555.             local data = message[2]
  556.             if channel == "CallFloor" then
  557.                 data = tonumber(data)
  558.                 rs.setBundledOutput(hostConfig.rsBcOutputSide, colorIndexTable[data])
  559.                 os.sleep(0.2)
  560.                 rs.setBundledOutput(hostConfig.rsBcOutputSide, 0)
  561.                 floors[data].isFloorCalled = true
  562.                 rednet.broadcast({"FloorUpdate", floors}, protocol)
  563.                 --basalt.debug("[Rednet]: FloorUpdate at " .. tostring(os.time()))
  564.                 counter = 0
  565.             end
  566.         else
  567.             if counter == 6 then
  568.                 rednet.broadcast({"FloorUpdate", floors}, protocol)
  569.                 --basalt.debug("[Rednet]: FloorUpdate at " .. tostring(os.time()))
  570.                 counter = 0
  571.             else
  572.                 counter = counter + 1
  573.             end
  574.         end        
  575.     end
  576. end
  577.  
  578. local function ReadRedstone()
  579.     while true do
  580.         os.pullEvent("redstone") -- Wait for a change to inputs.
  581.         local bundledInput = rs.getBundledInput(hostConfig.rsBcInputSide)
  582.         if bundledInput ~= 0 then
  583.             local elevatorPos = indexColorTable[bundledInput]
  584.             floors[elevatorPos].isElevatorOnFloor = true
  585.             floors[elevatorPos].isFloorCalled = false
  586.             rednet.broadcast({"FloorUpdate", floors}, protocol)
  587.             --basalt.debug("[Rednet]: FloorUpdate at " .. tostring(os.time()))
  588.         end
  589.     end
  590. end
  591.  
  592. local function startHost()
  593.     --MainFrame
  594.     local headerHostMain = mainFrame:addLabel()
  595.     headerHostMain:setPosition("parent.w /2 - 32",2)
  596.     headerHostMain:setText(("Elevator %d Controlboard"):format(hostConfig.elevatorID))
  597.     headerHostMain:setForeground(hostConfig.theme.displayForeground)
  598.     headerHostMain:setFontSize(2)
  599.     headerHostMain:setTextAlign("center")
  600.  
  601.     sidebar = mainFrame:addFrame()
  602.     --sidebar:setDirection("vertical")
  603.     sidebar:setBackground(hostConfig.theme.frameBackground)
  604.     sidebar:setPosition("parent.w", 1)
  605.     sidebar:setSize(15, "parent.h")
  606.     sidebar:setZIndex(25)
  607.     sidebar:onGetFocus(function(self)
  608.         self:setPosition("parent.w - (self.w-1)")
  609.     end)
  610.     sidebar:onLoseFocus(function(self)
  611.         self:setPosition("parent.w")
  612.     end)
  613.  
  614.     local settingsButton = sidebar:addButton()
  615.     settingsButton:setPosition(2,2)
  616.     settingsButton:setSize("parent.w - 2",3)
  617.     settingsButton:setBackground(hostConfig.theme.buttonBackground)
  618.     settingsButton:setForeground(hostConfig.theme.buttonForeground)
  619.     settingsButton:setBorder(hostConfig.theme.buttonBorder)
  620.     settingsButton:setText("Settings")
  621.     settingsButton:onClick(function()
  622.         if settingsFrame ~= nil then
  623.             if settingsFrame:isVisible() == true then
  624.                 settingsFrame:hide()
  625.             else
  626.                 settingsFrame:show()
  627.             end
  628.         end
  629.     end)
  630.  
  631.     local line = sidebar:addPane()
  632.     line:setSize("parent.w - 4", 1)
  633.     line:setPosition(3, 6)
  634.     line:setBackground(false, "\140", hostConfig.theme.buttonBorder)
  635.  
  636.     scrollAbleFrame = sidebar:addScrollableFrame()
  637.     scrollAbleFrame:setDirection("vertical")
  638.     scrollAbleFrame:setBackground(hostConfig.theme.frameBackground)
  639.     scrollAbleFrame:setPosition(1, 7)
  640.     scrollAbleFrame:setSize("parent.w", "parent.h - 7")
  641.  
  642.     for index, floor in pairs(floors) do
  643.         --basalt.debug(floor.id)
  644.         CreateFloorFrameAndButton(index)
  645.     end
  646.  
  647.  
  648.     --SettingsFrame
  649.     settingsFrame = mainFrame:addFrame()
  650.     settingsFrame:setPosition(1,1)
  651.     settingsFrame:setBackground(hostConfig.theme.frameBackground)
  652.     settingsFrame:setSize("parent.w - 1", "parent.h -1")
  653.     settingsFrame:hide()
  654.  
  655.     local headerSettings = settingsFrame:addLabel()
  656.     headerSettings:setPosition("parent.w / 2 - 32",2)
  657.     headerSettings:setText(("Elevator %d Settings"):format(hostConfig.elevatorID))
  658.     headerSettings:setForeground(hostConfig.theme.frameForeground)
  659.     headerSettings:setFontSize(2)
  660.     headerSettings:setTextAlign("center")
  661.  
  662.  
  663.     local rowCounter = 0
  664.     local rowY = 10
  665.     for index, floor in pairs(floors) do
  666.         if rowCounter == 8 then
  667.             rowCounter = 0
  668.             rowY = 12
  669.         end
  670.         local floorLabel = settingsFrame:addLabel()
  671.         floorLabel:setPosition(4 + (rowCounter * 12), rowY)
  672.         floorLabel:setText("Floor "..tostring(index))
  673.         floorLabel:setForeground(hostConfig.theme.frameForeground)
  674.        
  675.         local floorCheckbox = settingsFrame:addCheckbox()
  676.         floorCheckbox:setPosition(13 + (rowCounter * 12), rowY)
  677.         floorCheckbox:setBackground(hostConfig.theme.buttonBackground)
  678.         floorCheckbox:setForeground(hostConfig.theme.buttonForeground)
  679.         floorCheckbox:setValue(floors[index].state)
  680.         floorCheckbox:onChange(function(self)
  681.             floors[index].state = self:getValue()
  682.         end)
  683.         rowCounter = rowCounter + 1
  684.     end
  685.    
  686.     local leftFrame = settingsFrame:addFrame()
  687.     leftFrame:setSize("parent.w / 2", "parent.h -20")
  688.     leftFrame:setPosition(1,15)
  689.  
  690.     local rightFrame = settingsFrame:addFrame()
  691.     rightFrame:setSize("parent.w / 2", "parent.h -20")
  692.     rightFrame:setPosition("parent.w / 2", 15)
  693.  
  694.     local elevatorIDLabel = leftFrame:addLabel()
  695.     elevatorIDLabel:setSize("(parent.w / 2) - 2",1)
  696.     elevatorIDLabel:setPosition(2, 2)
  697.     elevatorIDLabel:setForeground(hostConfig.theme.frameForeground)
  698.     elevatorIDLabel:setText("Elevator ID : ")
  699.  
  700.     local elevatorIDInput = leftFrame:addInput()
  701.     elevatorIDInput:setSize("(parent.w / 2) - 2",1)
  702.     elevatorIDInput:setPosition("(parent.w / 2) - 2", 2)
  703.     elevatorIDInput:setForeground(hostConfig.theme.frameForeground)
  704.     elevatorIDInput:setBackground(colors.lightGray)
  705.     elevatorIDInput:setValue(tonumber(hostConfig.elevatorID))
  706.  
  707.     local elevatorNameLabel = leftFrame:addLabel()
  708.     elevatorNameLabel:setSize("(parent.w / 2) - 2",1)
  709.     elevatorNameLabel:setPosition(2, 4)
  710.     elevatorNameLabel:setForeground(hostConfig.theme.frameForeground)
  711.     elevatorNameLabel:setText("Elevator ID : ")
  712.  
  713.     local elevatorNameInput = leftFrame:addInput()
  714.     elevatorNameInput:setSize("(parent.w / 2) - 2",1)
  715.     elevatorNameInput:setPosition("(parent.w / 2) - 2", 4)
  716.     elevatorNameInput:setForeground(hostConfig.theme.frameForeground)
  717.     elevatorNameInput:setBackground(colors.lightGray)
  718.     elevatorNameInput:setValue(tostring(hostConfig.elevatorName))
  719.    
  720.     local saveButton = settingsFrame:addButton()
  721.     saveButton:setSize(10,3)
  722.     saveButton:setPosition("parent.w - 11", "parent.h - 4")
  723.     saveButton:setText("Save")
  724.     saveButton:onClick(function(self,event,button,x,y)
  725.         if(event=="mouse_click")and(button==1)then
  726.             if elevatorIDInput:getValue() ~= "" or elevatorNameInput:getValue() ~= "" then
  727.                 hostConfig.elevatorID = elevatorIDInput:getValue()
  728.                 hostConfig.elevatorName = elevatorNameInput:getValue()
  729.  
  730.                 for index, floor in pairs(floors) do
  731.                     floors[index].elevatorName = elevatorNameInput:getValue()
  732.                     if floors[index].state == true then
  733.                         floors[index].basaltConfigSideBarButton:show()
  734.                     else
  735.                         floors[index].basaltConfigSideBarButton:hide()
  736.                     end
  737.                 end
  738.                 basalt.debug(hostConfig.elevatorName)
  739.                 SaveConfig()
  740.             else
  741.                 basalt.debug("[Error] : ElevatorID cant be empty !!!")
  742.             end
  743.  
  744.         end
  745.     end)
  746.  
  747.     mainThread:start(ReadRednet)
  748.     secondThread:start(ReadRedstone)
  749. end
  750.  
  751. local function eleInit()
  752.     if fs.exists("elevatorHostConfig.txt") == false then
  753.         local headerLabel = mainFrame:addLabel()
  754.         headerLabel:setPosition("parent.w /2 - 32",3)
  755.         headerLabel:setText("Elevator Initialization")
  756.         headerLabel:setFontSize(2)
  757.         headerLabel:setTextAlign("center")
  758.  
  759.         local eleNumberLabel = mainFrame:addLabel()
  760.         eleNumberLabel:setPosition(2,7)
  761.         eleNumberLabel:setText("Elevator ID: ")
  762.         eleNumberLabel:setForeground(hostConfig.theme.displayForeground)
  763.         eleNumberLabel:setBackground(hostConfig.theme.displayBackground)
  764.  
  765.         local eleNumberInput = mainFrame:addInput()
  766.         eleNumberInput:setPosition(15,7)
  767.         eleNumberInput:setInputType("number")
  768.         eleNumberInput:setForeground(hostConfig.theme.frameForeground)
  769.         eleNumberInput:setBackground(hostConfig.theme.frameBackground)
  770.  
  771.         local eleRsInputLabel = mainFrame:addLabel()
  772.         eleRsInputLabel:setPosition(2,9)
  773.         eleRsInputLabel:setText("Redstone Bundled Cable Input Side ")
  774.         eleRsInputLabel:setForeground(hostConfig.theme.displayForeground)
  775.         eleRsInputLabel:setBackground(hostConfig.theme.displayBackground)
  776.  
  777.         local eleRsInputDropDown = mainFrame:addDropdown()
  778.         eleRsInputDropDown:setPosition(2,10)
  779.         eleRsInputDropDown:setForeground(hostConfig.theme.frameForeground)
  780.         eleRsInputDropDown:setBackground(hostConfig.theme.frameBackground)
  781.         eleRsInputDropDown:addItem("Top")
  782.         eleRsInputDropDown:addItem("Bottom")
  783.         eleRsInputDropDown:addItem("Left")
  784.         eleRsInputDropDown:addItem("Right")
  785.         eleRsInputDropDown:addItem("Front")
  786.         eleRsInputDropDown:addItem("Back")
  787.         eleRsInputDropDown:onChange(function(self, event, item)
  788.             hostConfig.rsBcInputSide = item.text
  789.         end)
  790.  
  791.         local eleRsOutputLabel = mainFrame:addLabel()
  792.         eleRsOutputLabel:setPosition(2,19)
  793.         eleRsOutputLabel:setForeground(hostConfig.theme.displayForeground)
  794.         eleRsOutputLabel:setBackground(hostConfig.theme.displayBackground)
  795.         eleRsOutputLabel:setText("Redstone Bundled Cable Output Side ")
  796.  
  797.         local eleRsOutputDropDown = mainFrame:addDropdown()
  798.         eleRsOutputDropDown:setPosition(2,20)
  799.         eleRsOutputDropDown:setForeground(hostConfig.theme.frameForeground)
  800.         eleRsOutputDropDown:setBackground(hostConfig.theme.frameBackground)
  801.         eleRsOutputDropDown:addItem("Top")
  802.         eleRsOutputDropDown:addItem("Bottom")
  803.         eleRsOutputDropDown:addItem("Left")
  804.         eleRsOutputDropDown:addItem("Right")
  805.         eleRsOutputDropDown:addItem("Front")
  806.         eleRsOutputDropDown:addItem("Back")
  807.         eleRsOutputDropDown:onChange(function(self, event, item)
  808.             hostConfig.rsBcOutputSide = item.text
  809.         end)
  810.  
  811.         local saveButton = mainFrame:addButton()
  812.         saveButton:setSize(10,3)
  813.         saveButton:setPosition("parent.w - 11", "parent.h - 4")
  814.         saveButton:setText("Save")
  815.         saveButton:onClick(function(self,event,button,x,y)
  816.             if(event=="mouse_click")and(button==1)then
  817.                 if eleNumberInput:getValue() ~= "" then
  818.                     hostConfig.elevatorID = eleNumberInput:getValue()
  819.                 else
  820.                     basalt.debug("Error: You need to enter a Elevator ID")
  821.                     return
  822.                 end
  823.                 protocol = ("Elevator %d"):format(hostConfig.elevatorID)
  824.                 mainFrame:removeChildren()
  825.                 startHost()
  826.             end
  827.         end)
  828.     else
  829.         protocol = ("Elevator %d"):format(hostConfig.elevatorID)
  830.         startHost()
  831.     end
  832. end
  833.  
  834. eleInit()
  835. basalt.autoUpdate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement