Skip to content

Commit 023b703

Browse files
committed
Lots of refactoring docs
1 parent 30d9ab1 commit 023b703

File tree

10 files changed

+52
-123
lines changed

10 files changed

+52
-123
lines changed

docs/code/gum-code-reference/component-runtimes.md

+16-32
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ Once you have instantiated your project, you can create a component as shown in
1515

1616
```csharp
1717
// This assumes that your project has at least 1 component
18-
ObjectFinder.Self.GumProjectSave.Components.First()
19-
.ToGraphicalUiElement(SystemManagers.Default, addToManagers:true);
18+
var componentRuntime = ObjectFinder.Self.GumProjectSave.Components.First()
19+
.ToGraphicalUiElement();
20+
21+
// Add this component to the desired container
2022
```
2123

2224
The Components property contains a list of all components, so you can also access components by name or other property. For example, the First method can be used to find a component by name as shown in the following code:
@@ -25,8 +27,8 @@ The Components property contains a list of all components, so you can also acces
2527
var componentSave = ObjectFinder.Self.GumProjectSave.Components
2628
.First(item => item.Name == "ColoredRectangleComponent");
2729

28-
var componentRuntime = componentSave.ToGraphicalUiElement(SystemManagers.Default, addToManagers: true);
29-
// the componentRuntime can be modified here
30+
var componentRuntime = componentSave.ToGraphicalUiElement();
31+
// the componentRuntime can be modified here, and added to the desired container
3032
```
3133

3234
Note that the name passed to the First method should match the name given in Gum. For example, in this case the code searches for a component named ColoredRectangleComponent.
@@ -37,21 +39,6 @@ If a component is in a folder, then use the qualified name relative to the Compo
3739

3840
<figure><img src="../../.gitbook/assets/image (42).png" alt=""><figcaption><p>StandardButton component in the Buttons folder in Gum</p></figcaption></figure>
3941

40-
The ToGraphicalUiElement method can automatically add the component to the root for rendering, or alternatively it can be added to an existing container. If adding to an existing container, then the ToGraphicalUiElement's addToManagers parameter should be false as shown in the following code:
41-
42-
```csharp
43-
var component = var componentSave = ObjectFinder.Self.GumProjectSave.Components
44-
.First(item => item.Name == "MyComponent");
45-
46-
var componentRuntime = componentSave.ToGraphicalUiElement(
47-
SystemManagers.Default, addToManagers: false);
48-
// This assumes that container has been directly added to managers, or is a
49-
// child of a root container which has been added to managers:
50-
container.Children.Add(componentRuntime);
51-
```
52-
53-
For more information about how to add a newly-created component runtime to managers or to a container, see the next section.
54-
5542
## Adding a Component Runtime to a Parent
5643

5744
A newly-created component must be added directly or indirectly to managers. The following are the possible ways to add to managers:
@@ -62,8 +49,7 @@ The newly instantiated component can be added to a container in the screen. This
6249

6350
```csharp
6451
// do not add to managers, since it will be added to a container
65-
var newComponentRuntime = componentSave.ToGraphicalUiElement(
66-
SystemManagers.Default, addToManagers:false);
52+
var newComponentRuntime = componentSave.ToGraphicalUiElement();
6753
// assuming ScreenRoot is a valid root
6854
var container = ScreenRoot.GetGraphicalUiElementByName("DesiredContainer");
6955
container.Children.Add(newComponentRuntime);
@@ -83,8 +69,7 @@ The following code shows how to create a Toast instance to display a message to
8369

8470
```csharp
8571
// assumes toastComponent is a valid component
86-
var newToastRuntime = toastComponent.ToGraphicalUiElement(
87-
SystemManagers.Default, addToManagers:false);
72+
var newToastRuntime = toastComponent.ToGraphicalUiElement();
8873
FrameworkElement.PopupRoot.Children.Add(newToastRuntime);
8974
// the newToastRuntime needs to be removed from PopupRoot later
9075
```
@@ -93,8 +78,7 @@ The following code shows how to create a MessageBox instance and add it to the M
9378

