UI: Show/hide stack in instance after creation

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
I will try and word this as carefully as possible as Ui coding is confusing enough!

I have a UI screen that displays a list of info in rows. In order to minimize size and not overload the user with info, I have broken up the results across 5 'stacks' within the 'instance'.

The question is, how do I show or hide these stacks of each instance after I have built the list without having to rebuild it again?

Incase I am not making sense, here is an outline of the UI screen code. The stacks in red are located outside the main grid and are a part of the instance section. I need to know how to show/hide them after building the list of entries.

Code:
<Context ColorSet="Beige_Black_Alpha" Font="TwCenMT18" FontStyle="Shadow" >

	<Grid ID="BackgroundBox" Size="1200,600" Offset="10,0" Anchor="C,C" Style="Grid9DetailFive140" ConsumeMouse="1">

		<!-- Main Heading -->
		<Label ID="MainHeadingLabel" Anchor="C,T" Offset="0,15" Font="TwCenMT24" FontStyle="Shadow" String="TXT_KEY_REGIONINFO_HEADING"/>

		<!--======================================================================================================================================-->
		<!-- Info Selection Stack -->
		<!--======================================================================================================================================-->
		<Stack ID="InfoButtonStack" Anchor="C,T" StackGrowth="Right" Offset="0,60" Padding="0" Hidden="0">
			<!-- General Info Button -->
			<Button ID="GeneralInfoButton" Anchor="L,C" Size="220,24"  Style="BoxButton" Font="TwCenMT18" String="TXT_KEY_CITYINFO_GENERAL_INFO" ToolTip="TXT_KEY_REGIONINFO_TOOLTIP_GENERAL" >
				....
			</Button>
			
			<!-- Military Forces  Info Button -->
			<Button ID="MilitaryInfoButton" Anchor="L,C" Size="260,24"  Style="BoxButton" Font="TwCenMT18" String="TXT_KEY_REGIONINFO_MILITARY_FORCES" ToolTip="TXT_KEY_REGIONINFO_TOOLTIP_MILITARY_FORCES" >
				....
			</Button>
			
			<!-- Industrial Info Button -->
			<Button ID="IndustrialInfoButton" Anchor="L,C" Size="220,24"  Style="BoxButton" Font="TwCenMT18" String="TXT_KEY_REGIONINFO_INDUSTRIAL" ToolTip="TXT_KEY_REGIONINFO_TOOLTIP_INDUSTRIAL" >
				....
			</Button>
			
			<!-- War Industry Info Button -->
			<Button ID="MilitaryProdInfoButton" Anchor="L,C" Size="220,24"  Style="BoxButton" Font="TwCenMT18" String="TXT_KEY_REGIONINFO_MILITARY_PRODUCTION" ToolTip="TXT_KEY_REGIONINFO_TOOLTIP_MILITARY_PRODUCTION" >
				....
			</Button>
			
			<!-- Agricultural Info Button -->
			<Button ID="AgriculturalInfoButton" Anchor="L,C" Size="220,24"  Style="BoxButton" Font="TwCenMT18" String="TXT_KEY_REGIONINFO_AGRICULTURAL" ToolTip="TXT_KEY_REGIONINFO_TOOLTIP_AGRICULTURAL" >
				....
			</Button>
		</Stack>


		<!--======================================================================================================================================-->
		<!-- Header -->
		<!--======================================================================================================================================-->
		<Box ID="RegionHeader" Offset="-40,132" Anchor="C,T" Size="1070,24" Color="255.255.200.0" Hidden="0" >

			<!--======================================================================================================================================-->
			<!-- Region Scroll Panel -->
			<!--======================================================================================================================================-->
			<ScrollPanel ID="RegionScrollPanel" Anchor="C,T" Offset="0,40"  Size="1070,330" Vertical="1" ID="ScrollPanel" >
				.... scroll panel stuff here
			</ScrollPanel>

		</Box>

	</Grid>

	<!--======================================================================================================================================-->
	<!-- Region Instance-->
	<!--======================================================================================================================================-->
	<Instance Name="RegionInstance" >

		<Box ID="Root" Offset="0,0" Anchor="L,T" Size="690,34"  Color="255,255,255,0" >

			<!--======================================================================================================================================-->
			<!-- Static Instance Stack -->
			<!--======================================================================================================================================-->
			<Stack Anchor="L,C" StackGrowth="Right" Offset="0,0" Padding="0" >

				....bunch of main boxes that always display when opening the UI screen

				<!--=====================================================================================================-->
				<!-- POPULATION AND CONSCRIPTION INFO ===================================================================-->
				<!--=====================================================================================================-->
