You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Removed `.Visual` which is no longer needed to position or size controls.
Updated link to the code section in Gum.
Updated to latest Gum NuGet package.
Clarified state creation, which is done differently in the AnimatedButton vs OptionsSlider
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/20_implementing_ui_with_gum/index.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ Gum addresses these challenges with ready-made solutions, allowing us to focus o
39
39
>
40
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.
41
41
>
42
-
> 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)
42
+
> 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/about](https://docs.flatredball.com/gum/code/about)
// Set the X and Y position so it is 20px from the left edge
220
220
// and 20px from the bottom edge.
221
-
startButton.Visual.X=20;
222
-
startButton.Visual.Y=-20;
221
+
startButton.X=20;
222
+
startButton.Y=-20;
223
223
```
224
224
225
225
The `Click` event is raised whenever the button is activated and provides a standard way to respond regardless of input device:
@@ -291,7 +291,7 @@ Gum allows you to customize visuals in two ways:
291
291
With simple property changes, you can directly assign values in code. For example, the following code example changes the width of a button:
292
292
293
293
```cs
294
-
startButton.Visual.Width=100;
294
+
startButton.Width=100;
295
295
```
296
296
297
297
Direct property assignment works well for initial setup, such as positioning elements or setting their dimensions when first creating your UI. However, when you need visual elements to respond to user interactions (like highlighting a button when it is focused), a different approach is required.
@@ -321,7 +321,7 @@ To add the Gum NuGet package in Visual Studio Code:
321
321
2. Choose `Add NuGet Package` from the context menu.
322
322
3. Enter `Gum.MonoGame` in the `Add NuGet Package` search prompt and press Enter.
323
323
4. When the search finishes, select the `Gum.MonoGame` package in the results
324
-
5. When prompted for a version choose version `2025.8.3.3`.
324
+
5. When prompted for a version choose version `2025.12.9.1`.
325
325
326
326
#### [Visual Studio 2022](#tab/vs2022)
327
327
@@ -332,7 +332,7 @@ To Add the Gum NuGet package in Visual Studio 2022:
332
332
3. In the NuGet Package Manager window, select the `Browse` tab if it is not already selected.
333
333
4. In the search box, enter `Gum.MonoGame`.
334
334
5. Select the "Gum.MonoGame" package from the search results.
335
-
6. On the right, in the version dropdown, select version `2025.8.3.3` and click the "Install" button.
335
+
6. On the right, in the version dropdown, select version `2025.12.9.1` and click the "Install" button.
336
336
337
337
#### [dotnet CLI](#tab/dotnetcli)
338
338
@@ -342,7 +342,7 @@ To add the Gum NuGet package using the dotnet CLI:
> 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 incase there are any breaking changes from the code that is presented in this tutorial.
358
+
> This tutorial uses version `2025.12.9.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/upgrading) before updating incase there are any breaking changes from the code that is presented in this tutorial.
359
359
360
360
### Adding UI Sound Effect
361
361
@@ -397,7 +397,7 @@ Finally, update the [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initializ
397
397
398
398
The following is a breakdown of this initialization process:
399
399
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.
400
+
1. **Basic Initialization**: `GumService.Default.Initialize(this, DefaultVisualsVersion.V3)` sets up the Gum system with our game instance. This is required for any gum project. The second parameter specifies the default visual styling. V3 is the latest version which makes it easy to style the default controls.
401
401
402
402
> [!NOTE]
403
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).
@@ -668,7 +668,7 @@ While this UI is now functional, you may have noticed that it uses Gum's default
668
668
:::question-answer
669
669
The two ways to customize Gum UI elements are:
670
670
671
-
1. **Direct property assignment**: Setting properties directly in code (like `MyButton.Visual.Width = 100`). This works well for initial setup and static properties.
671
+
1. **Direct property assignment**: Setting properties directly in code (like `MyButton.Width = 100`). This works well for initial setup and static properties.
672
672
2. **States**: Using Gum's state system (`StateSave` objects) to define different visual states that can be applied in response to specific conditions or events. States are automatically applied by Forms controls in response to user interactions (like focus or highlighting).
673
673
674
674
States are useful for dynamic changes that occur during gameplay, as they separate visual response logic from game logic.
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/20_implementing_ui_with_gum/snippets/titlescene/createoptionspanel.cs
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/21_customizing_gum_ui/index.md
+9-14Lines changed: 9 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,18 +84,12 @@ To convert from pixel coordinates to normalized values, you divide the pixel pos
84
84
85
85
#### Visual States
86
86
87
-
Rather than directly modifying properties when UI elements change state (like when a button is focused), Gum uses a state-based system. Each controltype has a specific category name that identifies its collection of states:
87
+
When UI elements change state (like when a button is focused), Gum uses a state-based system. A control's visual can be casted to a type specific to that control to access its states. Each control-specific visual has a type name matching its control, with the word Visual appended. For example, a button's `Visual` can be casted to type `ButtonVisual` to access button-specific properties and states. Once casted, the visual provides a `States` property containing all available states for that control type. Most control visuals, including `ButtonVisual`, provide the following States properties:
88
88
89
-
- Buttons use `Button.ButtonCategoryName`.
90
-
- Sliders use `Slider.SliderCategoryName`.
91
-
- Other control types have their own category names.
92
-
93
-
Within each category, you define named states that correspond to the control's possible conditions:
94
-
95
-
- "Enabled" (the normal, unfocused state).
96
-
- "Focused" (when the control has focus).
97
-
- "Highlighted" (when the mouse hovers over the control).
98
-
- "Disabled" (when the control cannot be interacted with).
89
+
- States.Enabled (the normal, unfocused state).
90
+
- States.Focused (when the control has focus).
91
+
- States.Highlighted (when the mouse hovers over the control).
92
+
- States.Disabled (when the control cannot be interacted with).
99
93
100
94
Each state contains an `Apply` action that defines what visual changes occur when that state becomes active. For example, when a button becomes focused, its state might change the background color or switch to an animated version.
101
95
@@ -301,6 +295,8 @@ The slider uses color changes to provide visual feedback:
301
295
302
296
When the slider is focused, all its elements change from gray to white, making it clear to the player which UI element currently has focus.
303
297
298
+
Notice that unlike the button which used existing states, we create the slider's states from scratch. These states must use specific names so that Gum knows to use them when the slider gains or loses focus. These names can be obtained through the `FrameworkElement` class. Our slider only needs to handle focused and unfocused (which returns to the `enabled` state), bug Gum provides additional states if needed.
299
+
304
300
#### Fill Visualization
305
301
306
302
One of the most important aspects of a slider is the visual representation of its value. We achieve this by updating the width of the `_fillRectangle` element:
@@ -397,7 +393,7 @@ The principles you have learned in this chapter extend beyond the specific compo
397
393
:::question-answer
398
394
The two main approaches are:
399
395
400
-
- **Direct property assignment**: Setting properties directly in code (like `button.Visual.Width = 100`). This approach is best for initial setup of UI elements and static properties that do not change during gameplay.
396
+
- **Direct property assignment**: Setting properties directly in code (like `button.Width = 100`). This approach is best for initial setup of UI elements and static properties that do not change during gameplay.
401
397
- **States (StateSave objects)**: Defining different visual states that are applied automatically in response to interactions. This approach is best for dynamic changes that happen during gameplay, like highlighting a button when it is focused or changing colors when a slider is adjusted.
402
398
403
399
:::
@@ -434,8 +430,7 @@ The principles you have learned in this chapter extend beyond the specific compo
434
430
:::question-answer
435
431
Gum's state system links with Forms controls through specifically named categories and states:
436
432
437
-
- Each Forms control type has a reserved category name (e.g., Button.ButtonCategoryName)
438
-
- Within that category, the control looks for states with specific names (Enabled, Focused, Highlighted, etc.)
433
+
- Each Forms control type has states specific to that type which are accessed through a casted visual (e.g., `buttonVisual.States.Enabled`)
439
434
- When the control's state changes (like gaining focus), it automatically applies the corresponding visual state
0 commit comments