Refactoring widget

Using a fake method to create fake widgets
Using constants from constants file
Creating a base widget
This commit is contained in:
Gabriel Moreira Minossi 2021-01-11 12:43:55 -03:00
parent e083b3f2ad
commit aea2f475e5
2 changed files with 70 additions and 43 deletions

View File

@ -0,0 +1,6 @@
local constants = {}
constants.baseWidth = 40
constants.baseHeight = 10
return constants

View File

@ -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