Skip to content

Commit 5076255

Browse files
Proof to ch 22
1 parent 2e419ef commit 5076255

File tree

6 files changed

+103
-67
lines changed

6 files changed

+103
-67
lines changed

articles/tutorials/building_2d_games/05_content_pipeline/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ To open the *Content.mgcb* content project file in the MGCB Editor using the dot
8888
> dotnet tool restore
8989
> ```
9090
>
91-
> This should restore the tool with the version that is configured. If at any time you upte your `dotnet-tools.json` configuration, e.g. when upgrading to a newer version of MonoGame, **you will need to run this command again**
91+
> This should restore the tool with the version that is configured. If at any time you update your `dotnet-tools.json` configuration, e.g. when upgrading to a newer version of MonoGame, **you will need to run this command again**
9292
9393
| ![Figure 5-3: MonoGame Content Builder Editor (MGCB Editor) Window](./images/mgcb-editor.png) |
9494
|:---------------------------------------------------------------------------------------------:|
@@ -113,7 +113,7 @@ To add assets (such as textures, audio, etc) to the content project:
113113
When adding assets to the content project, a pop-up dialog will appear with the following options:
114114
115115
- **Copy the file to the folder**: Creates a duplicate of the file inside your project's Content folder. This creates an independent copy, meaning any later changes to the original file wo not affect your project.
116-
- **Add a link**: Creates a reference to the original file without making a copy. This maintains a connection to the source file, so any updates to the original will be included when you build. Note that the link uses a path relative to the Content.mgcb file, so if either the source file or your project moves, you will need to re-establish the link.
116+
- **Add a link**: Creates a reference to the original file without making a copy. This maintains a connection to the source file, so any updates to the original will be included when you build. Note that the link uses a path relative to the `Content.mgcb` file, so if either the source file or your project moves, you will need to re-establish the link.
117117
- **Skip**: Cancels adding the current file while continuing with any other selected files.
118118
119119
| ![Figure 5-4: Add existing file pop-up](./images/add-file-popup.png) |
@@ -125,15 +125,15 @@ When adding assets to the content project, a pop-up dialog will appear with the
125125
To create a new asset using one of the built-in asset types in the MGCB Editor:
126126
127127
1. In the Project panel, select the folder where you want to add the new asset. If you want to add it to the root, select the main *Content* node.
128-
2. Right-click on the selected folder and choose *Add > New Item...* from the context menu.
128+
2. Right-click on the selected folder and choose `Add > New Item...` from the context menu.
129129
3. In the dialog that appears, select the type of asset you want to create from the list of available built-in types:
130130
- **Effect (.fx)**: A shader file that creates custom visual effects by controlling how graphics are rendered on the GPU.
131131
- **SpriteFont Description (.spritefont)**: A configuration file that defines how text will be displayed in your game, including character set and font properties.
132132
- **Sprite Effect (.fx)**: A shader specifically designed for use with 2D sprites to create special visual effects.
133133
- **Xml Content (.xml)**: A structured data file for storing game information like levels, dialogues, or configuration settings.
134134
- **LocalizedSpriteFont Description (.spritefont)**: A configuration file for creating fonts with support for multiple languages.
135135
4. Enter a name for your new asset in the *Name* field.
136-
5. Click *Create* to add the new asset to your project.
136+
5. Click `Create` to add the new asset to your project.
137137
138138
| ![Figure 5-5: New file pop-up](./images/new-file-popup.png) |
139139
|:-----------------------------------------------------------:|

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,26 @@ Every customized UI component in Gum starts with a top-level container that hold
2424

2525
The container hierarchy follows a parent-child relationship:
2626

27-
- The top-level container manages the overall size and positioning of the component
28-
- Visual elements like backgrounds, text, and icons are added as children
29-
- Child elements can be positioned relative to their parent container
30-
- Child elements can also be nested within other children, creating deeper hierarchies
27+
- The top-level container manages the overall size and positioning of the component.
28+
- Visual elements like backgrounds, text, and icons are added as children.
29+
- Child elements can be positioned relative to their parent container.
30+
- Child elements can also be nested within other children, creating deeper hierarchies.
3131

3232
This hierarchical structure allows you to build complex UI components from simpler parts, with each part playing a specific role in the overall design.
3333

3434
### Size Relationships with Width and WidthUnits
3535

3636
One powerful feature of Gum is how it handles size relationships between parent and child elements. By using different `WidthUnits` values, you can create dependencies that flow in different directions:
3737

38-
- **RelativeToChildren**: A parent container can size itself based on its children
39-
- **PercentageOfParent**: A child element can size itself as a percentage of its parent
40-
- **Absolute**: An element can have a fixed pixel size
41-
- **RelativeToParent**: An element can size itself relative to a specific container
38+
- **RelativeToChildren**: A parent container can size itself based on its children.
39+
- **PercentageOfParent**: A child element can size itself as a percentage of its parent.
40+
- **Absolute**: An element can have a fixed pixel size.
41+
- **RelativeToParent**: An element can size itself relative to a specific container.
4242

43-
For example, a button might use a text element with `WidthUnits` set to `RelativeToChildren`, which means the text will be exactly the size needed to display its content. The button's container might use `RelativeToChildren` with some additional padding, allowing the button to automatically resize based on its text content.
43+
For example:
44+
45+
- A button might use a text element with `WidthUnits` set to `RelativeToChildren`, which means the text will be exactly the size needed to display its content.
46+
- The button's container might use `RelativeToChildren` with some additional padding, allowing the button to automatically resize based on its text content.
4447

4548
Although we have not explicitly assigned WidthUnits and HeightUnits in our code, we have indirectly set these values by calling the Visual's `Dock` method. Specifically, by passing `Dock.Fill` as the parameter, `WidthUnits` and `HeightUnits` are both set to `RelativeToParent`.
4649

@@ -83,9 +86,9 @@ To convert from pixel coordinates to normalized values, you divide the pixel pos
8386

8487
Rather than directly modifying properties when UI elements change state (like when a button is focused), Gum uses a state-based system. Each control type has a specific category name that identifies its collection of states:
8588

86-
- Buttons use `Button.ButtonCategoryName`
87-
- Sliders use `Slider.SliderCategoryName`
88-
- Other control types have their own category names
89+
- Buttons use `Button.ButtonCategoryName`.
90+
- Sliders use `Slider.SliderCategoryName`.
91+
- Other control types have their own category names.
8992

9093
Within each category, you define named states that correspond to the control's possible conditions:
9194

@@ -114,7 +117,7 @@ Before we create our custom components, we need to update the game's resources t
114117

115118
### Update the Texture Atlas
116119

117-
Before we can start, we first need to update the *atlas.png* texture atlas file for the game. This new version of the texture atlas includes:
120+
First need to update the *atlas.png* texture atlas file for the game. This new version of the texture atlas includes:
118121

119122
- The characters for the font, generated using Bitmap Font Generator (BMFont)
120123
- The sprites for the UI components we will create
@@ -139,11 +142,11 @@ First, download the *.fnt* file by right-clicking the following link and saving
139142

140143
Next, add this font file to your content project using the MGCB Editor:
141144

142-
1. Open the *Content.mgcb* content project file in the MGCB Editor.
143-
2. Right-click the *fonts* folder and choose *Add* > *Existing Item...*.
144-
3. Navigate to and select the *04b_30.fnt* file you just downloaded.
145-
4. In the Properties panel, change the *Build Action* to *Copy*. The MonoGame Content Pipeline cannot process *.fnt* files; we just need it to copy it so we can give it to Gum.
146-
5. Save the changes and close the MGCB Editor.
145+
1. Open the `Content.mgcb` content project file in the MGCB Editor.
146+
2. Right-click the `fonts` folder and choose `Add > Existing Item...`.
147+
3. Navigate to and select the `04b_30.fnt` file you just downloaded.
148+
4. In the Properties panel, change the `Build Action` to `Copy`. The MonoGame Content Pipeline cannot process *.fnt* files; we just need it to copy it so we can give it to Gum.
149+
5. **Save the changes and close the MGCB Editor.**
147150

148151
| ![Figure 21-2: The MGCB Editor with the 04b_30.fnt added to the fonts folder and the Build property set to Copy](./images/mgcb-editor.png) |
149152
| :----------------------------------------------------------------------------------------------------------------------------------------: |
@@ -162,7 +165,7 @@ In [Chapter 18](../18_texture_sampling/index.md#texture-coordinates) we discusse
162165

163166
Since [Gum also uses this coordinate system](#animation-chains), we will update the `TextureRegion` class to easily provide these values for any given region.
164167

165-
Open the *TextureRegion.cs* file in the *MonoGameLibrary* project and add the following properties to the `TextureRegion` class:
168+
Open the `TextureRegion.cs` file in the *MonoGameLibrary* project and add the following properties to the `TextureRegion` class:
166169

167170
[!code-csharp[](./snippets/textureregion/properties.cs)]
168171

@@ -174,11 +177,11 @@ Now that we have all our resources prepared, we can create custom versions of th
174177

175178
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.
176179

177-
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:
180+
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:
178181

179182
[!code-csharp[](./snippets/animatedbutton.cs)]
180183

181-
Next, we will examine the key aspects of this implementation:
184+
Next, we will examine the key aspects of this new `AnimatedButton` implementation:
182185

183186
#### Top-level Container
184187

@@ -243,15 +246,15 @@ This creates a more responsive interface by immediately focusing elements that t
243246

244247
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.
245248

246-
In the *UI* folder fo the *DungeonSlime* project (your main game project), create a new file called *OptionsSlider.cs* and add the following code to it:
249+
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:
247250

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

250253
The `OptionsSlider` is more complex than then [`AnimatedButton`](#the-animatedbutton-class) because it contains more visual elements. Below are the key aspects of this implementation:
251254

252255
#### Slider Components
253256

254-
Our custom slider consists of several components
257+
Walking through the `OptionsSlider` implementation, it consists of several components
255258

256259
1. A background container with a label for the slider.
257260
2. An inner container that holds the slider track.
@@ -293,9 +296,9 @@ Now that we have created our custom controls, we need to update our game scenes
293296

294297
### Updating the TitleScene
295298

296-
First, open the *TitleScene.cs* file in the game project and add the following using declaration to the top of the `TitleScene` class:
299+
First, open the `TitleScene.cs` file in the game project and add the following using declaration to the top of the `TitleScene` class:
297300

298-
[!code-csharp[](./snippets/titlescene/usings.cs?highlight=2)]
301+
[!code-csharp[](./snippets/titlescene/usings.cs?highlight=2,11)]
299302

300303
Next, update both the `_optionsButton` and the `_optionsBackButton` fields to be of our new [`AnimatedButton`](#the-animatedbutton-class) type, and add a new field to store a reference to the texture atlas in.
301304

@@ -319,7 +322,7 @@ Finally, update the `CreateOptionsPanel` method so that:
319322

320323
### Updating the GameScene
321324

322-
Next, open the *GameScene.cs* file in the game project and add the following using declaration to the top of the `GameScene` class:
325+
Next, open the `GameScene.cs` file in the game project and add the following using declaration to the top of the `GameScene` class:
323326

324327
[!code-csharp[](./snippets/gamescene/usings.cs?highlight=2,4)]
325328

articles/tutorials/building_2d_games/21_customizing_gum_ui/snippets/optionsslider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using MonoGameGum.GueDeriving;
99
using MonoGameLibrary.Graphics;
1010

11-
namespace DungeonSlime.Components;
11+
namespace DungeonSlime.UI;
1212

1313
/// <summary>
1414
/// A custom slider control that inherits from Gum's Slider class.

articles/tutorials/building_2d_games/21_customizing_gum_ui/snippets/titlescene/usings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
using MonoGameGum.Forms.Controls;
99
using MonoGameGum.GueDeriving;
1010
using MonoGameLibrary;
11+
using MonoGameLibrary.Graphics;
1112
using MonoGameLibrary.Scenes;

0 commit comments

Comments
 (0)