9479
```csharp
9580
// assumes toastComponent is a valid component
96-
var newMessageBoxRuntime = messageBoxComponent.ToGraphicalUiElement(
97-
SystemManagers.Default, addToManagers:false);
81+
var newMessageBoxRuntime = messageBoxComponent.ToGraphicalUiElement();
9882
FrameworkElement.ModalRoot.Children.Add(newMessageBoxRuntime);
9983
// the newMessageBoxRuntime needs to be removed from ModalRoot later
10084
```
@@ -105,23 +89,23 @@ To destroy the component, remove it from its parent, or set its parent to null:
10589
newMessageBoxRuntime.Parent = null;
10690
```
10791

108-
### Adding to Managers
92+
### Adding to GumService Root
10993

110-
Components can be added directly to managers. Usually items are only added directly to managers in the following situations:
94+
Components can be added directly to the GumService Root. Usually items are only added directly to GumService Root in the following situations:
11195

112-
* If they are the root runtime. Usually this is a Screen, but it can be a component if your project is not using screens.
96+
* If they are a Screen, or will not be contained by any other object, such as if your project is not using screens.
11397
* For testing/debugging.
11498
* If your game has multiple roots, you can add instances to a list of GraphicalUiElements which are passed to Update. This is considered an advanced scenario.
11599

116100
```csharp
117-
var newComponentRuntime = componentSave.ToGraphicalUiElement(
118-
SystemManagers.Default, addToManagers:true);
101+
var newComponentRuntime = componentSave.ToGraphicalUiElement();
102+
newComponentRuntime.AddToRoot();
119103
```
120104

121-
To destroy the component, call RemoveFromManagers:
105+
To destroy the component, call RemoveFromRoot:
122106

123107
```csharp
124-
newComponentRuntime.RemoveFromManagers();
108+
newComponentRuntime.RemoveFromRoot();
125109
```
126110

127111
## Troubleshooting Component Creation

docs/code/gum-code-reference/elementsave/tographicaluielement.md

+3-37
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
ToGraphicalUiElement creates a new GraphicalUiElement (visual Gum object) from the calling ElementSave. This method is typically used to create new Screen or Component instances from a Gum project.
66

7-
The conversion of a GraphicalUiElement can optionally add the resulting GraphicalUiElement to managers. This value should be true if the object is not going to be added as a child of any other GraphicalUiElement.
7+
The conversion of a GraphicalUiElement can optionally add the resulting GraphicalUiElement to manager, but this is considered an advance approach. Almost all cases should use the no-argument version of ToGraphicalUiElement.
88

99
## Code Example
1010

@@ -15,40 +15,6 @@ The following code can be used to convert a Screen named "MainMenu" to a Graphic
1515
1616
var screen = gumProject.Screens.Find(item => item.Name == "MainMenu");
1717
// Calling GraphicalUiElement creates the visuals for the screen
18-
var graphicalUiElement = screen.ToGraphicalUiElement(
19-
RenderingLibrary.SystemManagers.Default, addToManagers: true);
18+
var graphicalUiElement = screen.ToGraphicalUiElement();
19+
graphicalUiElement.AddToRoot();
2020
```
21-
22-
### addToManagers Parameter
23-
24-
The `addToManagers` parameter determined whether the GraphicalUiElement is automatically added to managers so that it is directly drawn. This can be called after the element is created, so the following code shows two ways of adding the element to managers:
25-
26-
```csharp
27-
screen.ToGraphicalUiElement(
28-
RenderingLibrary.SystemManagers.Default, addToManagers: true);
29-
// is the same as:
30-
var graphicalUiElement = screen.ToGraphicalUiElement(
31-
RenderingLibrary.SystemManagers.Default, addToManagers: false);
32-
graphicalUiElement.AddToManagers();
33-
```
34-
35-
All GraphicalUiElements need to either be added to managers directly or indirectly. Elements added directly to managers are drawn by `GumService.Default.Draw();` . Any item that is added to managers automatically draws its children, so only root-most objects should be added to managers.&#x20;
36-
37-
Let's look at common scenarios and and discuss when to set `addToManagers` to true.
38-
39-
#### Setting addToManagers to true
40-
41-
Elements should have `addToManagers` set to `true` if any of the following are true:
42-
43-
1. The GraphicalUiElement serves as the root for all other elements. Typically this would be when a Gum project is loaded, the Screen is obtained from the Gum project's `Screens` property and its `ToGraphicalUiElement` method is called.
44-
2. You are performing simple tests such as adding a Gum component to screen for testing, you may want to set addToManagers to true so that it shows up on screen. Note that if this is an object with events (such as a Gum Forms object), it must either be passed to the `GumService.Default.Update` call or it will not respond to the mouse, keyboard, or gamepads.
45-
46-
The most common case for setting AddToManagers to true is if you are creating a Gum screen from a loaded Gum project.
47-
48-
#### Setting addToManagers to false
49-
50-
Elements should have `addToManagers` set to `false` if any of the following are true:
51-
52-
1. If the GraphicalUiElement is being added as a child of another element which has already been added to managers. For example, if a new element is being created and added to an existing Screen or one of its children.
53-
2. If the GraphicalUiElement is being added as a child to existing _root_ containers, such as `FrameworkElement.PopupRoot` or `FrameworkElement.ModalRoot` .
54-
3. If the GraphicalUiElement is being added to managers on a dedicated layer. For example, a Screen could be created and added to a dedicated UI layer to control sorting of all elements in the Screen relative to other manually-created GraphicalUiElements.

