Skip to content

Commit ed50658

Browse files
authored
Gum styling update 2025 8 (#164)
* Updated styling docs to use ButtonVisual Updated using statements to match new gum namespace * Fixed usings * Upped NuGet versions for MonoGame Docs * Oops forgot this using statement. * Upped version number * Addressed feedback from AristurtleDev
1 parent 0fd9f5b commit ed50658

File tree

11 files changed

+86
-83
lines changed

11 files changed

+86
-83
lines changed
-18.3 KB
Loading
-13.2 KB
Loading

articles/tutorials/building_2d_games/20_implementing_ui_with_gum/index.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Gum addresses these challenges with ready-made solutions, allowing us to focus o
3737
> [!IMPORTANT]
3838
> This tutorial uses the Gum NuGet package to help with layout and responding to user interactions. This tutorial does not require the use of the Gum editor, we will be doing everything in code.
3939
>
40-
> Keep in mind, that while it is possible to build a full UI system without any external dependencies, creating a layout engine is complicated and beyond the scope of this tutorial. Instead, we will be taking advantage of the Gum NuGet package.
40+
> Keep in mind that while it is possible to build a full UI system without any external dependencies, creating a layout engine is complicated and beyond the scope of this tutorial. Instead, we will be taking advantage of the Gum NuGet package.
4141
>
4242
> Gum is a powerful system enabling the creation of virtually any game UI, and we will be covering some of the basics of its use in this tutorial. The full Gum documentation can be found here: [https://docs.flatredball.com/gum/code/monogame](https://docs.flatredball.com/gum/code/monogame)
4343
@@ -321,7 +321,7 @@ To add the Gum NuGet package in Visual Studio Code:
321321
2. Choose `Add NuGet Package` from the context menu.
322322
3. Enter `Gum.MonoGame` in the `Add NuGet Package` search prompt and press Enter.
323323
4. When the search finishes, select the `Gum.MonoGame` package in the results
324-
5. When prompted for a version choose version `2025.5.1.1`.
324+
5. When prompted for a version choose version `2025.8.3.3`.
325325

326326
#### [Visual Studio 2022](#tab/vs2022)
327327

@@ -332,7 +332,7 @@ To Add the Gum NuGet package in Visual Studio 2022:
332332
3. In the NuGet Package Manager window, select the `Browse` tab if it is not already selected.
333333
4. In the search box, enter `Gum.MonoGame`.
334334
5. Select the "Gum.MonoGame" package from the search results.
335-
6. On the right, in the version dropdown, select version `2025.5.1.1` and click the "Install" button.
335+
6. On the right, in the version dropdown, select version `2025.8.3.3` and click the "Install" button.
336336

337337
#### [dotnet CLI](#tab/dotnetcli)
338338

@@ -342,7 +342,7 @@ To add the Gum NuGet package using the dotnet CLI:
342342
2. Enter the following command:
343343

344344
```sh
345-
dotnet add DungeonSlime.csproj package Gum.MonoGame --version 2025.5.1.1
345+
dotnet add DungeonSlime.csproj package Gum.MonoGame --version 2025.8.3.3
346346
```
347347

348348
---
@@ -351,11 +351,11 @@ To add the Gum NuGet package using the dotnet CLI:
351351
> You can verify the package was successfully added by examining your `DungeonSlime.csproj` file, which should now contain a reference like:
352352
>
353353
> ```xml
354-
> <PackageReference Include="Gum.MonoGame" Version="2025.5.1.1" />
354+
> <PackageReference Include="Gum.MonoGame" Version="2025.8.3.3" />
355355
> ```
356356

357357
> [!IMPORTANT]
358-
> This tutorial uses version `2025.5.1.1` of Gum, which is the latest version of Gum as of this writing. That exact version is specified to use in the section above when installing the NuGet package to ensure compatibility throughout this tutorial. If there are newer versions of Gum available, please consult the [Gum documentation](https://docs.flatredball.com/gum/gum-tool/breaking-changes) before updating in case there are any breaking changes from the code that is presented in this tutorial.
358+
> This tutorial uses version `2025.8.3.3` of Gum, which is the latest version of Gum as of this writing. That exact version is specified to use in the section above when installing the NuGet package to ensure compatibility throughout this tutorial. If there are newer versions of Gum available, please consult the [Gum documentation](https://docs.flatredball.com/gum/gum-tool/upgrading) before updating in case there are any breaking changes from the code that is presented in this tutorial.
359359

360360
### Adding UI Sound Effect
361361

@@ -385,7 +385,7 @@ With the Gum NuGet package added to our project, we need to initialize Gum in ou
385385
386386
First, open the `Game1.cs` file and add the following new using statements to the top:
387387
388-
[!code-csharp[](./snippets/game1/usings.cs?highlight=4-5)]
388+
[!code-csharp[](./snippets/game1/usings.cs?highlight=2-5)]
389389
390390
Next, add the following method to the `Game1` class to encapsulate the initializations of the Gum UI service:
391391
@@ -397,10 +397,10 @@ Finally, update the [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initializ
397397
398398
The following is a breakdown of this initialization process:
399399
400-
1. **Basic Initialization**: `GumService.Default.Initialize(this)` sets up the Gum system with our game instance. This is required for any gum project.
400+
1. **Basic Initialization**: `GumService.Default.Initialize(this, DefaultVisualsVersion.V2)` sets up the Gum system with our game instance. This is required for any gum project. The second parameter specifies the default visual styling. V2 is the latest version which makes it easy to style the default controls.
401401
402402
> [!NOTE]
403-
> We only need to pass our [**Game**](xref:Microsoft.Xna.Framework.Game) instance since we are using Gum as a code-first approach. Gum also offers a visual editor that creates Gum project files. When using the editor, you will need to also pass the Gum Project file here.
403+
> We only need to pass our [**Game**](xref:Microsoft.Xna.Framework.Game) instance and the visuals version since we are using Gum as a code-first approach. Gum also offers a visual editor that creates Gum project files. When using the editor, you will need to also pass the Gum Project file to `Initialize`. For more information on how to use the Gum visual editor, see the [Gum Project Forms Tutorial](https://docs.flatredball.com/gum/code/monogame/tutorials/gum-project-forms-tutorial).
404404
405405
2. **Content Loading**: Gum needs to be made aware of which content manager to use to load assets through the content pipeline. By setting `GumService.Default.ContentLoader.XnaContentManager = Core.Content`, we tell Gum to use our game's content manager when loading assets. By using the game's existing content manager, Gum also gets the benefit of the caching that the content manager performs when loading assets.
406406
3. **Input Configuration**:

articles/tutorials/building_2d_games/20_implementing_ui_with_gum/snippets/game1/initializegum.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
private void InitializeGum()
22
{
3-
// Initialize the Gum service
4-
GumService.Default.Initialize(this);
3+
// Initialize the Gum service. The second parameter specifies
4+
// the version of the default visuals to use. V2 is the latest
5+
// version.
6+
GumService.Default.Initialize(this, DefaultVisualsVersion.V2);
57

68
// Tell the Gum service which content manager to use. We will tell it to
79
// use the global content manager from our Core.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DungeonSlime.Scenes;
2-
using Microsoft.Xna.Framework.Media;
2+
using Gum.Forms;
3+
using Gum.Forms.Controls;
34
using MonoGameLibrary;
45
using MonoGameGum;
5-
using MonoGameGum.Forms.Controls;
6+
using Microsoft.Xna.Framework.Media;

articles/tutorials/building_2d_games/20_implementing_ui_with_gum/snippets/gamescene/usings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Microsoft.Xna.Framework.Graphics;
77
using Microsoft.Xna.Framework.Input;
88
using MonoGameGum;
9-
using MonoGameGum.Forms.Controls;
9+
using Gum.Forms.Controls;
1010
using MonoGameGum.GueDeriving;
1111
using MonoGameLibrary;
1212
using MonoGameLibrary.Graphics;

articles/tutorials/building_2d_games/20_implementing_ui_with_gum/snippets/titlescene/usings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Microsoft.Xna.Framework.Graphics;
55
using Microsoft.Xna.Framework.Input;
66
using MonoGameGum;
7-
using MonoGameGum.Forms.Controls;
7+
using Gum.Forms.Controls;
88
using MonoGameGum.GueDeriving;
99
using MonoGameLibrary;
1010
using MonoGameLibrary.Scenes;

articles/tutorials/building_2d_games/21_customizing_gum_ui/index.md

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -182,29 +182,41 @@ Now that we have all our resources prepared, we can create custom versions of th
182182

183183
### The AnimatedButton Class
184184

185-
Our first custom component will be an `AnimatedButton` that inherits from Gum's base `Button` class. This button will use the game's existing texture atlas for its visual appearance and provide animation when focused.
185+
Our first custom component is an `AnimatedButton` that inherits from Gum's base `Button` class. This button uses the game's existing texture atlas for its visual appearance and animates when focused.
186+
187+
By default all Gum components provide a Visual property which can be cast to a type specific to the particular control. By convention the visual type is named the same as the component with the word `Visual` appened. For example, we will be casting the AnimatedButton's Visual property to `ButtonVisual` to access button-specific properties.
188+
189+
This new `AnimatedButton` class casts the Visual property to `ButtonVisual` and modifies the button-specific properties such as background and text.
186190

187191
First, in the *DungeonSlime* project (your main game project), create a new folder named `UI` to store our custom UI components. Next, in that `UI` folder, create a new file called `AnimatedButton.cs` and add the following code to it:
188192

189193
[!code-csharp[](./snippets/animatedbutton.cs)]
190194

191195
Next, we will examine the key aspects of this new `AnimatedButton` implementation:
192196

193-
#### Top-level Container
197+
#### ButtonVisual
194198

195-
Every customized control needs a top-level container to hold all visual elements. For our button, we create a `ContainerRuntime` that manages the button's size and contains all other visual elements:
199+
As mentioned earlier, we first access the `Visual` object and cast it to a `ButtonVisual`. Doing so gives us access to button-specific properties including individual elements (such as the text and background visuals) as well as the states that are applied when the button is hovered or pressed.
196200

197-
[!code-csharp[](./snippets/animatedbutton.cs?start=26&end=32)]
201+
We can modify the Visual to give it the appropriate size.
198202

199-
The `WidthUnits` property set to `RelativeToChildren` means the container will automatically size itself based on its child elements, with 21 pixels of additional space. This allows the button to adapt its size depending on the text content.
203+
[!code-csharp[](./snippets/animatedbutton.cs?start=30&end=35)]
204+
205+
The `WidthUnits` property set to `RelativeToChildren` means the container automatically sizes itself based on its child elements, with 21 pixels of additional space. This allows the button to adapt its size depending on the text content.
200206

201207
#### Nine-slice Background
202208

203-
We use a `NineSliceRuntime` for the button's background. A nine-slice is a special graphic that can be stretch while preserving its corners and edges:
209+
`ButtonVisual` provides a `Background` which we can modify. This is of type `NineSliceRuntime` which is a special graphic that can be stretch while preserving its corners and edges:
210+
211+
[!code-csharp[](./snippets/animatedbutton.cs?start=39&end=42)]
204212

205-
[!code-csharp[](./snippets/animatedbutton.cs?start=34&end=41)]
213+
The `TextureAddress` property is set to `Custom` so we can specify exactly which portion of the atlas texture to use, while `Dock(Dock.Fill)` ensure the background fills the entire button area. The portion of the atlas is assigned using AnimationChains, which are discussed later in this tutorial.
206214

207-
The `TextureAddress` property is set to `Custom` so we can specify exactly which portion of the atlas texture to use, while `Dock(Dock.Fill)` ensure the background fills the entire button area.
215+
#### Text
216+
217+
`ButtonVisual` also provides a customizable `Text` property. In this case we assign the font, color, and size.
218+
219+
[!code-csharp[](./snippets/animatedbutton.cs?start=45&end=55)]
208220

209221
#### Animated Chains
210222

@@ -215,44 +227,44 @@ The most distinctive feature of our animated button is its ability to change app
215227

216228
Each animation frame specifies the coordinates within our texture atlas to display:
217229

218-
[!code-csharp[](./snippets/animatedbutton.cs?start=62&end=102)]
230+
[!code-csharp[](./snippets/animatedbutton.cs?start=58&end=93)]
219231

220232
#### States and Categories
221233

222-
In Gum, each control type has a specific category name that identifies its state collection. For buttons we use `Button.ButtonCategoryName`:
234+
In Gum, each control type has a specific category name that identifies its state collection. `ButtonVisual` provides access to ready-made states and catgories which we can modify. Before we speicfy how a state should modify the button's appearance, we clear out all existing functionality so that we can fully control the states:
223235

224-
[!code-csharp[](./snippets/animatedbutton.cs?start=104&end=107)]
236+
[!code-csharp[](./snippets/animatedbutton.cs?start=104&end=104)]
225237

226-
Within this category, we define how the button appears in different states by creating `StateSave` objects with specific state names:
238+
Each of the button's states can be accessed through `ButtonVisual`. Since the states were cleared previously, the code assigns only the necessary property assignments in the `Apply` delegate. In our case, we switch between animation chains to create the desired visual effect.
227239

228-
[!code-csharp[](./snippets/animatedbutton.cs?start=109&end=140)]
229-
230-
Each state's `Apply` action defines what visual changes occur when the state becomes active. In our case, we switch between animation chains to create the desired visual effect.
240+
[!code-csharp[](./snippets/animatedbutton.cs?start=107&end=130)]
231241

232242
#### Custom Input Handling
233243

234244
We add custom keyboard navigation to our button by handling the `KeyDown` event:
235245

236-
[!code-csharp[](./snippets/animatedbutton.cs?start=142&end=143)]
246+
[!code-csharp[](./snippets/animatedbutton.cs?start=133&end=133)]
237247

238-
[!code-csharp[](./snippets/animatedbutton.cs?start=152&end=167)]
248+
[!code-csharp[](./snippets/animatedbutton.cs?start=142&end=154)]
239249

240250
This allows players to navigate between buttons using the left and right arrow keys, providing additional control options beyond the default tab navigation.
241251

242252
#### Focus Management
243253

244254
We also add a `RollOn` event handler to ensure the button gets focus when the mouse hovers over it:
245255

246-
[!code-csharp[](./snippets/animatedbutton.cs?start=145&end=146)]
256+
[!code-csharp[](./snippets/animatedbutton.cs?start=136&end=136)]
247257

248-
[!code-csharp[](./snippets/animatedbutton.cs?start=169&end=175)]
258+
[!code-csharp[](./snippets/animatedbutton.cs?start=159&end=162)]
249259

250260
This creates a more responsive interface by immediately focusing elements that the player interacts with using the mouse.
251261

252262
### The OptionsSlider Class
253263

254264
Now we will create a custom `OptionsSlider` class to style the volume sliders. This class inherits from Gum's base `Slider` class and provides a styled appearance consistent with the game's visual theme.
255265

266+
Unlike `AnimatedButton`, the `OptionsSlider` creates a Visual completely from scratch. This class provides an example for how to completely customize a Forms control by recreating its Visual object entirely. We do this because the desired appearance and behavior of our `OptionsSlider` is differs enough from the existing Slider that it is easier to replace its `Visual` entirely.
267+
256268
In the `UI` folder of the *DungeonSlime* project (your main game project), create a new file called `OptionsSlider.cs` and add the following code to it:
257269

258270
[!code-csharp[](./snippets/optionsslider.cs)]

0 commit comments

Comments
 (0)