[COLOR="Red"]				<Stack ID="RegionGeneralInfoStack" Anchor="L,C" StackGrowth="Right" Offset="0,0" Padding="0" >[/COLOR]
					.... bunch of boxes that are shows on clicking GeneralInfoButton
				</Stack>

				<!--=====================================================================================================-->
				<!-- MILITARY UNITS IN REGION INFO ===================================================================-->
				<!--=====================================================================================================-->

[COLOR="Red"]				<Stack ID="RegionMilitaryInfoStack" Anchor="L,C" StackGrowth="Right" Offset="0,0" Padding="0" >[/COLOR]
					.... bunch of boxes that are shows on clicking MilitaryInfoButton
				</Stack>

				<!--=====================================================================================================-->
				<!-- INDUSTRIAL PRODUCTION INFO ===================================================================-->
				<!--=====================================================================================================-->
[COLOR="Red"]				<Stack ID="RegionIndustrialInfoStack" Anchor="L,C" StackGrowth="Right" Offset="0,0" Padding="0" >[/COLOR]
					.... bunch of boxes that are shows on clicking IndustrialInfoButton
				</Stack>

				<!--=====================================================================================================-->
				<!-- WAR INDUSTRY PRODUCTION INFO ===================================================================-->
				<!--=====================================================================================================-->
[COLOR="Red"]				<Stack ID="RegionMilitaryProdInfoStack" Anchor="L,C" StackGrowth="Right" Offset="0,0" Padding="0" >[/COLOR]
					.... bunch of boxes that are shows on clicking MilitaryProdInfoButton
				</Stack>

				<!--=====================================================================================================-->
				<!-- AGRICULTURAL PRODUCTION INFO ===================================================================-->
				<!--=====================================================================================================-->
[COLOR="Red"]				<Stack ID="RegionAgriculturalInfoStack" Anchor="L,C" StackGrowth="Right" Offset="0,0" Padding="0" >[/COLOR]
					.... bunch of boxes that are shows on clicking AgriculturalInfoButton
				</Stack>
			</Stack>



		</Box>
	</Instance>

</Context>
 
Easier to answer if you can just paste in the Lua that creates the instance (either by using the InstanceManager or the BuildInstanceForControl function)
 
Here is the code. I have snipped out all irrelevant code.

Code:
local SHOW_GENERAL				= 1
local SHOW_MILITARY_UNITS		= 2
local SHOW_INDUSTRIAL			= 3
local SHOW_MILITARY_PRODUCTION	= 4
local SHOW_AGRICULTURAL			= 5

