Gum in Code
Introduction
Gum can be used purely in code (no editor), or by referencing a Gum project. Both approaches are well-supported so you can use Gum to fit your development style.
Gum Code-Only
Gum can be used fully in code. A code-only setup requires minimal code. The following shows a simple MonoGame Game1 class with a few functional Gum controls. All lines of code related to Gum are highlighted:
using Gum.Forms.Controls;
using Gum.Wireframe;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGameGum;
using System;
namespace MonoGameAndGum;
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
GumService GumUI => GumService.Default;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
GumUI.Initialize(this, Gum.Forms.DefaultVisualsVersion.V2);
var stackPanel = new StackPanel();
stackPanel.AddToRoot();
stackPanel.Spacing = 6;
stackPanel.Anchor(Anchor.Center);
var button = new Button();
stackPanel.AddChild(button);
button.Text = "Click Me";
button.Click += (s, e) =>
{
button.Text = DateTime.Now.ToString();
};
var textBox = new TextBox();
textBox.Width = 150;
stackPanel.AddChild(textBox);
var listBox = new ListBox();
stackPanel.AddChild(listBox);
for(int i = 0; i < 10; i++)
{
listBox.Items.Add("Item " + i);
}
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
GumUI.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GumUI.Draw();
base.Draw(gameTime);
}
}

Gum Tool Projects in Code
The Gum Tool can be used to create projects visually. These projects can be loaded into your project and accessed in a type-safe way using Gum's generated code.
The following image shows a screen similar to the one above created in the Gum tool:

The following code can be used to load and interact with the controls:
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
GumService GumUI => GumService.Default;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
GumUI.Initialize(this, "GumProject/GumProject.gumx");
var screen = new ExampleScreen();
screen.AddToRoot();
screen.ButtonStandardInstance.Click += (s,e) =>
{
screen.ButtonStandardInstance.Text = DateTime.Now.ToString();
};
for(int i = 0; i < 10; i++)
{
screen.ListBoxInstance.Items.Add("Item " + (i + 1));
}
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
GumUI.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GumUI.Draw();
base.Draw(gameTime);
}
}
Generated code is optional - code can access Gum objects without any generated code as shown in the following block:
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
GumService GumUI => GumService.Default;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
GumUI.Initialize(this, "GumProject/GumProject.gumx");
var screen = ObjectFinder.Self.GumProjectSave.Screens
.First().ToGraphicalUiElement();
screen.AddToRoot();
var button = screen.GetFrameworkElementByName<Button>("ButtonStandardInstance");
button.Click += (s,e) =>
{
button.Text = DateTime.Now.ToString();
};
var listBox = screen.GetFrameworkElementByName<ListBox>("ListBoxInstance");
for (int i = 0; i < 10; i++)
{
listBox.Items.Add("Item " + (i + 1));
}
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
GumUI.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GumUI.Draw();
base.Draw(gameTime);
}
}Last updated
Was this helpful?

