mirror of
https://github.com/S4mpsa/InfOS.git
synced 2025-08-04 02:16:05 -04:00
Refactoring widget
Using a fake method to create fake widgets Using constants from constants file Creating a base widget
This commit is contained in:
parent
e083b3f2ad
commit
aea2f475e5
6
Programs/monitor-system/api/gui/constants.lua
Normal file
6
Programs/monitor-system/api/gui/constants.lua
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
local constants = {}
|
||||||
|
|
||||||
|
constants.baseWidth = 40
|
||||||
|
constants.baseHeight = 10
|
||||||
|
|
||||||
|
return constants
|
@ -2,18 +2,9 @@ Component = require("component")
|
|||||||
Unicode = require("unicode")
|
Unicode = require("unicode")
|
||||||
Colors = require("graphics.colors")
|
Colors = require("graphics.colors")
|
||||||
DoubleBuffer = require("graphics.doubleBuffering")
|
DoubleBuffer = require("graphics.doubleBuffering")
|
||||||
|
Constants = require("api.gui.constants")
|
||||||
|
|
||||||
local widget = {
|
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 states = {
|
local states = {
|
||||||
{name = "ON", color = Colors.workingColor},
|
{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),
|
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)
|
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 + 6 - lengths.first, y + 1, lengths.first, 1, color)
|
||||||
DoubleBuffer.drawSemiPixelRectangle(x + 1, y + 2, 1, lengths.second, color)
|
DoubleBuffer.drawSemiPixelRectangle(x + 1, y + 2, 1, lengths.second, color)
|
||||||
DoubleBuffer.drawSemiPixelRectangle(x + 1, y + height, lengths.third, 1, 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 - 4, y + height, lengths.first, 1, color)
|
||||||
DoubleBuffer.drawSemiPixelRectangle(x + width, y + height - lengths.second, 1, lengths.second, 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)
|
DoubleBuffer.drawSemiPixelRectangle(x + 1 + width - lengths.third, y + 1, lengths.third, 1, color)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function draw(self, index)
|
function widget.drawBaseWidget(x, y, scale, title)
|
||||||
if index > 10 then
|
local width = Constants.baseWidth * scale
|
||||||
return
|
local height = Constants.baseHeight
|
||||||
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(
|
DoubleBuffer.drawRectangle(
|
||||||
x + 1,
|
x + 1,
|
||||||
y + 1,
|
y + 1,
|
||||||
@ -57,12 +43,26 @@ local function draw(self, index)
|
|||||||
Colors.machineBackground,
|
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.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)
|
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
|
if self.state == states[4] then
|
||||||
drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.errorColor)
|
drawProgress(x, 2 * y, width - 1, 2 * (height - 1), 1, 1, Colors.errorColor)
|
||||||
else
|
else
|
||||||
@ -71,7 +71,7 @@ local function draw(self, index)
|
|||||||
DoubleBuffer.drawText(x + 3 + 3 + Unicode.len("IDLE"), y + height - 3, Colors.textColor, middleInfo)
|
DoubleBuffer.drawText(x + 3 + 3 + Unicode.len("IDLE"), y + height - 3, Colors.textColor, middleInfo)
|
||||||
end
|
end
|
||||||
DoubleBuffer.drawText(
|
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,
|
y + height - 3,
|
||||||
Colors.accentA,
|
Colors.accentA,
|
||||||
self.progress .. "/" .. self.maxProgress .. " s"
|
self.progress .. "/" .. self.maxProgress .. " s"
|
||||||
@ -79,7 +79,9 @@ local function draw(self, index)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local fakeNames = {
|
local fake = {}
|
||||||
|
|
||||||
|
fake.names = {
|
||||||
"Cleanroom",
|
"Cleanroom",
|
||||||
"Electric Blast Furnace",
|
"Electric Blast Furnace",
|
||||||
"Miner",
|
"Miner",
|
||||||
@ -92,9 +94,9 @@ local fakeNames = {
|
|||||||
"Implosion Compressor"
|
"Implosion Compressor"
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.machineWidget = {}
|
fake.machineWidget = {}
|
||||||
|
|
||||||
function widget.machineWidget:update()
|
function fake.machineWidget:update()
|
||||||
local breakWidget = math.random(10000) > 9999
|
local breakWidget = math.random(10000) > 9999
|
||||||
if breakWidget and self.state ~= states[3] then
|
if breakWidget and self.state ~= states[3] then
|
||||||
self.state = states[4]
|
self.state = states[4]
|
||||||
@ -120,7 +122,7 @@ function widget.machineWidget:update()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function widget.machineWidget:onClick()
|
function fake.machineWidget:onClick()
|
||||||
if self.state == states[1] or self.state == states[2] then
|
if self.state == states[1] or self.state == states[2] then
|
||||||
self.state = states[3]
|
self.state = states[3]
|
||||||
elseif self.state == states[3] then
|
elseif self.state == states[3] then
|
||||||
@ -138,41 +140,41 @@ function widget.machineWidget:onClick()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function widget.machineWidget:getMiddleString()
|
function fake.machineWidget:getMiddleString()
|
||||||
end
|
end
|
||||||
|
|
||||||
function widget.machineWidget.fake()
|
function fake.machineWidget.create()
|
||||||
local state = states[math.random(4)]
|
local state = states[math.random(4)]
|
||||||
return {
|
return {
|
||||||
name = fakeNames[math.random(10)] .. " " .. math.floor(math.random(3)),
|
name = fake.names[math.random(10)] .. " " .. math.floor(math.random(3)),
|
||||||
state = state,
|
state = state,
|
||||||
progress = 0,
|
progress = 0,
|
||||||
maxProgress = state ~= states[3] and state ~= states[4] and math.random(500) or 0,
|
maxProgress = state ~= states[3] and state ~= states[4] and math.random(500) or 0,
|
||||||
type = "machine",
|
type = "machine",
|
||||||
update = widget.machineWidget.update,
|
update = fake.machineWidget.update,
|
||||||
onClick = widget.machineWidget.onClick,
|
onClick = fake.machineWidget.onClick,
|
||||||
getMiddleString = widget.machineWidget.getMiddleString,
|
getMiddleString = fake.machineWidget.getMiddleString,
|
||||||
draw = draw
|
draw = draw
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
widget.powerWidget = {}
|
fake.powerWidget = {}
|
||||||
|
|
||||||
function widget.powerWidget:update()
|
function fake.powerWidget:update()
|
||||||
self.progress = self.progress + self.dProgress
|
self.progress = self.progress + self.dProgress
|
||||||
end
|
end
|
||||||
|
|
||||||
function widget.powerWidget:onClick()
|
function fake.powerWidget:onClick()
|
||||||
self.dProgress = -self.dProgress
|
self.dProgress = -self.dProgress
|
||||||
end
|
end
|
||||||
|
|
||||||
function widget.powerWidget:getMiddleString()
|
function fake.powerWidget:getMiddleString()
|
||||||
local remaining = self.dProgress > 0 and self.maxProgress - self.progress or -self.progress
|
local remaining = self.dProgress > 0 and self.maxProgress - self.progress or -self.progress
|
||||||
return (self.dProgress > 0 and "+" or "") ..
|
return (self.dProgress > 0 and "+" or "") ..
|
||||||
self.dProgress .. "EU/s. " .. (self.dProgress > 0 and "Full in: " or "Empty in: ") .. remaining / self.dProgress
|
self.dProgress .. "EU/s. " .. (self.dProgress > 0 and "Full in: " or "Empty in: ") .. remaining / self.dProgress
|
||||||
end
|
end
|
||||||
|
|
||||||
function widget.powerWidget.fake()
|
function fake.powerWidget.create()
|
||||||
return {
|
return {
|
||||||
name = "Power",
|
name = "Power",
|
||||||
state = states[1],
|
state = states[1],
|
||||||
@ -181,11 +183,30 @@ function widget.powerWidget.fake()
|
|||||||
scale = 2,
|
scale = 2,
|
||||||
type = "power",
|
type = "power",
|
||||||
dProgress = 1,
|
dProgress = 1,
|
||||||
update = widget.powerWidget.update,
|
update = fake.powerWidget.update,
|
||||||
onClick = widget.powerWidget.onClick,
|
onClick = fake.powerWidget.onClick,
|
||||||
getMiddleString = widget.powerWidget.getMiddleString,
|
getMiddleString = fake.powerWidget.getMiddleString,
|
||||||
draw = draw
|
draw = draw
|
||||||
}
|
}
|
||||||
end
|
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
|
return widget
|
||||||
|
Loading…
x
Reference in New Issue
Block a user