[B]function UpdateDisplay(showButton)[/B]

	Controls.RegionMainStack:DestroyAllChildren()

	-- Loop through region data
	for key, regionName in ipairs(g_RegionNames) do
	
		local instance = {}
		ContextPtr:BuildInstanceForControl( "RegionInstance", instance, Controls.RegionMainStack )

		...
		<Populate all fields in instance. We do not touch any stacks here>
		...

	if showButton == SHOW_GENERAL then
		instance.RegionGeneralInfoStack:SetHide( false )
		instance.RegionMilitaryInfoStack:SetHide( true )
		instance.RegionIndustrialInfoStack:SetHide( true )
		instance.RegionMilitaryProdInfoStack:SetHide( true )
		instance.RegionAgriculturalInfoStack:SetHide( true )
		
	elseif showButton == SHOW_MILITARY_UNITS then
		instance.RegionGeneralInfoStack:SetHide( true )
		instance.RegionMilitaryInfoStack:SetHide( false )
		instance.RegionIndustrialInfoStack:SetHide( true )
		instance.RegionMilitaryProdInfoStack:SetHide( true )
		instance.RegionAgriculturalInfoStack:SetHide( true )

	elseif showButton == SHOW_INDUSTRIAL then
		instance.RegionGeneralInfoStack:SetHide( true )
		instance.RegionMilitaryInfoStack:SetHide( true )
		instance.RegionIndustrialInfoStack:SetHide( false )
		instance.RegionMilitaryProdInfoStack:SetHide( true )
		instance.RegionAgriculturalInfoStack:SetHide( true )
		
	elseif showButton == SHOW_MILITARY_PRODUCTION then
		instance.RegionGeneralInfoStack:SetHide( true )
		instance.RegionMilitaryInfoStack:SetHide( true )
		instance.RegionIndustrialInfoStack:SetHide( true )
		instance.RegionMilitaryProdInfoStack:SetHide( false )
		instance.RegionAgriculturalInfoStack:SetHide( true )
		
	elseif showButton == SHOW_AGRICULTURAL then
		instance.RegionGeneralInfoStack:SetHide( true )
		instance.RegionMilitaryInfoStack:SetHide( true )
		instance.RegionIndustrialInfoStack:SetHide( true )
		instance.RegionMilitaryProdInfoStack:SetHide( true )
		instance.RegionAgriculturalInfoStack:SetHide( false )
	end
        
       end -- for statement
	
	Controls.RegionMainStack:CalculateSize();
        Controls.RegionScrollPanel:ReprocessAnchoring();
        Controls.RegionScrollPanel:CalculateInternalSize();

end

-------------------------------------------------------------------------------
-- Sort Function
-------------------------------------------------------------------------------
[B]function SortFunction( a, b )[/B]
	...
end

-------------------------------------------------------------------------------
-- On Sort Function
-------------------------------------------------------------------------------
[B]function OnSort( type )[/B]
	...
end

-------------------------------------------------
-- Show/Hide Enabler
-------------------------------------------------
[B]function ShowHideHandler( bIsHide )[/B]
	if( not bIsHide ) then
		GetRegionDynamicInfo(Game.GetActivePlayer())
		UpdateDisplay(SHOW_GENERAL)
	end
end
ContextPtr:SetShowHideHandler( ShowHideHandler )

-------------------------------------------------
-- Info Buttons Clicked
-------------------------------------------------
[B]function OnGeneralInfoButton()[/B]

	...
	UpdateDisplay(SHOW_GENERAL)

end
Controls.GeneralInfoButton:RegisterCallback( Mouse.eLClick, OnGeneralInfoButton );

[B]function OnMilitaryInfoButton()[/B]

	...
	UpdateDisplay(SHOW_MILITARY_UNITS)	
end
Controls.MilitaryInfoButton:RegisterCallback( Mouse.eLClick, OnMilitaryInfoButton );

[B]function OnIndustrialInfoButton()[/B]
	
	...	
	UpdateDisplay(SHOW_INDUSTRIAL)	
end
Controls.IndustrialInfoButton:RegisterCallback( Mouse.eLClick, OnIndustrialInfoButton );

[B]function OnMilitaryProdInfoButton()[/B]
	
	...
	UpdateDisplay(SHOW_MILITARY_PRODUCTION)	
end
Controls.MilitaryProdInfoButton:RegisterCallback( Mouse.eLClick, OnMilitaryProdInfoButton );

[B]function OnAgriculturalInfoButton()[/B]
	
	...
	UpdateDisplay(SHOW_AGRICULTURAL)
end
Controls.AgriculturalInfoButton:RegisterCallback( Mouse.eLClick, OnAgriculturalInfoButton );

-----------------------------------------------------------------
-- Link to Info Corner Panel
-----------------------------------------------------------------
[B]function OnOpenInfoCorner(iInfoType)[/B]
  if (iInfoType == InfoCornerID.GPW_Regions) then
    ContextPtr:SetHide(false)
  else
    ContextPtr:SetHide(true)
  end
end
Events.OpenInfoCorner.Add(OnOpenInfoCorner)

ContextPtr:SetHide(true)
 
This should do what I think you're asking

Code:
local SHOW_GENERAL				= 1
local SHOW_MILITARY_UNITS		= 2
local SHOW_INDUSTRIAL			= 3
local SHOW_MILITARY_PRODUCTION	= 4
local SHOW_AGRICULTURAL			= 5

