Text is not shown in some case

Started by tueda, 01 November 2013, 08:08:28


Sometimes we need to construct a whole panel before adding to GUI which includes RadioButton or other child widgets.
But no text is shown in that case.
I think the order independent scene construction is prefered by everyone.
I used TGUI.Net in .Net4.0(C#) / VS2010 / Windows7.

   static public void LoadWidgets (Gui gui) {
            // Create the background image
            var pic = gui.Add (new Picture ("media/Vanity.jpg"));
            pic.Size = new Vector2f (800, 600);

            var panel = new Panel ();
            panel.Position = new Vector2f (200, 100);
            panel.Size = new Vector2f (400, 400);
            panel.BackgroundColor = Color.White;

            // Before
            var btn1 = panel.Add (new RadioButton ("widgets/Black.conf"));
            btn1.Text = "Hello, world 1";
            btn1.TextColor = Color.Black;

            gui.Add (panel);

            // After
            var btn2 = panel.Add (new RadioButton ("widgets/Black.conf"));
            btn2.Text = "Hello, world 2";
            btn2.TextColor = Color.Black;
            btn2.Position = new Vector2f (0, 50);

            // quick hack.
            //btn1.TextFont = btn1.Parent.GlobalFont;
            //btn1.TextSize = 0;

(quick hack made me happy ;) )

And I found TGUI is very good library.
Thank you very much!


This 'bug' is a little bit tricky.

You can create widgets at a later moment, it should work if you don't so this with the widgets inside the panel but directly into the gui.

The thing is, every widget uses the font of its parent by default and will thus only receive its font when added to a parent. You are adding widgets to the panel, before the panel has a font, so the widgets will have no fonts. Once the panel gets added to the gui, it will get its font but it won't pass this to all it widgets (doing that would make it pretty hard to use multiple fonts within one panel).

So one solution is to give the panel the font directly after creating it.
var panel = new Panel ();
panel.GlobalFont = gui.GlobalFont;

But keep in mind that when you use a different font in the above code, the font of the panel will be overwritten again when it is added to the gui (but the widgets that were already added are still using the font that you wanted).

So this is al very complex, so maybe you should ask yourself if it is really necessary to go this way.
It all started with you saying that you want to construct the panel BEFORE adding it to the gui.
I think that what you are trying to do can be easily achieved by doing the following:
var panel = gui.Add (new Panel ());
panel.Visible = false;
// ...
panel.Visible = true; // This is the moment that you would add the panel to the gui