From e083b3f2ad87754b10a33b38289908bfbd074bcb Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Sun, 10 Jan 2021 18:58:02 -0300 Subject: [PATCH] Refactoring pages widget logic to be on widget file Moving page logic to be on page file --- Programs/monitor-system/api/gui/page/help.lua | 7 + Programs/monitor-system/api/gui/page/init.lua | 126 ++++++---- .../monitor-system/api/gui/page/overview.lua | 4 + .../monitor-system/api/gui/page/stock.lua | 7 + Programs/monitor-system/api/gui/widget.lua | 225 +++++++++++++---- Programs/monitor-system/init.lua | 226 +----------------- 6 files changed, 294 insertions(+), 301 deletions(-) create mode 100644 Programs/monitor-system/api/gui/page/help.lua create mode 100644 Programs/monitor-system/api/gui/page/stock.lua diff --git a/Programs/monitor-system/api/gui/page/help.lua b/Programs/monitor-system/api/gui/page/help.lua new file mode 100644 index 0000000..78bab6d --- /dev/null +++ b/Programs/monitor-system/api/gui/page/help.lua @@ -0,0 +1,7 @@ +--[[ +|gla| help | +|wid|ev ti|ev ti| +|ove|ev ti|ev ti| +|sto|ev ti|ev ti| +|not| power |b|f| +--]] diff --git a/Programs/monitor-system/api/gui/page/init.lua b/Programs/monitor-system/api/gui/page/init.lua index ebdcf5f..3f3a48c 100644 --- a/Programs/monitor-system/api/gui/page/init.lua +++ b/Programs/monitor-system/api/gui/page/init.lua @@ -1,54 +1,98 @@ -- Import section -local doubleBuffer = require("graphics.doubleBuffering") +DoubleBuffer = require("graphics.doubleBuffering") Colors = require("graphics.colors") Unicode = require("unicode") +Widget = require("api.gui.widget") -- +-- GPU resolution should be 160 x 50. +-- Screen should be 16 blocks by 10 blocks. (Could also be 8 x 5). +-- That way, each block should have a resolution of 10 x 10 +-- Organizing the page: +---- Title on top of the page (title) +---- Side panel on the left With a width of 20 pixels (panel) +---- Two buttons for page navigation (b, f) +------- Each with a width of 10 pixels +---- 1 Power widget on the bottom, with a width of 40 pixels (power) +---- 9 Regular widgets on the right, in a 3 x 3 grid (w) +------ Each one with a width of 20 pixels +--[[ +| p | title | +| a | w | w | w | +| n | w | w | w | +| e | w | w | w | +| l | power |b|f| +--]] +local page = {} + local pages = { - overview = require("Overview"), - notifications = require("notifications"), - stock = require("stock"), - help = require("help"), - widgets = require("widgets"), - glasses = require("glasses") + overview = require("api.gui.page.overview"), + notifications = require("api.gui.page.notifications"), + stock = require("api.gui.page.stock"), + help = require("api.gui.page.help"), + widgets = require("api.gui.page.widgets"), + glasses = require("api.gui.page.glasses") } -function pages:draw(component, index) - if index < 10 then - local x = 40 + 40 * ((index - 1) % 3) - local y = 10 * math.ceil((index) / 3) - doubleBuffer.drawRectangle(x + 1, y + 1, 38, 8, Colors.machineBackground, Colors.machineBackground, "█") - doubleBuffer.drawFrame(x + 1, y + 1, 38, 8, Colors.labelColor) - doubleBuffer.drawLine(x + 4, y + 4, x + 16, y + 4, Colors.machineBackground, Colors.mainColor, "_") - doubleBuffer.drawText(x + 1, y + 1, Colors.labelColor, component.name) - doubleBuffer.drawText( - x + 4, - y + 4, - component.working and Colors.workingColor or Colors.errorColor, - component.leftInfo - ) - if component.middleInfo then - doubleBuffer.drawText( - x + 20 - Unicode.len(component.middleInfo), - y + 5, - Colors.accentB, - component.middleInfo - ) - end - if component.rightInfo then - doubleBuffer.drawText( - x + 20 - Unicode.len(component.rightInfo) - 4, - y + 5, - Colors.accentA, - component.rightInfo - ) - end - elseif index == 10 then +local widgets = {} + +Event.listen( + "touch", + function(_, _, x, y) + local index = + 1 + + (math.floor( + 2 * + ((x - Widget.baseWidth) / Widget.baseWidth + + 3 * math.floor((y - Widget.baseHeight) / Widget.baseHeight)) + )) / + 2 + local widget = widgets[index] or widgets[index - 0.5] + + widget:onClick() end - return +) + +local function drawTitle(title) + local x = Widget.baseWidth + local y = 0 + local scale = 3 + local width = Widget.baseWidth * scale + local height = Widget.baseHeight + DoubleBuffer.drawRectangle( + x + 1, + y + 1, + width - 1, + height - 1, + Colors.machineBackground, + Colors.machineBackground, + "█" + ) + DoubleBuffer.drawFrame(x + 1, y + 1, width - 1, height - 1, Colors.labelColor) + DoubleBuffer.drawLine(x + 3, y + 6, x + width - 3, y + 6, Colors.machineBackground, Colors.textColor, "─") + DoubleBuffer.drawText(x + (width - Unicode.len(title)) / 2, y + 5, Colors.mainColor, title) end -function pages:render() +function page.create(page) + drawTitle(page.title) end -return pages +function page.fake() + for i = 1, 9 do + table.insert(widgets, Widget.machineWidget.fake()) + end + table.insert(widgets, Widget.powerWidget.fake()) + widgets[11] = widgets[10] + + page.create(pages.overview) +end + +function page.update() + for index, widget in ipairs(widgets) do + widget:update() + widget:draw(index) + end + DoubleBuffer.drawChanges() +end + +return page diff --git a/Programs/monitor-system/api/gui/page/overview.lua b/Programs/monitor-system/api/gui/page/overview.lua index 541df2d..bd7790d 100644 --- a/Programs/monitor-system/api/gui/page/overview.lua +++ b/Programs/monitor-system/api/gui/page/overview.lua @@ -5,3 +5,7 @@ |sto| w | w | w | |not| power |b|f| --]] + +return { + title = "Overview" +} diff --git a/Programs/monitor-system/api/gui/page/stock.lua b/Programs/monitor-system/api/gui/page/stock.lua new file mode 100644 index 0000000..73a7143 --- /dev/null +++ b/Programs/monitor-system/api/gui/page/stock.lua @@ -0,0 +1,7 @@ +--[[ +|gla| stock | +|wid|ev ti|ev ti| +|hel|ev ti|ev ti| +|ove|ev ti|ev ti| +|not| power |b|f| +--]] diff --git a/Programs/monitor-system/api/gui/widget.lua b/Programs/monitor-system/api/gui/widget.lua index 33938ae..dfa558d 100644 --- a/Programs/monitor-system/api/gui/widget.lua +++ b/Programs/monitor-system/api/gui/widget.lua @@ -1,46 +1,191 @@ Component = require("component") -GPU = Component.gpu -Screen = Component.screen - --- GPU resolution should be 160 x 50. --- Screen should be 16 blocks by 10 blocks. (Could also be 8 x 5). --- That way, each block should have a resolution of 10 x 10 --- Organizing the page: ----- Title on top of the page (title) ----- Side panel on the left With a width of 20 pixels (panel) ----- Two buttons for page navigation (b, f) -------- Each with a width of 10 pixels ----- 1 Power widget on the bottom, with a width of 40 pixels (power) ----- 9 Regular widgets on the right, in a 3 x 3 grid (w) ------- Each one with a width of 20 pixels ---[[ -| p | title | -| a | w | w | w | -| n | w | w | w | -| e | w | w | w | -| l | power |b|f| ---]] - - -local page = { - title = {}, - panel = {}, - back = {}, - forwards = {} -} +Unicode = require("unicode") +Colors = require("graphics.colors") +DoubleBuffer = require("graphics.doubleBuffering") local widget = { - name = "", - leftString = "", - middleString = "", - rightString = "", - height = 10, - width = 20, + baseHeight = 10, + baseWidth = 40 } -function widget.create(name, leftString, middleString, rightString, screenIndex) - widget.name = name or "Unused" - widget.leftString = leftString or "" - widget.middleString = middleString or "" - widget.rightString = rightString or "" +-- function widget.create(name, leftString, middleString, rightString, screenIndex) +-- widget.name = name or "Unused" +-- widget.leftString = leftString or "" +-- widget.middleString = middleString or "" +-- widget.rightString = rightString or "" +-- end + +local states = { + {name = "ON", color = Colors.workingColor}, + {name = "IDLE", color = Colors.idleColor}, + {name = "OFF", color = Colors.offColor}, + {name = "BROKEN", color = Colors.errorColor} +} + +local function drawProgress(x, y, width, height, progress, maxProgress, color) + progress = math.floor(progress * (width + height - 2) / (maxProgress ~= 0 and maxProgress or 1)) + + local lengths = { + first = progress > 5 and 5 or progress, + second = progress > height - 2 + 5 and height - 2 or progress - (5), + third = progress > width - 7 + height - 2 + 5 and width - 7 or progress - (height - 2 + 5) + } + DoubleBuffer.drawSemiPixelRectangle(x + 6 - lengths.first, y + 1, lengths.first, 1, color) + DoubleBuffer.drawSemiPixelRectangle(x + 1, y + 2, 1, lengths.second, color) + DoubleBuffer.drawSemiPixelRectangle(x + 1, y + height, lengths.third, 1, color) + DoubleBuffer.drawSemiPixelRectangle(x + width - 4, y + height, lengths.first, 1, color) + DoubleBuffer.drawSemiPixelRectangle(x + width, y + height - lengths.second, 1, lengths.second, color) + DoubleBuffer.drawSemiPixelRectangle(x + 1 + width - lengths.third, y + 1, lengths.third, 1, color) end + +local function draw(self, index) + if index > 10 then + return + end + local scale = self.scale or 1 + local x = widget.baseWidth + widget.baseWidth * ((index - 1) % 3) + local width = widget.baseWidth * scale + local height = widget.baseHeight + local y = height * math.ceil((index) / 3) + + DoubleBuffer.drawRectangle( + x + 1, + y + 1, + width - 1, + height - 1, + Colors.machineBackground, + Colors.machineBackground, + "█" + ) + drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.progressBackground) + DoubleBuffer.drawLine(x + 3, y + 5, x + width - 3, y + 5, Colors.machineBackground, Colors.textColor, "─") + DoubleBuffer.drawText(x + 3, y + 3, Colors.labelColor, self.name) + + drawProgress(x, 2 * y, width - 1, 2 * (height - 1), self.progress, self.maxProgress, Colors.barColor) + DoubleBuffer.drawText(x + 3, y + 7, self.state.color, self.state.name) + if self.state == states[4] then + drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.errorColor) + else + local middleInfo = self:getMiddleString() + if middleInfo then + DoubleBuffer.drawText(x + 3 + 3 + Unicode.len("IDLE"), y + height - 3, Colors.textColor, middleInfo) + end + DoubleBuffer.drawText( + x + width - Unicode.len(self.progress .. "/" .. self.maxProgress .. " s") - 3, + y + height - 3, + Colors.accentA, + self.progress .. "/" .. self.maxProgress .. " s" + ) + end +end + +local fakeNames = { + "Cleanroom", + "Electric Blast Furnace", + "Miner", + "Vacuum Freezer", + "Multi Smelter", + "Sifter", + "Large Chemical Reactor", + "Distillery", + "Oil Cracking Unit", + "Implosion Compressor" +} + +widget.machineWidget = {} + +function widget.machineWidget:update() + local breakWidget = math.random(10000) > 9999 + if breakWidget and self.state ~= states[3] then + self.state = states[4] + end + if self.state == states[1] then + self.progress = self.progress + 1 + if self.progress >= self.maxProgress then + self.progress = 0 + self.maxProgress = 0 + self.state = states[2] + end + elseif self.state == states[2] then + if math.random(1000) > 999 then + self.state = states[1] + self.maxProgress = math.random(500) + end + elseif self.state == states[3] then + self.progress = self.progress + 1 + if self.progress >= self.maxProgress then + self.progress = 0 + self.maxProgress = 0 + end + end +end + +function widget.machineWidget:onClick() + if self.state == states[1] or self.state == states[2] then + self.state = states[3] + elseif self.state == states[3] then + if self.progress < self.maxProgress then + self.state = states[1] + else + self.progress = 0 + self.maxProgress = 0 + self.state = states[2] + end + elseif self.state == states[4] then + self.progress = 0 + self.maxProgress = 0 + self.state = states[2] + end +end + +function widget.machineWidget:getMiddleString() +end + +function widget.machineWidget.fake() + local state = states[math.random(4)] + return { + name = fakeNames[math.random(10)] .. " " .. math.floor(math.random(3)), + state = state, + progress = 0, + maxProgress = state ~= states[3] and state ~= states[4] and math.random(500) or 0, + type = "machine", + update = widget.machineWidget.update, + onClick = widget.machineWidget.onClick, + getMiddleString = widget.machineWidget.getMiddleString, + draw = draw + } +end + +widget.powerWidget = {} + +function widget.powerWidget:update() + self.progress = self.progress + self.dProgress +end + +function widget.powerWidget:onClick() + self.dProgress = -self.dProgress +end + +function widget.powerWidget:getMiddleString() + local remaining = self.dProgress > 0 and self.maxProgress - self.progress or -self.progress + return (self.dProgress > 0 and "+" or "") .. + self.dProgress .. "EU/s. " .. (self.dProgress > 0 and "Full in: " or "Empty in: ") .. remaining / self.dProgress +end + +function widget.powerWidget.fake() + return { + name = "Power", + state = states[1], + progress = math.random(16000000), + maxProgress = 16000000, + scale = 2, + type = "power", + dProgress = 1, + update = widget.powerWidget.update, + onClick = widget.powerWidget.onClick, + getMiddleString = widget.powerWidget.getMiddleString, + draw = draw + } +end + +return widget diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index 6c678c7..303709e 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -1,8 +1,8 @@ -- Import section -Colors = require("graphics.colors") -Unicode = require("unicode") -DoubleBuffer = require("graphics.doubleBuffering") Event = require("event") +Colors = require("graphics.colors") +Widget = require("api.gui.widget") +Page = require("api.gui.page") -- Graphics = require("graphics.graphics") -- MultiBlock = require("data.datasource.multi-block") -- SingleBlock = require("data.datasource.single-block") @@ -64,228 +64,14 @@ end require("api.sound.zelda-secret")() --]] -local baseWidth = 40 -local baseHeight = 10 -local states = { - {name = "ON", color = Colors.workingColor}, - {name = "IDLE", color = Colors.idleColor}, - {name = "OFF", color = Colors.offColor}, - {name = "BROKEN", color = Colors.errorColor} -} +Page.fake() -local fakeNames = { - "Cleanroom", - "Electric Blast Furnace", - "Miner", - "Vacuum Freezer", - "Multi Smelter", - "Sifter", - "Large Chemical Reactor", - "Distillery", - "Oil Cracking Unit", - "Implosion Compressor" -} - -local machineWidget = {} - -function machineWidget:update() - local breakWidget = math.random(10000) > 9999 - if breakWidget and self.state ~= states[3] then - self.state = states[4] - end - if self.state == states[1] then - self.progress = self.progress + 1 - if self.progress >= self.maxProgress then - self.progress = 0 - self.maxProgress = 0 - self.state = states[2] - end - elseif self.state == states[2] then - if math.random(1000) > 999 then - self.state = states[1] - self.maxProgress = math.random(500) - end - elseif self.state == states[3] then - self.progress = self.progress + 1 - if self.progress >= self.maxProgress then - self.progress = 0 - self.maxProgress = 0 - end - end -end - -function machineWidget:onClick() - if self.state == states[1] or self.state == states[2] then - self.state = states[3] - elseif self.state == states[3] then - if self.progress < self.maxProgress then - self.state = states[1] - else - self.progress = 0 - self.maxProgress = 0 - self.state = states[2] - end - elseif self.state == states[4] then - self.progress = 0 - self.maxProgress = 0 - self.state = states[2] - end -end - -function machineWidget:getMiddleString() -end - -function machineWidget.fake() - local state = states[math.random(4)] - return { - name = fakeNames[math.random(10)] .. " " .. math.floor(math.random(3)), - state = state, - progress = 0, - maxProgress = state ~= states[3] and state ~= states[4] and math.random(500) or 0, - type = "machine", - update = machineWidget.update, - onClick = machineWidget.onClick, - getMiddleString = machineWidget.getMiddleString - } -end - -local powerWidget = {} - -function powerWidget:update() - self.progress = self.progress + self.dProgress -end - -function powerWidget:onClick() - self.dProgress = -self.dProgress -end - -function powerWidget:getMiddleString() - local remaining = self.dProgress > 0 and self.maxProgress - self.progress or -self.progress - return (self.dProgress > 0 and "+" or "") .. - self.dProgress .. "EU/s. " .. (self.dProgress > 0 and "Full in: " or "Empty in: ") .. remaining / self.dProgress -end - -function powerWidget.fake() - return { - name = "Power", - state = states[1], - progress = math.random(16000000), - maxProgress = 16000000, - scale = 2, - type = "power", - dProgress = 1, - update = powerWidget.update, - onClick = powerWidget.onClick, - getMiddleString = powerWidget.getMiddleString - } -end - -local widgets = {} - -for i = 1, 9 do - table.insert(widgets, machineWidget.fake()) -end -table.insert(widgets, powerWidget.fake()) -widgets[11] = widgets[10] - -Event.listen( - "touch", - function(_, _, x, y) - local index = - 1 + (math.floor(2 * ((x - baseWidth) / baseWidth + 3 * math.floor((y - baseHeight) / baseHeight)))) / 2 - local widget = widgets[index] or widgets[index - 0.5] - - widget:onClick() - end -) - -local function drawTitle(title) - local x = baseWidth - local y = 0 - local scale = 3 - local width = baseWidth * scale - local height = baseHeight - DoubleBuffer.drawRectangle( - x + 1, - y + 1, - width - 1, - height - 1, - Colors.machineBackground, - Colors.machineBackground, - "█" - ) - DoubleBuffer.drawFrame(x + 1, y + 1, width - 1, height - 1, Colors.labelColor) - DoubleBuffer.drawLine(x + 3, y + 6, x + width - 3, y + 6, Colors.machineBackground, Colors.textColor, "─") - DoubleBuffer.drawText(x + (width - Unicode.len(title)) / 2, y + 5, Colors.mainColor, title) -end - -local function drawProgress(x, y, width, height, progress, maxProgress, color) - progress = math.floor(progress * (width + height - 2) / (maxProgress ~= 0 and maxProgress or 1)) - - local lengths = { - first = progress > 5 and 5 or progress, - second = progress > height - 2 + 5 and height - 2 or progress - (5), - third = progress > width - 7 + height - 2 + 5 and width - 7 or progress - (height - 2 + 5) - } - DoubleBuffer.drawSemiPixelRectangle(x + 6 - lengths.first, y + 1, lengths.first, 1, color) - DoubleBuffer.drawSemiPixelRectangle(x + 1, y + 2, 1, lengths.second, color) - DoubleBuffer.drawSemiPixelRectangle(x + 1, y + height, lengths.third, 1, color) - DoubleBuffer.drawSemiPixelRectangle(x + width - 4, y + height, lengths.first, 1, color) - DoubleBuffer.drawSemiPixelRectangle(x + width, y + height - lengths.second, 1, lengths.second, color) - DoubleBuffer.drawSemiPixelRectangle(x + 1 + width - lengths.third, y + 1, lengths.third, 1, color) -end - -local function drawWidget(index, widget) - if index > 10 then - return - end - local scale = widget.scale or 1 - local x = baseWidth + baseWidth * ((index - 1) % 3) - local width = baseWidth * scale - local height = baseHeight - local y = height * math.ceil((index) / 3) - DoubleBuffer.drawRectangle( - x + 1, - y + 1, - width - 1, - height - 1, - Colors.machineBackground, - Colors.machineBackground, - "█" - ) - - drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.progressBackground) - drawProgress(x, 2 * y, width - 1, 2 * (height - 1), widget.progress, widget.maxProgress, Colors.barColor) - - DoubleBuffer.drawLine(x + 3, y + 5, x + width - 3, y + 5, Colors.machineBackground, Colors.textColor, "─") - DoubleBuffer.drawText(x + 3, y + 3, Colors.labelColor, widget.name) - DoubleBuffer.drawText(x + 3, y + 7, widget.state.color, widget.state.name) - if widget.state == states[4] then - drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.errorColor) - else - local middleInfo = widget:getMiddleString() - if middleInfo then - DoubleBuffer.drawText(x + 3 + 3 + Unicode.len("IDLE"), y + height - 3, Colors.textColor, middleInfo) - end - DoubleBuffer.drawText( - x + width - Unicode.len(widget.progress .. "/" .. widget.maxProgress .. " s") - 3, - y + height - 3, - Colors.accentA, - widget.progress .. "/" .. widget.maxProgress .. " s" - ) - end -end - -drawTitle("Overview") while true do - for index, widget in ipairs(widgets) do - widget:update() - drawWidget(index, widget) - end - DoubleBuffer.drawChanges() + Page.update() os.sleep(0) end + --[[ Page = require("api.gui.page") Notifications = {}