Refactoring pages widget logic to be on widget file

Moving page logic to  be on page file
This commit is contained in:
Gabriel Moreira Minossi 2021-01-10 18:58:02 -03:00
parent 2166eaf156
commit e083b3f2ad
6 changed files with 294 additions and 301 deletions

View File

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

View File

@ -1,54 +1,98 @@
-- Import section -- Import section
local doubleBuffer = require("graphics.doubleBuffering") DoubleBuffer = require("graphics.doubleBuffering")
Colors = require("graphics.colors") Colors = require("graphics.colors")
Unicode = require("unicode") 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 = { local pages = {
overview = require("Overview"), overview = require("api.gui.page.overview"),
notifications = require("notifications"), notifications = require("api.gui.page.notifications"),
stock = require("stock"), stock = require("api.gui.page.stock"),
help = require("help"), help = require("api.gui.page.help"),
widgets = require("widgets"), widgets = require("api.gui.page.widgets"),
glasses = require("glasses") glasses = require("api.gui.page.glasses")
} }
function pages:draw(component, index) local widgets = {}
if index < 10 then
local x = 40 + 40 * ((index - 1) % 3) Event.listen(
local y = 10 * math.ceil((index) / 3) "touch",
doubleBuffer.drawRectangle(x + 1, y + 1, 38, 8, Colors.machineBackground, Colors.machineBackground, "") function(_, _, x, y)
doubleBuffer.drawFrame(x + 1, y + 1, 38, 8, Colors.labelColor) local index =
doubleBuffer.drawLine(x + 4, y + 4, x + 16, y + 4, Colors.machineBackground, Colors.mainColor, "_") 1 +
doubleBuffer.drawText(x + 1, y + 1, Colors.labelColor, component.name) (math.floor(
doubleBuffer.drawText( 2 *
x + 4, ((x - Widget.baseWidth) / Widget.baseWidth +
y + 4, 3 * math.floor((y - Widget.baseHeight) / Widget.baseHeight))
component.working and Colors.workingColor or Colors.errorColor, )) /
component.leftInfo 2
) local widget = widgets[index] or widgets[index - 0.5]
if component.middleInfo then
doubleBuffer.drawText( widget:onClick()
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 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 end
function pages:render() function page.create(page)
drawTitle(page.title)
end 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

View File

@ -5,3 +5,7 @@
|sto| w | w | w | |sto| w | w | w |
|not| power |b|f| |not| power |b|f|
--]] --]]
return {
title = "Overview"
}

View File

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

View File

@ -1,46 +1,191 @@
Component = require("component") Component = require("component")
GPU = Component.gpu Unicode = require("unicode")
Screen = Component.screen Colors = require("graphics.colors")
DoubleBuffer = require("graphics.doubleBuffering")
-- 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 = { local widget = {
name = "", baseHeight = 10,
leftString = "", baseWidth = 40
middleString = "",
rightString = "",
height = 10,
width = 20,
} }
function widget.create(name, leftString, middleString, rightString, screenIndex) -- function widget.create(name, leftString, middleString, rightString, screenIndex)
widget.name = name or "Unused" -- widget.name = name or "Unused"
widget.leftString = leftString or "" -- widget.leftString = leftString or ""
widget.middleString = middleString or "" -- widget.middleString = middleString or ""
widget.rightString = rightString 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 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

View File

@ -1,8 +1,8 @@
-- Import section -- Import section
Colors = require("graphics.colors")
Unicode = require("unicode")
DoubleBuffer = require("graphics.doubleBuffering")
Event = require("event") Event = require("event")
Colors = require("graphics.colors")
Widget = require("api.gui.widget")
Page = require("api.gui.page")
-- Graphics = require("graphics.graphics") -- Graphics = require("graphics.graphics")
-- MultiBlock = require("data.datasource.multi-block") -- MultiBlock = require("data.datasource.multi-block")
-- SingleBlock = require("data.datasource.single-block") -- SingleBlock = require("data.datasource.single-block")
@ -64,228 +64,14 @@ end
require("api.sound.zelda-secret")() require("api.sound.zelda-secret")()
--]] --]]
local baseWidth = 40
local baseHeight = 10
local states = { Page.fake()
{name = "ON", color = Colors.workingColor},
{name = "IDLE", color = Colors.idleColor},
{name = "OFF", color = Colors.offColor},
{name = "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 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 while true do
for index, widget in ipairs(widgets) do Page.update()
widget:update()
drawWidget(index, widget)
end
DoubleBuffer.drawChanges()
os.sleep(0) os.sleep(0)
end end
--[[ --[[
Page = require("api.gui.page") Page = require("api.gui.page")
Notifications = {} Notifications = {}