docs/code/gum-code-reference/graphicaluielement/removefrommanagers.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Introduction
44

5-
RemoveFromManagers removes the calling GraphicalUiElement from the SystemManagers. This method should be called on GraphicalUiElements which need to be destroyed but do not have parents. To remove a GraphicalUiElement which has parents, remove it from its Parent's Children.
5+
RemoveFromManagers removes the calling GraphicalUiElement from the SystemManagers. This method is typically not called, and instead RemoveFromRoot should be called in most cases.
66

77
## Code Example
88

@@ -11,8 +11,10 @@ The following code shows how to add and remove a GraphicalUiElement.
1111
```csharp
1212
// You can create a GrahicalUiElement from an ElementSave.
1313
// Assume elementSave is valid, such as a Screen obtained from a Gum project
14-
var graphicalUiElement = elementSave.ToGraphicalUiElement(
15-
RenderingLibrary.SystemManagers.Default, addToManagers: true);
14+
var graphicalUiElement = elementSave.ToGraphicalUiElement();
15+
16+
graphicalUiElement.AddToManagers();
17+
1618
// alternatively, could pass addToManagers:false, and explicitly call
1719
// AddToManagers
1820

docs/code/monogame/gum-forms/README.md

+6-11
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,7 @@ Your project is now referenced in your game. Modify the Game file to initialize
125125
public class Game1 : Game
126126
{
127127
private GraphicsDeviceManager _graphics;
128-
129-
// Gum renders and updates using a hierarchy. At least
130-
// one object must have its AddToManagers method called.
131-
// If not loading from-file, then the easiest way to do this
132-
// is to create a ContainerRuntime and add it to the managers.
133-
GraphicalUiElement Root;
128+
GumService Gum => GumService.Default;
134129

135130
public Game1()
136131
{
@@ -141,28 +136,28 @@ public class Game1 : Game
141136

142137
protected override void Initialize()
143138
{
144-
var gumProject = MonoGameGum.GumService.Default.Initialize(
139+
var gumProject = Gum.Initialize(
145140
this,
146141
// This is relative to Content:
147142
"GumProject/GumProject.gumx");
148143

149144
// This assumes that your project has at least 1 screen
150-
Root = gumProject.Screens.First().ToGraphicalUiElement(
151-
SystemManagers.Default, addToManagers: true);
145+
var screen = gumProject.Screens.First().ToGraphicalUiElement();
146+
screen.AddToRoot();
152147

153148
base.Initialize();
154149
}
155150

156151
protected override void Update(GameTime gameTime)
157152
{
158-
MonoGameGum.GumService.Default.Update(this, gameTime, Root);
153+
Gum.Update(this, gameTime);
159154
base.Update(gameTime);
160155
}
161156

162157
protected override void Draw(GameTime gameTime)
163158
{
164159
GraphicsDevice.Clear(Color.CornflowerBlue);
165-
MonoGameGum.GumService.Default.Draw();
160+
Gum.Draw();
166161
base.Draw(gameTime);
167162
}
168163
}

docs/code/monogame/gum-forms/controls/frameworkelement/modalroot-and-popuproot.md

+4-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
The static ModalRoot and PopupRoot properties provide an InteractiveGue which serve as the root for any element which should appear on top of other elements. These properties have the following characteristics:
66

7-
* Automatically created by `FormsUtilities.InitializeDefaults`
7+
* Automatically created by `Gum.Initialize`
88
* Automatically resized to fit the entire screen, including if the `GraphicalUiElement.CanvasHeight` and `GraphicalUiElement.CanvasWidth` change.
99
* Both Remains on top of all other elements for its given layer. ModalRoot appears on top of PopupRoot.
1010

@@ -70,11 +70,8 @@ private void ShowPopup(string text, bool isModal)
7070
Popups can also be created if your game is loading a Gum project. Since the GraphicalUiElement will be added to either ModalRoot or PopupRoot, it should not also be added to managers.
7171

7272
```csharp
73-
// Don't add to managers because it will be contained in a container
74-
// which has already been added to managers
75-
bool addToManagers = false;
7673
var popupComponent = gumProject.Components.First(item => item.Name == "MyPopup")
77-
.ToGraphicalUiElement(SystemManagers.Default, addToManagers);
74+
.ToGraphicalUiElement();
7875

