TGB/MiniTutorials/Your First GUI Tutorial
From TDN
The purpose of this tutorial is to show you how to create a GUI and link it to your project so that you can use it with your game.
[edit]
Create Your GUI
First, let's create your GUI and plug it into your game so that it can be reference within the TGB GUI Editor.
- Load a level in TGB.
- On the menu bar, click Project > GUI Builder. The GUI Builder opens.
- On the menu bar, click File > New GUI. The Create New GUI dialog box opens.
- Click Create. Your new GUI opens in the GUI Builder.
- Click Toggle Palette in the top right of the Gui Builder window.
- Select a control from the Control Palette that opened and drag it into the Gui Builder window.
- On the menu bar, click File > Save GUI. The Save File dialog box opens.
- In the menu tree, navigate to your project, click on the gui folder, and click Save File. Your GUI, guiNewGui.gui, is saved.
- In your project folder, open main.cs in a text editor.
- Add the following line in initializeProject() near the top: exec("~/gui/guiNewGui.gui");
- Save main.cs. You can now reference your GUI in your game.
[edit]
Use Your GUI
Now that you have created your GUI, you are ready to use it in your game.
- Load a level in TGB.
- Click the Create tab.
- Drag a static sprite onto your level.
- In the level, click on the sprite.
- Click the Edit tab.
- Expand Scripting.
- Mark the Use Mouse Events checkbox.
- In the Class field, enter Menu.
- Press CTRL+S to save your level.
- Close TGB.
- In your project folder, open game.cs in a text editor.
- At the bottom of game.cs, add the following lines of code:
function Menu::onMouseDown(%this) { Canvas.pushDialog(guiNewGui); } - Add sceneWindow2D.setUseObjectMouseEvents(true); to your startGame function.
- Save game.cs.
- Reload your level in TGB (it is very important that you close TGB after you make any changes to game.cs).
- Test your level. When you click on the sprite, your GUI appears on screen.
Note: To release your GUI, use Canvas.popDialog(guiNewGui); in your code.



