Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local bridge
- if type(tArgs[1]) == "string" and peripheral.getType(tArgs[1]) == "openperipheral_bridge" then
- bridge = peripheral.wrap(tArgs[1])
- bridge.clear()
- else
- error("could not find bridge on side: "..tostring(tArgs[1]))
- end
- local requiredAPIs = {
- "tiles",
- "guiTiles",
- "advancedTiles",
- "remotePeripheralClient",
- }
- for _, apiName in ipairs(requiredAPIs) do
- if not _G.apiName then
- if not os.loadAPI(apiName) then
- error("Could not load API: ", apiName)
- end
- end
- end
- local function textToSize(text, width)
- if width <= 6 then
- return "..."
- elseif tiles.getMaxStringLength(text, width, 1) < string.len(text) then
- return string.sub(text, 1, tiles.getMaxStringLength(text, width - 6, 1)).."..."
- end
- return text
- end
- local remotePeripherals = remotePeripheralClient.new()
- local function newPlayerHandler(playerUUID, surfaceHandler)
- return function()
- local guiHandler = guiTiles.newGuiHandler(surfaceHandler)
- local windowHandler = guiTiles.newBasicWindowHandler()
- local minWidth, minHeight = 50, 20
- local function newGraphWindow(uniqueID, width, height, updateName, objectType)
- local width, height = math.max(width, minWidth), math.max(height, minHeight)
- local window = windowHandler:New(surfaceHandler:AddTile(-(width/2), -(height/2), 1), width, height, true)
- window:SetScreenAnchor("MIDDLE", "MIDDLE")
- local textBox = window:AddTextBox(1, 1, 1, width - 10, function(text) updateName(objectType, uniqueID, text) end)
- textBox.background:SetVisible(false)
- textBox:SetText(uniqueID)
- textBox:SetOnDeselect(function(text) updateName(objectType, uniqueID, textBox:GetText()) end)
- local closeButton = window:AddButton(width - 8, 1, 1, "X", 0xff0000, 0xffffff, 0x00ff00, 0x000000)
- closeButton:SetOnRelease(function() window:SetDrawn(false) end)
- return window
- end
- local selectionWindow = guiTiles.newBasicWindow(surfaceHandler:AddTile(10, 10, 1), 80, 50)
- local peripheralTypes = {}
- local sourceTypes = {}
- local peripheralButton = selectionWindow:AddButton(10, 10, 1, "Peripherals", 0x7fcc19, 0x000000, 0x57a64e, 0x000000)
- peripheralButton:SetOnRelease(
- function()
- sourceTypes.listWindow:SetVisible(false)
- sourceTypes.listWindow:SetClickable(false)
- peripheralTypes.listWindow:SetVisible(true)
- peripheralTypes.listWindow:SetClickable(true)
- end
- )
- local sourceButton = selectionWindow:AddButton(17, 30, 1, "Sources", 0x7fcc19, 0x000000, 0x57a64e, 0x000000)
- sourceButton:SetOnRelease(
- function()
- peripheralTypes.listWindow:SetVisible(false)
- peripheralTypes.listWindow:SetClickable(false)
- sourceTypes.listWindow:SetVisible(true)
- sourceTypes.listWindow:SetClickable(true)
- end
- )
- do -- peripheralTypes
- peripheralTypes.list = {}
- peripheralTypes.selectedTypeList = false
- peripheralTypes.listWindow = guiTiles.newBasicWindow(selectionWindow:AddSubTile(selectionWindow:GetWidth() + 5, 0, 1), 150, 80)
- peripheralTypes.listWindow:SetVisible(false)
- peripheralTypes.listWindow:SetClickable(false)
- peripheralTypes.listWindow:AddFancyText(6, 6, 1, "Peripherals", 0x000000, 1)
- local function onListSelect(index, peripheralType)
- if peripheralTypes.selectedTypeList then
- peripheralTypes.selectedTypeList:SetVisible(false)
- peripheralTypes.selectedTypeList:SetClickable(false)
- end
- local sourceList = peripheralTypes.list[peripheralType].listWindow
- if sourceList then
- sourceList:SetVisible(true)
- sourceList:SetClickable(true)
- peripheralTypes.selectedTypeList = sourceList
- end
- end
- peripheralTypes.listObject = peripheralTypes.listWindow:AddList(5, 25, 1, 140, 50, onListSelect, {})
- peripheralTypes.add = function(peripheralType, name)
- local peripheralData = peripheralTypes.list[peripheralType]
- if peripheralData then
- local window = newGraphWindow(name, peripheralData.width, peripheralData.height, peripheralTypes.update, peripheralType)
- local object = peripheralTypes.list[peripheralType].add(name, window)
- peripheralTypes.list[peripheralType].list[name] = object
- peripheralTypes.list[peripheralType].listObject:AddItem(1, {name, name})
- end
- end
- peripheralTypes.update = function(peripheralType, peripheralName, peripheralNewName)
- if peripheralTypes.list[peripheralType] then
- local list = peripheralTypes.list[peripheralType].listObject
- for index, itemData in ipairs(list.items) do
- if itemData[1] == peripheralName then
- list:SetItem(index, {itemData[1], peripheralNewName})
- break
- end
- end
- end
- end
- peripheralTypes.remove = function(peripheralType, name)
- local object = peripheralTypes.list[peripheralType].list[name]
- if object then
- windowHandler:Delete(object.windowID)
- for index, itemName in ipairs(peripheralTypes.list[peripheralType].listObject.items) do
- if itemName[1] == name then
- peripheralTypes.list[peripheralType].listObject:RemoveItem(index)
- break
- end
- end
- peripheralTypes.list[peripheralType].list[name] = nil
- end
- end
- end
- do -- sourceTypes
- sourceTypes.list = {}
- sourceTypes.selectedTypeList = false
- sourceTypes.listWindow = guiTiles.newBasicWindow(selectionWindow:AddSubTile(selectionWindow:GetWidth() + 5, 0, 1), 150, 80)
- sourceTypes.listWindow:SetVisible(false)
- sourceTypes.listWindow:SetClickable(false)
- sourceTypes.listWindow:AddFancyText(6, 6, 1, "Sources", 0x000000, 1)
- local function onListSelect(index, sourceType)
- if sourceTypes.selectedTypeList then
- sourceTypes.selectedTypeList:SetVisible(false)
- sourceTypes.selectedTypeList:SetClickable(false)
- end
- local sourceList = sourceTypes.list[sourceType].listWindow
- if sourceList then
- sourceList:SetVisible(true)
- sourceList:SetClickable(true)
- sourceTypes.selectedTypeList = sourceList
- end
- end
- sourceTypes.listObject = sourceTypes.listWindow:AddList(5, 25, 1, 140, 50, onListSelect, {})
- sourceTypes.add = function(sourceType, name)
- local sourceData = sourceTypes.list[sourceType]
- if sourceData then
- local window = newGraphWindow(name, sourceData.width, sourceData.height, sourceTypes.update, sourceType)
- local sourceObject = sourceTypes.list[sourceType].add(name, window)
- sourceTypes.list[sourceType].list[name] = sourceObject
- sourceTypes.list[sourceType].listObject:AddItem(1, {name, name})
- end
- end
- sourceTypes.update = function(sourceType, sourceName, sourceNewName)
- if sourceTypes.list[sourceType] then
- local list = sourceTypes.list[sourceType].listObject
- for index, itemData in ipairs(list.items) do
- if itemData[1] == sourceName then
- list:SetItem(index, {itemData[1], sourceNewName})
- break
- end
- end
- end
- end
- sourceTypes.remove = function(sourceType, name)
- local sourceObject = sourceTypes.list[sourceType].list[name]
- if sourceObject then
- windowHandler:Delete(sourceObject.windowID)
- for index, itemName in ipairs(sourceTypes.list[sourceType].listObject.items) do
- if itemName[1] == name then
- sourceTypes.list[sourceType].listObject:RemoveItem(index)
- break
- end
- end
- sourceTypes.list[sourceType].list[name] = nil
- end
- end
- end
- local peripherals = {
- furnace = {
- display_name = "Vanilla Furnace",
- width = 100,
- height = 80,
- add = function(name, window)
- local sourceObject = {
- windowID = window:GetUserdata()[2],
- displays = {
- fuelProgress = advancedTiles.addSimpleFluidBar(window, 10, 55, 1, 20, 10, "lava", 1),
- cookProgress = advancedTiles.addComplexBar(window, 30, 40, 1, 40, 10, 0xff0000, 1),
- [1] = window:AddItemSlot(15, 25, 1),
- [2] = window:AddItemSlot(15, 65, 1),
- [3] = window:AddItemSlot(85, 45, 1),
- },
- }
- sourceObject.displays.fuelProgress:SetRotation(270)
- return sourceObject
- end,
- update = function(name, sourceObject)
- local curBurn, maxBurn
- local isCooking, curCook
- local maxCook = 200
- local furnaceData = remotePeripherals:GetSourceData(name, "vanilla_furnace")
- if furnaceData then
- curBurn, maxBurn = furnaceData.getBurnTime, furnaceData.getCurrentItemBurnTime
- isCooking, curCook = furnaceData.isBurning, furnaceData.getCookTime
- end
- if type(curBurn) == "number" and type(maxBurn) == "number" then
- sourceObject.displays.fuelProgress:SetPercent(curBurn/maxBurn)
- else
- sourceObject.displays.fuelProgress:SetPercent(0)
- end
- if isCooking and type(curCook) == "number" then
- sourceObject.displays.cookProgress:SetPercent(curCook/maxCook)
- else
- sourceObject.displays.cookProgress:SetPercent(0)
- end
- local items
- local itemData = remotePeripherals:GetSourceData(name, "inventory")
- if itemData then
- items = itemData.getAllStacks
- end
- for i = 1, 3 do
- if items and items[i] then
- sourceObject.displays[i]:SetItemId(items[i].id)
- sourceObject.displays[i]:SetMeta(items[i].dmg)
- sourceObject.displays[i]:SetAmount(items[i].qty)
- if items[i].health_bar then
- sourceObject.displays[i]:SetDamageBar(items[i].health_bar)
- else
- sourceObject.displays[i]:SetDamageBar(0)
- end
- else
- sourceObject.displays[i]:SetAmount(false)
- end
- end
- end,
- },
- tile_thermalexpansion_machine_furnace_name = {
- display_name = "Redstone Furnace",
- width = 100,
- height = 80,
- add = function(name, window)
- local sourceObject = {
- windowID = window:GetUserdata()[2],
- cookProgress = 0,
- displays = {
- energy = advancedTiles.addComplexBar(window, 10, 70, 1, 50, 10, 0xff0000, 1),
- cookProgress = advancedTiles.addSimpleBoxBar(window, 50, 42, 1, 20, 6, 0xffffff, 1),
- [1] = window:AddItemSlot(40, 45, 1),
- [2] = window:AddItemSlot(80, 45, 1),
- },
- }
- sourceObject.displays.energy:SetRotation(270)
- return sourceObject
- end,
- update = function(name, sourceObject)
- local curEnergy, maxEnergy
- local energyPerTick
- local rfData = remotePeripherals:GetSourceData(name, "rf_info")
- if rfData then
- curEnergy = rfData.getEnergyInfo
- maxEnergy = rfData.getMaxEnergyInfo
- energyPerTick = rfData.getEnergyPerTickInfo
- end
- if type(curEnergy) == "number" and type(maxEnergy) == "number" then
- sourceObject.displays.energy:SetPercent(curEnergy/maxEnergy)
- else
- sourceObject.displays.energy:SetPercent(0)
- end
- if type(energyPerTick) == "number" and energyPerTick > 0 then
- sourceObject.cookProgress = (sourceObject.cookProgress + 1) % 5
- sourceObject.displays.cookProgress:SetPercent(sourceObject.cookProgress/4)
- else
- sourceObject.displays.cookProgress:SetPercent(0)
- end
- local items
- local itemData = remotePeripherals:GetSourceData(name, "inventory")
- if itemData then
- items = itemData.getAllStacks
- end
- for i = 1, 2 do
- if items and items[i] then
- sourceObject.displays[i]:SetItemId(items[i].id)
- sourceObject.displays[i]:SetMeta(items[i].dmg)
- sourceObject.displays[i]:SetAmount(items[i].qty)
- if items[i].health_bar then
- sourceObject.displays[i]:SetDamageBar(items[i].health_bar)
- else
- sourceObject.displays[i]:SetDamageBar(0)
- end
- else
- sourceObject.displays[i]:SetAmount(false)
- end
- end
- end,
- },
- }
- for peripheralType, peripheralData in pairs(peripherals) do
- local name = peripheralData.display_name or peripheralType
- peripheralData.list = {}
- peripheralData.listWindow = guiTiles.newBasicWindow(peripheralTypes.listWindow:AddSubTile(155, 0, 1), 150, 80)
- peripheralData.listWindow:SetVisible(false)
- peripheralData.listWindow:SetClickable(false)
- peripheralData.listWindow:AddFancyText(6, 6, 1, textToSize(name, 138), 0x000000, 1)
- local function onListSelect(index, name)
- peripheralData.listObject:SetSelected(false, true)
- peripheralData.listObject:SetHighlighted(false, true)
- local windowID = peripheralData.list[name] and peripheralData.list[name].windowID
- if windowID then
- local window = windowHandler:GetWindow(windowID)
- if window then
- windowHandler:ToFront(windowID)
- if not window:GetDrawn() then
- window:SetDrawn(true)
- else
- window:SetX(-(window:GetWidth()/2))
- window:SetY(-(window:GetHeight()/2))
- end
- end
- end
- end
- peripheralData.listObject = peripheralData.listWindow:AddList(5, 25, 1, 140, 50, onListSelect, {})
- peripheralTypes.listObject:AddItem(1, {peripheralType, name})
- peripheralTypes.list[peripheralType] = peripheralData
- end
- local sources = {
- rf_receiver = {
- width = 100,
- height = 80,
- add = function(name, window)
- local graph = advancedTiles.addBoxBarGraph(window, 5, 15, 1, 90, 60, 0xff0000, 1, 1)
- graph:GetBackground():SetClickable(false)
- local sourceObject = {
- windowID = window:GetUserdata()[2],
- displays = {
- mainGraph = graph,
- },
- backgroundUpdate = true,
- }
- return sourceObject
- end,
- update = function(name, sourceObject)
- local percent = 0
- local data = remotePeripherals:GetSourceData(name, "rf_receiver")
- if data then
- local curEnergy = data.getEnergyStored
- local maxEnergy = data.getMaxEnergyStored
- if type(curEnergy) == "number" and type(maxEnergy) == "number" then
- percent = curEnergy / maxEnergy
- end
- end
- sourceObject.displays.mainGraph:Update(percent)
- end,
- },
- fluid_handler = {
- width = 100,
- height = 80,
- add = function(name, window)
- local graph = advancedTiles.addBoxBarGraph(window, 5, 15, 1, 90, 60, 0xff0000, 1, 1)
- graph:GetBackground():SetClickable(false)
- local sourceObject = {
- windowID = window:GetUserdata()[2],
- displays = {
- mainGraph = graph,
- },
- backgroundUpdate = true,
- }
- return sourceObject
- end,
- update = function(name, sourceObject)
- local percent = 0
- local data = remotePeripherals:GetSourceData(name, "fluid_handler")
- if data then
- local tankInfo = data.getTankInfo
- if tankInfo and tankInfo[1] then
- if tankInfo[1].contents then
- percent = tankInfo[1].contents.amount / tankInfo[1].capacity
- end
- end
- end
- sourceObject.displays.mainGraph:Update(percent)
- end,
- },
- }
- for sourceType, sourceData in pairs(sources) do
- local name = sourceData.display_name or sourceType
- sourceData.list = {}
- sourceData.listWindow = guiTiles.newBasicWindow(sourceTypes.listWindow:AddSubTile(155, 0, 1), 150, 80)
- sourceData.listWindow:SetVisible(false)
- sourceData.listWindow:SetClickable(false)
- sourceData.listWindow:AddFancyText(6, 6, 1, textToSize(name, 138), 0x000000, 1)
- local function onListSelect(index, name)
- sourceData.listObject:SetSelected(false, true)
- sourceData.listObject:SetHighlighted(false, true)
- local windowID = sourceData.list[name] and sourceData.list[name].windowID
- if windowID then
- local window = windowHandler:GetWindow(windowID)
- if window then
- windowHandler:ToFront(windowID)
- if not window:GetDrawn() then
- window:SetDrawn(true)
- else
- window:SetX(-(window:GetWidth()/2))
- window:SetY(-(window:GetHeight()/2))
- end
- end
- end
- end
- sourceData.listObject = sourceData.listWindow:AddList(5, 25, 1, 140, 50, onListSelect, {})
- sourceTypes.listObject:AddItem(1, {sourceType, name})
- sourceTypes.list[sourceType] = sourceData
- end
- local function peripheralChecker()
- while true do
- local eventType, peripheralName = os.pullEvent()
- if eventType == "remote_peripheral_add" then
- print("adding peripheral = ", peripheralName)
- local peripheralData = remotePeripherals:GetAllData(peripheralName)
- if peripheralTypes.list[peripheralData.type] then
- peripheralTypes.add(peripheralData.type, peripheralName)
- end
- if peripheralData.sources then
- for source, _ in pairs(peripheralData.sources) do
- if sourceTypes.list[source] then
- sourceTypes.add(source, peripheralName)
- end
- end
- end
- elseif eventType == "remote_peripheral_remove" then
- for peripheralType, peripheralTypeData in pairs(peripheralTypes.list) do
- if peripheralTypeData.list[peripheralName] then
- peripheralTypes.remove(peripheralType, peripheralName)
- end
- end
- for sourceType, sourceTypeData in pairs(sourceTypes.list) do
- if sourceTypeData.list[peripheralName] then
- sourceTypes.remove(sourceType, peripheralName)
- end
- end
- end
- end
- end
- local function main()
- selectionWindow:SetDrawn(true)
- local UPDATE_INTERVAL = 1
- local updateTimer = os.startTimer(0)
- local nullClickTime, nullClickButton = -math.huge, false
- while true do
- local event = {os.pullEvent()}
- guiHandler:HandleEvent(event)
- if event[1] == "glasses_capture" then
- selectionWindow:SetVisible(true)
- for _, window in pairs(windowHandler:GetAllWindows()) do
- window:SetOpacity(1)
- end
- elseif event[1] == "glasses_release" then
- selectionWindow:SetVisible(false)
- for _, window in pairs(windowHandler:GetAllWindows()) do
- window:SetOpacity(0.75)
- end
- elseif event[1] == "glasses_mouse_down" then
- local newClickTime = os.clock()
- if nullClickButton == event[5] and newClickTime - nullClickTime < 0.25 then
- if nullClickButton == 0 then
- -- exit capture mode
- local capture = surfaceHandler:GetCapture()
- if capture then
- capture.stopCapturing()
- end
- elseif nullClickButton == 1 then
- -- reset window positions to centre of the screen
- for _, window in pairs(windowHandler:GetAllWindows()) do
- window:SetX(-(window:GetWidth()/2))
- window:SetY(-(window:GetHeight()/2))
- end
- end
- nullClickTime, nullClickButton = -math.huge, false
- else
- if event[5] == 1 then
- if peripheralTypes.listWindow:GetVisible() and peripheralTypes.selectedTypeList then
- peripheralTypes.selectedTypeList:SetVisible(false)
- peripheralTypes.selectedTypeList:SetClickable(false)
- peripheralTypes.selectedTypeList = false
- elseif sourceTypes.listWindow:GetVisible() and sourceTypes.selectedTypeList then
- sourceTypes.selectedTypeList:SetVisible(false)
- sourceTypes.selectedTypeList:SetClickable(false)
- sourceTypes.selectedTypeList = false
- else
- peripheralTypes.listWindow:SetVisible(false)
- peripheralTypes.listWindow:SetClickable(false)
- sourceTypes.listWindow:SetVisible(false)
- sourceTypes.listWindow:SetClickable(false)
- end
- end
- end
- nullClickTime = newClickTime
- nullClickButton = event[5]
- elseif event[1] == "timer" and event[2] == updateTimer then
- for _, peripheralData in pairs(peripheralTypes.list) do
- for name, object in pairs(peripheralData.list) do
- local window = windowHandler:GetWindow(object.windowID)
- if object.backgroundUpdate or (window and window:GetDrawn()) then
- peripheralData.update(name, object)
- end
- end
- end
- for _, sourceData in pairs(sourceTypes.list) do
- for name, object in pairs(sourceData.list) do
- local window = windowHandler:GetWindow(object.windowID)
- if object.backgroundUpdate or (window and window:GetDrawn()) then
- sourceData.update(name, object)
- end
- end
- end
- updateTimer = os.startTimer(UPDATE_INTERVAL)
- end
- end
- end
- parallel.waitForAny(main, peripheralChecker)
- end
- end
- local handler = tiles.newMultiSurfaceHandler(bridge, newPlayerHandler)
- parallel.waitForAny(function() handler:Run() end, function() remotePeripherals:Run() end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement