Setting a Progress Bar Color from Lua

S3rgeus

Emperor
Joined
Apr 10, 2011
Messages
1,270
Location
London, United Kingdom
I'm trying to set a progress bar's color from Lua, and having a surprising amount of difficulty. The SetColor function on other UI controls (like labels) expects a table containing the RGBA values (with fields x,y,z,w) and an integer. This works fine, but the progress bar appears to be different. In XML it has an FGColor and a BGColor (so it makes sense that there's something else to call aside from SetColor) but I can't find out what is. (I only need to set the FGColor, if that particularly matters.)

Here's the code I'm working with:

Code:
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by ModBuddy on 3/2/2014 7:31:22 PM -->
<Context ColorSet="Beige_Black" Font="TwCenMT20" FontStyle="Shadow">
	<Box Style="BGBlock" ID="BGBlock" />

	<Instance Name="AjahInfluence">
		<Stack ID="AjahStack" Anchor="C,B" Size="20,200" StackGrowth="Top" Padding="0,12">
			<Label ID="AjahLabel" Anchor="C,B" String="TXT_KEY_WHITE_TOWER_BLUE_AJAH" />
			<Label ID="AjahPercent" Anchor="C,B" />
			<Bar ID="InfluenceBar" Anchor="C,B" Size="10,150" Direction="Up" FGColor="Culture,255" />
		</Stack>
	</Instance>

	<Grid Size="990,650" Anchor="C,C" Offset="0,20" Style="Grid9DetailFive140" ConsumeMouse="1" ID="BGLastBattle">

		<!-- Upper Treatment -->
		<Image Anchor="C,T" AnchorSide="I.O" Offset="0,-27" Size="256,64" Texture="Top512IconTrim.dds"  >
			<Image Anchor="C,C" Offset="0,-10" Size="80,80" Texture="NotificationFrameBase.dds"  >
				<Image ID="CivIconBG" Size="80.80" Anchor="C,C" Offset="0,0" TextureOffset="141,0" Texture="CivIconBGSizes.dds" />
				<Image ID="CivIconShadow" Size="64.64" Anchor="C,C" Offset="1,1" Texture="CivSymbolAtlas64.dds" Color="Black.128"/>
				<Image ID="CivIcon" Size="64,64" Offset="0,0"  Anchor="C,C" Texture="CivSymbolsColor512.dds" />
			</Image>
		</Image>

		<!-- Side treatments -->
		<Box Style="MenuLeftSideTreatment"/>
		<Box Style="MenuRightSideTreatment"/>

		<Stack ID="InfluenceStack" Anchor="C,T" Size="972,572" StackGrowth="Right" Padding="50,0" />

		<!-- Description box -->
		<Grid Anchor="C,B" ID="WhiteTowerStatusMessageContainer"  Offset="0,100" Size="840,90" Padding="0" Style="Grid9FrameBlack" Hidden="0" >
			<Label ID="WhiteTowerStatusLabel" String="TXT_KEY_WHITE_TOWER_STATUS" WrapWidth="800"  LeadingOffset="-4" Anchor="C,C" Offset="0,15" Font="TwCenMT20" ColorSet="Beige_Black_Alpha" />
		</Grid>

		<!-- Choice buttons -->
		<Box Anchor="C,B" AnchorSide="I.I" Offset="0,54" Size="910,56" Color="255,255,255,0" >
			<!-- Bottom Buttons -->
			<Stack Anchor="C,B"  Offset="0,0" Padding="10" StackGrowth="Right">
				<GridButton Size="275,36" ID="OkButton" Style="BaseButton" String="TXT_KEY_OKAY" />
			</Stack>
		</Box>
	</Grid>
</Context>

Code:
... other display/event code up here ...

function OnDisplay()
	local pPlayer = Players[m_CityStateId]

	Controls.InfluenceStack:DestroyAllChildren()

	for pAjah in GameInfo.Ajahs() do
		print("Processing Ajah " .. pAjah.ID)

		if (pPlayer:IsAjahPermitted(pAjah.ID)) then
			local instance = {}
			local sAjahName = Locale.ConvertTextKey(pAjah.Description)
			local iAjahPercent = pPlayer:GetAjahInfluencePercent(pAjah.ID)
			local sAjahPercent = iAjahPercent .. "%"

			local colorInfo = GameInfo.Colors[pAjah.Color]

			local ajahColor = { x = colorInfo.Red, y = colorInfo.Green, z = colorInfo.Blue, w = colorInfo.Alpha }

			print ("Found color " .. colorInfo.Type .. " R:" .. colorInfo.Red .. " G:" .. colorInfo.Green .. " B:" .. colorInfo.Blue .. " A:" .. colorInfo.Alpha)

			ContextPtr:BuildInstanceForControl("AjahInfluence", instance, Controls.InfluenceStack);

			instance.AjahLabel:SetText(sAjahName)
			instance.AjahPercent:SetText(sAjahPercent)
			instance.InfluenceBar:SetPercent(iAjahPercent / 100)
			instance.InfluenceBar:SetColor(ajahColor)

			instance.AjahStack:SetToolTipString(Locale.ConvertTextKey(pAjah.Help))
		end
	end

	Controls.InfluenceStack:CalculateSize()
end

The code all works except for the color. (Setting the color just does nothing and it carries on.) The values color values reported in the print statement are correct (1,0,0,1 for red and 0,0,1,1 for blue with the DB data I'm using right now).

Conceptually, what I'm doing is building up a UI screen that's populated by a series of progress bars which represent 'influence' as a percentage of several competing factions.
 
Code:
include("FLuaVector")
Controls.InfluenceBar:SetBGColor(Color(r, g, b, alpha))
Controls.InfluenceBar:SetFGColor(Color(r, g, b, alpha))

where r, g, b and alpha are in the range 0 to 1
 
Code:
include("FLuaVector")
Controls.InfluenceBar:SetBGColor(Color(r, g, b, alpha))
Controls.InfluenceBar:SetFGColor(Color(r, g, b, alpha))

where r, g, b and alpha are in the range 0 to 1

Awesome sauce, thank you! Worked like a charm! :D

Do you have a reference to find functions like this? I didn't see it listed on the wiki.
 
Do you have a reference to find functions like this?

Nope, just a lot of hard-won experience. In general, if the UI control has an XML attribute of Xyz the Lua setter (if it exists, and a lot of them don't) will be called SetXyz.

The best source of info is the core UI lua/xml files - the unit health bar changes from green to orange to red, so I double checked my "best guess" by looking at how UnitPanel.lua codes it
 
Nope, just a lot of hard-won experience. In general, if the UI control has an XML attribute of Xyz the Lua setter (if it exists, and a lot of them don't) will be called SetXyz.

The best source of info is the core UI lua/xml files - the unit health bar changes from green to orange to red, so I double checked my "best guess" by looking at how UnitPanel.lua codes it

Your UI tutorials have been invaluable for me thus far (any of my new screens are built largely on the back of what you do there). You put a lot of awesome content into that and very well explained!

I was trying to think of a progress bar that changed color - I was planning on looking into the City State relationship bar, but health bar makes a lot of sense! I feel like I should put these functions onto the wiki now, so future modders will find them when they check. :D
 
Back
Top Bottom