Gum Forms is a set of classes which provide fully-functional forms controls which you can use in your game. Examples of forms controls include:
Button
Checkbox
ListBox
TextBox
Slider
Gum Forms can be used purely in code (no .gumx project required), or you can fully style your forms objects in a Gum project.
Quick Setup - Forms in code
The easiest way to add Forms to your project is to use the FormsUtilities class. Keep in mind that Forms is not a replacement for Gum; rather, it adds objects on top of Gum which provide common UI interaction. In other words, if you are using Forms, you are still using Gum as well. Therefore, when using Forms you must also initialize Gum.
The following code snippet shows how to initialize Forms and Gum, and how to add a single Button to your project:
publicclassGame1:Game{privateGraphicsDeviceManager _graphics; // Gum renders and updates using a hierarchy. At least // one object must have its AddToManagers method called. // If not loading from-file, then the easiest way to do this // is to create a ContainerRuntime and add it to the managers.ContainerRuntime Root;publicGame1() { _graphics =newGraphicsDeviceManager(this);Content.RootDirectory="Content"; IsMouseVisible =true; }protectedoverridevoidInitialize() {var gumProject =MonoGameGum.GumService.Default.Initialize(this.GraphicsDevice); Root =newContainerRuntime();Root.Width=0;Root.Height=0;Root.WidthUnits=Gum.DataTypes.DimensionUnitType.RelativeToContainer;Root.HeightUnits=Gum.DataTypes.DimensionUnitType.RelativeToContainer;Root.AddToManagers();var button =newButton();Root.Children.Add(button.Visual);button.X=50;button.Y=50;button.Width=100;button.Height=50;button.Text="Hello MonoGame!";int clickCount =0;button.Click+= (_, _) => { clickCount++;button.Text=$"Clicked {clickCount} times"; }; base.Initialize(); }protectedoverridevoidUpdate(GameTime gameTime) {MonoGameGum.GumService.Default.Update(this, gameTime, Root); base.Update(gameTime); }protectedoverridevoidDraw(GameTime gameTime) {GraphicsDevice.Clear(Color.CornflowerBlue);MonoGameGum.GumService.Default.Draw(); base.Draw(gameTime); }}
The code above produces a single button which can be clicked to increment the click count.
Quick Setup - Forms in the Gum Tool
Forms can be used with the Gum tool, allowing you to create fully functional UI layouts visually.
To use Forms in a project that loads a Gum project (.gumx) the following steps are needed:
Components must be added to the Gum project which have the necessary behaviors to be used as Forms visuals
The Gum project must be loaded in code, just like any other Gum project
Adding Forms Components to the Gum Project
To add components which are used for Forms visuals:
Open your project in Gum, or create a new project
If creating a new project, save the project in a subfolder of your game's Content folder
Select Content -> Add Forms Components
Check the option to include DemoScreenGum, or alternatively add a new screen to your project and drag+drop the desired components into your screen
Once you have saved your project, modify your Gum file to include the project:
Open your .csproj in a text editor (or click on it in Visual Studio)
Add the wildcard addition code to copy all of your Gum projects
Your project is now referenced in your game. Modify the Game file to initialize the Gum systems and to load the .gumx project. Your code might look like this:
publicclassGame1:Game{privateGraphicsDeviceManager _graphics; // Gum renders and updates using a hierarchy. At least // one object must have its AddToManagers method called. // If not loading from-file, then the easiest way to do this // is to create a ContainerRuntime and add it to the managers.GraphicalUiElement Root;publicGame1() { _graphics =newGraphicsDeviceManager(this);Content.RootDirectory="Content"; IsMouseVisible =true; }protectedoverridevoidInitialize() {var gumProject =MonoGameGum.GumService.Default.Initialize(this.GraphicsDevice, // This is relative to Content:"GumProject/GumProject.gumx"); // This assumes that your project has at least 1 screen Root =gumProject.Screens.First().ToGraphicalUiElement(SystemManagers.Default, addToManagers:true); base.Initialize(); }protectedoverridevoidUpdate(GameTime gameTime) {MonoGameGum.GumService.Default.Update(this, gameTime, Root); base.Update(gameTime); }protectedoverridevoidDraw(GameTime gameTime) {GraphicsDevice.Clear(Color.CornflowerBlue);MonoGameGum.GumService.Default.Draw(); base.Draw(gameTime); }}