Custom GUI code side v3.2

Original Source

Tested for 3.3 still working.

First things first.  Right click the folder where you want to add your gui, go down to add new item, make sure you are adding a .cs file, name is CustomUI

we have to initialize this component, the character stats console is loaded into all scenes automatically as the inherit the base demo scene and that initializes this. So instead, you need to open up your scene and find the OnStarted() function, from there you need to add your console to this so that the world knows to load it. For the purpose of this exercise, we will go with naming it CustomUI so add this line to onstarted.  If you are running into an error on this it is not because your code is wrong, its because something went wrong with the linking library.  Delete from .initialize on then retype and let intellisense take it over.

UI.CustomUI.Instance.Initalize(this)

I'm just guessing, but I believe it lets the level know that it can now use this. Next lets get back to our CustomUI
How to create a GUI
First off, creating a gui element requires quite a few things. I believe that the reason that this is so complex is because realmware is intending for this to be an end user designed thing through the editor. So how does that help the end user until they release 1.0? The short answer is it just means a little more work. First off to create your GUI, we will first need to prepare it. Since V3D is such a heavily object oriented program, the first step in creating a gui is actually giving the correct using files.  I'm not entirely certain that it needs all these files, the audio especially in this design.  However if you want opening a bag to play a sound, or low health to play a sound, this is good to add here.  From here on out its all written as the .CS file

using System;
using Microsoft.Xna.Framework;
using Visual3D.Input;
using Audio = Visual3D.Audio;
using Visual3D.UI;
using Visual3D.UI.Controls;
using XG = Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace Visual3D.Demo.UI
{

//As far as the [Serializable] keyword... I have no idea why its in there as I found commenting it out and running changed nothing, but I'm sure //somewhere in the code this would bite my ass. So feel free to add it if you want.

[Serializable]
public class CustomUI : GuiForm
{//I'm not 100% on this, however each interface that I've seen always has this
//The reasoning behind this seems to be if you dive through the other interfaces
//they seem to pull other UIs, this method allows them to all open the same window
//Without pointers I imagine I'd do the same thing... but I'd go through a lot of
//trial and error before realizing it.

static public CustomUI _instance;
public static CustomUI Instance
{
get
{
if (_instance != null)
return _instance;
else
{
CustomUI custom = new CustomUI();
custom.PrepareForUse();
return custom;
}
}
}
//destructor
protected override void OnDispose(bool isDisposing)
{
base.OnDispose(isDisposing);
_instance = null;
}

//declare any other consoles you want to be able to pull from here... this includes every form item so add em
protected Label _avatarName;//we'll show them their name.
protected ProgressBar _healthBar;//and I think that we'll show them their health, that should be enough for now.
public CustomUI() :base("Gui/Consoles/CustomUI", /*modal*/false, ScreenRectangle.FullSize)
//now, how this all works I have no clue, metadata hits on the screenrect thing, but we shall see
//modal means that the game freezes while its open, so assume that it means the same here.
{
_instance = this;//remember, the class is static.
}
protected override void OnActivated()
{
base.OnActivated();//I'm guessing that this calls the BuildGui function.
}
protected override void BuildGui()
{
if (_healthBar != null)//not entirely certain on this one... seems like redundant error checking, but whatever.
return;

float ScreenWidth = World.GuiRenderer.Width;
float ScreenHeight = World.GuiRenderer.Height;
int barWidth = 100;
//public Label AddText(string name, float posX, float posY, float width, float height, string text);
//is what you pull up if you look at it in metadata. So basically, it does what it says.
//I wonder about that last string though. The next few lines are ripped straight from CharacterStatsConsole.cs
_avatarName = (Label)this.AddText("AvatarName", 10, 65, barWidth, 20, "");
//I'm going to guess that the first bit is just to reference, the second and third is the same as above
//the barwidth.... I have no clue why its not just an int but its nice to know what that field does.
//and the next few I do not know.
_healthBar = (ProgressBar)this.AddColorProgressBar(
"HealthBar", 10, 90, barWidth, 20, 0, 100, 100,
ProgressBarConfig.Orientation.Horizontal);//can I call diagonal?
//thats kindof a nifty feature though.
_healthBar.Prefix = "H:";
//Not sure on this one but guessing where the thing appears?
this.TopSheet.ThemeFileName = SkinIDs.Fantasy;
}
public void UpdateBeforeRender()
{//I think this is on the todo list, so put it here to be safe.
}
public void UpdateStats()
{//ripped from their files, is obvious.
if (!this.IsActive || !this.IsShowing)
return; // not active!
Avatar avatar = World.Selections.FocusedObject as Avatar;
//need a reference on who to get the health on.
if (avatar != null)
{
_avatarName.Text = avatar.Name;

// We have a player, update his stats
_healthBar.MaxRange = avatar.MaxHitPoints;
_healthBar.Progress = avatar.HitPoints;
}
return;
}
}
}
//Das it!
//as long as the forums don't mess it up, should be pretty straight forward
/*
-Cullen Ogilvie. Chris Bossardet, Tashi Daune(he figured out I forgot to use a CustomUI datatype on _instance dec =P)

After someone responded with another post about the customUI not showing up this was added.

Did you change all references from the regular healthbar to the new customui you made in the avatar system? Anytime he is damaged, he tells the statbar to change, if you left those alone it will load the default statbar. If thats the case, then create a backup copy of the avatar.cs file, as that's a bad one to lose as its pretty much everything nifty. From there you just find all references to .updatestats() in the avatar class and replace the base class from CharacterStatsConsole to CustomUI. I have not tested this code out yet though so for all I know it could make things fall down go boom I should have called up the customUI now that I think of it instead of just going "It compiles, therefore it runs!" but we are in a hurry as we are putting together our game now. I just wanted to verify that we could and get all the research out of the way. I'm about to test this to make sure that it will work.

allright added in the automatically there when the scene loads and that worked fine, going to try to break it with the avatar addition.

and after finishing the edit for the robots, it works. Just comment out all references to statbar and replace all updates with customui instead. Didn't crash on my box, and bar came up just fine.
*/

[Serializable]

Adding [Serializable] allows an object derived from this class to be serialized for moving in or out of a stream.

We generally suggesting

We generally suggesting applying [DataContract] to classes instead of [Serializable].  
It can avoid exceptions in cases for classes derived from ComponentBase, and can allows you to easily opt-in just those members you know you want to persist, making it easier to control, and allows you to markup properties (including serialization only private ones) as well as fields with [DataMember] to persist them, unlike [Serializable], which just serializes all fields by default. 

If no attribute is applied to a class, then all the public fields and properties declared for that class will be serialized by default, unless [IgnoreDataMember] is applied to them.


love