7976
popupComponent.Parent = FrameworkElement.ModalRoot;
8077

@@ -86,16 +83,11 @@ popupComponent.Parent = null;
8683
If you are going to add a Screen to a ModalRoot, then the Screen must have a renderable contained object so that it can have its Parent assigned. You can do this by creating a Screen runtime which inherits from ContainerBase, or you can optionally add an InvisibleRenderable as shown in the following code:
8784

8885
```csharp
89-
// Don't add to managers because it will be contained in a container
90-
// which has already been added to managers
91-
bool addToManagers = false;
9286
var popupScreen = gumProject.Screens.First(item => item.Name == "MyScreen")
93-
.ToGraphicalUiElement(SystemManagers.Default, addToManagers);
94-
// Give the Screen a ContainedObject so that it can have its parent assigned
95-
popupScreen.SetContainedObject (new InvisibleRenderable());
87+
.ToGraphicalUiElement();
9688
popupScreen.Parent = FrameworkElement.ModalRoot;
9789

9890
// later, the popup can be removed:
9991
popupScreen.RemoveFromManagers();
10092
popupScreen.Parent = null;
101-
```
93+
```

docs/code/monogame/loading-.gumx-gum-project.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ protected override void Initialize()
7777
"GumProject/GumProject.gumx");
7878

7979
// This assumes that your project has at least 1 screen
80-
var screenRuntime = gumProject.Screens.First().ToGraphicalUiElement(
81-
SystemManagers.Default, addToManagers:false);
80+
var screenRuntime = gumProject.Screens.First().ToGraphicalUiElement();
8281
screenRuntime.AddToRoot();
8382

8483
base.Initialize();
@@ -96,7 +95,7 @@ Gum.Initialize(
9695

9796
Once a Gum project is loaded, all of its screens and components can be accessed through the object returned from the Initialize method. The code above stores the project in a variable called `gumProject`. Any screen or component can be converted to a GraphicalUiElement, which is the visual object that displays in game.
9897

99-
The code in the previous section creates a `GraphicalUiElement` from the first screen in the project. Note that the `ToGraphicalUiElement` method has an `addToManagers` parameter which determines whether the GraphicalUiElement is added to managers. This is almost always set to false since we add the created screen to the root container, which in turn has already been added to managers.
98+
The code in the previous section creates a `GraphicalUiElement` from the first screen in the project.
10099

101100
For an example of a Game1.cs file which loads a project file, see the MonoGameGumFromFile: [https://github.com/vchelaru/Gum/blob/0e266942560e585359f019ac090a6c1010621c0b/Samples/MonoGameGumFromFile/MonoGameGumFromFile/Game1.cs#L76-L82](https://github.com/vchelaru/Gum/blob/0e266942560e585359f019ac090a6c1010621c0b/Samples/MonoGameGumFromFile/MonoGameGumFromFile/Game1.cs#L76-L82)
102101

@@ -106,9 +105,7 @@ You can get a reference to elements within the screen by calling `GetGraphicalUi
106105

