From 69d3cf09aac498ed1e36d41b6c103a8b6106717f Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Fri, 8 Jan 2021 16:37:22 -0300 Subject: [PATCH 01/22] Adding gui stub --- Libraries/graphics/doubleBuffering.lua | 795 ++++++++++++++++++ Programs/monitor-system/api/gui/overview.lua | 5 - .../monitor-system/api/gui/page/glasses.lua | 7 + Programs/monitor-system/api/gui/page/init.lua | 54 ++ .../api/gui/page/notifications.lua | 7 + .../monitor-system/api/gui/page/overview.lua | 7 + .../monitor-system/api/gui/page/widgets.lua | 7 + .../energy/get-energy-status-usecase.lua | 2 +- 8 files changed, 878 insertions(+), 6 deletions(-) create mode 100644 Libraries/graphics/doubleBuffering.lua delete mode 100644 Programs/monitor-system/api/gui/overview.lua create mode 100644 Programs/monitor-system/api/gui/page/glasses.lua create mode 100644 Programs/monitor-system/api/gui/page/init.lua create mode 100644 Programs/monitor-system/api/gui/page/notifications.lua create mode 100644 Programs/monitor-system/api/gui/page/overview.lua create mode 100644 Programs/monitor-system/api/gui/page/widgets.lua diff --git a/Libraries/graphics/doubleBuffering.lua b/Libraries/graphics/doubleBuffering.lua new file mode 100644 index 0000000..39cf267 --- /dev/null +++ b/Libraries/graphics/doubleBuffering.lua @@ -0,0 +1,795 @@ +local component = require("component") +local unicode = require("unicode") +Computer = require("computer") +-------------------------------------------------------------------------------- + +local bufferWidth, bufferHeight +local currentFrameBackgrounds, + currentFrameForegrounds, + currentFrameSymbols, + newFrameBackgrounds, + newFrameForegrounds, + newFrameSymbols +local drawLimitX1, drawLimitX2, drawLimitY1, drawLimitY2 +local GPUProxy, + GPUProxyGetResolution, + GPUProxySetResolution, + GPUProxyBind, + GPUProxySetBackground, + GPUProxySetForeground, + GPUProxySet + +local mathCeil, mathFloor, mathAbs = math.ceil, math.floor, math.abs +local tableInsert, tableConcat = table.insert, table.concat +local colorBlend +if Computer.getArchitecture and Computer.getArchitecture() == "Lua 5.3" then + colorBlend = load([[function(color1, color2, transparency) + local invertedTransparency = 1 - transparency + return ((color2 >> 16) * invertedTransparency + (color1 >> 16) * transparency) // 1 << 16 | + ((color2 >> 8 & 0xFF) * invertedTransparency + (color1 >> 8 & 0xFF) * transparency) // 1 << 8 | + ((color2 & 0xFF) * invertedTransparency + (color1 & 0xFF) * transparency) // 1 + end]]) +else + colorBlend = load([[function(color1, color2, transparency) + local invertedTransparency = 1 - transparency + + local r1 = color1 / 65536 + r1 = r1 - r1 % 1 + local g1 = (color1 - r1 * 65536) / 256 + g1 = g1 - g1 % 1 + + local r2 = color2 / 65536 + r2 = r2 - r2 % 1 + local g2 = (color2 - r2 * 65536) / 256 + g2 = g2 - g2 % 1 + + local r, g, b = + r2 * invertedTransparency + r1 * transparency, + g2 * invertedTransparency + g1 * transparency, + (color2 - r2 * 65536 - g2 * 256) * invertedTransparency + (color1 - r1 * 65536 - g1 * 256) * transparency + + return (r - r % 1) * 65536 + (g - g % 1) * 256 + (b - b % 1) + end]]) +end +local unicodeLen, unicodeSub = unicode.len, unicode.sub + +-------------------------------------------------------------------------------- + +local function getIndex(x, y) + return bufferWidth * (y - 1) + x +end + +local function getCurrentFrameTables() + return currentFrameBackgrounds, currentFrameForegrounds, currentFrameSymbols +end + +local function getNewFrameTables() + return newFrameBackgrounds, newFrameForegrounds, newFrameSymbols +end + +-------------------------------------------------------------------------------- + +local function setDrawLimit(x1, y1, x2, y2) + drawLimitX1, drawLimitY1, drawLimitX2, drawLimitY2 = x1, y1, x2, y2 +end + +local function resetDrawLimit() + drawLimitX1, drawLimitY1, drawLimitX2, drawLimitY2 = 1, 1, bufferWidth, bufferHeight +end + +local function getDrawLimit() + return drawLimitX1, drawLimitY1, drawLimitX2, drawLimitY2 +end + +-------------------------------------------------------------------------------- + +local function flush(width, height) + if not width or not height then + width, height = GPUProxyGetResolution() + end + + currentFrameBackgrounds, + currentFrameForegrounds, + currentFrameSymbols, + newFrameBackgrounds, + newFrameForegrounds, + newFrameSymbols = {}, {}, {}, {}, {}, {} + bufferWidth = width + bufferHeight = height + resetDrawLimit() + + for y = 1, bufferHeight do + for x = 1, bufferWidth do + tableInsert(currentFrameBackgrounds, 0x010101) + tableInsert(currentFrameForegrounds, 0xFEFEFE) + tableInsert(currentFrameSymbols, " ") + + tableInsert(newFrameBackgrounds, 0x010101) + tableInsert(newFrameForegrounds, 0xFEFEFE) + tableInsert(newFrameSymbols, " ") + end + end +end + +local function setResolution(width, height) + GPUProxySetResolution(width, height) + flush(width, height) +end + +local function getResolution() + return bufferWidth, bufferHeight +end + +local function getWidth() + return bufferWidth +end + +local function getHeight() + return bufferHeight +end + +local function bindScreen(...) + GPUProxyBind(...) + flush(GPUProxyGetResolution()) +end + +local function getGPUProxy() + return GPUProxy +end + +local function updateGPUProxyMethods() + GPUProxyGet = GPUProxy.get + GPUProxyGetResolution = GPUProxy.getResolution + GPUProxyGetBackground = GPUProxy.getBackground + GPUProxyGetForeground = GPUProxy.getForeground + + GPUProxySet = GPUProxy.set + GPUProxySetResolution = GPUProxy.setResolution + GPUProxySetBackground = GPUProxy.setBackground + GPUProxySetForeground = GPUProxy.setForeground + + GPUProxyBind = GPUProxy.bind + GPUProxyFill = GPUProxy.fill +end + +local function bindGPU(address) + GPUProxy = component.proxy(address) + updateGPUProxyMethods() + flush(GPUProxyGetResolution()) +end + +-------------------------------------------------------------------------------- + +local function rawSet(index, background, foreground, symbol) + newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = background, foreground, symbol +end + +local function rawGet(index) + return newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] +end + +local function get(x, y) + if x >= 1 and y >= 1 and x <= bufferWidth and y <= bufferHeight then + local index = bufferWidth * (y - 1) + x + return newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] + else + return 0x000000, 0x000000, " " + end +end + +local function set(x, y, background, foreground, symbol) + if x >= drawLimitX1 and y >= drawLimitY1 and x <= drawLimitX2 and y <= drawLimitY2 then + local index = bufferWidth * (y - 1) + x + newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = background, foreground, symbol + end +end + +local function drawRectangle(x, y, width, height, background, foreground, symbol, transparency) + local index, indexStepOnReachOfSquareWidth = bufferWidth * (y - 1) + x, bufferWidth - width + for j = y, y + height - 1 do + if j >= drawLimitY1 and j <= drawLimitY2 then + for i = x, x + width - 1 do + if i >= drawLimitX1 and i <= drawLimitX2 then + if transparency then + newFrameBackgrounds[index], newFrameForegrounds[index] = + colorBlend(newFrameBackgrounds[index], background, transparency), + colorBlend(newFrameForegrounds[index], background, transparency) + else + newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = + background, + foreground, + symbol + end + end + + index = index + 1 + end + + index = index + indexStepOnReachOfSquareWidth + else + index = index + bufferWidth + end + end +end + +local function clear(color, transparency) + drawRectangle(1, 1, bufferWidth, bufferHeight, color or 0x0, 0x000000, " ", transparency) +end + +local function copy(x, y, width, height) + local copyArray, index = {width, height} + + for j = y, y + height - 1 do + for i = x, x + width - 1 do + if i >= 1 and j >= 1 and i <= bufferWidth and j <= bufferHeight then + index = bufferWidth * (j - 1) + i + tableInsert(copyArray, newFrameBackgrounds[index]) + tableInsert(copyArray, newFrameForegrounds[index]) + tableInsert(copyArray, newFrameSymbols[index]) + else + tableInsert(copyArray, 0x0) + tableInsert(copyArray, 0x0) + tableInsert(copyArray, " ") + end + end + end + + return copyArray +end + +local function paste(startX, startY, picture) + local imageWidth = picture[1] + local bufferIndex, pictureIndex, bufferIndexStepOnReachOfImageWidth = + bufferWidth * (startY - 1) + startX, + 3, + bufferWidth - imageWidth + + for y = startY, startY + picture[2] - 1 do + if y >= drawLimitY1 and y <= drawLimitY2 then + for x = startX, startX + imageWidth - 1 do + if x >= drawLimitX1 and x <= drawLimitX2 then + newFrameBackgrounds[bufferIndex] = picture[pictureIndex] + newFrameForegrounds[bufferIndex] = picture[pictureIndex + 1] + newFrameSymbols[bufferIndex] = picture[pictureIndex + 2] + end + + bufferIndex, pictureIndex = bufferIndex + 1, pictureIndex + 3 + end + + bufferIndex = bufferIndex + bufferIndexStepOnReachOfImageWidth + else + bufferIndex, pictureIndex = bufferIndex + bufferWidth, pictureIndex + imageWidth * 3 + end + end +end + +local function rasterizeLine(x1, y1, x2, y2, method) + local inLoopValueFrom, + inLoopValueTo, + outLoopValueFrom, + outLoopValueTo, + isReversed, + inLoopValueDelta, + outLoopValueDelta = x1, x2, y1, y2, false, mathAbs(x2 - x1), mathAbs(y2 - y1) + if inLoopValueDelta < outLoopValueDelta then + inLoopValueFrom, + inLoopValueTo, + outLoopValueFrom, + outLoopValueTo, + isReversed, + inLoopValueDelta, + outLoopValueDelta = y1, y2, x1, x2, true, outLoopValueDelta, inLoopValueDelta + end + + if outLoopValueFrom > outLoopValueTo then + outLoopValueFrom, outLoopValueTo = outLoopValueTo, outLoopValueFrom + inLoopValueFrom, inLoopValueTo = inLoopValueTo, inLoopValueFrom + end + + local outLoopValue, outLoopValueCounter, outLoopValueTriggerIncrement = + outLoopValueFrom, + 1, + inLoopValueDelta / outLoopValueDelta + local outLoopValueTrigger = outLoopValueTriggerIncrement + for inLoopValue = inLoopValueFrom, inLoopValueTo, inLoopValueFrom < inLoopValueTo and 1 or -1 do + if isReversed then + method(outLoopValue, inLoopValue) + else + method(inLoopValue, outLoopValue) + end + + outLoopValueCounter = outLoopValueCounter + 1 + if outLoopValueCounter > outLoopValueTrigger then + outLoopValue, outLoopValueTrigger = outLoopValue + 1, outLoopValueTrigger + outLoopValueTriggerIncrement + end + end +end + +local function rasterizeEllipse(centerX, centerY, radiusX, radiusY, method) + local function rasterizeEllipsePoints(XP, YP) + method(centerX + XP, centerY + YP) + method(centerX - XP, centerY + YP) + method(centerX - XP, centerY - YP) + method(centerX + XP, centerY - YP) + end + + local x, y, changeX, changeY, ellipseError, twoASquare, twoBSquare = + radiusX, + 0, + radiusY * radiusY * (1 - 2 * radiusX), + radiusX * radiusX, + 0, + 2 * radiusX * radiusX, + 2 * radiusY * radiusY + local stoppingX, stoppingY = twoBSquare * radiusX, 0 + + while stoppingX >= stoppingY do + rasterizeEllipsePoints(x, y) + + y, stoppingY, ellipseError = y + 1, stoppingY + twoASquare, ellipseError + changeY + changeY = changeY + twoASquare + + if (2 * ellipseError + changeX) > 0 then + x, stoppingX, ellipseError = x - 1, stoppingX - twoBSquare, ellipseError + changeX + changeX = changeX + twoBSquare + end + end + + x, y, changeX, changeY, ellipseError, stoppingX, stoppingY = + 0, + radiusY, + radiusY * radiusY, + radiusX * radiusX * (1 - 2 * radiusY), + 0, + 0, + twoASquare * radiusY + + while stoppingX <= stoppingY do + rasterizeEllipsePoints(x, y) + + x, stoppingX, ellipseError = x + 1, stoppingX + twoBSquare, ellipseError + changeX + changeX = changeX + twoBSquare + + if (2 * ellipseError + changeY) > 0 then + y, stoppingY, ellipseError = y - 1, stoppingY - twoASquare, ellipseError + changeY + changeY = changeY + twoASquare + end + end +end + +local function drawLine(x1, y1, x2, y2, background, foreground, symbol) + rasterizeLine( + x1, + y1, + x2, + y2, + function(x, y) + set(x, y, background, foreground, symbol) + end + ) +end + +local function drawEllipse(centerX, centerY, radiusX, radiusY, background, foreground, symbol) + rasterizeEllipse( + centerX, + centerY, + radiusX, + radiusY, + function(x, y) + set(x, y, background, foreground, symbol) + end + ) +end + +local function drawText(x, y, textColor, data, transparency) + if y >= drawLimitY1 and y <= drawLimitY2 then + local charIndex, bufferIndex = 1, bufferWidth * (y - 1) + x + + for charIndex = 1, unicodeLen(data) do + if x >= drawLimitX1 and x <= drawLimitX2 then + if transparency then + newFrameForegrounds[bufferIndex] = + colorBlend(newFrameBackgrounds[bufferIndex], textColor, transparency) + else + newFrameForegrounds[bufferIndex] = textColor + end + + newFrameSymbols[bufferIndex] = unicodeSub(data, charIndex, charIndex) + end + + x, bufferIndex = x + 1, bufferIndex + 1 + end + end +end + +local function drawImage(startX, startY, picture, blendForeground) + local bufferIndex, pictureIndex, imageWidth, backgrounds, foregrounds, alphas, symbols = + bufferWidth * (startY - 1) + startX, + 1, + picture[1], + picture[3], + picture[4], + picture[5], + picture[6] + local bufferIndexStepOnReachOfImageWidth = bufferWidth - imageWidth + + for y = startY, startY + picture[2] - 1 do + if y >= drawLimitY1 and y <= drawLimitY2 then + for x = startX, startX + imageWidth - 1 do + if x >= drawLimitX1 and x <= drawLimitX2 then + if alphas[pictureIndex] == 0 then + newFrameBackgrounds[bufferIndex], newFrameForegrounds[bufferIndex] = + backgrounds[pictureIndex], + foregrounds[pictureIndex] + elseif alphas[pictureIndex] > 0 and alphas[pictureIndex] < 1 then + newFrameBackgrounds[bufferIndex] = + colorBlend( + newFrameBackgrounds[bufferIndex], + backgrounds[pictureIndex], + alphas[pictureIndex] + ) + + if blendForeground then + newFrameForegrounds[bufferIndex] = + colorBlend( + newFrameForegrounds[bufferIndex], + foregrounds[pictureIndex], + alphas[pictureIndex] + ) + else + newFrameForegrounds[bufferIndex] = foregrounds[pictureIndex] + end + elseif alphas[pictureIndex] == 1 and symbols[pictureIndex] ~= " " then + newFrameForegrounds[bufferIndex] = foregrounds[pictureIndex] + end + + newFrameSymbols[bufferIndex] = symbols[pictureIndex] + end + + bufferIndex, pictureIndex = bufferIndex + 1, pictureIndex + 1 + end + + bufferIndex = bufferIndex + bufferIndexStepOnReachOfImageWidth + else + bufferIndex, pictureIndex = bufferIndex + bufferWidth, pictureIndex + imageWidth + end + end +end + +local function drawFrame(x, y, width, height, color) + local stringUp, stringDown, x2 = + "┌" .. string.rep("─", width - 2) .. "┐", + "└" .. string.rep("─", width - 2) .. "┘", + x + width - 1 + + drawText(x, y, color, stringUp) + y = y + 1 + for i = 1, height - 2 do + drawText(x, y, color, "│") + drawText(x2, y, color, "│") + y = y + 1 + end + drawText(x, y, color, stringDown) +end + +-------------------------------------------------------------------------------- + +local function semiPixelRawSet(index, color, yPercentTwoEqualsZero) + local upperPixel, lowerPixel, bothPixel = "▀", "▄", " " + local background, foreground, symbol = + newFrameBackgrounds[index], + newFrameForegrounds[index], + newFrameSymbols[index] + + if yPercentTwoEqualsZero then + if symbol == upperPixel then + if color == foreground then + newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = + color, + foreground, + bothPixel + else + newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = + color, + foreground, + symbol + end + elseif symbol == bothPixel then + if color ~= background then + newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = + background, + color, + lowerPixel + end + else + newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = + background, + color, + lowerPixel + end + else + if symbol == lowerPixel then + if color == foreground then + newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = + color, + foreground, + bothPixel + else + newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = + color, + foreground, + symbol + end + elseif symbol == bothPixel then + if color ~= background then + newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = + background, + color, + upperPixel + end + else + newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = + background, + color, + upperPixel + end + end +end + +local function semiPixelSet(x, y, color) + local yFixed = mathCeil(y / 2) + if x >= drawLimitX1 and yFixed >= drawLimitY1 and x <= drawLimitX2 and yFixed <= drawLimitY2 then + semiPixelRawSet(bufferWidth * (yFixed - 1) + x, color, y % 2 == 0) + end +end + +local function drawSemiPixelRectangle(x, y, width, height, color) + local index, indexStepForward, indexStepBackward, jPercentTwoEqualsZero, jFixed = + bufferWidth * (mathCeil(y / 2) - 1) + x, + (bufferWidth - width), + width + for j = y, y + height - 1 do + jPercentTwoEqualsZero = j % 2 == 0 + + for i = x, x + width - 1 do + jFixed = mathCeil(j / 2) + semiPixelRawSet(index, color, jPercentTwoEqualsZero) + index = index + 1 + end + + if jPercentTwoEqualsZero then + index = index + indexStepForward + else + index = index - indexStepBackward + end + end +end + +local function drawSemiPixelLine(x1, y1, x2, y2, color) + rasterizeLine( + x1, + y1, + x2, + y2, + function(x, y) + semiPixelSet(x, y, color) + end + ) +end + +local function drawSemiPixelEllipse(centerX, centerY, radiusX, radiusY, color) + rasterizeEllipse( + centerX, + centerY, + radiusX, + radiusY, + function(x, y) + semiPixelSet(x, y, color) + end + ) +end + +-------------------------------------------------------------------------------- + +local function getPointTimedPosition(firstPoint, secondPoint, time) + return { + x = firstPoint.x + (secondPoint.x - firstPoint.x) * time, + y = firstPoint.y + (secondPoint.y - firstPoint.y) * time + } +end + +local function getConnectionPoints(points, time) + local connectionPoints = {} + for point = 1, #points - 1 do + tableInsert(connectionPoints, getPointTimedPosition(points[point], points[point + 1], time)) + end + return connectionPoints +end + +local function getMainPointPosition(points, time) + if #points > 1 then + return getMainPointPosition(getConnectionPoints(points, time), time) + else + return points[1] + end +end + +local function drawSemiPixelCurve(points, color, precision) + local linePoints = {} + for time = 0, 1, precision or 0.01 do + tableInsert(linePoints, getMainPointPosition(points, time)) + end + + for point = 1, #linePoints - 1 do + drawSemiPixelLine( + mathFloor(linePoints[point].x), + mathFloor(linePoints[point].y), + mathFloor(linePoints[point + 1].x), + mathFloor(linePoints[point + 1].y), + color + ) + end +end + +-------------------------------------------------------------------------------- + +local function drawChanges(force) + local index, indexStepOnEveryLine, changes = + bufferWidth * (drawLimitY1 - 1) + drawLimitX1, + (bufferWidth - drawLimitX2 + drawLimitX1 - 1), + {} + local x, equalChars, equalCharsIndex, charX, charIndex, currentForeground + local currentFrameBackground, + currentFrameForeground, + currentFrameSymbol, + changesCurrentFrameBackground, + changesCurrentFrameBackgroundCurrentFrameForeground + + local changesCurrentFrameBackgroundCurrentFrameForegroundIndex + + for y = drawLimitY1, drawLimitY2 do + x = drawLimitX1 + while x <= drawLimitX2 do + -- Determine if some pixel data was changed (or if argument was passed) + if + currentFrameBackgrounds[index] ~= newFrameBackgrounds[index] or + currentFrameForegrounds[index] ~= newFrameForegrounds[index] or + currentFrameSymbols[index] ~= newFrameSymbols[index] or + force + then + -- Make pixel at both frames equal + currentFrameBackground, currentFrameForeground, currentFrameSymbol = + newFrameBackgrounds[index], + newFrameForegrounds[index], + newFrameSymbols[index] + currentFrameBackgrounds[index] = currentFrameBackground + currentFrameForegrounds[index] = currentFrameForeground + currentFrameSymbols[index] = currentFrameSymbol + + -- Look for pixels with equal chars from right of current pixel + equalChars, equalCharsIndex, charX, charIndex = {currentFrameSymbol}, 2, x + 1, index + 1 + while charX <= drawLimitX2 do + -- Pixels becomes equal only if they have same background and (whitespace char or same foreground) + if + currentFrameBackground == newFrameBackgrounds[charIndex] and + (newFrameSymbols[charIndex] == " " or + currentFrameForeground == newFrameForegrounds[charIndex]) + then + -- Make pixel at both frames equal + currentFrameBackgrounds[charIndex] = newFrameBackgrounds[charIndex] + currentFrameForegrounds[charIndex] = newFrameForegrounds[charIndex] + currentFrameSymbols[charIndex] = newFrameSymbols[charIndex] + + equalChars[equalCharsIndex], equalCharsIndex = + currentFrameSymbols[charIndex], + equalCharsIndex + 1 + else + break + end + + charX, charIndex = charX + 1, charIndex + 1 + end + + -- Group pixels that need to be drawn by background and foreground + changes[currentFrameBackground] = changes[currentFrameBackground] or {} + changesCurrentFrameBackground = changes[currentFrameBackground] + changesCurrentFrameBackground[currentFrameForeground] = + changesCurrentFrameBackground[currentFrameForeground] or {index = 1} + changesCurrentFrameBackgroundCurrentFrameForeground = + changesCurrentFrameBackground[currentFrameForeground] + changesCurrentFrameBackgroundCurrentFrameForegroundIndex = + changesCurrentFrameBackgroundCurrentFrameForeground.index + + changesCurrentFrameBackgroundCurrentFrameForeground[ + changesCurrentFrameBackgroundCurrentFrameForegroundIndex + ], + changesCurrentFrameBackgroundCurrentFrameForegroundIndex = + x, + changesCurrentFrameBackgroundCurrentFrameForegroundIndex + 1 + changesCurrentFrameBackgroundCurrentFrameForeground[ + changesCurrentFrameBackgroundCurrentFrameForegroundIndex + ], + changesCurrentFrameBackgroundCurrentFrameForegroundIndex = + y, + changesCurrentFrameBackgroundCurrentFrameForegroundIndex + 1 + changesCurrentFrameBackgroundCurrentFrameForeground[ + changesCurrentFrameBackgroundCurrentFrameForegroundIndex + ], + changesCurrentFrameBackgroundCurrentFrameForegroundIndex = + tableConcat(equalChars), + changesCurrentFrameBackgroundCurrentFrameForegroundIndex + 1 + + x, index, changesCurrentFrameBackgroundCurrentFrameForeground.index = + x + equalCharsIndex - 2, + index + equalCharsIndex - 2, + changesCurrentFrameBackgroundCurrentFrameForegroundIndex + end + + x, index = x + 1, index + 1 + end + + index = index + indexStepOnEveryLine + end + + -- Draw grouped pixels on screen + for background, foregrounds in pairs(changes) do + GPUProxySetBackground(background) + + for foreground, pixels in pairs(foregrounds) do + if currentForeground ~= foreground then + GPUProxySetForeground(foreground) + currentForeground = foreground + end + + for i = 1, #pixels, 3 do + GPUProxySet(pixels[i], pixels[i + 1], pixels[i + 2]) + end + end + end + + changes = nil +end + +-------------------------------------------------------------------------------- + +bindGPU(component.getPrimary("gpu").address) + +return { + -- getCoordinates = getCoordinates, + getIndex = getIndex, + setDrawLimit = setDrawLimit, + resetDrawLimit = resetDrawLimit, + getDrawLimit = getDrawLimit, + flush = flush, + setResolution = setResolution, + bindScreen = bindScreen, + bindGPU = bindGPU, + getGPUProxy = getGPUProxy, + getResolution = getResolution, + getWidth = getWidth, + getHeight = getHeight, + getCurrentFrameTables = getCurrentFrameTables, + getNewFrameTables = getNewFrameTables, + rawSet = rawSet, + rawGet = rawGet, + get = get, + set = set, + clear = clear, + copy = copy, + paste = paste, + rasterizeLine = rasterizeLine, + rasterizeEllipse = rasterizeEllipse, + semiPixelRawSet = semiPixelRawSet, + semiPixelSet = semiPixelSet, + drawChanges = drawChanges, + drawRectangle = drawRectangle, + drawLine = drawLine, + drawEllipse = drawEllipse, + drawText = drawText, + drawImage = drawImage, + drawFrame = drawFrame, + drawSemiPixelRectangle = drawSemiPixelRectangle, + drawSemiPixelLine = drawSemiPixelLine, + drawSemiPixelEllipse = drawSemiPixelEllipse, + drawSemiPixelCurve = drawSemiPixelCurve +} diff --git a/Programs/monitor-system/api/gui/overview.lua b/Programs/monitor-system/api/gui/overview.lua deleted file mode 100644 index 2312ca2..0000000 --- a/Programs/monitor-system/api/gui/overview.lua +++ /dev/null @@ -1,5 +0,0 @@ -local function render() - -end - -return render diff --git a/Programs/monitor-system/api/gui/page/glasses.lua b/Programs/monitor-system/api/gui/page/glasses.lua new file mode 100644 index 0000000..62d6844 --- /dev/null +++ b/Programs/monitor-system/api/gui/page/glasses.lua @@ -0,0 +1,7 @@ +--[[ +|ove| glasses | +|wid| w | w | w | +|hel| w | w | w | +|sto| w | w | w | +|not| power |b|f| +--]] diff --git a/Programs/monitor-system/api/gui/page/init.lua b/Programs/monitor-system/api/gui/page/init.lua new file mode 100644 index 0000000..ebdcf5f --- /dev/null +++ b/Programs/monitor-system/api/gui/page/init.lua @@ -0,0 +1,54 @@ +-- Import section +local doubleBuffer = require("graphics.doubleBuffering") +Colors = require("graphics.colors") +Unicode = require("unicode") +-- + +local pages = { + overview = require("Overview"), + notifications = require("notifications"), + stock = require("stock"), + help = require("help"), + widgets = require("widgets"), + glasses = require("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 + end + return +end + +function pages:render() +end + +return pages diff --git a/Programs/monitor-system/api/gui/page/notifications.lua b/Programs/monitor-system/api/gui/page/notifications.lua new file mode 100644 index 0000000..e00ed86 --- /dev/null +++ b/Programs/monitor-system/api/gui/page/notifications.lua @@ -0,0 +1,7 @@ +--[[ +|gla| notifs. | +|wid|ev ti|ev ti| +|hel|ev ti|ev ti| +|sto|ev ti|ev ti| +|ove| power |b|f| +--]] diff --git a/Programs/monitor-system/api/gui/page/overview.lua b/Programs/monitor-system/api/gui/page/overview.lua new file mode 100644 index 0000000..541df2d --- /dev/null +++ b/Programs/monitor-system/api/gui/page/overview.lua @@ -0,0 +1,7 @@ +--[[ +|gla| overview | +|wid| w | w | w | +|hel| w | w | w | +|sto| w | w | w | +|not| power |b|f| +--]] diff --git a/Programs/monitor-system/api/gui/page/widgets.lua b/Programs/monitor-system/api/gui/page/widgets.lua new file mode 100644 index 0000000..e444c56 --- /dev/null +++ b/Programs/monitor-system/api/gui/page/widgets.lua @@ -0,0 +1,7 @@ +--[[ +|gla| widgets | +|ove| w | w | w | +|hel| w | w | w | +|sto| w | w | w | +|not| power |b|f| +--]] diff --git a/Programs/monitor-system/domain/energy/get-energy-status-usecase.lua b/Programs/monitor-system/domain/energy/get-energy-status-usecase.lua index 9a4e346..b08630d 100755 --- a/Programs/monitor-system/domain/energy/get-energy-status-usecase.lua +++ b/Programs/monitor-system/domain/energy/get-energy-status-usecase.lua @@ -3,7 +3,7 @@ local getConsumption = require("domain.energy.get-consumption-usecase") local getProduction = require("domain.energy.get-production-usecase") -- -local function exec(energyProducers, energyBuffer) +local function exec(energyBuffer) -- local comsumption = getConsumption(energyBuffer) -- local production = getProduction(energyBuffer) local consumption = energyBuffer:getAverageInput() From f1964f7af0964ae357a0c77d072e802ca37cc22f Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Fri, 8 Jan 2021 16:37:42 -0300 Subject: [PATCH 02/22] Creating a simple interface --- Programs/monitor-system/api/gui/panel.lua | 27 ++++ Programs/monitor-system/api/gui/widget.lua | 46 ++++++ Programs/monitor-system/init.lua | 156 ++++++++++++++++++--- 3 files changed, 212 insertions(+), 17 deletions(-) create mode 100644 Programs/monitor-system/api/gui/panel.lua create mode 100644 Programs/monitor-system/api/gui/widget.lua diff --git a/Programs/monitor-system/api/gui/panel.lua b/Programs/monitor-system/api/gui/panel.lua new file mode 100644 index 0000000..6db0328 --- /dev/null +++ b/Programs/monitor-system/api/gui/panel.lua @@ -0,0 +1,27 @@ +-- Import section +Page = require("api.gui.page") +Graphics = require("graphics.graphics") +Colors = require("colors") +Component = require("component") +GPU = Component.gpu +-- + +local panel = { + currentPage = Page.overview +} + +function panel.render() + Graphics.rectangle(GPU, 0, 0, 20, 160, Colors.background) + for index, page in ipairs(Page) do + if page ~= panel.currentPage then + Graphics.text(GPU, 0, 10 * (index - 1), Colors.labelColor, panel.title) + end + end +end + +function panel.navigate(page) + panel.findText(page.title) + page.render() +end + +return panel diff --git a/Programs/monitor-system/api/gui/widget.lua b/Programs/monitor-system/api/gui/widget.lua new file mode 100644 index 0000000..33938ae --- /dev/null +++ b/Programs/monitor-system/api/gui/widget.lua @@ -0,0 +1,46 @@ +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 = {} +} + +local widget = { + name = "", + leftString = "", + middleString = "", + rightString = "", + height = 10, + width = 20, +} + +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 diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index f236b8d..86831f1 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -2,10 +2,14 @@ MultiBlock = require("data.datasource.multi-block") SingleBlock = require("data.datasource.single-block") EnergyProvider = require("data.datasource.energy-provider") +Colors = require("graphics.colors") +Unicode = require("unicode") +local doubleBuffer = require("graphics.doubleBuffering") +-- local cleanroomAddresses = require("config.addresses.cleanroom") local multiBlockAddresses = require("config.addresses.multi-blocks") -local energyBufferAddress = require("config.addresses.energy-buffers") +local energyBufferAddresses = require("config.addresses.energy-buffers") local protectCleanroomRecipes = require("domain.cleanroom.protect-recipes-usecase") local getMultiblockStatuses = require("domain.multiblock.get-multiblock-status-usecase") @@ -14,29 +18,29 @@ local listMiners = require("domain.miner.list-miners-usecase") local getMinersStatuses = require("domain.miner.get-miner-status-usecase") -- -local cleanroom = MultiBlock:new(multiBlockAddresses.cleanroom) -cleanroom.name = "cleanroom" local cleanroomMachines = {} -for _, address in pairs(cleanroomAddresses) do - table.insert(cleanroomMachines, SingleBlock:new(address)) +for name, address in pairs(cleanroomAddresses) do + table.insert(cleanroomMachines, SingleBlock:new(address, name)) end -local EBF11 = MultiBlock:new(multiBlockAddresses.EBF11) -EBF11.name = "EBF11" +local multiblocks = {} +for name, address in pairs(multiBlockAddresses) do + table.insert(multiblocks, MultiBlock:new(address, name)) +end -local multiblocks = {cleanroom, EBF11} +local batteryBuffers = {} +for name, address in pairs(energyBufferAddresses) do + table.insert(batteryBuffers, EnergyProvider:new(address, name)) +end -local energyBuffer = EnergyProvider:new(energyBufferAddress.batteryBuffer1) - -local energyProducers = {} local multiblocksStatuses = {} for i = 1, 100 do print(i) - protectCleanroomRecipes(cleanroom, cleanroomMachines) + protectCleanroomRecipes(multiblocks[1], cleanroomMachines) multiblocksStatuses = getMultiblockStatuses(multiblocks) - local energyStatus = getEnergyStatus(energyProducers, energyBuffer) + -- local energyStatus = getEnergyStatus(batteryBuffers[1]) local minersList = listMiners() local minersStatuses = getMinersStatuses(minersList) @@ -45,9 +49,127 @@ for i = 1, 100 do i = i + 1 end -for multiblock, status in pairs(multiblocksStatuses) do - print(multiblock .. ": \ - problems: " .. status.problems .. "\ - efficiency: " .. status.efficiencyPercentage) +for multiblockName, status in pairs(multiblocksStatuses) do + print( + multiblockName .. ":", + "\n problems: " .. status.problems, + "\n efficiency: " .. status.efficiencyPercentage, + "\n probably uses: " .. status.probablyUses + ) end + require("api.sound.zelda-secret")() + +local component = { + name = "Machine", + working = true, + leftInfo = "LV", + middleInfo = "something", + rightInfo = "16 / 32 s", + progress = 0, + maxProgress = 16000 +} + +local width = 40 +local height = 10 +local function drawWidget(index, component, scale) + scale = scale or 1 + local x = width + width * ((index - 1) % 3) + local y = height * math.ceil((index) / 3) + doubleBuffer.drawRectangle( + x + 1, + y + 1, + width * scale - 1, + height - 1, + Colors.machineBackground, + Colors.machineBackground, + "█" + ) + doubleBuffer.drawFrame(x + 1, y + 1, width * scale - 1, height - 1, Colors.labelColor) + doubleBuffer.drawLine(x + 3, y + 5, x + width * scale - 3, y + 5, Colors.machineBackground, Colors.mainColor, "─") + doubleBuffer.drawText(x + 3, y + 2, Colors.labelColor, component.name) + doubleBuffer.drawText( + x + 3, + y + 7, + component.working and Colors.workingColor or Colors.errorColor, + component.leftInfo + ) + if component.middleInfo then + doubleBuffer.drawText( + x + width * scale / 2 - Unicode.len(component.middleInfo) + 3, + y + height - 3, + Colors.accentB, + component.middleInfo + ) + end + if component.rightInfo then + doubleBuffer.drawText( + x + width * scale - Unicode.len(tostring(component.rightInfo)) - 3, + y + height - 3, + Colors.accentA, + tostring(component.rightInfo) + ) + end +end + +while true do + component.progress = component.progress + math.random(0, 1000) + component.rightInfo = component.progress .. " / " .. component.maxProgress + if component.progress >= component.maxProgress then + component.progress = 0 + end + for index = 1, 10 do + if index < 10 then + drawWidget(index, component, 1) + elseif index == 10 then + drawWidget(index, component, 2) + end + end + doubleBuffer.drawChanges() + os.sleep(0.5) +end + +--[[ +Page = require("api.gui.page") +Notifications = {} +local components = {} +local function getComponents() + local multiBlockAddresses = require("config.addresses.multi-blocks") + local energyBufferAddresses = require("config.addresses.energy-buffers") + + local multiblocks = {} + for name, address in pairs(multiBlockAddresses) do + table.insert(multiblocks, MultiBlock:new(address, name)) + end + + local batteryBuffers = {} + for name, address in pairs(energyBufferAddresses) do + table.insert(batteryBuffers, EnergyProvider:new(address, name)) + end + + return {table.unpack(multiblocks), batteryBuffers} +end + +local function setup() + components = getComponents() + Page.overview.setup() +end + +local function loop() + while true do + for index, component in ipairs(components) do + local updated, notification = component:update() + if updated then + Page:draw(component, index) + end + if notification then + table.insert(Notifications, 1, {notification, os.time()}) + end + end + Page:render() + end +end + +setup() +loop() +--]] From feca39a4ed9fc3b813c73132cbcf4ac1585764d6 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Fri, 8 Jan 2021 20:40:44 -0300 Subject: [PATCH 03/22] Adding progress bars to widgets Changing colors --- Libraries/graphics/colors.lua | 28 +++++----- Libraries/graphics/widgets.lua | 5 +- Programs/monitor-system/init.lua | 87 ++++++++++++++++++++++++-------- 3 files changed, 82 insertions(+), 38 deletions(-) diff --git a/Libraries/graphics/colors.lua b/Libraries/graphics/colors.lua index d1288dd..76c95f8 100644 --- a/Libraries/graphics/colors.lua +++ b/Libraries/graphics/colors.lua @@ -20,22 +20,22 @@ local colors = { ---[[ local newColors = { - machineBackground = colors.darkGray, - progressBackground = colors.lightGray, - labelColor = colors.chocolate, - errorColor = colors.red, - idleColor = colors.purple, - workingColor = colors.steelBlue, - positiveEUColor = colors.lime, - negativeEUColor = colors.brown, - timeColor = colors.purple, - textColor = colors.black, - hudColor = colors.darkSlateGrey, - mainColor = colors.rosyBrown, background = colors.black, + machineBackground = colors.darkSlateGrey, + progressBackground = colors.purple, + barColor = colors.magenta, + labelColor = colors.chocolate, + idleColor = colors.brown, + workingColor = colors.green, + errorColor = colors.red, + positiveEUColor = colors.lime, + negativeEUColor = colors.red, + timeColor = colors.purple, + textColor = colors.brown, + hudColor = colors.darkSlateGrey, + mainColor = colors.steelBlue, accentA = colors.cyan, - accentB = colors.magenta, - barColor = colors.blue + accentB = colors.blue } for name, color in pairs(newColors) do diff --git a/Libraries/graphics/widgets.lua b/Libraries/graphics/widgets.lua index 6672bd4..b9b1e30 100644 --- a/Libraries/graphics/widgets.lua +++ b/Libraries/graphics/widgets.lua @@ -4,7 +4,7 @@ Colors = require("colors") local widgets = {} -function widgets.gtMachineInit(GPU, name, address) +function widgets.gtMabchineInit(GPU, name, address) local maintenanceIndex = 0 local machine = util.machine(address) Graphics.rectangle(GPU, 1, 1, 28, 9, Colors.background) @@ -99,7 +99,8 @@ function widgets.gtMachine(GPU, name, address) end _, f, _ = GPU.get(6, 1) if - ((Graphics.windows[name].data == 0 or string.match(machine.getSensorInformation()[Graphics.windows[name].data], ".*c0.*")) and + ((Graphics.windows[name].data == 0 or + string.match(machine.getSensorInformation()[Graphics.windows[name].data], ".*c0.*")) and machine.isWorkAllowed()) == true then if f ~= Colors.background then diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index 86831f1..6db7930 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -4,8 +4,8 @@ SingleBlock = require("data.datasource.single-block") EnergyProvider = require("data.datasource.energy-provider") Colors = require("graphics.colors") Unicode = require("unicode") -local doubleBuffer = require("graphics.doubleBuffering") --- +Graphics = require("graphics.graphics") +DoubleBuffer = require("graphics.doubleBuffering") local cleanroomAddresses = require("config.addresses.cleanroom") local multiBlockAddresses = require("config.addresses.multi-blocks") @@ -16,8 +16,11 @@ local getMultiblockStatuses = require("domain.multiblock.get-multiblock-status-u local getEnergyStatus = require("domain.energy.get-energy-status-usecase") local listMiners = require("domain.miner.list-miners-usecase") local getMinersStatuses = require("domain.miner.get-miner-status-usecase") + +local GPU = Component.gpu -- +--[[ local cleanroomMachines = {} for name, address in pairs(cleanroomAddresses) do table.insert(cleanroomMachines, SingleBlock:new(address, name)) @@ -59,24 +62,25 @@ for multiblockName, status in pairs(multiblocksStatuses) do end require("api.sound.zelda-secret")() - +--]] local component = { name = "Machine", working = true, - leftInfo = "LV", + leftInfo = "ON", middleInfo = "something", rightInfo = "16 / 32 s", progress = 0, - maxProgress = 16000 + maxProgress = 1000 } local width = 40 local height = 10 -local function drawWidget(index, component, scale) - scale = scale or 1 - local x = width + width * ((index - 1) % 3) - local y = height * math.ceil((index) / 3) - doubleBuffer.drawRectangle( + +local function drawTitle(title) + local x = width + local y = 0 + local scale = 3 + DoubleBuffer.drawRectangle( x + 1, y + 1, width * scale - 1, @@ -85,25 +89,62 @@ local function drawWidget(index, component, scale) Colors.machineBackground, "█" ) - doubleBuffer.drawFrame(x + 1, y + 1, width * scale - 1, height - 1, Colors.labelColor) - doubleBuffer.drawLine(x + 3, y + 5, x + width * scale - 3, y + 5, Colors.machineBackground, Colors.mainColor, "─") - doubleBuffer.drawText(x + 3, y + 2, Colors.labelColor, component.name) - doubleBuffer.drawText( + DoubleBuffer.drawFrame(x + 1, y + 1, width * scale - 1, height - 1, Colors.labelColor) + DoubleBuffer.drawLine(x + 3, y + 6, x + width * scale - 3, y + 6, Colors.machineBackground, Colors.textColor, "─") + DoubleBuffer.drawText(x + (width * scale - 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) + + local lengths = { + first = progress > 5 and 5 or progress, + second = progress > height - 2 + 5 and height - 2 or progress - (5), + third = progress > width - 6 + height - 2 + 5 and width - 6 or progress - (height - 2 + 5) + } + DoubleBuffer.drawRectangle(x + 6 - lengths.first, y + 1, lengths.first, 1, color, color, "█") + DoubleBuffer.drawRectangle(x + 1, y + 2, 1, lengths.second, color, color, "█") + DoubleBuffer.drawRectangle(x + 1, y + height, lengths.third, 1, color, color, "█") + DoubleBuffer.drawRectangle(x + width - 4, y + height, lengths.first, 1, color, color, "█") + DoubleBuffer.drawRectangle(x + width, y + height - lengths.second, 1, lengths.second, color, color, "█") + DoubleBuffer.drawRectangle(x + 1 + width - lengths.third, y + 1, lengths.third, 1, color, color, "█") +end + +local function drawWidget(index, component, scale) + scale = scale or 1 + local x = width + width * ((index - 1) % 3) + local y = height * math.ceil((index) / 3) + DoubleBuffer.drawRectangle( + x + 1, + y + 1, + width * scale - 1, + height - 1, + Colors.machineBackground, + Colors.machineBackground, + "█" + ) + + drawProgress(x, y, width * scale - 1, height - 1, 1, 1, Colors.progressBackground) + drawProgress(x, y, width * scale - 1, height - 1, component.progress, component.maxProgress, Colors.barColor) + + DoubleBuffer.drawLine(x + 3, y + 5, x + width * scale - 3, y + 5, Colors.machineBackground, Colors.textColor, "─") + DoubleBuffer.drawText(x + 3, y + 3, Colors.labelColor, component.name) + DoubleBuffer.drawText( x + 3, y + 7, component.working and Colors.workingColor or Colors.errorColor, component.leftInfo ) if component.middleInfo then - doubleBuffer.drawText( - x + width * scale / 2 - Unicode.len(component.middleInfo) + 3, + DoubleBuffer.drawText( + x + 3 + 3 + Unicode.len(component.leftInfo), y + height - 3, - Colors.accentB, + Colors.textColor, component.middleInfo ) end if component.rightInfo then - doubleBuffer.drawText( + DoubleBuffer.drawText( x + width * scale - Unicode.len(tostring(component.rightInfo)) - 3, y + height - 3, Colors.accentA, @@ -112,10 +153,12 @@ local function drawWidget(index, component, scale) end end +drawTitle("Overview") while true do - component.progress = component.progress + math.random(0, 1000) - component.rightInfo = component.progress .. " / " .. component.maxProgress + component.progress = component.progress + 1 + component.rightInfo = component.progress .. " / " .. component.maxProgress .. " s" if component.progress >= component.maxProgress then + os.sleep(0.5) component.progress = 0 end for index = 1, 10 do @@ -125,8 +168,8 @@ while true do drawWidget(index, component, 2) end end - doubleBuffer.drawChanges() - os.sleep(0.5) + DoubleBuffer.drawChanges() + os.sleep(0) end --[[ From 7daf955b89c3ee280adcfc1751d73896491d860e Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Fri, 8 Jan 2021 22:45:27 -0300 Subject: [PATCH 04/22] Adding semiPixel drawing Increasing progressBar resolution Improving progressBar appearance --- Libraries/graphics/colors.lua | 9 +++-- Programs/monitor-system/init.lua | 66 +++++++++++++++++++------------- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/Libraries/graphics/colors.lua b/Libraries/graphics/colors.lua index 76c95f8..ae134b3 100644 --- a/Libraries/graphics/colors.lua +++ b/Libraries/graphics/colors.lua @@ -25,15 +25,16 @@ local newColors = { progressBackground = colors.purple, barColor = colors.magenta, labelColor = colors.chocolate, - idleColor = colors.brown, - workingColor = colors.green, + idleColor = colors.green, + workingColor = colors.lime, + offColor = colors.brown, errorColor = colors.red, positiveEUColor = colors.lime, negativeEUColor = colors.red, timeColor = colors.purple, - textColor = colors.brown, + textColor = colors.steelBlue, hudColor = colors.darkSlateGrey, - mainColor = colors.steelBlue, + mainColor = colors.chocolate, accentA = colors.cyan, accentB = colors.blue } diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index 6db7930..0fe8a3d 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -65,7 +65,7 @@ require("api.sound.zelda-secret")() --]] local component = { name = "Machine", - working = true, + state = true, leftInfo = "ON", middleInfo = "something", rightInfo = "16 / 32 s", @@ -73,71 +73,76 @@ local component = { maxProgress = 1000 } -local width = 40 -local height = 10 +local baseWidth = 40 +local baseHeight = 10 local function drawTitle(title) - local x = width + local x = baseWidth local y = 0 local scale = 3 + local width = baseWidth * scale + local height = baseHeight DoubleBuffer.drawRectangle( x + 1, y + 1, - width * scale - 1, + width - 1, height - 1, Colors.machineBackground, Colors.machineBackground, "█" ) - DoubleBuffer.drawFrame(x + 1, y + 1, width * scale - 1, height - 1, Colors.labelColor) - DoubleBuffer.drawLine(x + 3, y + 6, x + width * scale - 3, y + 6, Colors.machineBackground, Colors.textColor, "─") - DoubleBuffer.drawText(x + (width * scale - Unicode.len(title)) / 2, y + 5, Colors.mainColor, title) + 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) + 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 - 6 + height - 2 + 5 and width - 6 or progress - (height - 2 + 5) + third = progress > width - 7 + height - 2 + 5 and width - 7 or progress - (height - 2 + 5) } - DoubleBuffer.drawRectangle(x + 6 - lengths.first, y + 1, lengths.first, 1, color, color, "█") - DoubleBuffer.drawRectangle(x + 1, y + 2, 1, lengths.second, color, color, "█") - DoubleBuffer.drawRectangle(x + 1, y + height, lengths.third, 1, color, color, "█") - DoubleBuffer.drawRectangle(x + width - 4, y + height, lengths.first, 1, color, color, "█") - DoubleBuffer.drawRectangle(x + width, y + height - lengths.second, 1, lengths.second, color, color, "█") - DoubleBuffer.drawRectangle(x + 1 + width - lengths.third, y + 1, lengths.third, 1, color, color, "█") + 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, component, scale) + local globalWidth = baseWidth scale = scale or 1 - local x = width + width * ((index - 1) % 3) + local x = globalWidth + globalWidth * ((index - 1) % 3) + local width = globalWidth * scale + local height = baseHeight local y = height * math.ceil((index) / 3) DoubleBuffer.drawRectangle( x + 1, y + 1, - width * scale - 1, + width - 1, height - 1, Colors.machineBackground, Colors.machineBackground, "█" ) - drawProgress(x, y, width * scale - 1, height - 1, 1, 1, Colors.progressBackground) - drawProgress(x, y, width * scale - 1, height - 1, component.progress, component.maxProgress, Colors.barColor) + drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.progressBackground) + drawProgress(x, 2 * y, width - 1, 2 * (height - 1), component.progress, component.maxProgress, Colors.barColor) - DoubleBuffer.drawLine(x + 3, y + 5, x + width * scale - 3, y + 5, Colors.machineBackground, Colors.textColor, "─") + DoubleBuffer.drawLine(x + 3, y + 5, x + width - 3, y + 5, Colors.machineBackground, Colors.textColor, "─") DoubleBuffer.drawText(x + 3, y + 3, Colors.labelColor, component.name) DoubleBuffer.drawText( x + 3, y + 7, - component.working and Colors.workingColor or Colors.errorColor, + component.state and Colors.workingColor or Colors.idleColor, component.leftInfo ) if component.middleInfo then DoubleBuffer.drawText( - x + 3 + 3 + Unicode.len(component.leftInfo), + x + 3 + 3 + Unicode.len("IDLE"), y + height - 3, Colors.textColor, component.middleInfo @@ -145,7 +150,7 @@ local function drawWidget(index, component, scale) end if component.rightInfo then DoubleBuffer.drawText( - x + width * scale - Unicode.len(tostring(component.rightInfo)) - 3, + x + width - Unicode.len(tostring(component.rightInfo)) - 3, y + height - 3, Colors.accentA, tostring(component.rightInfo) @@ -156,11 +161,14 @@ end drawTitle("Overview") while true do component.progress = component.progress + 1 - component.rightInfo = component.progress .. " / " .. component.maxProgress .. " s" if component.progress >= component.maxProgress then - os.sleep(0.5) component.progress = 0 + component.state = false + component.leftInfo = "IDLE" + component.maxProgress = 0 + os.sleep(0.5) end + component.rightInfo = component.progress .. "/" .. component.maxProgress .. " s" for index = 1, 10 do if index < 10 then drawWidget(index, component, 1) @@ -169,6 +177,12 @@ while true do end end DoubleBuffer.drawChanges() + if not component.state then + component.state = true + component.leftInfo = "ON" + component.maxProgress = 1000 + os.sleep(3.5) + end os.sleep(0) end From 8921e57d7731259a48febf3cc8c9905b7154242e Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Fri, 8 Jan 2021 23:52:31 -0300 Subject: [PATCH 05/22] Setting up fake components with states --- Programs/monitor-system/init.lua | 154 +++++++++++++++++++------------ 1 file changed, 94 insertions(+), 60 deletions(-) diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index 0fe8a3d..861a765 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -1,23 +1,23 @@ -- Import section -MultiBlock = require("data.datasource.multi-block") -SingleBlock = require("data.datasource.single-block") -EnergyProvider = require("data.datasource.energy-provider") +-- MultiBlock = require("data.datasource.multi-block") +-- SingleBlock = require("data.datasource.single-block") +-- EnergyProvider = require("data.datasource.energy-provider") Colors = require("graphics.colors") Unicode = require("unicode") Graphics = require("graphics.graphics") DoubleBuffer = require("graphics.doubleBuffering") -local cleanroomAddresses = require("config.addresses.cleanroom") -local multiBlockAddresses = require("config.addresses.multi-blocks") -local energyBufferAddresses = require("config.addresses.energy-buffers") +-- local cleanroomAddresses = require("config.addresses.cleanroom") +-- local multiBlockAddresses = require("config.addresses.multi-blocks") +-- local energyBufferAddresses = require("config.addresses.energy-buffers") -local protectCleanroomRecipes = require("domain.cleanroom.protect-recipes-usecase") -local getMultiblockStatuses = require("domain.multiblock.get-multiblock-status-usecase") -local getEnergyStatus = require("domain.energy.get-energy-status-usecase") -local listMiners = require("domain.miner.list-miners-usecase") -local getMinersStatuses = require("domain.miner.get-miner-status-usecase") +-- local protectCleanroomRecipes = require("domain.cleanroom.protect-recipes-usecase") +-- local getMultiblockStatuses = require("domain.multiblock.get-multiblock-status-usecase") +-- local getEnergyStatus = require("domain.energy.get-energy-status-usecase") +-- local listMiners = require("domain.miner.list-miners-usecase") +-- local getMinersStatuses = require("domain.miner.get-miner-status-usecase") -local GPU = Component.gpu +-- local GPU = Component.gpu -- --[[ @@ -63,16 +63,53 @@ end require("api.sound.zelda-secret")() --]] -local component = { - name = "Machine", - state = true, - leftInfo = "ON", - middleInfo = "something", - rightInfo = "16 / 32 s", - progress = 0, - maxProgress = 1000 +local states = { + {state = "ON", color = Colors.workingColor}, + {state = "IDLE", color = Colors.idleColor}, + {state = "OFF", color = Colors.offColor}, + {state = "BROKEN", color = Colors.errorColor} } +local fakeNames = { + "Cleanroom", + "Electric Blast Furnace", + "Miner", + "Vacuum Freezer", + "Multi Smelter", + "Sifter", + "Large Chemical Reactor", + "Distillery", + "Oil Cracking Unit", + "Implosion Compressor" +} + +local function fakeComponent() + return { + name = fakeNames[math.random(10)] .. " " .. math.random(3), + state = states[math.random(4)], + progress = 0, + maxProgress = math.random(500), + idleTime = 0 + } +end + +local components = {} + +for i = 1, 9 do + table.insert(components, fakeComponent()) +end +table.insert( + components, + { + name = "Power", + state = states[1], + progress = math.random(16000000), + maxProgress = 16000000, + idleTime = 0, + scale = 2 + } +) + local baseWidth = 40 local baseHeight = 10 @@ -112,9 +149,9 @@ local function drawProgress(x, y, width, height, progress, maxProgress, color) DoubleBuffer.drawSemiPixelRectangle(x + 1 + width - lengths.third, y + 1, lengths.third, 1, color) end -local function drawWidget(index, component, scale) +local function drawWidget(index, component) local globalWidth = baseWidth - scale = scale or 1 + local scale = component.scale or 1 local x = globalWidth + globalWidth * ((index - 1) % 3) local width = globalWidth * scale local height = baseHeight @@ -134,58 +171,55 @@ local function drawWidget(index, component, scale) DoubleBuffer.drawLine(x + 3, y + 5, x + width - 3, y + 5, Colors.machineBackground, Colors.textColor, "─") DoubleBuffer.drawText(x + 3, y + 3, Colors.labelColor, component.name) - DoubleBuffer.drawText( - x + 3, - y + 7, - component.state and Colors.workingColor or Colors.idleColor, - component.leftInfo - ) - if component.middleInfo then + DoubleBuffer.drawText(x + 3, y + 7, component.state.color, component.state.state) + if component.state == states[4] then + drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.errorColor) + else + if component.middleInfo then + DoubleBuffer.drawText( + x + 3 + 3 + Unicode.len("IDLE"), + y + height - 3, + Colors.textColor, + component.middleInfo + ) + end DoubleBuffer.drawText( - x + 3 + 3 + Unicode.len("IDLE"), - y + height - 3, - Colors.textColor, - component.middleInfo - ) - end - if component.rightInfo then - DoubleBuffer.drawText( - x + width - Unicode.len(tostring(component.rightInfo)) - 3, + x + width - Unicode.len(component.progress .. "/" .. component.maxProgress) - 3, y + height - 3, Colors.accentA, - tostring(component.rightInfo) + component.progress .. "/" .. component.maxProgress ) end end drawTitle("Overview") while true do - component.progress = component.progress + 1 - if component.progress >= component.maxProgress then - component.progress = 0 - component.state = false - component.leftInfo = "IDLE" - component.maxProgress = 0 - os.sleep(0.5) - end - component.rightInfo = component.progress .. "/" .. component.maxProgress .. " s" - for index = 1, 10 do - if index < 10 then - drawWidget(index, component, 1) - elseif index == 10 then - drawWidget(index, component, 2) + for index, component in ipairs(components) do + local breakComponent = math.random(10000) > 9999 + if breakComponent then + component.state = states[4] end + if component.state == states[1] then + component.progress = component.progress + 1 + if component.progress >= component.maxProgress then + component.progress = 0 + component.state = states[2] + component.maxProgress = 0 + component.idleTime = 0 + end + elseif component.state == states[2] then + component.idleTime = component.idleTime + 1 + if component.idleTime > math.random(1000) then + component.state = states[1] + component.maxProgress = math.random(500) + end + end + + drawWidget(index, component) end DoubleBuffer.drawChanges() - if not component.state then - component.state = true - component.leftInfo = "ON" - component.maxProgress = 1000 - os.sleep(3.5) - end os.sleep(0) end - --[[ Page = require("api.gui.page") Notifications = {} From a79c9c402d1c85cd07b0e6eef682abc4a52acda2 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Sat, 9 Jan 2021 01:03:56 -0300 Subject: [PATCH 06/22] Adding clickable areas that change their corresponding widget's state --- Programs/monitor-system/init.lua | 113 +++++++++++++++++++------------ 1 file changed, 68 insertions(+), 45 deletions(-) diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index 861a765..a2613ad 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -6,6 +6,7 @@ Colors = require("graphics.colors") Unicode = require("unicode") Graphics = require("graphics.graphics") DoubleBuffer = require("graphics.doubleBuffering") +Event = require("event") -- local cleanroomAddresses = require("config.addresses.cleanroom") -- local multiBlockAddresses = require("config.addresses.multi-blocks") @@ -63,6 +64,9 @@ end require("api.sound.zelda-secret")() --]] +local baseWidth = 40 +local baseHeight = 10 + local states = { {state = "ON", color = Colors.workingColor}, {state = "IDLE", color = Colors.idleColor}, @@ -83,35 +87,57 @@ local fakeNames = { "Implosion Compressor" } -local function fakeComponent() +local function fakewidget() + local state = states[math.random(4)] return { name = fakeNames[math.random(10)] .. " " .. math.random(3), - state = states[math.random(4)], + state = state, progress = 0, - maxProgress = math.random(500), - idleTime = 0 + maxProgress = state ~= states[3] and state ~= states[4] and math.random(500) or 0, + idleTime = 0, + area = {height = baseHeight, width = baseWidth} } end -local components = {} +local widgets = {} for i = 1, 9 do - table.insert(components, fakeComponent()) + local widget = fakewidget() + widget.area.x = baseWidth + baseWidth * ((i - 1) % 3) + widget.area.y = baseHeight * math.ceil(i / 3) + table.insert(widgets, fakewidget()) end table.insert( - components, + widgets, { name = "Power", state = states[1], progress = math.random(16000000), maxProgress = 16000000, idleTime = 0, - scale = 2 + scale = 2, + area = {x = baseWidth, y = baseHeight * 5, width = baseWidth * 2, height = baseHeight} } ) +widgets[11] = widgets[10] -local baseWidth = 40 -local baseHeight = 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.progress = 0 + widget.maxProgress = 0 + widget.idleTime = 0 + if widget.state == states[1] or widget.state == states[2] then + widget.state = states[3] + elseif widget.state == states[3] or widget.state == states[4] then + widget.state = states[2] + end + end +) local function drawTitle(title) local x = baseWidth @@ -149,11 +175,13 @@ local function drawProgress(x, y, width, height, progress, maxProgress, color) DoubleBuffer.drawSemiPixelRectangle(x + 1 + width - lengths.third, y + 1, lengths.third, 1, color) end -local function drawWidget(index, component) - local globalWidth = baseWidth - local scale = component.scale or 1 - local x = globalWidth + globalWidth * ((index - 1) % 3) - local width = globalWidth * scale +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( @@ -167,55 +195,50 @@ local function drawWidget(index, component) ) drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.progressBackground) - drawProgress(x, 2 * y, width - 1, 2 * (height - 1), component.progress, component.maxProgress, Colors.barColor) + 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, component.name) - DoubleBuffer.drawText(x + 3, y + 7, component.state.color, component.state.state) - if component.state == states[4] then + DoubleBuffer.drawText(x + 3, y + 3, Colors.labelColor, widget.name) + DoubleBuffer.drawText(x + 3, y + 7, widget.state.color, widget.state.state) + if widget.state == states[4] then drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.errorColor) else - if component.middleInfo then - DoubleBuffer.drawText( - x + 3 + 3 + Unicode.len("IDLE"), - y + height - 3, - Colors.textColor, - component.middleInfo - ) + if widget.middleInfo then + DoubleBuffer.drawText(x + 3 + 3 + Unicode.len("IDLE"), y + height - 3, Colors.textColor, widget.middleInfo) end DoubleBuffer.drawText( - x + width - Unicode.len(component.progress .. "/" .. component.maxProgress) - 3, + x + width - Unicode.len(widget.progress .. "/" .. widget.maxProgress .. " s") - 3, y + height - 3, Colors.accentA, - component.progress .. "/" .. component.maxProgress + widget.progress .. "/" .. widget.maxProgress .. " s" ) end end drawTitle("Overview") while true do - for index, component in ipairs(components) do - local breakComponent = math.random(10000) > 9999 - if breakComponent then - component.state = states[4] + for index, widget in ipairs(widgets) do + local breakWidget = math.random(10000) > 9999 + if breakWidget and index ~= 10 and index ~= 11 and widget.state ~= states[3] then + widget.state = states[4] end - if component.state == states[1] then - component.progress = component.progress + 1 - if component.progress >= component.maxProgress then - component.progress = 0 - component.state = states[2] - component.maxProgress = 0 - component.idleTime = 0 + if widget.state == states[1] then + widget.progress = widget.progress + 1 + if widget.progress >= widget.maxProgress then + widget.progress = 0 + widget.state = states[2] + widget.maxProgress = 0 + widget.idleTime = 0 end - elseif component.state == states[2] then - component.idleTime = component.idleTime + 1 - if component.idleTime > math.random(1000) then - component.state = states[1] - component.maxProgress = math.random(500) + elseif widget.state == states[2] then + widget.idleTime = widget.idleTime + 1 + if widget.idleTime > math.random(1000) then + widget.state = states[1] + widget.maxProgress = math.random(500) end end - drawWidget(index, component) + drawWidget(index, widget) end DoubleBuffer.drawChanges() os.sleep(0) From 6e2513f75e5131b99c614d717a510b4d5abb5c56 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Sat, 9 Jan 2021 18:59:43 -0300 Subject: [PATCH 07/22] Commenting out unused dependency --- Programs/monitor-system/init.lua | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index a2613ad..855d591 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -4,7 +4,7 @@ -- EnergyProvider = require("data.datasource.energy-provider") Colors = require("graphics.colors") Unicode = require("unicode") -Graphics = require("graphics.graphics") +-- Graphics = require("graphics.graphics") DoubleBuffer = require("graphics.doubleBuffering") Event = require("event") @@ -94,7 +94,6 @@ local function fakewidget() state = state, progress = 0, maxProgress = state ~= states[3] and state ~= states[4] and math.random(500) or 0, - idleTime = 0, area = {height = baseHeight, width = baseWidth} } end @@ -114,7 +113,6 @@ table.insert( state = states[1], progress = math.random(16000000), maxProgress = 16000000, - idleTime = 0, scale = 2, area = {x = baseWidth, y = baseHeight * 5, width = baseWidth * 2, height = baseHeight} } @@ -130,7 +128,6 @@ Event.listen( widget.progress = 0 widget.maxProgress = 0 - widget.idleTime = 0 if widget.state == states[1] or widget.state == states[2] then widget.state = states[3] elseif widget.state == states[3] or widget.state == states[4] then @@ -228,11 +225,9 @@ while true do widget.progress = 0 widget.state = states[2] widget.maxProgress = 0 - widget.idleTime = 0 end elseif widget.state == states[2] then - widget.idleTime = widget.idleTime + 1 - if widget.idleTime > math.random(1000) then + if math.random(1000) > 999 then widget.state = states[1] widget.maxProgress = math.random(500) end From 0d6426666a73e9c561e7b17da0726a4f8e603140 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Sun, 10 Jan 2021 13:42:14 -0300 Subject: [PATCH 08/22] Refactoring main loop on init Separating updateWidget logic Adding and adjusting colors for compatibility with BSL and Complementary shader packs --- Libraries/graphics/colors.lua | 34 ++++++++++++------ Libraries/graphics/doubleBuffering.lua | 2 +- Programs/monitor-system/init.lua | 50 +++++++++++++------------- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/Libraries/graphics/colors.lua b/Libraries/graphics/colors.lua index ae134b3..03f72fc 100644 --- a/Libraries/graphics/colors.lua +++ b/Libraries/graphics/colors.lua @@ -5,28 +5,40 @@ local colors = { magenta = 0xFF00FF, yellow = 0xFFFF00, cyan = 0x00FFFF, + greenYellow = 0xADFF2F, green = 0x008000, + darkOliveGreen = 0x556B2F, + indigo = 0x4B0082, purple = 0x800080, + deepSkyBlue = 0x00BFFF, + dodgerBlue = 0x1E90FF, steelBlue = 0x4682B4, - brown = 0xA52A2A, - chocolate = 0xD2691E, + darkSlateBlue = 0x483D8B, + midnightBlue = 0x191970, + navy = 0x000080, + darkOrange = 0xFFA500, rosyBrown = 0xBC8F8F, + goldenRod = 0xDAA520, + chocolate = 0xD2691E, + brown = 0xA52A2A, + maroon = 0x800000, white = 0xFFFFFF, lightGray = 0xD3D3D3, darkGray = 0xA9A9A9, darkSlateGrey = 0x2F4F4F, + notBlack = 0x101020, black = 0x000000 } ---[[ local newColors = { background = colors.black, - machineBackground = colors.darkSlateGrey, - progressBackground = colors.purple, - barColor = colors.magenta, - labelColor = colors.chocolate, - idleColor = colors.green, - workingColor = colors.lime, + machineBackground = colors.notBlack, + progressBackground = colors.indigo, + barColor = colors.deepSkyBlue, + labelColor = colors.goldenRod, + idleColor = colors.lime, + workingColor = colors.deepSkyBlue, offColor = colors.brown, errorColor = colors.red, positiveEUColor = colors.lime, @@ -34,7 +46,7 @@ local newColors = { timeColor = colors.purple, textColor = colors.steelBlue, hudColor = colors.darkSlateGrey, - mainColor = colors.chocolate, + mainColor = colors.goldenRod, accentA = colors.cyan, accentB = colors.blue } @@ -59,13 +71,15 @@ end colors.RGB = RGB +--]] setmetatable( colors, { __index = function(self, color) - return self.RGB[color] or 0, 0, 0 + return self[color] or 0, 0, 0 end } ) + --]] return colors diff --git a/Libraries/graphics/doubleBuffering.lua b/Libraries/graphics/doubleBuffering.lua index 39cf267..8d3f219 100644 --- a/Libraries/graphics/doubleBuffering.lua +++ b/Libraries/graphics/doubleBuffering.lua @@ -475,7 +475,7 @@ end -------------------------------------------------------------------------------- local function semiPixelRawSet(index, color, yPercentTwoEqualsZero) - local upperPixel, lowerPixel, bothPixel = "▀", "▄", " " + local upperPixel, lowerPixel, bothPixel = "▀", "▄", "█" local background, foreground, symbol = newFrameBackgrounds[index], newFrameForegrounds[index], diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index 855d591..b193bf8 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -87,23 +87,41 @@ local fakeNames = { "Implosion Compressor" } +local function updateWidget(self) + local breakWidget = math.random(10000) > 9999 + if breakWidget and self.type ~= "power" 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.state = states[2] + self.maxProgress = 0 + end + elseif self.state == states[2] then + if math.random(1000) > 999 then + self.state = states[1] + self.maxProgress = math.random(500) + end + end +end + local function fakewidget() local state = states[math.random(4)] return { - name = fakeNames[math.random(10)] .. " " .. math.random(3), + 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, - area = {height = baseHeight, width = baseWidth} + type = "machine", + update = updateWidget } end local widgets = {} for i = 1, 9 do - local widget = fakewidget() - widget.area.x = baseWidth + baseWidth * ((i - 1) % 3) - widget.area.y = baseHeight * math.ceil(i / 3) table.insert(widgets, fakewidget()) end table.insert( @@ -114,7 +132,8 @@ table.insert( progress = math.random(16000000), maxProgress = 16000000, scale = 2, - area = {x = baseWidth, y = baseHeight * 5, width = baseWidth * 2, height = baseHeight} + type = "power", + update = updateWidget } ) widgets[11] = widgets[10] @@ -215,24 +234,7 @@ end drawTitle("Overview") while true do for index, widget in ipairs(widgets) do - local breakWidget = math.random(10000) > 9999 - if breakWidget and index ~= 10 and index ~= 11 and widget.state ~= states[3] then - widget.state = states[4] - end - if widget.state == states[1] then - widget.progress = widget.progress + 1 - if widget.progress >= widget.maxProgress then - widget.progress = 0 - widget.state = states[2] - widget.maxProgress = 0 - end - elseif widget.state == states[2] then - if math.random(1000) > 999 then - widget.state = states[1] - widget.maxProgress = math.random(500) - end - end - + widget:update() drawWidget(index, widget) end DoubleBuffer.drawChanges() From 510846d1ec745a342c7ba5db84703252d797e285 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Sun, 10 Jan 2021 13:51:08 -0300 Subject: [PATCH 09/22] Refactoring event listener on init Separating onClick logic --- Programs/monitor-system/init.lua | 55 ++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index b193bf8..2f74e73 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -87,24 +87,34 @@ local fakeNames = { "Implosion Compressor" } -local function updateWidget(self) +local function updateMachineWidget(self) local breakWidget = math.random(10000) > 9999 - if breakWidget and self.type ~= "power" and self.state ~= states[3] then - self.state = states[4] + if breakWidget and self.type ~= "power" 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.state = states[2] + self.maxProgress = 0 end - if self.state == states[1] then - self.progress = self.progress + 1 - if self.progress >= self.maxProgress then - self.progress = 0 - self.state = states[2] - self.maxProgress = 0 - 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[2] then + if math.random(1000) > 999 then + self.state = states[1] + self.maxProgress = math.random(500) end + end +end + +local function machineOnClick(self) + self.progress = 0 + self.maxProgress = 0 + if self.state == states[1] or self.state == states[2] then + self.state = states[3] + elseif self.state == states[3] or self.state == states[4] then + self.state = states[2] + end end local function fakewidget() @@ -115,7 +125,8 @@ local function fakewidget() progress = 0, maxProgress = state ~= states[3] and state ~= states[4] and math.random(500) or 0, type = "machine", - update = updateWidget + update = updateMachineWidget, + onClick = machineOnClick } end @@ -133,7 +144,9 @@ table.insert( maxProgress = 16000000, scale = 2, type = "power", - update = updateWidget + update = updateMachineWidget, + onClick = function() + end } ) widgets[11] = widgets[10] @@ -145,13 +158,7 @@ Event.listen( 1 + (math.floor(2 * ((x - baseWidth) / baseWidth + 3 * math.floor((y - baseHeight) / baseHeight)))) / 2 local widget = widgets[index] or widgets[index - 0.5] - widget.progress = 0 - widget.maxProgress = 0 - if widget.state == states[1] or widget.state == states[2] then - widget.state = states[3] - elseif widget.state == states[3] or widget.state == states[4] then - widget.state = states[2] - end + widget:onClick() end ) From f84c082d47932b2f361b6d3e59fa3dba76c01642 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Sun, 10 Jan 2021 14:05:39 -0300 Subject: [PATCH 10/22] Improving onClick/update mocks Making on to off to on and on to off to idle wait for progress to finish --- Libraries/graphics/colors.lua | 2 +- Programs/monitor-system/init.lua | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Libraries/graphics/colors.lua b/Libraries/graphics/colors.lua index 03f72fc..eed19dd 100644 --- a/Libraries/graphics/colors.lua +++ b/Libraries/graphics/colors.lua @@ -26,7 +26,7 @@ local colors = { lightGray = 0xD3D3D3, darkGray = 0xA9A9A9, darkSlateGrey = 0x2F4F4F, - notBlack = 0x101020, + notBlack = 0x181828, black = 0x000000 } diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index 2f74e73..061dc24 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -96,24 +96,35 @@ local function updateMachineWidget(self) self.progress = self.progress + 1 if self.progress >= self.maxProgress then self.progress = 0 - self.state = states[2] 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 local function machineOnClick(self) - self.progress = 0 - self.maxProgress = 0 if self.state == states[1] or self.state == states[2] then self.state = states[3] - elseif self.state == states[3] or self.state == states[4] then - self.state = states[2] + elseif self.state == states[3] then + if self.progress < self.maxProgress then + self.state = states[1] + else + self.state = states[2] + end + elseif self.state == states[4] then + self.progress = 0 + self.maxProgress = 0 end end From 2166eaf15682ed591200fb9132e110e75efb868b Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Sun, 10 Jan 2021 17:05:51 -0300 Subject: [PATCH 11/22] Renaming state.state to state.name --- Programs/monitor-system/init.lua | 84 +++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 28 deletions(-) diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index 061dc24..6c678c7 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -1,12 +1,12 @@ -- Import section +Colors = require("graphics.colors") +Unicode = require("unicode") +DoubleBuffer = require("graphics.doubleBuffering") +Event = require("event") +-- Graphics = require("graphics.graphics") -- MultiBlock = require("data.datasource.multi-block") -- SingleBlock = require("data.datasource.single-block") -- EnergyProvider = require("data.datasource.energy-provider") -Colors = require("graphics.colors") -Unicode = require("unicode") --- Graphics = require("graphics.graphics") -DoubleBuffer = require("graphics.doubleBuffering") -Event = require("event") -- local cleanroomAddresses = require("config.addresses.cleanroom") -- local multiBlockAddresses = require("config.addresses.multi-blocks") @@ -68,10 +68,10 @@ local baseWidth = 40 local baseHeight = 10 local states = { - {state = "ON", color = Colors.workingColor}, - {state = "IDLE", color = Colors.idleColor}, - {state = "OFF", color = Colors.offColor}, - {state = "BROKEN", color = Colors.errorColor} + {name = "ON", color = Colors.workingColor}, + {name = "IDLE", color = Colors.idleColor}, + {name = "OFF", color = Colors.offColor}, + {name = "BROKEN", color = Colors.errorColor} } local fakeNames = { @@ -87,9 +87,11 @@ local fakeNames = { "Implosion Compressor" } -local function updateMachineWidget(self) +local machineWidget = {} + +function machineWidget:update() local breakWidget = math.random(10000) > 9999 - if breakWidget and self.type ~= "power" and self.state ~= states[3] then + if breakWidget and self.state ~= states[3] then self.state = states[4] end if self.state == states[1] then @@ -113,22 +115,28 @@ local function updateMachineWidget(self) end end -local function machineOnClick(self) +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 -local function fakewidget() +function machineWidget:getMiddleString() +end + +function machineWidget.fake() local state = states[math.random(4)] return { name = fakeNames[math.random(10)] .. " " .. math.floor(math.random(3)), @@ -136,30 +144,49 @@ local function fakewidget() progress = 0, maxProgress = state ~= states[3] and state ~= states[4] and math.random(500) or 0, type = "machine", - update = updateMachineWidget, - onClick = machineOnClick + update = machineWidget.update, + onClick = machineWidget.onClick, + getMiddleString = machineWidget.getMiddleString } end -local widgets = {} +local powerWidget = {} -for i = 1, 9 do - table.insert(widgets, fakewidget()) +function powerWidget:update() + self.progress = self.progress + self.dProgress end -table.insert( - widgets, - { + +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", - update = updateMachineWidget, - onClick = function() - end + 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( @@ -233,12 +260,13 @@ local function drawWidget(index, widget) 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.state) + 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 - if widget.middleInfo then - DoubleBuffer.drawText(x + 3 + 3 + Unicode.len("IDLE"), y + height - 3, Colors.textColor, widget.middleInfo) + 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, From e083b3f2ad87754b10a33b38289908bfbd074bcb Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Sun, 10 Jan 2021 18:58:02 -0300 Subject: [PATCH 12/22] 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 = {} From aea2f475e5d8449a34f9e16104a9bf3d3f151c1f Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Mon, 11 Jan 2021 12:43:55 -0300 Subject: [PATCH 13/22] Refactoring widget Using a fake method to create fake widgets Using constants from constants file Creating a base widget --- Programs/monitor-system/api/gui/constants.lua | 6 + Programs/monitor-system/api/gui/widget.lua | 107 +++++++++++------- 2 files changed, 70 insertions(+), 43 deletions(-) create mode 100644 Programs/monitor-system/api/gui/constants.lua diff --git a/Programs/monitor-system/api/gui/constants.lua b/Programs/monitor-system/api/gui/constants.lua new file mode 100644 index 0000000..b0c6562 --- /dev/null +++ b/Programs/monitor-system/api/gui/constants.lua @@ -0,0 +1,6 @@ +local constants = {} + +constants.baseWidth = 40 +constants.baseHeight = 10 + +return constants diff --git a/Programs/monitor-system/api/gui/widget.lua b/Programs/monitor-system/api/gui/widget.lua index dfa558d..a0c8e83 100644 --- a/Programs/monitor-system/api/gui/widget.lua +++ b/Programs/monitor-system/api/gui/widget.lua @@ -2,18 +2,9 @@ Component = require("component") Unicode = require("unicode") Colors = require("graphics.colors") DoubleBuffer = require("graphics.doubleBuffering") +Constants = require("api.gui.constants") -local widget = { - 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 "" --- end +local widget = {} local states = { {name = "ON", color = Colors.workingColor}, @@ -30,24 +21,19 @@ local function drawProgress(x, y, width, height, progress, maxProgress, color) 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.drawRectangle(x + 6, y / 2 + 1, 2, 1, Colors.machineBackground, Colors.machineBackground, "█") 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.drawRectangle(x + width - 6, (y + height) / 2, 2, 1, Colors.machineBackground, Colors.machineBackground, "█") 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) - +function widget.drawBaseWidget(x, y, scale, title) + local width = Constants.baseWidth * scale + local height = Constants.baseHeight DoubleBuffer.drawRectangle( x + 1, y + 1, @@ -57,12 +43,26 @@ local function draw(self, index) Colors.machineBackground, "█" ) - drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.progressBackground) + DoubleBuffer.drawFrame(x + 1, y + 1, width - 1, height - 1, Colors.labelColor) 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) + DoubleBuffer.drawText(x + (width - Unicode.len(title)) / 2, y + 3, Colors.labelColor, title) +end +local function draw(self, index) + if self.type == "power" then + index = 10 + end + local scale = self.scale or 1 + local width = Constants.baseWidth * scale + local height = Constants.baseHeight + local x = Constants.baseWidth + Constants.baseWidth * ((index - 1) % 3) + local y = height * math.ceil((index) / 3) + + widget.drawBaseWidget(x, y, scale, self.name) + + drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.progressBackground) 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) + DoubleBuffer.drawText(x + 4, 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 @@ -71,7 +71,7 @@ local function draw(self, index) 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, + x + width - Unicode.len(self.progress .. "/" .. self.maxProgress .. " s") - 2, y + height - 3, Colors.accentA, self.progress .. "/" .. self.maxProgress .. " s" @@ -79,7 +79,9 @@ local function draw(self, index) end end -local fakeNames = { +local fake = {} + +fake.names = { "Cleanroom", "Electric Blast Furnace", "Miner", @@ -92,9 +94,9 @@ local fakeNames = { "Implosion Compressor" } -widget.machineWidget = {} +fake.machineWidget = {} -function widget.machineWidget:update() +function fake.machineWidget:update() local breakWidget = math.random(10000) > 9999 if breakWidget and self.state ~= states[3] then self.state = states[4] @@ -120,7 +122,7 @@ function widget.machineWidget:update() end end -function widget.machineWidget:onClick() +function fake.machineWidget:onClick() if self.state == states[1] or self.state == states[2] then self.state = states[3] elseif self.state == states[3] then @@ -138,41 +140,41 @@ function widget.machineWidget:onClick() end end -function widget.machineWidget:getMiddleString() +function fake.machineWidget:getMiddleString() end -function widget.machineWidget.fake() +function fake.machineWidget.create() local state = states[math.random(4)] return { - name = fakeNames[math.random(10)] .. " " .. math.floor(math.random(3)), + name = fake.names[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, + update = fake.machineWidget.update, + onClick = fake.machineWidget.onClick, + getMiddleString = fake.machineWidget.getMiddleString, draw = draw } end -widget.powerWidget = {} +fake.powerWidget = {} -function widget.powerWidget:update() +function fake.powerWidget:update() self.progress = self.progress + self.dProgress end -function widget.powerWidget:onClick() +function fake.powerWidget:onClick() self.dProgress = -self.dProgress end -function widget.powerWidget:getMiddleString() +function fake.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() +function fake.powerWidget.create() return { name = "Power", state = states[1], @@ -181,11 +183,30 @@ function widget.powerWidget.fake() scale = 2, type = "power", dProgress = 1, - update = widget.powerWidget.update, - onClick = widget.powerWidget.onClick, - getMiddleString = widget.powerWidget.getMiddleString, + update = fake.powerWidget.update, + onClick = fake.powerWidget.onClick, + getMiddleString = fake.powerWidget.getMiddleString, draw = draw } end +function widget.fakeWidgets() + local fakeWidgets = {} + + for _ = 1, 9 do + table.insert(fakeWidgets, fake.machineWidget.create()) + end + + return fakeWidgets +end + +function widget.fakePowerWidget() + local fakePowerWidgets = {} + + table.insert(fakePowerWidgets, fake.powerWidget.create()) + fakePowerWidgets[11] = fakePowerWidgets[10] + + return fakePowerWidgets +end + return widget From 1720f162e437d79bdaf025bd0eac1c66c89e8d8c Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Mon, 11 Jan 2021 13:03:50 -0300 Subject: [PATCH 14/22] Adding titles to pages --- Programs/monitor-system/api/gui/page/glasses.lua | 4 ++++ Programs/monitor-system/api/gui/page/help.lua | 4 ++++ Programs/monitor-system/api/gui/page/notifications.lua | 4 ++++ Programs/monitor-system/api/gui/page/stock.lua | 4 ++++ Programs/monitor-system/api/gui/page/widgets.lua | 4 ++++ 5 files changed, 20 insertions(+) diff --git a/Programs/monitor-system/api/gui/page/glasses.lua b/Programs/monitor-system/api/gui/page/glasses.lua index 62d6844..a2032da 100644 --- a/Programs/monitor-system/api/gui/page/glasses.lua +++ b/Programs/monitor-system/api/gui/page/glasses.lua @@ -5,3 +5,7 @@ |sto| w | w | w | |not| power |b|f| --]] + +return { + title = "Glasses" +} diff --git a/Programs/monitor-system/api/gui/page/help.lua b/Programs/monitor-system/api/gui/page/help.lua index 78bab6d..220cf46 100644 --- a/Programs/monitor-system/api/gui/page/help.lua +++ b/Programs/monitor-system/api/gui/page/help.lua @@ -5,3 +5,7 @@ |sto|ev ti|ev ti| |not| power |b|f| --]] + +return { + title = "Help" +} diff --git a/Programs/monitor-system/api/gui/page/notifications.lua b/Programs/monitor-system/api/gui/page/notifications.lua index e00ed86..edfabbf 100644 --- a/Programs/monitor-system/api/gui/page/notifications.lua +++ b/Programs/monitor-system/api/gui/page/notifications.lua @@ -5,3 +5,7 @@ |sto|ev ti|ev ti| |ove| power |b|f| --]] + +return { + title ="Notifications" +} diff --git a/Programs/monitor-system/api/gui/page/stock.lua b/Programs/monitor-system/api/gui/page/stock.lua index 73a7143..24a8387 100644 --- a/Programs/monitor-system/api/gui/page/stock.lua +++ b/Programs/monitor-system/api/gui/page/stock.lua @@ -5,3 +5,7 @@ |ove|ev ti|ev ti| |not| power |b|f| --]] + +return { + title = "Stock" +} diff --git a/Programs/monitor-system/api/gui/page/widgets.lua b/Programs/monitor-system/api/gui/page/widgets.lua index e444c56..f02073f 100644 --- a/Programs/monitor-system/api/gui/page/widgets.lua +++ b/Programs/monitor-system/api/gui/page/widgets.lua @@ -5,3 +5,7 @@ |sto| w | w | w | |not| power |b|f| --]] + +return { + title = "Widgets" +} From f0ac9ac7b01c01070162d7a54eb1fc9f1050efe8 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Mon, 11 Jan 2021 13:04:52 -0300 Subject: [PATCH 15/22] Adding pages init to main init --- Programs/monitor-system/init.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Programs/monitor-system/init.lua b/Programs/monitor-system/init.lua index 303709e..5e186c3 100755 --- a/Programs/monitor-system/init.lua +++ b/Programs/monitor-system/init.lua @@ -1,7 +1,7 @@ -- Import section -Event = require("event") -Colors = require("graphics.colors") -Widget = require("api.gui.widget") +-- 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") From 1c90d83f88b69d2578494613b40bab050b22b429 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Mon, 11 Jan 2021 13:08:06 -0300 Subject: [PATCH 16/22] Adding panel elements Improving touch envent listener Using baseWidget method Creating page fake method --- Libraries/graphics/doubleBuffering.lua | 4 +- Programs/monitor-system/api/gui/page/init.lua | 139 +++++++++++------- 2 files changed, 88 insertions(+), 55 deletions(-) diff --git a/Libraries/graphics/doubleBuffering.lua b/Libraries/graphics/doubleBuffering.lua index 8d3f219..933f6d1 100644 --- a/Libraries/graphics/doubleBuffering.lua +++ b/Libraries/graphics/doubleBuffering.lua @@ -458,8 +458,8 @@ end local function drawFrame(x, y, width, height, color) local stringUp, stringDown, x2 = - "┌" .. string.rep("─", width - 2) .. "┐", - "└" .. string.rep("─", width - 2) .. "┘", + "╭" .. string.rep("─", width - 2) .. "╮", + "╰" .. string.rep("─", width - 2) .. "╯", x + width - 1 drawText(x, y, color, stringUp) diff --git a/Programs/monitor-system/api/gui/page/init.lua b/Programs/monitor-system/api/gui/page/init.lua index 3f3a48c..1822c4a 100644 --- a/Programs/monitor-system/api/gui/page/init.lua +++ b/Programs/monitor-system/api/gui/page/init.lua @@ -1,21 +1,23 @@ -- Import section +Unicode = require("unicode") +Event = require("event") DoubleBuffer = require("graphics.doubleBuffering") Colors = require("graphics.colors") -Unicode = require("unicode") Widget = require("api.gui.widget") +Constants = require("api.gui.constants") -- -- 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 +-- Screen should be 8 x 5 blocks. +-- That way, each block should have a resolution of 20 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) +---- Side panel on the left With a width of 40 pixels (panel) +---- 2 buttons for page navigation (b, f) +------- Each with a width of 20 pixels +---- 1 Power widget on the bottom, with a width of 80 pixels (power) ---- 9 Regular widgets on the right, in a 3 x 3 grid (w) ------- Each one with a width of 20 pixels +------ Each one with a width of 40 pixels --[[ | p | title | | a | w | w | w | @@ -23,74 +25,105 @@ Widget = require("api.gui.widget") | e | w | w | w | | l | power |b|f| --]] +local pages = { + glasses = require("api.gui.page.glasses"), + widgets = require("api.gui.page.widgets"), + help = require("api.gui.page.help"), + stock = require("api.gui.page.stock"), + notifications = require("api.gui.page.notifications"), + overview = require("api.gui.page.overview") +} +pages[1] = pages.glasses +pages[2] = pages.widgets +pages[3] = pages.help +pages[4] = pages.stock +pages[5] = pages.notifications +pages[6] = pages.overview + local page = {} -local pages = { - 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") +local elements = { + machineWidgets = {}, + powerWidgets = {}, + panelSections = {}, + navigationButtons = {} } -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] + local xContribution = x / Constants.baseWidth + local yContribution = 4 * math.floor(y / Constants.baseHeight) + local screenIndex = 1 + (math.floor(2 * (xContribution + yContribution))) / 2 - widget:onClick() + local selected = elements[screenIndex] or elements[screenIndex - 0.5] + selected:onClick() end ) local function drawTitle(title) - local x = Widget.baseWidth + local x = Constants.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) + Widget.drawBaseWidget(x, y, scale, title) end -function page.create(page) - drawTitle(page.title) +local function drawPanelSection(index, title) + local x = 0 + local y = (index - 1) * Constants.baseHeight + local scale = 1 + Widget.drawBaseWidget(x, y, scale, title) +end + +function page.create(element) + drawTitle(element.title) + + local panelIndex = 1 + for _, pg in pairs(pages) do + if pg ~= element then + elements.panelSections[panelIndex] = pg + drawPanelSection(panelIndex, pg.title) + panelIndex = panelIndex + 1 + end + end + + elements[6] = elements.machineWidgets[1] + elements[7] = elements.machineWidgets[2] + elements[8] = elements.machineWidgets[3] + elements[10] = elements.machineWidgets[4] + elements[11] = elements.machineWidgets[5] + elements[12] = elements.machineWidgets[6] + elements[14] = elements.machineWidgets[7] + elements[15] = elements.machineWidgets[8] + elements[16] = elements.machineWidgets[9] + + elements[18] = elements.powerWidgets[1] + elements[19] = elements.powerWidgets[1] + + elements[1] = elements.panelSections[1] + elements[5] = elements.panelSections[2] + elements[9] = elements.panelSections[3] + elements[13] = elements.panelSections[3] + elements[17] = elements.panelSections[5] + + elements[20] = elements.navigationButtons[1] + elements[20.5] = elements.navigationButtons[2] end 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] - + elements.machineWidgets = Widget.fakeWidgets() + elements.powerWidgets = Widget.fakePowerWidget() page.create(pages.overview) end function page.update() - for index, widget in ipairs(widgets) do - widget:update() - widget:draw(index) + for index, machineWidget in ipairs(elements.machineWidgets) do + machineWidget:update() + machineWidget:draw(index) + end + for index, powerWidget in ipairs(elements.powerWidgets) do + powerWidget:update() + powerWidget:draw(index) end DoubleBuffer.drawChanges() end From 9e49d86bd96e2afff36443298a02f39e455fece1 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Mon, 11 Jan 2021 13:44:48 -0300 Subject: [PATCH 17/22] Adding reboot button on top right Fixing drawBaseWidget bug --- Programs/monitor-system/api/gui/page/init.lua | 11 ++++++++--- Programs/monitor-system/api/gui/widget.lua | 6 +++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Programs/monitor-system/api/gui/page/init.lua b/Programs/monitor-system/api/gui/page/init.lua index 1822c4a..0ef136e 100644 --- a/Programs/monitor-system/api/gui/page/init.lua +++ b/Programs/monitor-system/api/gui/page/init.lua @@ -1,10 +1,11 @@ -- Import section +Computer = require("computer") Unicode = require("unicode") Event = require("event") DoubleBuffer = require("graphics.doubleBuffering") +Constants = require("api.gui.constants") Colors = require("graphics.colors") Widget = require("api.gui.widget") -Constants = require("api.gui.constants") -- -- GPU resolution should be 160 x 50. @@ -79,14 +80,18 @@ function page.create(element) drawTitle(element.title) local panelIndex = 1 - for _, pg in pairs(pages) do + for _, pg in ipairs(pages) do if pg ~= element then - elements.panelSections[panelIndex] = pg + elements.panelSections[panelIndex] = pg.title drawPanelSection(panelIndex, pg.title) panelIndex = panelIndex + 1 end end + elements[4.5] = {onClick = function() + Computer.shutdown(true) + end} + elements[6] = elements.machineWidgets[1] elements[7] = elements.machineWidgets[2] elements[8] = elements.machineWidgets[3] diff --git a/Programs/monitor-system/api/gui/widget.lua b/Programs/monitor-system/api/gui/widget.lua index a0c8e83..36fd601 100644 --- a/Programs/monitor-system/api/gui/widget.lua +++ b/Programs/monitor-system/api/gui/widget.lua @@ -1,8 +1,8 @@ Component = require("component") Unicode = require("unicode") -Colors = require("graphics.colors") DoubleBuffer = require("graphics.doubleBuffering") Constants = require("api.gui.constants") +Colors = require("graphics.colors") local widget = {} @@ -45,7 +45,7 @@ function widget.drawBaseWidget(x, y, scale, title) ) DoubleBuffer.drawFrame(x + 1, y + 1, width - 1, height - 1, Colors.labelColor) DoubleBuffer.drawLine(x + 3, y + 5, x + width - 3, y + 5, Colors.machineBackground, Colors.textColor, "─") - DoubleBuffer.drawText(x + (width - Unicode.len(title)) / 2, y + 3, Colors.labelColor, title) + DoubleBuffer.drawText(x + math.floor((width - Unicode.len(title)) / 2), y + 3, Colors.labelColor, title) end local function draw(self, index) @@ -71,7 +71,7 @@ local function draw(self, index) 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") - 2, + x + width - Unicode.len(self.progress .. "/" .. self.maxProgress .. " s") - 3, y + height - 3, Colors.accentA, self.progress .. "/" .. self.maxProgress .. " s" From 6d4d97229bebce5780b0781cab97ad20df21c93c Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Mon, 11 Jan 2021 14:03:15 -0300 Subject: [PATCH 18/22] Adding panel buttons --- Programs/monitor-system/api/gui/page/init.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Programs/monitor-system/api/gui/page/init.lua b/Programs/monitor-system/api/gui/page/init.lua index 0ef136e..ab1bf95 100644 --- a/Programs/monitor-system/api/gui/page/init.lua +++ b/Programs/monitor-system/api/gui/page/init.lua @@ -82,15 +82,22 @@ function page.create(element) local panelIndex = 1 for _, pg in ipairs(pages) do if pg ~= element then - elements.panelSections[panelIndex] = pg.title + elements.panelSections[panelIndex] = { + title = pg.title, + onClick = function() + page.create(pg) + end + } drawPanelSection(panelIndex, pg.title) panelIndex = panelIndex + 1 end end - elements[4.5] = {onClick = function() + elements[4.5] = { + onClick = function() Computer.shutdown(true) - end} + end + } elements[6] = elements.machineWidgets[1] elements[7] = elements.machineWidgets[2] @@ -108,7 +115,7 @@ function page.create(element) elements[1] = elements.panelSections[1] elements[5] = elements.panelSections[2] elements[9] = elements.panelSections[3] - elements[13] = elements.panelSections[3] + elements[13] = elements.panelSections[4] elements[17] = elements.panelSections[5] elements[20] = elements.navigationButtons[1] From 90612b7ae5f412e34aade3d74cb6325153cce5e9 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Mon, 11 Jan 2021 16:17:27 -0300 Subject: [PATCH 19/22] Resizing panel buttons and title --- Programs/monitor-system/api/gui/page/init.lua | 16 +++++----- Programs/monitor-system/api/gui/widget.lua | 29 ++++++++++++++----- .../data/datasource/multi-block.lua | 5 ++++ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Programs/monitor-system/api/gui/page/init.lua b/Programs/monitor-system/api/gui/page/init.lua index ab1bf95..0c43dee 100644 --- a/Programs/monitor-system/api/gui/page/init.lua +++ b/Programs/monitor-system/api/gui/page/init.lua @@ -64,16 +64,18 @@ Event.listen( local function drawTitle(title) local x = Constants.baseWidth - local y = 0 - local scale = 3 - Widget.drawBaseWidget(x, y, scale, title) + local y = 1 + local width = 3 * Constants.baseWidth + local height = 0.8 * Constants.baseHeight + Widget.drawBaseWidget(x, y, width, height, title) end local function drawPanelSection(index, title) - local x = 0 - local y = (index - 1) * Constants.baseHeight - local scale = 1 - Widget.drawBaseWidget(x, y, scale, title) + local width = 0.6 * Constants.baseWidth + local height = 0.6 * Constants.baseHeight + local x = (Constants.baseWidth - width) / 2 + local y = (index - 1) * Constants.baseHeight + (Constants.baseHeight - height) / 2 + Widget.drawBaseWidget(x, y, width, height, title) end function page.create(element) diff --git a/Programs/monitor-system/api/gui/widget.lua b/Programs/monitor-system/api/gui/widget.lua index 36fd601..a598cdd 100644 --- a/Programs/monitor-system/api/gui/widget.lua +++ b/Programs/monitor-system/api/gui/widget.lua @@ -25,15 +25,21 @@ local function drawProgress(x, y, width, height, progress, maxProgress, color) 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.drawRectangle(x + width - 6, (y + height) / 2, 2, 1, Colors.machineBackground, Colors.machineBackground, "█") + DoubleBuffer.drawRectangle( + x + width - 6, + (y + height) / 2, + 2, + 1, + Colors.machineBackground, + Colors.machineBackground, + "█" + ) 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 -function widget.drawBaseWidget(x, y, scale, title) - local width = Constants.baseWidth * scale - local height = Constants.baseHeight +function widget.drawBaseWidget(x, y, width, height, title) DoubleBuffer.drawRectangle( x + 1, y + 1, @@ -43,9 +49,18 @@ function widget.drawBaseWidget(x, y, scale, title) Colors.machineBackground, "█" ) + DoubleBuffer.drawLine( + x + 3, + y + math.ceil(0.5 * height), + x + width - 3, + y + math.ceil(0.5 * height), + Colors.machineBackground, + Colors.textColor, + "─" + ) DoubleBuffer.drawFrame(x + 1, y + 1, width - 1, height - 1, Colors.labelColor) - DoubleBuffer.drawLine(x + 3, y + 5, x + width - 3, y + 5, Colors.machineBackground, Colors.textColor, "─") - DoubleBuffer.drawText(x + math.floor((width - Unicode.len(title)) / 2), y + 3, Colors.labelColor, title) + title = Unicode.len(title) < width - 8 and " " .. title .. " " or " " .. string.gsub(title, "%l*%s", "") .. " " + DoubleBuffer.drawText(x + math.floor((width - Unicode.len(title) + 1) / 2), y + 3, Colors.labelColor, title) end local function draw(self, index) @@ -58,7 +73,7 @@ local function draw(self, index) local x = Constants.baseWidth + Constants.baseWidth * ((index - 1) % 3) local y = height * math.ceil((index) / 3) - widget.drawBaseWidget(x, y, scale, self.name) + widget.drawBaseWidget(x, y, width, height, self.name) drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.progressBackground) drawProgress(x, 2 * y, width - 1, 2 * (height - 1), self.progress, self.maxProgress, Colors.barColor) diff --git a/Programs/monitor-system/data/datasource/multi-block.lua b/Programs/monitor-system/data/datasource/multi-block.lua index fad817c..bee6ef9 100755 --- a/Programs/monitor-system/data/datasource/multi-block.lua +++ b/Programs/monitor-system/data/datasource/multi-block.lua @@ -19,6 +19,11 @@ function MultiBlock:getNumberOfProblems() return Parser.parseProblems(sensorInformation[5]) end +function MultiBlock:getProgress() + local sensorInformation = self:getSensorInformation() + return Parser.parseProgress(sensorInformation[1]) +end + function MultiBlock:getEfficiencyPercentage() local sensorInformation = self:getSensorInformation() return Parser.parseEfficiency(sensorInformation[5]) From d5ccd4a86d6226957b642ad65431d1ad8ffcc3f2 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Mon, 11 Jan 2021 16:34:39 -0300 Subject: [PATCH 20/22] Rounding down divisions on panel buttons scaling --- Programs/monitor-system/api/gui/page/init.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Programs/monitor-system/api/gui/page/init.lua b/Programs/monitor-system/api/gui/page/init.lua index 0c43dee..9e0b8c3 100644 --- a/Programs/monitor-system/api/gui/page/init.lua +++ b/Programs/monitor-system/api/gui/page/init.lua @@ -66,15 +66,15 @@ local function drawTitle(title) local x = Constants.baseWidth local y = 1 local width = 3 * Constants.baseWidth - local height = 0.8 * Constants.baseHeight + local height = math.floor(0.8 * Constants.baseHeight) Widget.drawBaseWidget(x, y, width, height, title) end local function drawPanelSection(index, title) - local width = 0.6 * Constants.baseWidth - local height = 0.6 * Constants.baseHeight + local width = math.floor(0.6 * Constants.baseWidth) + local height = math.floor(0.6 * Constants.baseHeight) local x = (Constants.baseWidth - width) / 2 - local y = (index - 1) * Constants.baseHeight + (Constants.baseHeight - height) / 2 + local y = (index - 1) * Constants.baseHeight + math.floor((Constants.baseHeight - height) / 2) Widget.drawBaseWidget(x, y, width, height, title) end From 0656b92f77467bdf92e56b2f3fb5c78a489ce0b9 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Mon, 11 Jan 2021 16:50:06 -0300 Subject: [PATCH 21/22] Addind button layout --- Programs/monitor-system/api/gui/page/init.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Programs/monitor-system/api/gui/page/init.lua b/Programs/monitor-system/api/gui/page/init.lua index 9e0b8c3..f09e4f3 100644 --- a/Programs/monitor-system/api/gui/page/init.lua +++ b/Programs/monitor-system/api/gui/page/init.lua @@ -73,11 +73,21 @@ end local function drawPanelSection(index, title) local width = math.floor(0.6 * Constants.baseWidth) local height = math.floor(0.6 * Constants.baseHeight) - local x = (Constants.baseWidth - width) / 2 + local x = math.floor((Constants.baseWidth - width) / 2) local y = (index - 1) * Constants.baseHeight + math.floor((Constants.baseHeight - height) / 2) Widget.drawBaseWidget(x, y, width, height, title) end +local function drawNavigationButtons() + local width = math.floor(0.3 * Constants.baseWidth) + local height = math.floor(0.6 * Constants.baseHeight) + local x = math.floor(2.8 * Constants.baseWidth) + math.floor((Constants.baseWidth - width) / 2) + local y = 4 * Constants.baseHeight + math.floor((Constants.baseHeight - height) / 2) + Widget.drawBaseWidget(x, y, width, height, "<") + x = math.floor(3.2 * Constants.baseWidth) + math.floor((Constants.baseWidth - width) / 2) + Widget.drawBaseWidget(x, y, width, height, ">") +end + function page.create(element) drawTitle(element.title) @@ -95,6 +105,8 @@ function page.create(element) end end + drawNavigationButtons() + elements[4.5] = { onClick = function() Computer.shutdown(true) From 1965e38a5c0414493713a217e25542d496adaf78 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Minossi Date: Mon, 11 Jan 2021 20:48:16 -0300 Subject: [PATCH 22/22] Adding logic for navigation buttons --- Programs/monitor-system/api/gui/page/init.lua | 88 +++++++++++++++---- Programs/monitor-system/api/gui/widget.lua | 14 ++- 2 files changed, 84 insertions(+), 18 deletions(-) diff --git a/Programs/monitor-system/api/gui/page/init.lua b/Programs/monitor-system/api/gui/page/init.lua index f09e4f3..0d413b1 100644 --- a/Programs/monitor-system/api/gui/page/init.lua +++ b/Programs/monitor-system/api/gui/page/init.lua @@ -78,14 +78,39 @@ local function drawPanelSection(index, title) Widget.drawBaseWidget(x, y, width, height, title) end -local function drawNavigationButtons() +local function drawNavigationButton(self, index) local width = math.floor(0.3 * Constants.baseWidth) local height = math.floor(0.6 * Constants.baseHeight) - local x = math.floor(2.8 * Constants.baseWidth) + math.floor((Constants.baseWidth - width) / 2) + local x = math.floor((2.4 + 0.4 * index) * Constants.baseWidth) + math.floor((Constants.baseWidth - width) / 2) local y = 4 * Constants.baseHeight + math.floor((Constants.baseHeight - height) / 2) - Widget.drawBaseWidget(x, y, width, height, "<") - x = math.floor(3.2 * Constants.baseWidth) + math.floor((Constants.baseWidth - width) / 2) - Widget.drawBaseWidget(x, y, width, height, ">") + if self.active then + Widget.drawBaseWidget(x, y, width, height, self.title) + end +end + +local function clickNavigationButton(self) + if not self.active then + return + end + if self.title == "◀" then + elements.machineWidgets.active.index = elements.machineWidgets.active.index - 1 + else + elements.machineWidgets.active.index = elements.machineWidgets.active.index + 1 + end + for i = 1, 9 do + elements.machineWidgets.active[i] = elements.machineWidgets[9 * (elements.machineWidgets.active.index - 1) + i] + end + + elements[6] = elements.machineWidgets.active[1] + elements[7] = elements.machineWidgets.active[2] + elements[8] = elements.machineWidgets.active[3] + elements[10] = elements.machineWidgets.active[4] + elements[11] = elements.machineWidgets.active[5] + elements[12] = elements.machineWidgets.active[6] + elements[14] = elements.machineWidgets.active[7] + elements[15] = elements.machineWidgets.active[8] + elements[16] = elements.machineWidgets.active[9] + Widget.clear() end function page.create(element) @@ -105,7 +130,29 @@ function page.create(element) end end - drawNavigationButtons() + elements.machineWidgets.active = {} + elements.machineWidgets.active.index = 1 + for i = 1, 9 do + elements.machineWidgets.active[i] = elements.machineWidgets[9 * (elements.machineWidgets.active.index - 1) + i] + end + elements.navigationButtons[1] = { + title = "◀", + active = true, + update = function(self) + self.active = elements.machineWidgets[elements.machineWidgets.active.index * 9 - 10] ~= nil + end, + onClick = clickNavigationButton, + draw = drawNavigationButton + } + elements.navigationButtons[2] = { + title = "▶", + active = true, + update = function(self) + self.active = elements.machineWidgets[elements.machineWidgets.active.index * 9 + 1] ~= nil + end, + onClick = clickNavigationButton, + draw = drawNavigationButton + } elements[4.5] = { onClick = function() @@ -113,15 +160,15 @@ function page.create(element) end } - elements[6] = elements.machineWidgets[1] - elements[7] = elements.machineWidgets[2] - elements[8] = elements.machineWidgets[3] - elements[10] = elements.machineWidgets[4] - elements[11] = elements.machineWidgets[5] - elements[12] = elements.machineWidgets[6] - elements[14] = elements.machineWidgets[7] - elements[15] = elements.machineWidgets[8] - elements[16] = elements.machineWidgets[9] + elements[6] = elements.machineWidgets.active[1] + elements[7] = elements.machineWidgets.active[2] + elements[8] = elements.machineWidgets.active[3] + elements[10] = elements.machineWidgets.active[4] + elements[11] = elements.machineWidgets.active[5] + elements[12] = elements.machineWidgets.active[6] + elements[14] = elements.machineWidgets.active[7] + elements[15] = elements.machineWidgets.active[8] + elements[16] = elements.machineWidgets.active[9] elements[18] = elements.powerWidgets[1] elements[19] = elements.powerWidgets[1] @@ -143,14 +190,21 @@ function page.fake() end function page.update() - for index, machineWidget in ipairs(elements.machineWidgets) do + for _, machineWidget in ipairs(elements.machineWidgets) do machineWidget:update() - machineWidget:draw(index) + end + for index, activeMachineWidget in ipairs(elements.machineWidgets.active) do + activeMachineWidget.draw(activeMachineWidget, index) end for index, powerWidget in ipairs(elements.powerWidgets) do powerWidget:update() powerWidget:draw(index) end + for index, navigationButton in ipairs(elements.navigationButtons) do + navigationButton:update() + navigationButton:draw(index) + end + DoubleBuffer.drawChanges() end diff --git a/Programs/monitor-system/api/gui/widget.lua b/Programs/monitor-system/api/gui/widget.lua index a598cdd..19f38e8 100644 --- a/Programs/monitor-system/api/gui/widget.lua +++ b/Programs/monitor-system/api/gui/widget.lua @@ -94,6 +94,18 @@ local function draw(self, index) end end +function widget.clear() + DoubleBuffer.drawRectangle( + Constants.baseWidth, + Constants.baseHeight, + 3 * Constants.baseWidth, + 4 * Constants.baseHeight, + Colors.background, + Colors.background, + "█" + ) +end + local fake = {} fake.names = { @@ -208,7 +220,7 @@ end function widget.fakeWidgets() local fakeWidgets = {} - for _ = 1, 9 do + for _ = 1, math.random(30) do table.insert(fakeWidgets, fake.machineWidget.create()) end