All pages
Powered by GitBook
1 of 22

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Tutorials and Examples

Intro Tutorials

Introduction

The tutorials contained within this section provide an introduction to working with the Gum UI tool. This tutorial is a great place to start whether you are an artist who will be creating screens and components in Gum, or if you are a programmer who will be integrating Gum into your game or application.

If you are using Gum in a game project, you may want to continue reading other sections after you finish these tutorials.

Animation Tutorials

4 - Combining Multiple Categories

Animation keyframes are defined by creating states in categories. If an animation uses two states in the same category then the animation will tween between them

Examples

2 - Variables Tab

Introduction

The Gum Variables tab displays all available variables when editing an instance or element. While the Editor tab is handy for quick edits such as positioning and resizing instances, the Variables tab exposes all variables. It is also useful for making fine changes to instances, such as by moving an instance by a single pixel.

The Variables tab shows variables for the selected instance or element.

Variables tab in Gum

Editing Variables

Variables can be edited by changing values on the selected variable. For example, to move the text to the right, change its X value to a positive number. Press Enter or Tab to apply the changes:

Positioning Instances

Gum provides a flexible positioning system. The position of an element is a result of a number of variables. We'll go over a few here.

By default all instances are positioned by their top-left corner. For example, setting the Text instance's X and Y to 0 aligned its top-left position to the top-left of the screen (which is identified by a dotted line.

We can change the origin of the Text object by setting its X Origin and Y Origin values. Notice that if X Origin is set to Center then the Text object is positioned by its center:

You may need to pan the view in the Editor tab to be able to see the Text object. Gum provides multiple ways to pan the view:

  • Press and hold the middle mouse button while the cursor is over the preview window. While the middle mouse button is down, move the mouse cursor.

  • Use the scroll bars on the bottom and side of the view

  • Hold down CTRL and press the arrow keys

Changing the X Origin value changes the origin of the selected instance; however, it is still positioned relative to the top-left corner of the Text instance's container - which in this case is the entire screen designated by the dotted outline rectangle.

We can change the origin that the Text is relative to by changing the X Units. By default the X Units variable is set to Pixels From Left and Y Units is set to Pixels From Top.

Changing the X Units to Pixels From Right causes the Text to be positioned on the right-side of the screen.

Text Alignment

The X,Y, Origin, and Units values are all available for every type of element in Gum; however, these values only change the bounds. In the case of a Text object we may be interested in how the text is aligned within the bounds. The Text object offers two variables for aligning its text: Horizontal Alignment and Vertical Alignment. Changing the Horizontal Alignment to Center centers the Text within its bounds:

Default and overriding values

You may have noticed that some variables in the Variables tab have a green background while others have a white background. For example, in the image above the TextInstance's Vertical Alignment is green. This is because instances are not required to define values for every variable. Whenever an instance does not set a variable value, it uses the value that is defined in the Standard Element definition.

To see in action, select the Text item under the Standard folder. Notice that all values have a white background. Keep in mind the the default values for Horizontal Alignment and Vertical Alignment:

If the default Horizontal Alignment and Vertical Alignment values are changed, the changes will immediately be reflected in the preview window for the default Text configuration:

Now if we select the TextIntance we will see that the Vertical Alignment is using the Bottom value; however the Horizontal Alignment is still using Center - this is because a value that is explicitly set on an instance will always override the default value set in the Standard element. Notice that Horizontal Alignment has a white background (indicating a custom value) and Vertical Alignment has a green background (indicating a default value).

Values can be reverted back to their default simply by right-clicking on the variable name in the Variables tab and selecting Make Default

3 - Files

Introduction

Gum supports loading image files for Sprites and NineSlices. This tutorial discusses how to load files, and how they are referenced in Gum.

Setting up a workspace

First we'll set up a workspace:

  1. Create a Screen called SpriteScreen.

  2. Drag+drop a Sprite into the newly-created Screen

Setting the Sprite Source File

The Source File property is the image that the Sprite displays. Usually Source Files are of the .png file format. To set the source file:

  1. Select SpriteInstance

  2. Find Source File in the Variables tab

  3. Click the "..." button to bring up a file window

  4. Navigate to the location of the file you would like to load

If you select a file which is not located in the same folder or a sub folder of your gum project, Gum asks if you would like to reference the file in its original location or create a copy.

Usually it's best to copy the file to the Gum project folder so that the Gum project can be moved to different computers without breaking file references.

Texture Coordinates

Sprites can display portions of their Source File. Files which combine multiple images are often called sprite sheets or tile sheets, and are commonly used in game development to keep art organized and to improve performance.

For example, the following file contains images for an animated character, ground tiles, and other entities for a platformer game.

If we download this file and set it as our , then the sprite displays the entire file.

We can display a portion of the Sprite rather than the entire file:

  1. Click on the Texture Coordinates tab

  2. Check the Snap to grid option to make it easier to select a region

  3. Double-click anywhere on the image to select a region around the cursor

  4. Move the selected region to the desired location to adjust the sprite's texture coordinate values

5 - Exposing Variables

Introduction

The ability to expose variables in Gum makes components flexible. For this example we will continue the Button example from the last tutorial.

Recap

The last tutorial created a Button component with Text and ColoredRectangle instances. The instances were set up to be positioned correctly according to the size of the button.

We then created a MainMenu screen and added a few instances of the Button component to the MainMenu screen.

While the size and positioning functionality in our button works well, the Text itself always says "Hello".

Exposing the Text variable

By default a Button only exposes its top level variables. Variables on instances inside the button are not available outside of the button. In programming terms these variables are considered protected.

