Advertisement
Downfire

elevatorHost

Jun 17th, 2024 (edited)
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 31.87 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.     end
  515.    
  516.     floors[index].basaltConfigSideBarButton = sideBarButton
  517.     sideBarButton:setSize("parent.w - 2", 3)
  518.     sideBarButton:setText("Floor No" .. tostring(floors[index].id))
  519.     if floors[index].id == 0 then
  520.         sideBarButton:setPosition(2, 2)
  521.     else
  522.         sideBarButton:setPosition(2, (floors[index].id * 4) + 2)
  523.     end
  524.  
  525.     if floors[index].state == true then
  526.         sideBarButton:show()
  527.     else
  528.         sideBarButton:hide()
  529.     end
  530.    
  531.  
  532.     sideBarButton:onClick(function(self,event,button,x,y)
  533.         if(event=="mouse_click")and(button==1)then
  534.             local isVisible = floors[index].basaltConfigFrame:isVisible()
  535.             for indexLoop, floorLoop in pairs(floors) do
  536.                 if floors[indexLoop].basaltConfigFrame ~= nil then
  537.                     floors[indexLoop].basaltConfigFrame:hide()
  538.                 end
  539.             end
  540.             if isVisible == false then
  541.                 floors[index].basaltConfigFrame:show()
  542.             end
  543.             if settingsFrame ~= nil  then
  544.                 if settingsFrame:isVisible() == true then settingsFrame:hide() end
  545.             end
  546.            
  547.         end
  548.     end)
  549. end
  550.  
  551.  
  552. local function ReadRednet()
  553.     local counter = 0
  554.     while true do
  555.         local id, message = rednet.receive(protocol, 5)
  556.         if message ~= nil then
  557.             local channel = message[1]
  558.             local data = message[2]
  559.             if channel == "CallFloor" then
  560.                 data = tonumber(data)
  561.                 rs.setBundledOutput(hostConfig.rsBcOutputSide, colorIndexTable[data])
  562.                 os.sleep(0.2)
  563.                 rs.setBundledOutput(hostConfig.rsBcOutputSide, 0)
  564.                 floors[data].isFloorCalled = true
  565.                 rednet.broadcast({"FloorUpdate", floors}, protocol)
  566.                 basalt.debug("[Rednet]: FloorUpdate at " .. tostring(os.time()))
  567.                 counter = 0
  568.             end
  569.         else
  570.             if counter == 6 then
  571.                 rednet.broadcast({"FloorUpdate", floors}, protocol)
  572.                 basalt.debug("[Rednet]: FloorUpdate at " .. tostring(os.time()))
  573.                 counter = 0
  574.             else
  575.                 counter = counter + 1
  576.             end
  577.         end        
  578.     end
  579. end
  580.  
  581. local function ReadRedstone()
  582.     while true do
  583.         os.pullEvent("redstone") -- Wait for a change to inputs.
  584.         local bundledInput = rs.getBundledInput(hostConfig.rsBcInputSide)
  585.         if bundledInput ~= 0 then
  586.             local elevatorPos = indexColorTable[bundledInput]
  587.             floors[elevatorPos].isElevatorOnFloor = true
  588.             floors[elevatorPos].isFloorCalled = false
  589.             rednet.broadcast({"FloorUpdate", floors}, protocol)
  590.             basalt.debug("[Rednet]: FloorUpdate at " .. tostring(os.time()))
  591.         end
  592.     end
  593. end
  594.  
  595. local function startHost()
  596.     --MainFrame
  597.     local headerHostMain = mainFrame:addLabel()
  598.     headerHostMain:setPosition("parent.w /2 - 32",2)
  599.     headerHostMain:setText(("Elevator %d Controlboard"):format(hostConfig.elevatorID))
  600.     headerHostMain:setForeground(hostConfig.theme.displayForeground)
  601.     headerHostMain:setFontSize(2)
  602.     headerHostMain:setTextAlign("center")
  603.  
  604.     sidebar = mainFrame:addFrame()
  605.     --sidebar:setDirection("vertical")
  606.     sidebar:setBackground(hostConfig.theme.frameBackground)
  607.     sidebar:setPosition("parent.w", 1)
  608.     sidebar:setSize(15, "parent.h")
  609.     sidebar:setZIndex(25)
  610.     sidebar:onGetFocus(function(self)
  611.         self:setPosition("parent.w - (self.w-1)")
  612.     end)
  613.     sidebar:onLoseFocus(function(self)
  614.         self:setPosition("parent.w")
  615.     end)
  616.  
  617.     local settingsButton = sidebar:addButton()
  618.     settingsButton:setPosition(2,2)
  619.     settingsButton:setSize("parent.w - 2",3)
  620.     settingsButton:setBackground(hostConfig.theme.buttonBackground)
  621.     settingsButton:setForeground(hostConfig.theme.buttonForeground)
  622.     settingsButton:setBorder(hostConfig.theme.buttonBorder)
  623.     settingsButton:setText("Settings")
  624.     settingsButton:onClick(function()
  625.         if settingsFrame ~= nil then
  626.             if settingsFrame:isVisible() == true then
  627.                 settingsFrame:hide()
  628.             else
  629.                 settingsFrame:show()
  630.             end
  631.         end
  632.     end)
  633.  
  634.     local line = sidebar:addPane()
  635.     line:setSize("parent.w - 4", 1)
  636.     line:setPosition(3, 6)
  637.     line:setBackground(false, "\140", hostConfig.theme.buttonBorder)
  638.  
  639.     scrollAbleFrame = sidebar:addScrollableFrame()
  640.     scrollAbleFrame:setDirection("vertical")
  641.     scrollAbleFrame:setBackground(hostConfig.theme.frameBackground)
  642.     scrollAbleFrame:setPosition(1, 7)
  643.     scrollAbleFrame:setSize("parent.w", "parent.h - 7")
  644.  
  645.     for index, floor in pairs(floors) do
  646.         --basalt.debug(floor.id)
  647.         CreateFloorFrameAndButton(index)
  648.     end
  649.  
  650.  
  651.     --SettingsFrame
  652.     settingsFrame = mainFrame:addFrame()
  653.     settingsFrame:setPosition(1,1)
  654.     settingsFrame:setBackground(hostConfig.theme.frameBackground)
  655.     settingsFrame:setSize("parent.w - 1", "parent.h -1")
  656.     settingsFrame:hide()
  657.  
  658.     local headerSettings = settingsFrame:addLabel()
  659.     headerSettings:setPosition("parent.w / 2 - 32",2)
  660.     headerSettings:setText(("Elevator %d Settings"):format(hostConfig.elevatorID))
  661.     headerSettings:setForeground(hostConfig.theme.frameForeground)
  662.     headerSettings:setFontSize(2)
  663.     headerSettings:setTextAlign("center")
  664.  
  665.  
  666.     local rowCounter = 0
  667.     local rowY = 10
  668.     for index, floor in pairs(floors) do
  669.         if rowCounter == 8 then
  670.             rowCounter = 0
  671.             rowY = 12
  672.         end
  673.         local floorLabel = settingsFrame:addLabel()
  674.         floorLabel:setPosition(4 + (rowCounter * 12), rowY)
  675.         floorLabel:setText("Floor "..tostring(index))
  676.         floorLabel:setForeground(hostConfig.theme.frameForeground)
  677.        
  678.         local floorCheckbox = settingsFrame:addCheckbox()
  679.         floorCheckbox:setPosition(13 + (rowCounter * 12), rowY)
  680.         floorCheckbox:setBackground(hostConfig.theme.buttonBackground)
  681.         floorCheckbox:setForeground(hostConfig.theme.buttonForeground)
  682.         floorCheckbox:setValue(floors[index].state)
  683.         floorCheckbox:onChange(function(self)
  684.             floors[index].state = self:getValue()
  685.         end)
  686.         rowCounter = rowCounter + 1
  687.     end
  688.    
  689.     local leftFrame = settingsFrame:addFrame()
  690.     leftFrame:setSize("parent.w / 2", "parent.h -20")
  691.     leftFrame:setPosition(1,15)
  692.  
  693.     local rightFrame = settingsFrame:addFrame()
  694.     rightFrame:setSize("parent.w / 2", "parent.h -20")
  695.     rightFrame:setPosition("parent.w / 2", 15)
  696.  
  697.     local elevatorIDLabel = leftFrame:addLabel()
  698.     elevatorIDLabel:setSize("(parent.w / 2) - 2",1)
  699.     elevatorIDLabel:setPosition(2, 2)
  700.     elevatorIDLabel:setForeground(hostConfig.theme.frameForeground)
  701.     elevatorIDLabel:setText("Elevator ID : ")
  702.  
  703.     local elevatorIDInput = leftFrame:addInput()
  704.     elevatorIDInput:setSize("(parent.w / 2) - 2",1)
  705.     elevatorIDInput:setPosition("(parent.w / 2) - 2", 2)
  706.     elevatorIDInput:setForeground(hostConfig.theme.frameForeground)
  707.     elevatorIDInput:setBackground(colors.lightGray)
  708.     elevatorIDInput:setValue(tonumber(hostConfig.elevatorID))
  709.  
  710.     local elevatorNameLabel = leftFrame:addLabel()
  711.     elevatorNameLabel:setSize("(parent.w / 2) - 2",1)
  712.     elevatorNameLabel:setPosition(2, 4)
  713.     elevatorNameLabel:setForeground(hostConfig.theme.frameForeground)
  714.     elevatorNameLabel:setText("Elevator ID : ")
  715.  
  716.     local elevatorNameInput = leftFrame:addInput()
  717.     elevatorNameInput:setSize("(parent.w / 2) - 2",1)
  718.     elevatorNameInput:setPosition("(parent.w / 2) - 2", 4)
  719.     elevatorNameInput:setForeground(hostConfig.theme.frameForeground)
  720.     elevatorNameInput:setBackground(colors.lightGray)
  721.     elevatorNameInput:setValue(tostring(hostConfig.elevatorName))
  722.    
  723.     local saveButton = settingsFrame:addButton()
  724.     saveButton:setSize(10,3)
  725.     saveButton:setPosition("parent.w - 11", "parent.h - 4")
  726.     saveButton:setText("Save")
  727.     saveButton:onClick(function(self,event,button,x,y)
  728.         if(event=="mouse_click")and(button==1)then
  729.             if elevatorIDInput:getValue() ~= "" or elevatorNameInput:getValue() ~= "" then
  730.                 hostConfig.elevatorID = elevatorIDInput:getValue()
  731.                 hostConfig.elevatorName = elevatorNameInput:getValue()
  732.  
  733.                 for index, floor in pairs(floors) do
  734.                     floors[index].elevatorName = elevatorNameInput:getValue()
  735.                     if floors[index].state == true then
  736.                         floors[index].basaltConfigSideBarButton:show()
  737.                     else
  738.                         floors[index].basaltConfigSideBarButton:hide()
  739.                     end
  740.                 end
  741.                 basalt.debug(hostConfig.elevatorName)
  742.                 SaveConfig()
  743.             else
  744.                 basalt.debug("[Error] : ElevatorID cant be empty !!!")
  745.             end
  746.  
  747.         end
  748.     end)
  749.  
  750.     mainThread:start(ReadRednet)
  751.     secondThread:start(ReadRedstone)
  752. end
  753.  
  754. local function eleInit()
  755.     if fs.exists("elevatorHostConfig.txt") == false then
  756.         local headerLabel = mainFrame:addLabel()
  757.         headerLabel:setPosition("parent.w /2 - 32",3)
  758.         headerLabel:setText("Elevator Initialization")
  759.         headerLabel:setFontSize(2)
  760.         headerLabel:setTextAlign("center")
  761.  
  762.         local eleNumberLabel = mainFrame:addLabel()
  763.         eleNumberLabel:setPosition(2,7)
  764.         eleNumberLabel:setText("Elevator ID: ")
  765.         eleNumberLabel:setForeground(hostConfig.theme.displayForeground)
  766.         eleNumberLabel:setBackground(hostConfig.theme.displayBackground)
  767.  
  768.         local eleNumberInput = mainFrame:addInput()
  769.         eleNumberInput:setPosition(15,7)
  770.         eleNumberInput:setInputType("number")
  771.         eleNumberInput:setForeground(hostConfig.theme.frameForeground)
  772.         eleNumberInput:setBackground(hostConfig.theme.frameBackground)
  773.  
  774.         local eleRsInputLabel = mainFrame:addLabel()
  775.         eleRsInputLabel:setPosition(2,9)
  776.         eleRsInputLabel:setText("Redstone Bundled Cable Input Side ")
  777.         eleRsInputLabel:setForeground(hostConfig.theme.displayForeground)
  778.         eleRsInputLabel:setBackground(hostConfig.theme.displayBackground)
  779.  
  780.         local eleRsInputDropDown = mainFrame:addDropdown()
  781.         eleRsInputDropDown:setPosition(2,10)
  782.         eleRsInputDropDown:setForeground(hostConfig.theme.frameForeground)
  783.         eleRsInputDropDown:setBackground(hostConfig.theme.frameBackground)
  784.         eleRsInputDropDown:addItem("Top")
  785.         eleRsInputDropDown:addItem("Bottom")
  786.         eleRsInputDropDown:addItem("Left")
  787.         eleRsInputDropDown:addItem("Right")
  788.         eleRsInputDropDown:addItem("Front")
  789.         eleRsInputDropDown:addItem("Back")
  790.         eleRsInputDropDown:onChange(function(self, event, item)
  791.             hostConfig.rsBcInputSide = item.text
  792.         end)
  793.  
  794.         local eleRsOutputLabel = mainFrame:addLabel()
  795.         eleRsOutputLabel:setPosition(2,19)
  796.         eleRsOutputLabel:setForeground(hostConfig.theme.displayForeground)
  797.         eleRsOutputLabel:setBackground(hostConfig.theme.displayBackground)
  798.         eleRsOutputLabel:setText("Redstone Bundled Cable Output Side ")
  799.  
  800.         local eleRsOutputDropDown = mainFrame:addDropdown()
  801.         eleRsOutputDropDown:setPosition(2,20)
  802.         eleRsOutputDropDown:setForeground(hostConfig.theme.frameForeground)
  803.         eleRsOutputDropDown:setBackground(hostConfig.theme.frameBackground)
  804.         eleRsOutputDropDown:addItem("Top")
  805.         eleRsOutputDropDown:addItem("Bottom")
  806.         eleRsOutputDropDown:addItem("Left")
  807.         eleRsOutputDropDown:addItem("Right")
  808.         eleRsOutputDropDown:addItem("Front")
  809.         eleRsOutputDropDown:addItem("Back")
  810.         eleRsOutputDropDown:onChange(function(self, event, item)
  811.             hostConfig.rsBcOutputSide = item.text
  812.         end)
  813.  
  814.         local saveButton = mainFrame:addButton()
  815.         saveButton:setSize(10,3)
  816.         saveButton:setPosition("parent.w - 11", "parent.h - 4")
  817.         saveButton:setText("Save")
  818.         saveButton:onClick(function(self,event,button,x,y)
  819.             if(event=="mouse_click")and(button==1)then
  820.                 if eleNumberInput:getValue() ~= "" then
  821.                     hostConfig.elevatorID = eleNumberInput:getValue()
  822.                 else
  823.                     basalt.debug("Error: You need to enter a Elevator ID")
  824.                     return
  825.                 end
  826.                 protocol = ("Elevator %d"):format(hostConfig.elevatorID)
  827.                 mainFrame:removeChildren()
  828.                 startHost()
  829.             end
  830.         end)
  831.     else
  832.         protocol = ("Elevator %d"):format(hostConfig.elevatorID)
  833.         startHost()
  834.     end
  835. end
  836.  
  837. eleInit()
  838. basalt.autoUpdate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement