Skip to content

Commit

Permalink
Updated ProgressBarElement and profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
scfmod committed Dec 20, 2023
1 parent 5c0eb9d commit a7c9c89
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 34 deletions.
4 changes: 2 additions & 2 deletions scripts/gui/InGameMenuConstructionsFrame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ function InGameMenuConstructionsFrame:populateCellForItemInSection(list, section
cell:getAttribute('progressBar'):setPrimary(1 / input.amount * input.deliveredAmount)
cell:getAttribute('progressBar'):setSecondary(1 / input.amount * input.processedAmount)

cell:setDisabled(false)
cell:getAttribute('progressBar'):setDisabled(false)
else
cell:getAttribute('fillLevel'):setText(ConstructionUtils.formatNumber(input.amount))

Expand All @@ -370,7 +370,7 @@ function InGameMenuConstructionsFrame:populateCellForItemInSection(list, section
cell:getAttribute('progressBar'):setSecondary(0)
end

cell:setDisabled(true)
cell:getAttribute('progressBar'):setDisabled(true)
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion scripts/gui/dialogs/ConstructionInputsDialog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,15 @@ function ConstructionInputsDialog:populateCellForItemInSection(list, sectionInde
local input = self.inputs[index]

if input ~= nil then
---@type ProgressBarElement
local progressBar = cell:getAttribute('progressBar')

progressBar:setPrimary(1 / input.totalAmount * input.deliveredAmount)

cell:setDisabled(progressBar.value == 1)

cell:getAttribute('title'):setText(input.title)
cell:getAttribute('icon'):setImageFilename(input.icon)
cell:getAttribute('progressBar'):setPrimary(1 / input.totalAmount * input.deliveredAmount)
cell:getAttribute('progressText'):setText(('%s / %s'):format(ConstructionUtils.formatNumber(input.deliveredAmount), ConstructionUtils.formatNumber(input.totalAmount)))
end
end
Expand Down
119 changes: 105 additions & 14 deletions scripts/gui/elements/ProgressBarElement.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@
---@field fullWidth number
---@field fullHeight number
---@field active boolean
---@field value number
---
---@field primaryElement BitmapElement
---@field secondaryElement BitmapElement
---
---@field primaryColor number[]
---@field primaryDisabledColor number[]
---
---@field secondaryColor number[]
---@field secondaryDisabledColor number[]
---
---@field backgroundColor number[]
---@field backgroundDisabledColor number[]
---
---@field frameColor number[]
---@field frameDisabledColor number[]
---@field frameInactiveColor number[]
---
---@field superClass fun(): BitmapElement
ProgressBarElement = {}
Expand All @@ -20,12 +29,21 @@ function ProgressBarElement.new(target, custom_mt)
---@type ProgressBarElement
local self = BitmapElement.new(target, custom_mt or ProgressBarElement_mt)

self.value = 0
self.active = true

self.primaryColor = { 1, 1, 1, 1 }
self.primaryDisabledColor = { 1, 1, 1, 0.1 }

self.secondaryColor = { 0, 0, 1, 1 }
self.secondaryDisabledColor = { 0, 0, 1, 0.1 }

self.frameColor = { 0, 0, 1, 1 }
self.frameDisabledColor = { 1, 0, 0, 1 }
self.frameInactiveColor = { 1, 0, 0, 1 }
self.frameDisabledColor = { 1, 0, 0, 0.1 }

self.backgroundColor = { 0, 0, 0, 0.75 }
self.backgroundDisabledColor = { 0, 0, 0, 0.15 }

return self
end
Expand All @@ -34,20 +52,34 @@ function ProgressBarElement:loadFromXML(xmlFile, key)
self:superClass().loadFromXML(self, xmlFile, key)

self.primaryColor = GuiUtils.getColorArray(getXMLString(xmlFile, key .. '#primaryColor'), self.primaryColor)
self.primaryDisabledColor = GuiUtils.getColorArray(getXMLString(xmlFile, key .. '#primaryDisabledColor'), self.primaryDisabledColor)

self.secondaryColor = GuiUtils.getColorArray(getXMLString(xmlFile, key .. '#secondaryColor'), self.secondaryColor)
self.secondaryDisabledColor = GuiUtils.getColorArray(getXMLString(xmlFile, key .. '#secondaryDisabledColor'), self.secondaryDisabledColor)

self.backgroundColor = GuiUtils.getColorArray(getXMLString(xmlFile, key .. '#backgroundColor'), self.backgroundColor)
self.backgroundDisabledColor = GuiUtils.getColorArray(getXMLString(xmlFile, key .. '#backgroundDisabledColor'), self.backgroundDisabledColor)

self.frameColor = GuiUtils.getColorArray(getXMLString(xmlFile, key .. '#frameColor'), self.frameColor)
self.frameDisabledColor = GuiUtils.getColorArray(getXMLString(xmlFile, key .. '#frameDisabledColor'), self.frameDisabledColor)
self.frameInactiveColor = GuiUtils.getColorArray(getXMLString(xmlFile, key .. '#frameInactiveColor'), self.frameInactiveColor)
end

function ProgressBarElement:loadProfile(profile, applyProfile)
self:superClass().loadProfile(self, profile, applyProfile)

self.primaryColor = GuiUtils.getColorArray(profile:getValue('primaryColor'), self.primaryColor)
self.primaryDisabledColor = GuiUtils.getColorArray(profile:getValue('primaryDisabledColor'), self.primaryDisabledColor)

self.secondaryColor = GuiUtils.getColorArray(profile:getValue('secondaryColor'), self.secondaryColor)
self.secondaryDisabledColor = GuiUtils.getColorArray(profile:getValue('secondaryDisabledColor'), self.secondaryDisabledColor)

self.backgroundColor = GuiUtils.getColorArray(profile:getValue('backgroundColor'), self.backgroundColor)
self.backgroundDisabledColor = GuiUtils.getColorArray(profile:getValue('backgroundDisabledColor'), self.backgroundDisabledColor)

self.frameColor = GuiUtils.getColorArray(profile:getValue('frameColor'), self.frameColor)
self.frameDisabledColor = GuiUtils.getColorArray(profile:getValue('frameDisabledColor'), self.frameDisabledColor)
self.frameInactiveColor = GuiUtils.getColorArray(profile:getValue('frameInactiveColor'), self.frameInactiveColor)
end

function ProgressBarElement:onGuiSetupFinished()
Expand All @@ -58,32 +90,35 @@ function ProgressBarElement:onGuiSetupFinished()

self:registerElements()

self:setBorderColor(self:getIsDisabled() and self.frameDisabledColor or self.frameColor)
self:updateBorderColor()
self:updateBackgroundColor()
self:updateElementColors()
end

function ProgressBarElement:registerElements()
self.primaryElement = self.elements[1]
self.secondaryElement = self.elements[2]

if self.primaryElement then
self.primaryElement:setImageColor(nil, unpack(self.primaryColor))
else
if not self.primaryElement then
Logging.warning('ProgressBarElement:registerElements() primaryElement not found')
end

if self.secondaryElement then
self.secondaryElement:setImageColor(nil, unpack(self.secondaryColor))
end
end

function ProgressBarElement:clone(parent, includeId, suppressOnCreate)
local clone = self:superClass().clone(self, parent, includeId, suppressOnCreate)

clone.primaryColor = table.copy(self.primaryColor)
clone.primaryDisabledColor = table.copy(self.primaryDisabledColor)

clone.secondaryColor = table.copy(self.secondaryColor)
clone.secondaryDisabledColor = table.copy(self.secondaryDisabledColor)

clone.backgroundColor = table.copy(self.backgroundColor)
clone.backgroundDisabledColor = table.copy(self.backgroundDisabledColor)

clone.frameColor = table.copy(self.frameColor)
clone.frameDisabledColor = table.copy(self.frameDisabledColor)
clone.frameInactiveColor = table.copy(self.frameInactiveColor)

clone:registerElements()

Expand All @@ -93,9 +128,60 @@ end
---@param disabled boolean
---@param doNotUpdateChildren boolean | nil
function ProgressBarElement:setDisabled(disabled, doNotUpdateChildren)
local previous = self.disabled

self:superClass().setDisabled(self, disabled, doNotUpdateChildren)

self:setBorderColor(disabled and self.frameDisabledColor or self.frameColor)
if disabled ~= previous then
self:updateBorderColor()
self:updateElementColors()
self:updateBackgroundColor()
end
end

---@param active boolean
function ProgressBarElement:setActive(active)
if self.active ~= active then
self.active = active

self:updateBorderColor()
end
end

function ProgressBarElement:updateBackgroundColor()
if self.disabled then
self:setImageColor(nil, unpack(self.backgroundDisabledColor))
else
self:setImageColor(nil, unpack(self.backgroundColor))
end
end

function ProgressBarElement:updateBorderColor()
if self.hasFrame then
if self.disabled then
self:setBorderColor(self.frameDisabledColor)
else
self:setBorderColor(self.active and self.frameColor or self.frameInactiveColor)
end
end
end

function ProgressBarElement:updateElementColors()
if self.disabled then
self:setElementColor(self.primaryElement, self.primaryDisabledColor)
self:setElementColor(self.secondaryElement, self.secondaryDisabledColor)
else
self:setElementColor(self.primaryElement, self.primaryColor)
self:setElementColor(self.secondaryElement, self.secondaryColor)
end
end

---@param element BitmapElement | nil
---@param color number[]
function ProgressBarElement:setElementColor(element, color)
if element ~= nil then
element:setImageColor(nil, unpack(color))
end
end

---@param color number[]
Expand All @@ -110,7 +196,8 @@ end

---@param element BitmapElement
---@param value number
function ProgressBarElement:updateElementSize(element, value)
---@param setAsValue boolean | nil
function ProgressBarElement:updateElementSize(element, value, setAsValue)
---@diagnostic disable-next-line: cast-local-type
value = MathUtil.round(value or 0, 4)

Expand All @@ -124,14 +211,18 @@ function ProgressBarElement:updateElementSize(element, value)
element:setSize(width * value, self.absSize[2])
element:setVisible(true)
end

if setAsValue then
---@diagnostic disable-next-line: assign-type-mismatch
self.value = value
end
end

---@param value number
function ProgressBarElement:setPrimary(value)
if self.primaryElement then
self:updateElementSize(self.primaryElement, value)

self:setDisabled(value <= 0)
self:updateElementSize(self.primaryElement, value, true)
self:setActive(value > 0)
end
end

Expand Down
2 changes: 1 addition & 1 deletion xml/gui/InGameMenuConstructionsFrame.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</GuiElement>

<GuiElement type="listItem" profile="construction_listSectionHeader" name="section">
<GuiElement type="text" profile="construction_listSectionHeaderText" name="title" />
<GuiElement type="text" profile="construction_inputListSectionHeaderText" name="title" />
</GuiElement>
</GuiElement>

Expand Down
24 changes: 23 additions & 1 deletion xml/gui/guiProfiles.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,44 @@
"value": "$ref_textDisabled"
},
// Progress
{
"name": "progressBackgroundColor",
"value": "0 0 0 0.5"
},
{
"name": "progressDisabledBackgroundColor",
"value": "0 0 0 0.15"
},
{
"name": "progressBorderColor",
"value": "$ref_mainColor"
},
{
"name": "progressDisabledBorderColor",
"value": "$ref_mainColor 0.25"
"value": "0 0 0 0.15"
},
{
"name": "progressInactiveBorderColor",
// "value": "0 0 0 0.25"
"value": "$ref_mainColor 0.5"
},
{
"name": "progressColorPrimary",
"value": "$ref_mainColor 0.25"
},
{
"name": "progressDisabledColorPrimary",
"value": "0.5 0.5 0.5 0.5"
},
{
"name": "progressColorSecondary",
"value": "$ref_mainColor"
},
{
"name": "progressDisabledColorSecondary",
// "value": "1 0.15 0 0.1"
"value": "0.5 0.5 0.5 0.5"
},
// HUD
{
"name": "hudBackgroundColor",
Expand Down
20 changes: 13 additions & 7 deletions xml/gui/guiProfiles.template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
<Value name="textMaxWidth" value="532px" />
<Value name="textMaxNumLines" value="2" />
<Value name="textSize" value="13px" />
<Value name="textColor" value="$_rgb(text, 0.85)" />
<Value name="textColor" value="$_rgb(text) 0.85" />
</Profile>

<!-- Dialog profiles -->
Expand Down Expand Up @@ -267,15 +267,19 @@
<Profile name="theme_progressBar" extends="baseReference">
<Value name="size" value="320px 8px" />

<Value name="imageColor" value="0 0 0 0.5" />
<Value name="backgroundColor" value="$_rgba(progressBackgroundColor)" />
<Value name="backgroundDisabledColor" value="$_rgba(progressDisabledBackgroundColor)" />

<Value name="hasFrame" value="true" />
<Value name="frameThickness" value="2dp 2dp 2dp 2dp" />
<Value name="frameColor" value="$_rgba(progressBorderColor)" />
<Value name="frameDisabledColor" value="$_rgba(progressDisabledBorderColor)" />
<Value name="frameInactiveColor" value="$_rgba(progressInactiveBorderColor)" />

<Value name="primaryColor" value="$_rgba(progressColorPrimary)" />
<Value name="primaryDisabledColor" value="$_rgba(progressDisabledColorPrimary)" />
<Value name="secondaryColor" value="$_rgba(progressColorSecondary)" />
<Value name="secondaryDisabledColor" value="$_rgba(progressDisabledColorSecondary)" />
</Profile>

<Profile name="theme_progressBarElement" extends="baseReference" with="anchorMiddleLeft">
Expand Down Expand Up @@ -351,11 +355,12 @@
<Value name="textBold" value="true" />
<Value name="textSize" value="20px" />
<Value name="textUpperCase" value="true" />
<Value name="textColor" value="$preset_colorHeaderText" />
<Value name="textColor" value="0.8 0.8 0.8 1" />
<Value name="textDisabledColor" value="0.17 0.2 0.25 1" />
</Profile>

<Profile name="construction_listSectionHeaderTextActive" extends="construction_listSectionHeaderText">
<Value name="textColor" value="$preset_colorGreen" />
<Profile name="construction_inputListSectionHeaderText" extends="construction_listSectionHeaderText">
<Value name="textUpperCase" value="false" />
</Profile>

<Profile name="construction_inputListLayout" extends="emptyPanel" with="anchorTopLeft">
Expand Down Expand Up @@ -618,6 +623,7 @@
<Value name="position" value="-20px -12px" />

<Value name="primaryColor" value="$_rgba(progressColorSecondary)" />
<Value name="primaryDisabledColor" value="$_rgba(progressDisabledColorSecondary)" />
</Profile>

<Profile name="constructionInputs_listVerticalSlider" extends="verticalListSliderRightDocked" with="anchorTopRight">
Expand Down Expand Up @@ -704,7 +710,7 @@
<Value name="position" value="-16px -9px" />

<Value name="frameColor" value="$_rgb(progressBorderColor) 0.6" />
<Value name="frameDisabledColor" value="$_rgb(progressBorderColor) 0.6" />
<Value name="frameInactiveColor" value="$_rgb(progressBorderColor) 0.6" />
</Profile>

<!--
Expand Down Expand Up @@ -756,7 +762,7 @@
<Value name="position" value="0 8px" />

<Value name="frameColor" value="$_rgb(progressBorderColor) 0.6" />
<Value name="frameDisabledColor" value="$_rgb(progressBorderColor) 0.6" />
<Value name="frameInactiveColor" value="$_rgb(progressBorderColor) 0.6" />
</Profile>

</GUIProfiles>
Loading

0 comments on commit a7c9c89

Please sign in to comment.