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
* 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
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/20_implementing_ui_with_gum/index.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ Gum addresses these challenges with ready-made solutions, allowing us to focus o
37
37
> [!IMPORTANT]
38
38
> 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.
39
39
>
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.
41
41
>
42
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)
43
43
@@ -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.5.1.1`.
324
+
5. When prompted for a version choose version `2025.8.3.3`.
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.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.
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.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 incase 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 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
@@ -385,7 +385,7 @@ With the Gum NuGet package added to our project, we need to initialize Gum in ou
385
385
386
386
First, open the `Game1.cs` file and add the following new using statements to the top:
Next, add the following method to the `Game1` class to encapsulate the initializations of the Gum UI service:
391
391
@@ -397,10 +397,10 @@ 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)` 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.
401
401
402
402
> [!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).
404
404
405
405
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.
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/21_customizing_gum_ui/index.md
+31-19Lines changed: 31 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -182,29 +182,41 @@ Now that we have all our resources prepared, we can create custom versions of th
182
182
183
183
### The AnimatedButton Class
184
184
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.
186
190
187
191
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:
188
192
189
193
[!code-csharp[](./snippets/animatedbutton.cs)]
190
194
191
195
Next, we will examine the key aspects of this new `AnimatedButton` implementation:
192
196
193
-
#### Top-level Container
197
+
#### ButtonVisual
194
198
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.
We can modify the Visual to give it the appropriate size.
198
202
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.
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.
200
206
201
207
#### Nine-slice Background
202
208
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:
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.
206
214
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.
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:
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.
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.
This allows players to navigate between buttons using the left and right arrow keys, providing additional control options beyond the default tab navigation.
241
251
242
252
#### Focus Management
243
253
244
254
We also add a `RollOn` event handler to ensure the button gets focus when the mouse hovers over it:
This creates a more responsive interface by immediately focusing elements that the player interacts with using the mouse.
251
261
252
262
### The OptionsSlider Class
253
263
254
264
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.
255
265
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
+
256
268
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:
0 commit comments