However, we can expose variables on instances so that they can be modified in our MainMenu screen.

To do this:

  1. Select TextInstance under Button

  2. Find the Text variable in the Variables tab (Second column, under the "States" panel)

  3. Right-click on the text box and select Expose Variable

You can verify that the Text variable is exposed by clicking on the the Button component and seeing the Text variable under the Exposed category:

Setting instance variables

Now that the Text variable is an exposed variable, it can be changed on each Button instance. To do this:

  1. Select one of the Buttons in MainMenu

  2. Change its Text to "Button 1"

Feel free to set different Text values on all of the buttons. Notice that the Text may word-wrap.

Conclusion

This tutorial shows how to expose Text values per-instance. You can expose other instance variables in your components to customize instances. Other examples of variables which may be exposed include:

  • Visibility of icons on a Button component

  • Font sizes on a Label component

  • Sprite visibility showing the number of connected gamepads on a JoinGame component

  • Width (percentage) on a HealthBar component

It's best to experiment with exposed variables to get a feel for how you can use them in your own components.

1 - The Basics

Introduction

This page walks you through the basics of using the Gum UI tool, which we'll refer to simply as Gum for this and all other documentation.

Empty Gum Project

Gum Elements

Gum separates its elements into three categories: Screens, Components, and Standard. Behaviors are an advanced topic that we'll skip for these tutorials.

Standard elements represent the building-blocks for screens and components, and all projects use the same set of standard elements. To see the list of elements, expand the Standard tree item. Clicking on any element displays it in the preview window.

Sprite element is selected in the image above. Notice that since a SourceFile is not set, the Sprite renders as a red X.

Standard Types

  • Circle - circle outline. These are usually not used for UI, but can be used if you are defining collision in your Gum objects for a game.

  • ColoredRectangle - filled-in rectangle. These are often used for solid-colored backgrounds and frames.

  • Container - invisible object used to contain other objects. These are used to provide margins, change layouts (such as vertical vs horizontal stacking), and to organize your UI.

Plugins can add additional standard elements. The list above is the default list of standard elements before plugins have made any changes.

Components

Components are objects which can contain standard elements and instances of other components. Components can be very simple, such as a Label, or very complex, such as an options menu with dozens of instances. Items added to components or screens are called instances.

Screens

Screens are objects which can contain standard elements and instances of other components. Unlike Components, Screens cannot be added to other Screens. Screens exist mainly for organization. Screens can be simple, such as a loading screen, or complex such as a HUD in an RTS game.

Components vs. Screens

Components and screens are similar - both can contain instances of standard elements, and both can contain other components. The only difference between screens and components is that screens cannot contain other screens.

For example you can think of screens in a video game which might include a main menu, credits screen, options screen, and level selection screen. You can think of components as elements which are composed of multiple standard elements. Examples include a Button component which is made up of a Sprite instance and a Text instance, or a Logo component which may be made up of multiple Sprites and Text objects.

Creating a Screen

To create a screen:

  1. Right-click on the Screens tree item and select Add Screen

  2. Enter the name of the new screen - such as MainMenu

  3. Click OK. The newly-created screen is created and selected

Adding instances

