local computer = require("computer") local component = require("component") local GUI = require("GUI") local buffer = require("doubleBuffering") local filesystem = require("filesystem") local color = require("color") local image = require("image") local unicode = require("unicode") local MineOSInterface = require("MineOSInterface") local MineOSPaths = require("MineOSPaths") local MineOSCore = require("MineOSCore") -------------------------------------------------------------------------------- local modulesPath = MineOSCore.getCurrentScriptDirectory() .. "Modules/" local localization = MineOSCore.getCurrentScriptLocalization() local scrollSpeed = 2 -------------------------------------------------------------------------------- local mainContainer, window = MineOSInterface.addWindow(GUI.filledWindow(1, 1, 100, 29, 0xF0F0F0)) local leftPanel = window:addChild(GUI.panel(1, 1, 30, 1, 0xE1E1E1)) window.backgroundPanel.localX = leftPanel.width + 1 window.actionButtons.localY = 2 window.actionButtons:moveToFront() local modulesLayout = window:addChild(GUI.layout(1, 3, leftPanel.width, 1, 1, 1)) modulesLayout:setAlignment(1, 1, GUI.ALIGNMENT_HORIZONTAL_LEFT, GUI.ALIGNMENT_VERTICAL_TOP) modulesLayout:setSpacing(1, 1, 1) window.contentLayout = window:addChild(GUI.layout(window.backgroundPanel.localX, 1, 1, 1, 1, 1)) local function moduleDraw(object) local textColor = object.pressed and 0xE1E1E1 or 0x2D2D2D if object.pressed then buffer.drawRectangle(object.x, object.y, object.width, object.height, 0x336DFF, textColor, " ") buffer.drawText(object.x, object.y - 1, 0x336DFF, string.rep("▄", object.width)) buffer.drawText(object.x, object.y + object.height, 0x336DFF, string.rep("▀", object.width)) end buffer.drawImage(object.x + 2, object.y, object.icon) buffer.drawText(object.x + 12, object.y + 1, textColor, object.module.name) end local function runModule(object) window.contentLayout:removeChildren() window.contentLayout:setMargin(1, 1, 0, object.module.margin) window.contentLayout.eventHandler = function(mainContainer, _, e1, e2, e3, e4, e5) if e1 == "scroll" then local cell = window.contentLayout.cells[1][1] local to = -math.floor(cell.childrenHeight / 2) cell.verticalMargin = cell.verticalMargin + (e5 > 0 and scrollSpeed or -scrollSpeed) if cell.verticalMargin > object.module.margin then cell.verticalMargin = object.module.margin elseif cell.verticalMargin < to then cell.verticalMargin = to end mainContainer:drawOnScreen() end end object.module.onTouch() mainContainer:drawOnScreen() end local function selectModule(object) local child for i = 1, #modulesLayout.children do child = modulesLayout.children[i] child.pressed = object == child end runModule(object) end local function moduleEventHandler(mainContainer, object, e1) if e1 == "touch" then selectModule(object) end end local modules = {} for file in filesystem.list(modulesPath) do table.insert(modules, file) end table.sort(modules, function(a, b) return a < b end) for i = 1, #modules do local result, reason = loadfile(modulesPath .. modules[i] .. "Main.lua") if result then local success, result = pcall(result, mainContainer, window, localization) if success then local object = modulesLayout:addChild(GUI.object(1, 1, modulesLayout.width, 3)) object.icon = image.load(modulesPath .. modules[i] .. "Icon.pic") object.module = result object.pressed = false object.draw = moduleDraw object.eventHandler = moduleEventHandler else error("Failed to execute module " .. modules[i] .. ": " .. tostring(result)) end else error("Failed to load module " .. modules[i] .. ": " .. tostring(reason)) end end modulesLayout.eventHandler = function(mainContainer, object, e1, e2, e3, e4, e5) if e1 == "scroll" then local cell = modulesLayout.cells[1][1] local to = -(#modulesLayout.children - 1) * 4 + 1 cell.verticalMargin = cell.verticalMargin + (e5 > 0 and scrollSpeed or -scrollSpeed) if cell.verticalMargin > 1 then cell.verticalMargin = 1 elseif cell.verticalMargin < to then cell.verticalMargin = to end mainContainer:drawOnScreen() end end window.onResize = function(width, height) modulesLayout:setMargin(1, 1, 0, 1) window.backgroundPanel.width, window.backgroundPanel.height = width - leftPanel.width, height window.contentLayout.width, window.contentLayout.height = window.backgroundPanel.width, window.backgroundPanel.height leftPanel.height = height modulesLayout.height = height - 2 for i = 1, #modulesLayout.children do if modulesLayout.children[i].pressed then runModule(modulesLayout.children[i]) break end end end window:resize(window.width, window.height) selectModule(modulesLayout.children[1])