A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/TorqueGameEngines/Torque2D/wiki/GUI-Guide below:

GUI Guide · TorqueGameEngines/Torque2D Wiki · GitHub

The GUI system in T2D allows you build dialogs with controls to handle things like menus and in-game option screens. The system has been handed down from T3D but has seen a lot of changes over the past few years. Even if you plan to build your user interfaces directly out of sprites, a basic knowledge of the GUI system is still necessary to setting up a SceneWindow. Because this topic is so expansive, we will start with an overview of how the GUI system works and them move on to the details of each control and how it can be used.

Please note: This documentation applies to version 4.0 (including early access versions of 4.0) and onward. Much of this information will not apply to previous versions of the engine.

All GUI controls inherit from a single object aptly called GuiControl. GuiControl inherits from SimGroup which means that it can hold other controls inside of it. Each control has a set of its own properties, such as position and extent, and a set of shared properties which are contained in a different object called a GuiControlProfile. Every control needs to have a profile. The profile contains values such as the fill color. So the obvious question is: why do it this way? There are several advantages. First, you could change the entire look of a control simply by giving it a different profile. You could also change a value of a profile and immediately affect every control that uses it. So if all your buttons share a single button profile, you can change the button profile in one place and every button will change. This idea has been used with great success in other programming fields like web development where HTML and CSS fill the same roles as controls and profiles.

Unfortunately, there is also a downside to doing it this way. Namely, the GuiControlProfile has a lot of fields and each control can pick and choose which values it will actually use. How will you know which fields of the profile will be used with each control? By reading this documentation of course!

Putting Your GUIs on the Screen

Before we go an further, we need to cover some basics. Let's walk through the steps needed to get your GUIs up on the screen.

  1. Call createCanvas() to create an instance of GuiCanvas and wire it up to a window. The canvas will be a named object called "Canvas" and you can only have one. The canvas is actually a GuiControl as well and you can learn more about it in the List of Controls below. For now, let's just say the canvas is top level GUI element. If you want anything to appear before the user, you need to hand it to the canvas.

    createCanvas("Window Name");

  2. Call Canvas.setContent() to set a GuiControl as the current "screen" that appears before the user.

    Canvas.setContent(%myScreen);

In this example, %myScreen would hold a GuiControl and that control would probably have several children, including a SceneWindow. More on that later.

  1. When you want another GUI to go over the main screen, you push a "dialog" onto the Canvas.

    Canvas.pushDialog(%myDialog);

Here %myDialog also contains a GuiControl. This dialog could actually cover the whole screen, but then you might as well use setContent() instead. So it makes sense if you use this for the control to cover part of the screen. This has been compared to a save or open dialog that you see in most applications. Most importantly, when you push a dialog onto the canvas, the dialog will capture all input events until it is removed with popDialog(). You can stack multiple dialogs in the Canvas.

These are properties that exist on every control because they inherit them from GuiControl. It's super important to understand these so we'll cover them here before we get to the master list.

This is the position of the top left corner of the control with (0,0) being the top left corner of its parent control's content area (i.e. the parent's area after applying margin, border, and padding). Larger y positions are further down the screen. This matches the way most screen/window positioning works in most operating systems, but it is backwards from the way a Scene works.

This is the width and height of the control. The extent will change automatically when its parent's extent changes based on the values of HorizSizing and VertSizing which are covered next. There's also a callback when the extent changes called onExtentChange(). It is important for SceneWindows to catch this to adjust their camera ratio. More on that in the SceneWindow section below. As you would expect, MinExtent is the smallest possible extent.

HorizSizing and VertSizing

Here's where things get confusing. These settings determine how a control is repositioned and resized when its parent is resized. This is based on its related size/position when it is added to its parent. The possible options are center, relative, left/top, right/bottom, and width/height.

  1. Center: The control will stay centered in its containing parent. The extent of the object will not change - only the position.
  2. Relative: The control will maintain the same position and extent to its parent. So if the parent doubles in size so will the control. Also the distance between the edges will double. Basically this matches zooming in and out on the GUI.
  3. Left/Top and Right/Bottom: Any change to the parent's size will be applied to the distance between the control and the specified edge which effectively anchors the control to the opposite edge.
  4. Width/Height: These settings allow the only extent of the control to change. So the control will remain the same number of pixels from the edges of its parent. This is kind of like anchoring the control to both edges.
  5. Fill: This is new experimental option that attempts to have the control fill the entire content area of its parent. This will likely become a heavily used option in the future but may be a little unstable for now.

This is the GuiControlProfile that the control will use. The profile holds a lot of information on how a control should look and can be quickly swapped to change the entire look of a control. See below for more.

As the name suggests, true will cause the control to be rendered. False will cause the control and it's children to not render.

TooltipProfile, HoverTime, TooltipWidth, and Tooltip

Every control can have a tool tip. These four fields control how that tip works. The HoverTime determines how long the cursor should "hover" over a control before the tip appears (1000 = 1 second) and the width is used to determine how wide the tip is in pixels. The field Tooltip is the actual text of the tip. The TooltipProfile is another GuiControlProfile that is used to determine the look of the tip. If this is not set it will fall back to the profile for the control. It uses the profile's font, fontSize, fillColor, fontColor, and border settings.

The text of a control can appear in different places depending on the control. For example, the button control will place the text on the button. The plain GuiControl can render text as well, both single line and wrapped. The textID finds the text in the localization system and sets the text. You should only need one of these. Not all controls render their text.

These two flags impact the way text is rendered and are most useful when you want to display text in a plain GuiControl. When TextWrap is set to true, the text in a control (not every control) will wrap to the another line when it exceeds the horizontal space for text. TextExtend causes a GuiControl to increase its extent dynamically to fit its text. The GuiControl will shrink too, so use MinExtent to prevent this if you want. This is most useful inside of a GuiChainCtrl which repositions children based on their extent.

This powerful field can prevent a control and all of its children from receiving any input. When input occurs, say for example a mouse click, the engine first checks to see if any control is in focus (referred to as the first responder). If no control is in focus, the engine then looks down through the tree of GuiControls until it reaches a control with no children - or at least no children with the clicked point inside of them. The engine then passes the event to this inner-most control. The control can then consume the event or let the event "bubble up" to its parent. If UseInput is set to false, it removes the control and it's children from this process. Normally you will not need to change this.

There are some properties that are inherited from the GuiControl but aren't always used. These properties are more specific in their function.

Both of these properties take a string of code that will be evaluated at certain points. For example, the GuiButtonCtrl calls the command when it is clicked. Most controls don't use both command and altCommand. Generally, the command is code that is called during the use of the control, whereas altCommand is called when the control is done being used. But there are no hard rules here! Just two commands that each control can use as they see fit. See the detailed section below for each control to see which command you should set.

The Accelerator Key gives you an easy way to set a key that will activate the control. What that means exactly depends on the control. A button control for example will simulate a click. If another control absorbs a key press nothing will happen. This basically give you an easy way to build hot keys into your GUIs.

As stated above, this class is not actually a GuiControl. Instead it holds a handful of common values that each control uses to render itself. Here is a list of all of those properties. Most controls won't use all of these properties and you'll want to go the section for the control in question to see exactly which properties should be set for a control. Every control must have a profile. If no profile is provided the engine will search for a named profile of the form [GuiControlName - "Ctrl"]Profile. For example, for the GuiTextEditCtrl it will look for GuiTextEditProfile as a default profile if one is not provided. If it can't find a profile it will fall back to a named profile of GuiDefaultProfile. It is wise to have default profiles defined.

There are three ways to render a control: assets, bitmaps, and default rendering. A profile defines which of these options will be used by its controls. If multiple ways are defined then the asset will be used first, followed by the bitmap, and finally default rendering. Every control has the option of using these three. Default rendering simply uses the fill color and border settings to render the control. You should note that even if you use ImageAssets or Bitmaps, border settings like margins will still apply. Additionally, padding and actual borders (even though they aren't visible) will still affect child and text placement.

As you can see, each section is divided by a red line. The dividing color is determined by the top left pixel. The corner images will not be stretched. The other images will be stretched to fit the size of the button. If you want the control to use default rendering then don't set this entirely. Setting it to an empty string will result in an error.

There are a handful of times when you can override the profile directly in the control. These are generally connected to the text. First, you can set the align and vAlign properties directly on the control to override text positioning. Next, you can adjust the font size with the FontSizeAdjust value. This setting is multipled by the fontSize on the profile to get the actual font size. So if the profile has a font size of 12, you could set the FontSizeAdjust to 0.75 and the control would display with a font size of 9 (because 12 * 0.75 = 9). Last, you can directly override the font color of the profile. Simply add the FontColor to the control and then turn on the OverrideFontColor flag. You can revert to using the profile's color by turning the flag back off.

These profiles can be assigned to a GuiControlProfile either as a default border profile that covers all sides, or as a profile that covers a specific side. This allows you to control the look of each side separately from the others. This also has variations for the different control states, allowing you to customize the way each control looks. The spacing used for borders mimics that of the CSS Box Model with margin on the outside, then borders, then padding and finally content.

Content or the Inner Rect

Occasionally in this guide, the content or inner rect of a control will be referred to. This means the area of the control after applying the margin, border, and padding for all sides. Both text and child controls are positioned within the inner rect. So placing a child control at (0,0) will place the top left corner in the top left corner of the content. In most cases, you will want the inner rect to remain unchanged by the state variations. So for example, if you have a margin of 1 in the normal state and a margin of 0 when the control is hovered, then you should offset the change with a 1 pixel increase in border or padding when the control is hovered. This will keep text from moving around when the control changes states and keep child controls resizing in a consistent way. Child controls assume the size of the content will not change between states.

Up to this point, we've covered in a general way how the Gui system works. This section will take an in depth look at each control. These are grouped with similar controls together. The most important controls come first.

All controls inherit from the GuiControl and it is extremely useful both as a container and for displaying text. The GuiControl renders using the standard fillColor and border settings. If you add text to the GuiControl then it will be rendered in the content section of the control. Here is an example of a plain GuiControl rendered over the Truck Toy.

The GuiCanvas is a very special control that sits at the root of the GuiControl tree. There is only one canvas: a named variable aptly called "Canvas". This is initiated through createCanvas(). The canvas can then set a GuiControl as its content and show additional GuiControls as dialogs. Details of all the functions available to the canvas can be found in the Doxygen documentation. A simple review of the code shows that the canvas does not use a GuiControlProfile at all. Like other controls, it uses position and extent.

This is the only GuiControl that doesn't start with "Gui" so you know it's special! As it turns out, the concept of the SceneWindow is extremely important to developing with T2D which is why it is already covered in the Getting Started Guide. To summarize, the SceneWindow is a GuiControl that renders a Scene to the screen.

Settings

Callbakcs

A Note On Camera Ratios The position and extent of the SceneWindow is also important. When the extent of the SceneWindow changes, it will not adjust the camera area covering the scene. If the ratio of width to height for the scene does not match that of the camera then the image will appear stretched. To combat this, you should respond to the onExtentChange() callback and adjust the area of the camera to match the new extent.

MySceneWindow::onExtentChange(%this, %bounds)

The new bounds for the SceneWindow are passed into this callback. This is a space-separated string that contains positionX, positionY, width, and height.

Profile Fields Used The scene window uses its profile's font and fontColor to render metrics. The background of the metrics banner uses the fillColor.

Methods Brace yourself. There's a lot!

This is designed to be a button, but functionally it is the same as a basic GuiControl that interacts with the mouse or with touch. This is because the button can also act as a container for children. Buttons also support easing for the fill color during default rendering. Easing will likely expand to other controls in the future.

Settings

Note: you can learn more about easing functions in the Easing Guide.

Callbacks

Profile Fields Used

Methods Easing only applies to default rendering.

The checkbox inherits from GuiButtonCtrl. The information for that control still applies to the checkbox but it won't be repeated here. Checkboxes expand on basic buttons by allowing them to toggle on and off. Please note that easing functions available on the button's fill color will not be applied to checkboxes or radio buttons.

Settings

Callbacks - Has the same callbacks as GuiButtonCtrl.

Profile Fields Used - Uses the same fields as GuiButtonCtrl.

Methods

The radio button inherits from GuiCheckBoxCtrl so the information about that control applies to the radio button as well. Radio buttons stay on when clicked, but only one radio button in a group can be on at a time. Default rendering for radio buttons render them as circles instead of squares.

Settings

Callbacks - Has the same callbacks as GuiButtonCtrl.

Profile Fields Used - Uses the same fields as GuiButtonCtrl.

Methods The radio button inherits from the checkbox and button so it has the same methods.

The text edit control in some ways is one of the most important controls available and packs a lot of punch in a small package. It has multiple input modes that can be used to ensure the data is correct. It has a wide array of commands and callbacks that can be used including validation.

Note: several features in the GuiTextEditCtrl have changed since versions before 4.0. Be sure to check this documentation for changes.

Settings

Callbacks

Commands There are three code commands that are evaluated at different times. It is worth knowing when each fires.

Profile Fields Used The text edit box renders using the usual settings in the normal state. The HL state is used when the box is hovered. The SL state is used when the text edit box has the focus and NA is used when it is inactive. The fillColorTextSL and fontColorTextSL are used together when text is selected. It also uses the cursor color setting for the cursor. And of course, the font, font colors, and alignment settings are used for the text.

Methods - In addition to the methods below, the methods getValue() and setValue() can be used to retrieve and set the text.

The GuiTabBookCtrl is the main control that is responsible for keeping a list of "pages" and rendering tabs for them. Clicking on the tab switches to the page. Tabs can be positioned on the top, bottom, left or right and have a wealth of rendering options.

Only a GuiTabPageCtrl can be added to the book as a child. To work as intended, the page should have the same extent as the book and be positioned at (0,0). The page and its contents will then be resized to fit the page area when the page is added to the book. For this reason, it is important that the contents of the page are added before the page is added to the book if possible.

Settings

Profile Fields Used - In case you missed it, this control has two profiles: profile and tabProfile! The normal profile's border and fill color is applied to the area behind the tabs. This allows you customize the background. Only the normal state is used so don't bother setting the other states (i.e. set border but don't bother with borderHL). The tabs are rendered in the inner rect of the profile. So adding padding to the top border of the profile will add some space between the tabs and the top of the tab section. The tabProfile is applied to the tabs. This includes the fill color, font settings, and border settings with all states. Like other controls, imageAssets or bitmaps can also be used.

Methods

The only possible child control of the tab book above. The GuiTabPageCtrl is basically a special container. It's profile is used to render the page section of the tab book when the page is active. The page should be set to use the entire area of the book and will be resized as needed. The text set on this control will be used in its matching tab. Here's an image to recap how the book and page work together.

Settings

This control is a simple container with scrollbars if the contents happen to overflow the container. The control's profile will be applied like other controls with margin on the outside, then the border, and last the padding. The actual scrollbars will appear between the border and the padding and the padding will appear within the scrolled area between the child controls and the border. Child controls should always be placed at positive coordinates. The scrollable area will not extend to the left or top past zero.

Some quick terminology: The scrollbar has arrow buttons on either end of it. The moving part between the arrow buttons is called the thumb. The thumb moves along the track.

Settings

Profile Fields Used The scrollbar has four different profiles! The main profile applies to the body of the control and includes the margin, border, and padding in the normal state. Fonts and other states are ignored. The trackProfile uses the margin, border and padding of the normal and disabled states. The arrowProfile and thumbProfile both use the margin, border, and padding of all four states. Additionally, the arrowProfile uses the font colors of the four states to color the arrows which are drawn within the content of the arrow buttons.

Methods

This control renders just like a normal GuiControl, but its children are automatically positioned and sized into a grid. There are a handful of options that allow you to customize how the grid looks and what order the objects appear in. The control can also automatically expand to fit the number of children. For this reason, it often appears inside a GuiScrollCtrl.

Settings

Profile Fields Used The GuiGridCtrl renders itself as a normal GuiControl. So it can use margins, borders, padding, and text using all the normal settings.

Methods All the usual suspects are here, but it's important to note what's not here. You can't change the Order Mode or IsExtentDynamic once the GuiGridCtrl is created. Everything else is fair game.

Like the grid, this control focuses on positioning its children, but only in a single row. More importantly, this control will not change the extent of the children - only the position in one direction. This allows the children to be different sizes. It places its children in a single line vertically or horizontally.

Settings

Profile Fields Used The GuiChainCtrl renders itself as a normal GuiControl. So it can use margins, borders, padding, and text using all the normal settings.

Methods You should note that you cannot change the direction once the control is created.

This is just like a standard GuiControl when in a "collapsed" state, but when it expands it grows to accommodate its children. Most controls clip off their children when they are outside their content area with some exceptions such as the GuiScrollCtrl. This control can actually switch between the two modes. The extent of the collapsed state is based on the initial extent given to the control. The extent of the expanded state is based on the size of the control's children. Note that only the extent of the control changes so children with a negative position will not be accommodated (i.e. the control only grows down and to the right).

Settings

Profile Fields Used This renders itself as a normal GuiControl. So it can use margins, borders, padding, and text using all the normal settings.

Methods

This is a special expand control that creates a child button the size of the collapsible region. The expand control is then built to expand and collapse when the button is clicked. Child controls should be placed below the button. The Profile assigned to a panel will be directly handed to the child button control so the control itself will ignore margins, borders and padding. Otherwise, the GuiPanelCtrl has the same methods and settings as the GuiExpandCtrl.

This magic little control can directly load an ImageAsset, AnimationAsset, or Bitmap without having to create a whole new profile for each one. As such it excels as an icon or still image in the GUI. Naturally, the ability to play an animation is also useful. It also renders like a normal control behind the image using the normal state. You can use this to add margin, border, padding and a background color to the image. The image can be set to fill the entire content area or you can give it a size independent of the control. An image smaller than the content area will be positioned by the alignment and vertical alignment of the profile. You can further offset this position to get the image exactly where you want it. The image also has its own blend color. Finally you can control all of these over time with moveTo, growTo, and fadeTo methods. These allow you to animate the image and even take an optional easing function.

Settings

Callbacks

Profile Fields Used The control renders all profile fields in the normal state. The fill color will appear behind the image. Text is not rendered and font colors are not used. Additionally, alignment settings are used to position the image.

Methods

This control is a window. It has a title bar that allows the window to be moved and a series of buttons. Most features are optional and can be turned on and off. Child controls are rendered in the content of the window and there is no need to compensate the child's offset for the window's title bar.

Settings

Callbacks

Profile Fields Used The GuiWindowCtrl renders it's title bar using its standard Profile. It supports all the normal items with a few notable differences. The NA options of border and text will be applied when the window is minimized. The HL options will apply when the title bar is hovered and the SL options will apply when a child control of the window is the current target of key events. The content of the window has it's own profile (the ContentProfile) that controls the window content. The HL and NA states of the content profile will be ignored.

Methods Although this may be expanded on in the future, there are currently only setter function for the profiles and cursors of the window.

This control allows you to create a list with each item in its own box. The main profile applies to each individual list item instead of to the whole list. Each item can also have a color assigned to it which will add a small color dot to the left of the text. The meaning of this color is up to you. The extent of this control is dynamic. It will extend vertically to fit its items. It's horizontal extent depends on FitParentWidth.

Settings

Callbacks All these callbacks pass the item index, the item text, and the item ID as parameters.

Profile Fields Used The standard profile is used to render each item. The highlight state (HL) is used when an item is hovered. The select state (SL) is used when an item is selected. Finally, the disabled state (NA) is used when the control is not active.

Methods

This control is a cross between a button, a list box and a scroll control and it can do many of the same things as all three of those controls. The result is a classic drop down control that shows a list of options when clicked.

Please note: In versions of Torque2D before 4.0, this control was called a GuiPopUpMenuCtrl. To gain consistency with the GuiListBoxCtrl, many of the console function have changed slightly as well.

Settings

Callbacks

Methods

This handy utility control can be moved by the cursor for drag-and-drop behavior. It renders just like a standard GuiControl in the normal state. Much of its power comes from callbacks that allow script to implement custom behavior.

Please note: In versions of Torque2D before 4.0, this control was named GuiDragAndDropControl. The name was shorten to be consistent with other controls.

Settings

Callbacks Not all of these callbacks fire on the GuiDragAndDropCtrl itself. In some cases, the callback is fired on the target that the control is dropped on.

Profile Fields Used Renders like a standard GuiControl. Uses only the normal state (no HL, SL, or NA). In many cases, you will probably use this as a wrapper and use an invisible profile but you could also render it like any other control if you so desire.

Methods

This controls displays a progress bar. It can smoothly animate from one state to another and it makes creative use of the profile states to display the different parts of the bar.

Settings

Profile Fields Used The control renders the background of the progress bar using the normal state. This includes border, padding, and margins. The actual progress bar is rendered using the highlight state (HL) if the progress is incomplete. A complete progress bar is rendered using the selected state (SL). These two states are also used to render any text set to the progress bar. It's a bit unusual, but the FontColorHL and FontColorSL values are used but the normal FontColor is never used. The disabled state is also not used. Like other controls, you can use default rendering, bitmaps or assets to render the contents of the control.

Methods

GuiMenuBarCtrl and GuiMenuItemCtrl

As you might expect, these two controls create a menu bar. The menu bar automatically positions itself in the top of whatever container it is placed in. Menu items are the only children that can be placed in the menu bar and they can be nested to create sub-menus. The menu bar itself takes more profiles than any ctrl mentioned so far but its children, the menu items, don't take a profile at all. This is to keep you from having to place the same profile on each child.

The menu bar comes with a host of features: hot keys, spacers, toggle options, radio options, sub-menus, hidden menus, disabled menus, and keyboard controls. Each of these is described below. In addition, it's possible to have more than one menu bar on the screen. For example, you may want several window controls, each with their own menu bar.

Settings for GuiMenuBarCtrl

Settings for GuiMenuItemCtrl

Callbacks

Profile Fields Used

Methods Its worth noting that the menu items are just plain children of the menu bar. Since they inherit from SimSet (as do all GuiControls), adding a menu or changing their order can be accomplished in the same way as adding an item to a SimSet or changing the order of a SimSet. SimSets have methods like add(), remove() and BringToFront() that can all be used to manipulate your menus. A full list of SimSet methods can be found here.

The color picker is a low-level control that allows you to construct more complicated color controls. It has multiple modes offering a lot of functionality. The down side is that it is complicated. If you want something simplier you should see the color popup below. Also note that the color picker and popup both use decimal values to define a color since this allows for more percision than integer values.

Settings

Callbacks

Profile Fields Used First, it is worth noting that this control will render nothing when in Dropper mode (since the point is to capture the color of whatever is under it). It will also not apply margins, border or padding. When in any other mode, the color picker will render as any control and then render its color box over the content area. When chosing one of the alpha modes, the color picker will render a checker pattern under the transparent portions. The color picker will also render text using all the same profile settings as a regular gui control.

Methods

The color popup uses several color pickers to build a working user interface to choose a color with. It takes very little configuration to get a color popup to work. The popup is a button that displays a color. When clicked, it open up standard color picker overlay where the user can select a color. It can optionally include transparency.

Settings

Callbacks

Profile Fields Used The popup renders as a normal gui control using all the expected values. It then renders the solid selected color over content area.

Methods

The tree view displays the contents of a Sim Set or file system. This control is mostly intended for internal use with T2D's editors, but you are welcome to use it in a game if it makes sense. The TreeView inherits from the ListBoxCtrl and will have all of the features of the list box as a result.

Please note: This control was rewitten for Torque2D 4.0 Early Access 3.0. As such, its fields, methods and behaviors are different and older code will likely need to be updated to work as intended.

Callbacks

Profile Fields Used Like the ListBoxCtrl, each line of the tree is rendered using the profile. A triangle and focus line is drawn using the reverse of the fill state (between normal state and selected state).

Methods

The frame set is a powerful, complex control that breaks a space up into specific frames that can be adjusted in size using a bar between them. This is common behavior in many apps today and greatly improves the user experience. If a window is a direct child of a frame set then it can be dragged out into a floating window by the user. It can then be placed into a new frame if desired or placed into an exsiting frame to form a tab book. When a control is added as a child to the frame set, the frame set tries to add it to the first empty frame available starting with the left-top-most empty frame.

Settings

Profile Fields Used The GuiFrameSetCtrl only uses its profile to render the dividers between frames. However, it does not apply margins.

Methods


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4