107106
```csharp
108107
// Load the gum project (see code above)
109-
var screenRuntime = gumProject.Screens.First().ToGraphicalUiElement(
110-
SystemManagers.Default,
111-
addToManagers:false);
108+
var screenRuntime = gumProject.Screens.First().ToGraphicalUiElement();
112109
screenRuntime.AddToRoot();
113110

114111
// Items in the screen can be accessed using the GetGraphicalUiElementByName method:
@@ -141,8 +138,7 @@ public class Game1 : Game
141138
var gumProject = Gum.Initialize(
142139
this, "GumProject/GumProject.gumx");
143140
// This assumes that your project has at least 1 screen
144-
var screenRuntime = gumProject.Screens.First().ToGraphicalUiElement(
145-
SystemManagers.Default, addToManagers: false);
141+
var screenRuntime = gumProject.Screens.First().ToGraphicalUiElement();
146142
screenRuntime.AddToRoot();
147143

148144
base.Initialize();

docs/code/monogame/tutorials/gum-project-.gumx-tutorial/gum-forms.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public class Game1 : Game
4545

4646
var screen = gumProject.Screens.Find(item => item.Name == "TitleScreen");
4747

48-
var screenRuntime = screen.ToGraphicalUiElement(
49-
RenderingLibrary.SystemManagers.Default, addToManagers: false);
48+
var screenRuntime = screen.ToGraphicalUiElement();
5049
screenRuntime.AddToRoot();
5150

5251
base.Initialize();
@@ -101,8 +100,7 @@ protected override void Initialize()
101100

102101
var screen = gumProject.Screens.Find(item => item.Name == "TitleScreen");
103102

104-
var screenRuntime = screen.ToGraphicalUiElement(
105-
RenderingLibrary.SystemManagers.Default, addToManagers: false);
103+
var screenRuntime = screen.ToGraphicalUiElement();
106104
screenRuntime.AddToRoot();
107105

108106
+ var listBox = screenRuntime.GetFrameworkElementByName<ListBox>("ListBoxInstance");

docs/code/monogame/tutorials/gum-project-.gumx-tutorial/gum-screens.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ To show the screen in game, modify the Initialize method as shown in the followi
5252
<strong>+ // the Screens list contains all screens. Find the screen you want
5353
</strong>+ var screen = gumProject.Screens.Find(item => item.Name == "TitleScreen");
5454
+ // Calling GraphicalUiElement creates the visuals for the screen
55-
<strong>+ var screenRuntime = screen.ToGraphicalUiElement(
56-
</strong><strong>+ RenderingLibrary.SystemManagers.Default, addToManagers: false);
55+
<strong>+ var screenRuntime = screen.ToGraphicalUiElement();
5756
</strong><strong>+ screenRuntime.AddToRoot();
5857
</strong>
5958
base.Initialize();
@@ -91,8 +90,7 @@ We can modify the displayed string by getting an instance of the Text and modify
9190

9291
var screen = gumProject.Screens.Find(item => item.Name == "TitleScreen");
9392

94-
var screenRuntime = screen.ToGraphicalUiElement(
95-
RenderingLibrary.SystemManagers.Default, addToManagers: false);
93+
var screenRuntime = screen.ToGraphicalUiElement();
9694

9795
screenRuntime.AddToRoot();
9896

@@ -120,8 +118,7 @@ protected override void Initialize()
120118

121119
var screen = gumProject.Screens.Find(item => item.Name == "TitleScreen");
122120

123-
var screenRuntime = screen.ToGraphicalUiElement(
124-
RenderingLibrary.SystemManagers.Default, addToManagers: false);
121+
var screenRuntime = screen.ToGraphicalUiElement();
125122

126123
screenRuntime.AddToRoot();
127124

0 commit comments

Comments
 (0)