Instances of standard and component elements can be added to screens and components. To add an instance:

  1. Select the destination screen or component. For example, select the MainMenu screen

  2. Push the left mouse button (but don't release it) on the Text item. If you happen to release the mouse button, this selects the Text item, so you need to re-select the destination (MainMenu).

  3. Drag the Text item onto the Editor tab

Alternatively, you can also drag+drop a standard element into a screen in the tree view.

If an element is dropped in the Editor window, it appears at the location where it has been dropped - setting its X and Y values. If an element is dropped in the Project tab, then it preserves its default X and Y values.

Editing in the preview window

Once an instance is a part of a screen or component it can be edited visually in the preview window. The selected instance has eight (8) handles surrounding it. These are called the resize handles and can be used to change the selected instance's width and height.

In the case of the Text object, the resize handles are used to control how the text object performs line wrapping.

You can use the resize handles to resize the instance, or you can simply push the mouse button and drag inside the instance to change its position. Notice that an object's outline is displayed when the cursor is hovering over the instance.

Conclusion

This tutorial introduces the basics of working with standard elements and adding them to Screens. The next tutorial covers the Variables tab which can be used to access all element variables.

6 - Parent

Introduction

The Components tutorial shows how Text and ColoredRectangle instances can be sized and positioned according to the Button that they are a part of. Instances can use other instances as their parents. Gum does not place a limit to the depth of the parent/child hierarchy, enabling flexible and responsive layouts through.

Parent/child relationships are useful for

  • Automatically adjusting margins and backgrounds

  • Word-wrapping text relative to a parent

3 - Playing Animations inside other Animations

Introduction

Gum supports creating animations which can play other animations. This is especially useful when creating animations in Screens that contain components which themselves have animations. This tutorial will build upon the where we created an animated component called TextComponent.

1 - Introduction to Animation

Introduction

Gum supports creating and previewing animations in the editor through the use of states. The general workflow for creating an animation is as follows:

  1. Create states representing the keyframes in the animation (usually one category per animation)

Silhouette (Solid Colored Sprite)

Introduction

Render targets and blend modes can be combined to create a silhouette of any shape. This example uses a Sprite with transparency. It uses the Bear.png file:

Padding

Introduction

The concept of padding is often used to add spacing between the edge of a container and its children. Padding can be achieved by adding sub-containers.

Creating a Container

Enter the name "Text" for the variable name
Right-click to expose a variable
Text variable is exposed
Text values set per button instance
Changing a Text's X variable
Setting X and Y to 0 positions the instance at the top-left of the screen
Text with X Origin set to Center
Default X Units
Text moved to the right-side of the screen by changing its X Units
Centered text in its bounds
Default values for Text Standard Element
Changed values update immediately in the Editor tab
Default values are green, explicitly set values are white
Right-click Make Default option
  • Click "Open" in the file window

  • Once Source File is set, the Sprite displays the image in the Editor tab

  • Drag+drop a Sprite onto SpriteScreen
    SpriteInstance displaying a bear Source File
    Gum asks to copy files if they are outside of the project directory
    Sprite displaying entire sprite sheet
    Texture coordinate values can be adjusted in the Texture Coordinates tab
    NineSlice - visual object which uses nine sprites to create a resizable object from a source PNG (or portion of a PNG). The corner sprites (4) are not resized. The top, bottom, left, and right sprites are stretched on one axis. The middle sprite stretches both horizontally and vertically. These are used to create resizable frames.
  • Polygon - polygon outline which can have any number of points. These are usually not used for UI, but can be used if you are defining collision in your Gum objects for a game.

  • Rectangle - rectangle outline. These can be used for single-line frames or if you are defining collision in your Gum objects for a game.

  • Sprite - a visual object which displays a source PNG (or a portion of a PNG). These are used for icons, backgrounds, and other visual objects which are usually not resized dynamically.

  • Text - a visual object which can display characters. These are used for any situation where text needs to be displayed such as labels and paragraphs.

  • Release the mouse button. A new text instance appears in your screen.
    Adding a Text instance to the MainMenu screen in the Editor tab
    Screens, Components, and Standard folders
    A Sprite standard element with no source file set
    Right-click, Add Screen option
    The MainMenu screen in Gum
    Adding a Text instance to the MainMenu screen in the Project tab
    A selected Text instance with resize handles
    Moving and resizing a Text object.

    Stacking objects on top of each other or side by side

  • Placing objects next to other objects which are dynamically sized

  • Creating tables and other complicated layout objects

  • Creating a Component

    For this example we'll create a Component for displaying distance. This component has two Text instances:

    • ValueText - the Text instance responsible for displaying the value for the distance. For example "100".

    • UnitsDisplay - the Text instance responsible for displaying the units for the distance. For example "km" for kilometers.

    The two Text instances will have different colors so that ValueText stands out.

    A single Text instance can support inline styling. We use two text instances here to show how to work with the Parent variable, but for more info see the Text property page.

    To create our component:

    1. Create a new component named MeasurementDisplay

    2. Drop two Text objects in the newly-created component

    3. Name the first ValueText

    4. Name the second UnitsDisplay

    MeasurementDisplay component

    Positioning according to a parent

    Next we'll make the UnitsDisplay use the ValueText as its parent:

    1. Select the UnitsDisplay Text instance

    2. Change its Parent to ValueText

    3. Change its X Units to Pixels From Right. This makes the text object positioned according to its parent's right edge

    4. Change its X to 10 . This means the UnitsDisplay text is 10 units offset from the right edge of its parent ValueText

    5. Verify its Y is 0

    UnitsDisplay positioned relative to the right-side of ValueText

    The ValueText actual width should be based on its contents, so we'll do the following:

    1. Select ValueText

    2. Change Width Units to Relative to Children

    3. Change Width to 0

    By setting these values, ValueText is now sized according to its children, which in this case are its letters.

    Width and Width Units set to size ValueText according to its contained Text variable

    You may have noticed that UnitsDisplay is also a child of the ValueText. However, since UnitsDisplay is explicitly positioned outside of the bounds of its parent ValueText, then ValueText ignores this child when calculating its own Width. For a detailed discussion of Width Units and whether children are ignored, see the Width Units page.

    Adjusting Colors

    Now that we have adjusted the position, size, and parent values on our Text instances, let's modify the color of the UnitsDisplay:

    1. Select the UnitsDisplay

    2. Change Red to 200

    3. Change Green to 150

    4. Change Blue to 0

    Text with changed color values

    Changing ValueText

    Now that we have set up a parent/child relationship between ValueText and UnitsDisplay, UnitsDisplay automatically adjusts its position in response to changes in ValueText. Any change on ValueText resulting in the right-side of the parent changing automatically adjusts the position of UnitsDisplay.

    For example, if we change the Text property on ValueText, it grows or shrinks in response.

    Changing the Text property results in ValueText changing its effective width

    Width and Effective Width

    As mentioned above, changing the Text property causes ValueText to grow or shrink. However, regardless of its size, the Width property is still set to 0.

    Width is 0 despite the size being larger than 0

    The Width variable is used in combination with its Width Units to calculate an effective width. In this case, the effective width is determined by the Text property on ValueText. It's important to note that all Gum objects have effective values for x, y, width, and height, all of which are determined by their respective units values.

    Children always depend on their parents' effective values rather than their explicitly set values. Gum helps us visualize the effective values when we mouse over one of the resize handles on the selected object. For example, the following image shows the Width, Width Units, and effective width values of our ValueText.

    Width is 0, Width Units is Relative To Children, effective width is 65
    Creating a Screen

    First we'll create a Screen called AnimatedScreen. To do this:

    1. Right-click on Screens

    2. Select "Add Screen"

    3. Enter the name "AnimatedScreen" and click the OK button

    4. Drag+drop a few TextComponents into the Screen and spread them out visually

    Defining the initial state

    The animation we will be creating in our Screen will start with all TextComponents being invisible, then each one appearing by playing their Show animation. The animations will be slightly staggered. First we'll add the initial state where all of the TextComponents are invisible. To do this:

    1. Verify that the AnimatedScreen is selected

    2. Right-click in the states area and select "Add Category" name it "ScreenCategory"

    3. Right-click the category and select "Add State"

    4. Name the state "AllInvisible" and click OK

    5. Click to select the "AllInvisible" state.

    6. Select one of the TextComponents

    7. Set its HideShow State to Hidden

    8. Repeat setting the State to Hidden for the other TextComponents

    Creating the Animation

    Now we have all of the states and animations that we'll use as keyframes in our animation. To create the animation:

    1. Select AnimatedScreen

    2. Select "View" -> "View Animations"

    3. Click "Add Animation"

    4. Name the animation "ShowAll"

    5. Select the ShowAll animation

    6. Click "Add State"

    7. Select "ScreenCategory/AllInvisible" and click OK

    The animation now sets all TextComponents to their Hidden state initially.

    Adding Sub-Animations

    Next we'll be adding animations to animate the TextComponent instances to visible. To do this:

    1. Bring up the animation window for AnimatedScreen if it is not already showing

    2. Select "ShowAll"

    3. Click "Add Sub-animation"

    4. Select the first TextComponentInstance

    5. Select the Show animation and click OK

    6. Select the newly-created animation and set its Time to 0.5

    7. Repeat the above steps to add animations for the other two TextComponents, but set their times to 1.0 and 1.5

    Now the animation can be played or previewed with the slider bar:

    previous tutorial

    Add an animation to the component or screen

  • Add states to the animation and adjust their time

  • Set interpolation values for each keyframe to control "easing"

  • Examples of animations

    Gum's animation system is very powerful and can be used in a variety of situations:

    • Animations which play when a screen or component is shown or hidden

    • Animations used to transition between behavior states such as changing a button from regular to highlighted state

    • Lengthy animations which can last multiple seconds for complex transitions

    Storage of animations

    Animations are stored separate from the screen or component on the file system. If a screen or component contains at least one animation then Gum will save a Gum Animation XML file (.ganx extension) with the word "Animations" appended on the screen or component's name. In other words GameScreen would have a file called GameScreenAnimations.ganx in the same folder containing information about its animations.

    Playing animations

    Once an animation has been made it can be played back in editor. The following shows how an animation is played back, both in real time and also by dragging the slider.

    Creating a Silhouette Component

    To create a Component that can display a Silhouette, first create a new component named SilhouetteSprite.

    Add a ColoredRectangle instance and a Sprite instance. The order matters - the Sprite should be drawn on top of the ColoredRectangle (it should show up 2nd in the Project tree view).

    For this example, we will use the Alignment tab to adjust each item:

    • SilhouetteSprite (main component) - Dock Size to Children

    • ColoredRectangleInstance - Dock Fill

    The Sprite should already be sized according to its source file, although you can change this size if desired.

    The ColoredRectangleInstance defines the color of the silhouette so change it to a desired color.

    Your component should look similar to the following image:

    SilhouetteSprite initial setup

    Next we'll adjust the transparency values. To do this:

    1. Set SilhouetteSprite Is Render Target to true

    2. Set SpriteInstance Blend to MinAlpha

    Your sprite's silhouette should now appear, using the underlying color as its own color.

    First we'll create a top-level container. This container controls the size of all objects internally, including the background. We will also include a background object which is sized according to the container. To keep things simple, this example uses a dark blue ColoredRectangle.

    The ColoredRectangle is set so its size matches its parent.

    Container with a blue background

    Next we can add another container to the top-level container. By default this container sits at the top-left of the parent when it is added with a drag+drop.

    Container added with drag+drop

    To have the container fill its parent, but also include padding:

    1. Select the inner container

    2. Click the Alignment tab

    3. Enter the desired padding in the Margin text box

    4. Click the Dock Fill button

    Creating padding by havign an internal continer use Margin of 10

    This inner container can now be used to hold all children. Note that if you are creating a Component and you want to make this be the default container for children, you may want to set the Default Child Container to this inner container. For more information see the Default Child Container page.

    Bottom-Up Stack

    Bottom-up stacks can be used to display stacks of elements which should move up as more are added. This concept is similar to messages received in a chat window. Gum layout can be used to produce this type of stack.

    This example shows a bottom-up stack (vertical) but the same approach could be used to create a right-aligned stack.

    Items in a bottom-up stack need a parent Container. This Container could be an instance of a Container or a component since components usually have their Base Type set to Container. For this example we'll use a container.

    Container instance

    This container to stack needs the following variables set:

    • Children Layout set to Top to Bottom Stack so all children stack vertically

    • Height Units set to Relative to Children so the container resizes itself as more children are added

    • Height set to 0 so the effective height of the container is based purely on its children

    • Stack Spacing set to 2 (optional) to add spacing between each child

    Now children of the Container stack vertically. This concept works for any type of child, but we'll use ColoredRectangles for this example. Add a few instances to the Container and they stack vertically.

    Finally we can have the stack grow up instead of down. To do this, change the following variables on the parent container:

    • Y Origin set to Bottom

    • Y Units set to Pixels from Bottom

    Now as new children are added, the parent stack grows and all items shift up.

    Children Layout set to Top to Bottom Stack
    Height Units set to Relative to Children
    Height set to 0
    Stack Spacing set to 2
    ColoredRectangle instances stacking vertically in a container
    Y Origin set to Bottom
    Y Units set to Pixels from Bottom
    Stack grows upward as more children are added

    8 - State Categories

    Introduction

    States are a powerful way to create expressive groups of variables. Some UI elements may require a combination of states to be applied simultaneously.

    For example, consider a Button component which displays a button callout.

    Button with Y button callout. Credit Game UI Database

    This button might be a standard Button with the following states:

    • Enabled

    • Disabled

    • Highlighted

    • Pushed

    But it may also have states for which button icon to display:

    • A

    • B

    • X

    • Y

    These two sets of states could be set independently and combined at runtime. Categories let you create groups of states so that multiple states can be set simultaneously.

    Creating Categorized States

    For this tutorial we'll create a new component. This component has state categories for size and for color. To do this:

    1. Open Gum

    2. Create a new Component called CategoryDemo

    3. Right-click anywhere in the State box and select Add Category

    4. Enter the name "Size" for the new category and click OK

    Now we can add states to the categories. To do this:

    1. Right-click on the Size category and select Add State

    2. Enter the name "Small" for the new state

    3. Right-click on the Size category again and select Add State

    Adding Visuals

    Now that we have states set up we need to add a visual element to the component so that we can see our changes.

    To do this, select the Default state and drag+drop a ColoredRectangle into your component

    Setting Variables in States

    To modify a state, you can select it and edit in the Editor tab or change properties in the Variables tab to modify what the state sets. Notice that normally for a component like this the ColoredRectangleInstance would have its width and height be relative to its container, but we're not doing this for the sake of keeping the tutorial shorter.

    First we'll set the Size states. To do this:

    1. Select the Big state

    2. Resize the colored rectangle so it is larger than the default

    3. Select the Small state

    4. Resize the colored rectangle so it is smaller than the default

    Next we'll set the Color states. To do this:

    1. Select the Red state

    2. Set the Red, Green, and Blue values to: 255, 0, 0

    Viewing Multiple States on an Instance

    Now that we have our CategoryDemo component set up with multiple categories, we can view these states on any CategoryDemo instance. To do this:

    1. Create a Screen called "CategoryDemoScreen"

    2. Drop an instance of the CategoryDemo component into the CategoryDemoScreen

    3. Select the newly-created CategoryDemoInstance

    4. Scroll down in the Variables list and notice that the instance has drop-downs for each category.

    We can revert the states back to their unset values by right-clicking on the state variable and selecting the Make Default item.

    Categories and Variables

    If a variable is modified in one of the states in a category, then all of the states in that category are automatically assigned the default value, and this value is explicitly set. This concept makes working with states far more predictable.

    For example, consider a component which has:

    • A single ColoredRectangle

    • A category called RectangleSizeCategory

    • States called Big, Medium, and Small

    Initially, all states in a category do not explicitly assign any variables. We can see this by selecting the category and observing the Variables tab.

    If a variable is changed in one of the states in the category, then that variable propagates to all other states, and the Category lists this as one of the variables that it modifies.

    For example, we can select the Big category and change the ColoredRectangle.Width property to 150.

    Once this value is changed, the RectangleSizeCategory lists this as a variable that it modifies in the Variables tab.

    If we select any of the other states in the category, they show that they explicitly set the Width value as well (the value has a white background instead of light green). The value is inherited from the default state.

    Once a variable is set in a category, all states are required to set this value. A variable cannot be removed from a single state in a category. Rather, to remove a variable, all states in the category must remove the variable. This can be done by selecting the category and pressing the X button next to the variable name.

    7 - States

    Introduction

    States allow you to set multiple variables at one time. Examples of states might include:

    • A button with Regular, Highlighted, Pressed, and Disabled states

    • A game logo in Large and Small modes

    • A game HUD which can appear on and off screen

    Prerequisites

    This tutorial builds upon the previous tutorial where a Button component was created. If you haven't yet, you should read through the earlier tutorials to create a Button component.

    Defining States

    First we'll define two new states. All components and screens have a Default state automatically. This Default state is uncategorized, but all other states must be in a category. Therefore, we'll first add a new category:

    1. Right-click in the States tab

    2. Select Add Category\

    3. Enter the name ButtonStateCategory

    To add a new state:

    1. Right-click on ButtonStateCategory

    2. Select Add State\

    3. Enter the name "Highlighted"

    4. Click OK

    The Button component now has a new state called Highlighted:

    Setting Variables in States

    Once a state is defined and selected, setting a variable associates that variable with the selected state. In other words, any variable that is set when the Highlighted state is selected results in that variable being added to that state.

    For this example, we can make the button become a lighter blue when highlighted. To do this:

    1. Verify the Highlighted state is selected

    2. Select the ColoredRectangleInstance

    3. Set the Green and Red values to 100

    Notice that the Green and Red values are rendered with a white background rather than green - indicating that they are explicitly set in the Highlight state.

    Be careful when editing objects with multiple states. You may end up making changes without realizing that you are doing so in the wrong state.

    Whenever you have a non-Default state selected, Gum displays a label telling you which state you are editing.

    Switching Between States

    The values that have just been set apply only to the state that was selected when the changes were made - the Highlight state. This means that clicking on the Default state switches the button back to the default colors. By clicking on the states in Gum you can preview and edit states easily.

    Category Variables

    Whenever a state in a category sets a variable, that variable is set across all states in that category. So far this tutorial only created a single state called Highlighted, but if additional states are set, all will explicitly set the Red and Green variables. This topic is covered in more detail in the next tutorial.

    4 - Components

    Introduction

    Components can contain instances of other components and of standard objects. Examples of components include:

    • Check boxes

    2 - Creating an Animation

    Introduction

    This article shows how to create an animated component. It will contain an animation which can be used when the component first appears.

    Creating the Component

    Custom NineSlice

    Introduction

    Although Gum includes a standard NineSlice element, the Gum layout system can be used to create a custom NineSlice component. Such a component could be used if additional flexibility beyond what is provided by the standard NineSlice is needed.

    Health Bar

    Introduction

    Introduction

    Health bars are common UI elements in games. They are similar to progress bars, so this example could be used to create either. This example explains how to create a health bar component.

    Add Category right-click option
    Add State right-click option
    Enter the new state name
    Highlighted state
    Red and Green Variables in Highlighted State
    Changing states updates the Editor and Variables tabs to show the selected state
    LeftStick
  • RightStick

  • Repeat the above steps to create a "Color" category

    Add a second state to "Big"
  • Right-click on the Color category and select Add State

  • Add a state called "Red"

  • Right-click on the Color category again and select Add State

  • Add a state called "Blue"

  • Select the Blue state

  • Set the Red, Green, Blue values to: 0, 0, 255

  • You can set each state independently and the states combine

  • https://www.gameuidatabase.com/gameData.php?id=36&autoload=975
    Size and Color categories
    Red and Blue states with the correct color values
    Combining states on an instance
    States can be made default through the right-click make Default item
    RectangleSizeCategory which does not set any variables
    Setting ColoredRectangleInstance Width to 150
    RectangleSizeCategory lists any variables that it changes
    Width value set by Medium state
    Press the X next to a variable on a category to remove the assignment of that variable on all states in the category
    Buttons
  • Popup Menus

  • Components can also be simple such as a button or more complex UI elements such as a full Options screen with dozens of instances.

    Simple Button Example - Creating the Entity

    To understand how components work, we'll create a simple Button component. To do this:

    1. Right-click on the Components folder in Gum and select Add Component

    2. Name the Component "Button"

    3. Drag+drop a ColoredRectangle standard element into the Button component

    4. Drag+drop a Text standard element into the Button component

    A single Button Component with ColoredRectangle and Text. Note that the text is white.

    Since ColoredRectangleInstance and TextInstance are both white you may not be able to see the Text. Let's change the ColoredRectangleInstance's color:

    1. Select ColoredRectangleInstance

    2. Change Red to 0

    3. Change Green to 0

    4. Keep Blue as 255

    Notice that as you change the values, the background on the text box changes from green to white to indicate that the value has an explicit value. The Blue text box keeps a green value since it has not been changed from its default value.

    Values backgrounds change from green to white when explicitly set

    Now you should be able to see the Text on top of the rectangle:

    White text on top of a blue ColoredRectangle

    Sizing the colored rectangle

    At this point we have made some progress towards creating our first button, but it still needs some work.

    First, we're going to adjust the size of the instances inside of our Button component. At this point you can see that the ColoredRectangle (the blue background) is not the same size as the button. Not only do we want to make the blue ColoredRectangle larger, but we also want it to automatically match the Button's size (the dotted outline).

    To do this:

    1. Select the ColoredRectangleInstance

    2. Select the Alignment tab

    3. Click the Fill Dock button

    Fill Dock expands the ColoredRectangleInstance to occupy its full parent

    Alternatively you can adjust the individual values. Keep in mind that using the Alignment tab is the fastest way to get your instances setup. The next section shows all of the changes that the Alignment tab performs for you:

    1. Select the ColoredRectangleInstance

    2. Change Height Units to Relative To Parent

    3. Change Width Units to Relative to Parent

    4. Change Height to 0. This means that the actual height of the ColoredRectangleInstance matches the actual height of its container (the Button Component). Since Height Units is set to Relative To Parent.

    5. Change Width to 0. This means that the actual width of the ColoredRectangleInstance matches the actual width of its parent.

    Now the ColoredRectangleInstance automatically matches the Button's actual with and height:

    ColoredRectangleInstance fills its parent by matching its width and height

    Next we'll position the TextInstance. We'll want to adjust the Text so that it is always centered, and line-wraps with the size of the button.

    To do this:

    1. Select TextInstance

    2. Click the Alignment tab

    3. Set Margin to 20

    4. Click the Fill Dock button

    Alternatively you can set each individual value on the Text by following these steps:

    1. Select TextInstance

    2. Change its Horizontal Alignment to Center

    3. Change its Vertical Alignment to Center

    At this point the Text is vertically and horizontally centered within its boundaries, but we want to have the boundaries centered within the Button. To do this:

    1. Keep TextInstance selected

    2. Change the X Units to Pixels From Center

    3. Change the X Origin to Center

    4. Change X to 0

    Now let's make it centered on the Y as well:

    1. Keep the TextInstance selected

    2. Change the Y Units to Pixels From Center

    3. Change the Y Origin to Center

    4. Change Y to 0

    Finally, let's make the width of the text match the width of the button. For the Text we'll actually leave a border around the edge so the Text doesn't line wrap right against the edge of the button. To do this:

    1. Keep the TextInstance selected

    2. Change the Width Units to Relative to Parent

    3. Change Width to -40. This means the actual width of the Text is 40 pixels less than the actual width of its container. Since the button is centered this means a 20 pixel border on the left and 20 on the right (20+20=40).

    Width and Width Units set explicitly to give the text a margin

    Setting the Button Default Variables

    Buttons are typically wider than they are tall. To match this common layout, let's set some variables on the Button:

    1. Select the Button component

    2. Change Width to 120

    3. Change Height to 36

    Notice that whenever you change these values, the contained objects (text and colored rectangle) adjust automatically.

    Text adjusted to be wider than it is tall

    Creating Component Instances

    Now that we have a component created, we can add instances of this component the same way we have added standard elements. To do this:

    1. Create a new Screen. I'll call mine MainMenu

    2. Drag+drop the Button component into the Screen

    We can now resize and position the Button instance. We can reuse our Button component to create multiple instances. Each instance can be adjusted by changing its X, Y, Width, and Height values.

    Multiple Button instances in MainMenu

    We can change top level variables on the Button such as X, Y, Width, and Height, but we cannot change variables on instances within the Button - all of our buttons currently display "Hello" text.

    The next tutorial shows how to use exposed variables to further customize each button instance.

    First we'll create a component which will be animated. To do this:
    1. Right-click on Components

    2. Select Add Component

    3. Enter the name TextComponent and click OK

    4. Drag+drop the Text Standard into the TextComponent to create a Text instance

    5. Select the Alignment tab and click the middle button to have the TextInstance fill the TextComponent

    Creating the States

    Now that we have a component we'll add the states needed for animation. We'll add all states in a category called HideShow. Animation states should always be categorized. To create the states:

    1. Right click in the States list box

    2. Select Add Category

    3. Enter the name HideShow

    4. Right-click on the HideShow folder

    5. Select Add State

    6. Enter the name Hidden and click OK

    7. Right-click on the HideShow folder

    8. Select Add State

    9. Select Shown

    Setting values in the states

    Now that we have the states defined we can set values for the states. In this case the only thing we'll be modifying is the TextInstance's Font Scale value. To do this:

    1. Select TextInstance

    2. Select the Hidden state

    3. Set the Font Scale to 0. This makes the Text so small that it's invisible

    4. Select the Shown state

    5. Verify the Font Scale is 1, or set it to 1 if not. This makes the Text regular size

    Creating the Show animation

    The two states we created above will be used as the keyframes for our animation. The animation will begin in the Hidden state then interpolate to the Shown state. To add this animation:

    1. Verify that TextComponent or any objects under it are selected

    2. Select View -> View Animations

    3. The Animations tab appears in the bottom right

    4. Click the Add Animation button

    5. Name the animation Show and click OK

    6. Select the Show animation and click Add State

    7. Select the Hidden state and click OK - this is the first keyframe in our animation

    8. Click Add State again

    9. Select Shown and click OK

    The animation can now be played or previewed:

    Adjusting Interpolation Type

    The Interpolation Type value sets how one keyframe blends to another. By default keyframes use Linear interpolation, which is a constant change from one state to another. When interpolating from one keyframe to another, the first keframe defines the interpolation type. In our case the Hidden frame defines the interpolation type. We can change the Interpolation Type and preview the animation:

    1. Select the Hidden keyframe

    2. Change Interpolation Type to Elastic

    Playing the animation will reflect these changes.

    Creating the Component

    As implied by the name, the NineSlice element is composed of nine Sprites. First we'll create the component:

    1. Open Gum

    2. Open or create a new Gum project

    3. Right-click on the Components folder

    4. Select Add Component

    5. Name the Component "CustomNineSlice"

    Component named CustomNineSlice

    Adding Corner Sprites

    Next, we'll add corner Sprite instances to our CustomNineSlice. We'll be using the alignment tab to position Sprites. The alignment tab provides a quick way to place objects, but the same could be achieved using the following variables individually:

    • Width Units

    • X Origin

    • X Units

    • Height Units

    To create the corner Sprites:

    1. Drag+drop a Sprite element onto the CustomNineSlice component

    Drag+drop a Sprite onto CustomNineSlice
    1. Click the Alignment tab

    2. Anchor the newly-created Sprite to the top-left of its container

      Anchor the Sprite to the top-left of its container
    3. Repeat the steps above three more times, creating one Sprite for each of the four corners, anchoring each one to their respective corner

    Four sprites in CustomNineSlice

    Notice if we resize our CustomNineSlice component, each of the four Sprites remains in its respective corner.

    Resized CustomNineSlice keeping its Sprites in the corner

    Adding Edge Sprites

    Next we'll add the four Sprites which will sit on the edges of our component:

    1. Drag+drop a Sprite element onto the CustomNineSlice component

    2. Click on the Alignment tab

    3. Dock the newly-created Sprite to the top of its container. Docking sets the width of the sprite to match the width of the component. We'll address this in the next step.\

      Click the top dock tab
    4. To accommodate for the corner Sprites, we need to adjust the width of the top Sprite. Set the newly-created Sprite's Width to -128. Since the Sprite uses a Width Units of Relative to Parent, setting the value to -128 makes the Sprite 128 units smaller than its parent. We picked 128 because each of the corner sprites is 64.\

    5. Repeat the above steps, but instead setting the dock to create sprites on the left, right, and bottom. adjust width and height values as necessary.

    Adding the Center Sprite

    Finall we'll add the center Sprite:

    1. Drag+drop a Sprite element onto the CustomNineSlice component

    2. Click on the alignment tab

    3. Dock the newly-created Sprite to the center of its container.

    4. Set both the newly created Sprite's Width and Height to -128

    Now the Sprites stretch and adjust whenever the CustomNineSlice is resized.

    Assigning values on CustomNineSlice

    Unlike the regular NineSlice, changing the texture values requires a considerable amount of variable assignment. To change the CustomNineSlice to use 9 separate textures, the following values must be set:

    • Each of the Sprite instances must have its SourceFile value set

    • The edge Sprites will have to have their Width and Height values modified to account for the possible resizing of the corner sprites

    • The center Sprite will have to have both its Width and Height values modified

    If using a sprite sheet, then all of the work above will need to be done plus the texture coordinate values will need to be modified.

    Creating the Component

    First we'll define the component:

    1. Open Gum

    2. Open or create a new Gum project

    3. Right-click on the Components folder

    4. Name the component "HealthBar"

    5. Resize the HealthBar component so it is wider than it is tall. For example, assign Width to 200 and Height to 32.

    Adding a Background

    Next we'll add a background to our HealthBar Component

    1. Drag+drop a ColoredRectangle into the HealthBar\

      Add ColoredRectangle to HealthBar
    2. Select the newly-created ColoredRectangleInstance

    3. Select the Alignment tab

    4. Click the Fill Dock button\

    5. Change the ColoredRectangleInstance color to black\

    Now we have a black background in our HealthBar

    Creating an Inner Container

    The HealthBar displays its current health with another rectangle. This second rectangle will be contained inside a container to provide a boundary. To add an inner container:

    1. Drag+drop a Container onto the HealthBar

    2. Select the Alignment tab

    3. Enter a Margin value of 4

    4. Click the Fill Dock button

    Set Margin to 4, then click the Fill Dock button

    Now we have a ContainerInstance with the proper margin

    Adding the Foreground Rectangle

    Finally we'll add the foreground rectangle which displays the health:

    1. Drag+drop another ColoredRectangle onto the ContainerInstance. Be sure to drop it on ContainerInstance so that the newly-added ColoredRectangle is a child of the container

    2. Click the Alignment tab

    3. Set Margin back to 0

    4. Click the Fill Dock button

    5. Change the following values:

      1. X Units to Pixels from Left

      2. X Origin to Left

      3. Width to Percentage of Container

    Now, the Width value can change between 0 and 100 to indicate the health percentage.

    Expose Width

    Next we'll expose the inner ColoredRectangle's Width property so it can be assigned per HealthBar instance:

    1. Select the inner ColoredRectangle instance

    2. Right-click on its Width variable and select Expose Variable\

      Click Expose Variable
    3. Enter an appropriate name such as "Percentage" and click OK

    Now we can add instances of the HealthBar to a screen and control its fill percentage.

    Percentage value updated on a ScrollBar instance

    \

    Centering

    Introduction

    Gum provides simple controls for centering objects inside of their parents. This page shows how to center objects in a variety of situations. For brevity this document uses vertical centering, but the same concepts apply to horizontal centering.

    Centering with Anchors (Alignment Tab)

    The easiest way to center an object is to use the center Anchor. This sets all of the values necessary to center an object both vertically and horizontally.

    Centering Using Units

    Objects can be centered by setting their unit and numerical values. The Alignment tab is a shortcut for these values, but we can assign each value individually so an object is centered vertically:

    • Set Y to 0

    • Set Y Units to Pixels from Center

    • Set Y Origin to Center

    Centering with Margins

    Centering can be performed with margins by adding an additional container to create the necessary margins. For example, consider a situation where we want to center the green rectangle inside the blue rectangle, but leave a 32 pixel margin at the top.

    We may want something similar to the following image:

    To do this, an additional container can be added as shown in the following image:

    In this case, the container has the following relevant properties:

    • Y = 0 (so it is pressed against the bottom)

    • Y Units = Pixels From Bottom (so it is bottom justified)

    • Height = -32

    The green rectangle can be added as a child to the container, and then centered within the container. This results in the green rectangle always being centered within the area that leaves a 32 pixel margin at the top even if the main rectangle is resized, as shown in the following animation:

    Additional margin can also be added to the bottom by changing the container's Y value. For example, a 20 margin border can be added at the bottom, leaving a 32 pixel margin at the top by setting the following values on the container:

    • Y = -20 (move the bottom of the container up by 20 pixels)

    • Height = -52 (leaving a 32 pixel margin at the top, and accounting for the container being moved up an extra 20 pixels)

    Center Stacks

    Stacks can be centered horizontally or vertically. To center a stack of objects, an internal container is needed.

    A centered stack might look like this:

    For this example, we'll begin with a Container and a background ColoredRectangle. The background is not necessary, but it helps visualize the main Container's size.

    Next we'll add another container which will hold our stacking instances.

    1. Drag+drop a container onto the MainContainer

    2. Click the Alignment tab

    3. Click Anchor Center

    We can add children to the container:

    1. Set the inner container's Children Layout to Top to Bottom Stack

    2. Drag+drop children onto the inner container to have them stack

    3. Optionally adjust the Stack Spacing variable to add gaps between the children

    For this example I modified each child rectangle to have

    • Width = 128

    • Height = 32

    • Color = Green

    Finally, we mark the inner container to be sized according to its children. Since it remains centered, whenever its size adjusts (by adding or removing children), the inner container adjusts to remain centered.

    Now if children are added or removed, the container remains centered.

    Width to 100
    Fill Dock to make the background take up the entire size of its parent HealthBar component
    (leaving a 32 pixel margin)
  • Height Units = Relative to Parent (so that it always has a 32 pixel margin regardless of parent size)

  • Optionally adjust the children such as changing their size or color
    Centering using the alignment tab
    Centering using Y, Y Units, and Y Origin
    Green rectangle is centered in the remaining area below the yellow area
    Container inside the blue rectangle defining the centering space
    Blue rectangle resized, keeping the green rectangle centered within the area leaving a 32 pixel margin at the top
    Centering leaving an extra margin on both top and bottom
    Centered stacking ColoredRectangles
    MainContainer with a blue ColoredRectangle background
    New inner container centered
    Add childre nto the stack
    Adjusted rectangle sizes and colors
    Set the inner container to Size to Children
    Inner container expanding in response to new children
    Y Origin
    Y Units
    Top Sprite in CustomNineSlice