Background:
I'm working on a control library for XNA to use in a couple game clients I've written. The way I've implemented Dialogs is to have them derive from the base XNAControl
class.
XNAControl
objects follow the Game Component model and are all derived from DrawableGameComponent
. They are added to the Game.Components list as soon as the base class XNAControl
constructor is called.
Problem:
Dialogs are popped up in game by instantiating an XNADialog
object, unlike MessageBox.Show()
which uses a static method. The constructor of XNADialog, in turn, instantiates a couple controls that are 'children' of the parent dialog control. Children are tracked by the dialog, and removed from the Game.Components
list automatically when the Dialog is closed.
The problem with the model that I've created is that the child controls are added to the Game's components when they're constructed, so they start updating and drawing and thus show on the screen before the dialog is fully constructed. The case I'm trying to debug is where the labels for a dialog box will appear in the top left, and then move to the center of the screen a split second later once the Dialog is constructed.
What I've tried:
- Separate constructor for base class XNAControl that doesn't add the control to the components when it's constructed. Instead, it relies on the child control (in this case dialog) to add itself to the
Game.Components
collection. This is what I have in place so far and it isn't working, which makes sense, because the labels are being added to the components collection prematurely anyway. Initialized
property of XNAControl. This property is set in theXNAControl.Initialize()
method, and the base classXNAControl.Update
andXNAControl.Draw
return automatically if it is false. However, this isn't working either (also in place) because child controls have to call the base methods of update and draw before after they do their own drawing logic because otherwise some things would be drawn in the wrong order. I also don't want to have to check theInitialized
property in every single child class I make because there are a lot of them and I'm looking for something more elegant- Moving the addition of the control to the components collection to the
Initialize
method. This didn't work because the control's initialize method didn't get called automatically because the control wasn't in the components collection yet.
Question:
What is the best way to keep the child controls from being updated and drawn prior to their parent control's construction? My only other ideas are:
- Using a theading sync object, but this seems like overkill/unnecessary (or maybe it's exactly what I need?). I can't think of an elegant way to do it.
- Finding a way to cancel any drawing that's been done by the SpriteBatch in the child classes, in
XNAControl.Draw
. I don't think this is possible though, and I can't find anything searching.