Creating New Controls
Introduction
Creating a New Class
using MonoGameGum.Forms.Controls;
using MonoGameGum.GueDeriving;
using MonoGameGum.Forms.DefaultVisuals;
namespace MonoGameAndGum.Components;
// By inheriting from Panel we get a
// Visual object automatically instead
// of having to set one manually.
public class TextInputDialog : Panel
{
Label prompt;
public string PromptText
{
get => prompt.Text;
set => prompt.Text = value;
}
public TextInputDialog()
{
// panels all size themselves according to their children.
// let's give this extra size so it has a border.
// we want 12 pixels on each side, so that's 12+12 = 24
this.Width = 24;
this.Height = 24;
// Let's give this a background:
var background = new NineSliceRuntime();
this.AddChild(background);
background.Dock(Gum.Wireframe.Dock.Fill);
// We can use the built-in styling for the background:
background.Texture = Styling.ActiveStyle.SpriteSheet;
background.ApplyState(Styling.NineSlice.Panel);
// An innerPanel stacks our children.
// This innerPanel is sized according to
// its children, and "this" will be 24 pixels
// larger, creating a border.
var innerPanel = new StackPanel();
innerPanel.Spacing = 10;
// Center this so the parent adds borders to all sides
innerPanel.Anchor(Gum.Wireframe.Anchor.Center);
this.AddChild(innerPanel);
// Our label. We want to use a class
// member so we can access this later if needed.
prompt = new Label();
innerPanel.AddChild(prompt);
prompt.Text = "Enter text:";
var textBox = new TextBox();
innerPanel.AddChild(textBox);
// Make the text box fill the available width:
textBox.Width = 0;
textBox.Visual.WidthUnits = Gum.DataTypes.DimensionUnitType.RelativeToParent;
var buttonPanel = new StackPanel();
innerPanel.AddChild(buttonPanel);
buttonPanel.Spacing = 4;
buttonPanel.Orientation = Orientation.Horizontal;
var okButton = new Button();
buttonPanel.AddChild(okButton);
okButton.Text = "OK";
var cancelButton = new Button();
buttonPanel.AddChild(cancelButton);
cancelButton.Text = "Cancel";
}
}
Panel Inheritance
Margin


NineSliceRuntime Background

Prompt Label

Sizing (RelativeToParent and RelativeToChildren)
TextBox Sizing

Conclusion
Last updated
Was this helpful?

