This project is read-only.

Strange order in mainScreen.Desktop.Children

Feb 14, 2012 at 7:25 PM
Edited Feb 14, 2012 at 7:31 PM

hello guys,

i just noticed something really weird.
my code looks something like this:


Screen mainScreen =  new Screen( viewport.Width, viewport.Height );
guiManager.Screen = mainScreen;

topBar = new TopBar(); debugControlsWindow = new DebugControlsWindow(); mainScreen.Desktop.Children.Add( topBar );

as u can see, i'm directly adding the topBar to the screen, so it has index 0 in guiManager.Screen.Desktop.Children.
if i press the d key, the following line is executed:

guiManager.Screen.Desktop.Children.Add( debugControlsWindow );

so the debugControlsWindow has the index 1 in guiManager.Screen.Desktop.Children.
the window itself just consists of some labels, a button to close it and just to make this clear, one eventhandler wich listens for the buttonClickedEvent. and now comes the weird part: if i click any point in the debugControlsWindow the order in guiManager.Screen.Desktop.Children gets turned upside down. wich means the topBar has index 1 and the debugControlsWindow index 0!

Why is this happening? could it be a bug?

Feb 21, 2012 at 10:21 AM

The order of the Controls in the list is the Z order.

Instead of using a 'ZOrder' property or something like that where you could have several controls with the same Z value (and an extra sorting pass would be required before drawing), my GUI implicitly stores the control's order through their position in the child list.

If a window moves in front of another window, the two windows trade places in the desktop's children list. Same happens when controls on a dialog a focused, the topmost / active control will be last in the list.

Sorry if this causes confusion :) - it's a nice design because it doesn't allow the Z order to go wrong and improves performance by eliminating the sorting pass, but I just now noticed it might be unexpected if the list's seemingly resort themselves at random! If you're accessing controls via index, try to find another way of keeping a reference to the control.

Feb 21, 2012 at 5:12 PM

thanks for your reply.
just a few hours after creating this thread, the thought came to me, that the list determines the drawing order, so i already got that^^.