local allStacks = {}
local g_lastShowButton = SHOW_GENERAL

function UpdateDisplay()

	Controls.RegionMainStack:DestroyAllChildren()
	allStacks[SHOW_GENERAL] = {}
	allStacks[SHOW_MILITARY_UNITS] = {}
	allStacks[SHOW_INDUSTRIAL] = {}
	allStacks[SHOW_MILITARY_PRODUCTION] = {}
	allStacks[SHOW_AGRICULTURAL] = {}

	-- Loop through region data
	for key, regionName in ipairs(g_RegionNames) do
	
		local instance = {}
		ContextPtr:BuildInstanceForControl( "RegionInstance", instance, Controls.RegionMainStack )

		...
		<Populate all fields in instance. We do not touch any stacks here>
		...

		table.insert(allStacks[SHOW_GENERAL], instance.RegionGeneralInfoStack)
		table.insert(allStacks[SHOW_MILITARY_UNITS], instance.RegionMilitaryInfoStack)
		table.insert(allStacks[SHOW_INDUSTRIAL], instance.RegionIndustrialInfoStack)
		table.insert(allStacks[SHOW_MILITARY_PRODUCTION], instance.RegionMilitaryProdInfoStack)
		table.insert(allStacks[SHOW_AGRICULTURAL], instance.RegionAgriculturalInfoStack)
	end
	
	ShowStack(g_lastShowButton)
        
end

function ShowStack(showButton)
	for i = SHOW_GENERAL, SHOW_AGRICULTURAL, 1 do
		for _, stack in pairs(allStacks[i]) do
			stack:SetHide(showButton != i)
		end
	end

	Controls.RegionMainStack:CalculateSize();
    Controls.RegionScrollPanel:ReprocessAnchoring();
    Controls.RegionScrollPanel:CalculateInternalSize();
	
	g_lastShowButton = showButton
end


-------------------------------------------------
-- Show/Hide Enabler
-------------------------------------------------
function ShowHideHandler( bIsHide )
	if( not bIsHide ) then
		GetRegionDynamicInfo(Game.GetActivePlayer())
		UpdateDisplay()
	end
end
ContextPtr:SetShowHideHandler( ShowHideHandler )

-------------------------------------------------
-- Info Buttons Clicked
-------------------------------------------------
function OnGeneralInfoButton()
	ShowStack(SHOW_GENERAL)
end
Controls.GeneralInfoButton:RegisterCallback( Mouse.eLClick, OnGeneralInfoButton );

// etc for the other 4 button callbacks
 
Unfortunately it did not work. Aside from the != which I easily picked up and changed to ~= , the code seems to be failing on this line:

Code:
function ShowStack(showButton)
	for i =SHOW_GENERAL, SHOW_AGRICULTURAL, 1 do
		[COLOR="Red"]for key, stack in pairs(allStacks[i]) do[/COLOR]
			stack:SetHide(showButton ~= i)
		end
	end

	Controls.RegionMainStack:CalculateSize();
    Controls.RegionScrollPanel:ReprocessAnchoring();
    Controls.RegionScrollPanel:CalculateInternalSize();
	
	g_lastShowButton = showButton

end

with the following error:

Code:
Runtime Error: C:\Users\user\Documents\My Games\Sid Meier's Civilization 5\MODS\Great Patriotic War (v 1)\UI\RegionsOverview.lua:393: bad argument #1 to 'pairs' (table expected, got nil)

I assume the instances are not carrying through once out of the loop!

EDIT: But dont worry, I can live with a complete reload ;)
 
For that line to fail, one of

Code:
	allStacks[SHOW_GENERAL] = {}
	allStacks[SHOW_MILITARY_UNITS] = {}
	allStacks[SHOW_INDUSTRIAL] = {}
	allStacks[SHOW_MILITARY_PRODUCTION] = {}
	allStacks[SHOW_AGRICULTURAL] = {}
can't of executed - so check for copy/paste/edit errors (or SHOW_GENERAL .. SHOW_AGRICULTURAL aren't consecutive ints)
 
Back
Top Bottom