Expanding research menu(chilli question)

Expanding research menu(chilli question)

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Expanding research menu(chilli question)

Post by smoth »

code below:

Ok, so here is what I am doing: I have a research button on the side of the screen. When clicks it expands and displays available items.

NOW I have thought of 2 methods to handle this:

A: Dispose
I have see the method using dispose to dump the root item and kill the tree(not sure if I need to delete all children or suffer or it is actually managed?) This will hide all the items and give the appearance of a collapsed menu.
Cons: everything has to be rebuilt every time I expand the menu
Pros: would be done already.

B: Alter all children to be nonclickable and transparent
The idea is to literally hide the menu, it is still there you just don't know it :P
Cons: I have to Invalidate all objects in order for their properties to refresh.
Pros: No costly rebuild like the dispose option.

C: Just shrink the window
I noticed that if I resize a chilli window smaller than a child element the window will cut off the child element.
Cons: doesn't work if you resize the window it has to be click to refresh and show elements as properly hidden.
Pros: no display lists, no rebuild :)

So what I am asking you chili gurus is what option should I do. If I can get C working it might be pretty awesome, I mean I have the code for it right here, but I can always do B or A if I really am desperate. Thoughts?

Code: Select all

function widget:GetInfo()
	return {
		name			= "Chili Research Bars",
		desc			= "",
		author			= "Smoth",
		date			= "2011",
		license		 	= "PD",
		layer		 	= 0,
		experimental	= false,
		enabled	 		= true
	}
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local Chili

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local researchBar
local researchBarSubExp
local butt_show_research
local butt_hide_research
local primer = true
local vsx,vsy = gl.GetViewSizes()

local windowExpanded = false
local WindowExpand

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function WindowExpand()
	if windowExpanded == false then
		windowExpanded = true 
		Spring.Echo("show")
		
		researchBar.width	= 350
		researchBar.height	= 400
		--researchBar:Invalidate()
	else
		windowExpanded = false 
		
		researchBar.width	= 30
		researchBar.height	= 70		
		--researchBar:Invalidate()
		Spring.Echo("hide")
	end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:Initialize()
	Chili = WG.Chili

	if (not Chili) then
		widgetHandler:RemoveWidget()
		return
	end
	
	researchBar	= Chili.Window:New{
		color	= {1,0,0,0.2},
		parent	= Chili.Screen0,
		left	= 0,
		y		= (vsx * 0.14)+4,
		padding = {0,0,0,0},
		dockable		= true,
		clientWidth		= 30,
		clientHeight	= 70,
		draggable		= true,
		resizable		= true,
	}	
	
	butt_show_research = Chili.Button:New{
		parent		= researchBar,
		left		= 0,
		y			= 0,
		height		= 70,
		width		= 30, 
		caption		= "",
		OnMouseUp	= {WindowExpand},
	}
	
	researchBarSubExp	= Chili.Window:New{
		parent		= researchBar,
		draggable	= false,
		resizable	= false,
		x			= 30,
		width		= 350, 
		height		= 400, 
	}
		
	local gridSet1 = Chili.ScrollPanel:New{
		parent	= researchBarSubExp,
		width	= 300,  
		height	= 400,  
		
		children	= {
			Chili.Button:New{
				caption		= "Tech item",
				width		= "26%",					
				height		= "20%", 
			},
			
			Chili.Button:New{
				x			= "30%",
				y			= "10%",
				width		= "20%",					
				height		= "15%", 
				caption = "item A"
			},
			Chili.Button:New{
				x			= "60%",
				y			= "10%",
				width		= "20%", 
				height		= "15%", 
				caption = "item B"
			},
		}
	}
end
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

Re: Expanding research menu(chilli question)

Post by Google_Frog »

Don't make orphans as they are slow. Excessive orphaning and rebuilding was the cause of the old carrepairer widgets that ran very poorly.

For fast widgets we are now manipulating the structure instead of removing and rebuilding. To hide something just remove it as a child of screen0, or whatever is it's parent. But make sure you have a reference to the hidden structure as you can reveal it just by giving it a parent.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Expanding research menu(chilli question)

Post by smoth »

I was unaware I could assign different parents to a child object! that is pretty cool. Can you give an example or is it something simple like doing a color override using invalidate?
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

Re: Expanding research menu(chilli question)

Post by Google_Frog »

From Chilli Selections:

Code: Select all

local function Show(obj)
	if (not obj:IsDescendantOf(screen0)) then
		screen0:AddChild(obj)
	end
end

local function Hide(obj)
	obj:ClearChildren()
	screen0:RemoveChild(obj)
end
I am unsure what the purpose of obj:ClearChildren(). It is probably specific to this widget and not really part of hiding. Maybe because the selections window is hidden when nothing is selected to the child structure of the window itself will have to change when it is shown again.
User avatar
Licho
Zero-K Developer
Posts: 3803
Joined: 19 May 2006, 19:13

Re: Expanding research menu(chilli question)

Post by Licho »

Do not assign same children to 2 different parents.
But you can reuse existing children and do not dispose it and instead connect/disconnect.

We do it in chili integral menu and new tooltip2 to improve performance
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Expanding research menu(chilli question)

Post by jK »

Yup, use AddChild&RemoveChild (btw you cannot use SetParent for it, because it doesn't add the object to the children table).
And yeah as Licho said, multi-parent support was dropped for performance optimizations.

PS: The ClearChildren in ChiliSelections was left-over from car, not that my code in it does it better. It calls ClearChildren ways to often, too. The best is to have multiple object containers with `presets` and then to edit the data in those instead of recreating every control. So you have something like `frames` (in WinAPI terminology).

PPS: My currently uncommited Chili even has child:Hide() & child:Show().
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Expanding research menu(chilli question)

Post by smoth »

oh, is that coming soon? I can wait a bit for it.
Post Reply

Return to “Lua Scripts”