GeonBit Class Example

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Creating a Base Class

The first step is to create a base class which will be inherited by each interface screen.

With a project open, click project and add new class. A suitable name should be used (eg UI, UIScreen etc) it will create your class:

class UIScreen
{

}

Add the following to the using section of the class:

using Microsoft.Xna.Framework;
using GeonBit.UI;
using GeonBit.UI.Entities;

class UIScreen
{

}

Within the curly brackets you should create a public Panel , this will be what the user will see.

using Microsoft.Xna.Framework;
using GeonBit.UI;
using GeonBit.UI.Entities;

class UIScreen
{
    public Panel window;
}

A constructor method is called the same name as the class, and is normally public:

using Microsoft.Xna.Framework;
using GeonBit.UI;
using GeonBit.UI.Entities;

class UIScreen
{
    public Panel window;

    public UIScreen()
    {

    }
}

Now we need to add the code to create the window panel and add it to the interface:

using Microsoft.Xna.Framework;
using GeonBit.UI;
using GeonBit.UI.Entities;

class UIScreen
{
    public Panel window;

    public UIScreen()
    {
        window = new Panel(new Vector2(300, 300), PanelSkin.Default, Anchor.Center);
        UserInterface.Active.AddEntity(window);
    }
}

The vector2 sets the size of the window, panelskin has several options to change the appearence, anchor will position the panel and again many other options are available. You could set these up as parameters to be passed into the constructor when the class is used.

You could also add methods to change the window.Visible and the window.Draggable:

using Microsoft.Xna.Framework;
using GeonBit.UI;
using GeonBit.UI.Entities;

class UIScreen
{
    public Panel window;

    public UIScreen()
    {
        window = new Panel(new Vector2(300, 300), PanelSkin.Default, Anchor.Center);
        UserInterface.Active.AddEntity(window);
    }

    public void show(bool s)
    {
        window.Visible=s;
    }

    public void drag(bool d)
    {
        window.Draggable=d;
    }
}

Create a Screen Class

Create a new class as before and call it screen (or menu, gameover etc).

class Screen
{

}

Add the following to the using section of the class:

using Microsoft.Xna.Framework;
using GeonBit.UI;
using GeonBit.UI.Entities;

class Screen
{

}

You need to say it is a class of your base class so add:

using Microsoft.Xna.Framework;
using GeonBit.UI;
using GeonBit.UI.Entities;

class Screen:UIScreen
{

}

You could create a constructor, however this time i'll create a method to initialize the screen:

using Microsoft.Xna.Framework;
using GeonBit.UI;
using GeonBit.UI.Entities;

class Screen:UIScreen
{
    public void Init()
    {
        
    }
}

We can now add the components of this screen into the Init section, remember the data values from the base class will be inherited::

using Microsoft.Xna.Framework;
using GeonBit.UI;
using GeonBit.UI.Entities;

class Screen:UIScreen
{
    public void Init()
    {
        Paragraph text = new Paragraph("Hello World");
        window.AddChild(text);

        Button button1 = new Button("Click");
        window.AddChild(button1);

        button1.OnClick = (Entity btn) =>
        {
            text.Text = "Clicked";
        }

        Button button2 = new Button("Hide");
        window.AddChild(button2);

        button2.OnClick = (Entity btn) =>
        {
            window.Visible = false;
        }
    }
}

Creating a Screen in Game1.cs

Make sure you have GeonBit installed, and the references are in the using section. In the Initialize method you will need to initialize the user interface first:

UserInterface.Intialize(Content, BuiltinThemes.editor);

you should be able to create a screen by creating an instance of your screen:

Screen test = new Screen();
test.Init();

My Full Example of GeonBit & Classes