nifty_gui_scenarios.adoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. = Nifty GUI 1.4.2 - Usecase Scenarios
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :keywords: gui, documentation, nifty, hud, click, state, states, sound, effect
  6. :relfileprefix: ../../
  7. :imagesdir: ../..
  8. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  9. This document contains typical NiftyGUI usecase scenarios, such as adding effects, game states, and creating typical game screens.
  10. Requirements: These tips assume that you have read and understood the <<jme3/advanced/nifty_gui#,Creating JME3 User Interfaces with Nifty GUI>> tutorial, and have already laid out a basic +++<abbr title="Graphical User Interface">GUI</abbr>+++ that interacts with your JME3 application. Here you learn how you integrate the +++<abbr title="Graphical User Interface">GUI</abbr>+++ better, and add effects and advanced controls.
  11. == Switch Game States
  12. In a JME game, you typically have three game states:
  13. . Stopped: The game is stopped, a StartScreen is displayed.
  14. . Running: The game is running, the in-game HudScreen is displayed.
  15. . Paused: The game is paused, a PausedScreen is displayed.
  16. (Aside: Additionally, the Stopped state often contains a LoadScreen, LogonScreen, OptionsScreen, CharacterCreationScreen, HighScoreScreen, CreditsScreen, etc. Some games let you access the OptionsScreen in the Paused state as well. The Running state can also contain an InventoryScreen, ItemShopScreen, StatsScreen, SkillScreen, etc.)
  17. In JME, game states are implemented as custom <<jme3/advanced/application_states#,AppState>> objects. Write each AppState so it brings its own input mappings, rootNode content, update loop behaviour, etc with it.
  18. . Stopped: StartScreen AppState + GuiInputs AppState
  19. . Paused: PausedScreen AppState + GuiInputs AppState
  20. . Running: HudScreen AppState + InGameInputs AppState + BulletAppState (jme physics), …
  21. When the player switches between game states, you detach one set of AppStates, and attach another. For example, when the player pauses the running game, you use a boolean switch to pause the game loop and deactivate the game inputs (shooting, navigation). The screen is overlayed with a PausedScreen, which contains a visible mouse pointer and a Continue button. When the player clicks Continue, the mouse pointer is deactivated, the in-game input and navigational mappings are activated, and the game loop continues.
  22. == Get Access to Application and Update Loop
  23. Since you are writing a jME3 application, you can additionally make any ScreenController class extend the <<jme3/advanced/application_states#,BaseAppState>> class.
  24. This gives the ScreenController access to the application object and to the update loop!
  25. [source,java]
  26. ----
  27. public class StartScreenState extends BaseAppState implements ScreenController {
  28. private Node localRootNode = new Node("Start Screen RootNode");
  29. private Node localGuiNode = new Node("Start Screen GuiNode");
  30. private final ColorRGBA backgroundColor = ColorRGBA.Gray;
  31. @Override
  32. protected void initialize(Application app) {
  33. //It is technically safe to do all initialization and cleanup in the
  34. //onEnable()/onDisable() methods. Choosing to use initialize() and
  35. //cleanup() for this is a matter of performance specifics for the
  36. //implementor.
  37. //TODO: initialize your AppState, e.g. attach spatials to rootNode
  38. ((SimpleApplication) app).getRootNode().attachChild(localRootNode);
  39. ((SimpleApplication) app).getGuiNode().attachChild(localGuiNode);
  40. ((SimpleApplication) app).getViewPort().setBackgroundColor(backgroundColor);
  41. /** init the screen */
  42. }
  43. @Override
  44. protected void cleanup(Application app) {
  45. //TODO: clean up what you initialized in the initialize method,
  46. //e.g. remove all spatials from rootNode
  47. ((SimpleApplication) app).getRootNode().detachChild(localRootNode);
  48. ((SimpleApplication) app).getGuiNode().detachChild(localGuiNode);
  49. }
  50. //onEnable()/onDisable() can be used for managing things that should
  51. //only exist while the state is enabled. Prime examples would be scene
  52. //graph attachment or input listener attachment.
  53. @Override
  54. protected void onEnable() {
  55. //Called when the state is fully enabled, ie: is attached and
  56. //isEnabled() is true or when the setEnabled() status changes after the
  57. //state is attached.
  58. }
  59. @Override
  60. protected void onDisable() {
  61. //Called when the state was previously enabled but is now disabled
  62. //either because setEnabled(false) was called or the state is being
  63. //cleaned up.
  64. }
  65. @Override
  66. public void update(float tpf) {
  67. //TODO: implement behavior during runtime
  68. }
  69. @Override
  70. public void bind(Nifty nifty, Screen screen) {
  71. }
  72. @Override
  73. public void onStartScreen() {
  74. }
  75. @Override
  76. public void onEndScreen() {
  77. }
  78. }
  79. ----
  80. [IMPORTANT]
  81. ====
  82. It is not sufficient to just inherit from BaseAppState. You need to instantiate your controller class, register it with app's stateManager and then pass it to nifty. Remember, to connect a screen with a ScreenController you still need to specify the fully qualified class name of
  83. your ScreenController in the controller attribute of the <screen> tag in the xml file. See code sample below.
  84. ====
  85. .XML example
  86. [source,java]
  87. ----
  88. public class TestNiftyGui extends SimpleApplication {
  89. public void simpleInitApp() {
  90. StartScreenState startScreenState = new StartScreenState();
  91. stateManager.attach(startScreenState);
  92. // [...] boilerplate init nifty omitted
  93. nifty.fromXml("Interface/myGui.xml", "start", startScreenState); //one of the XML screen elements needs to reference StartScreenState controller class
  94. }
  95. }
  96. ----
  97. == Know Your Variables
  98. [cols="2", options="header"]
  99. |===
  100. a|Variable
  101. a|Description
  102. a|${CALL.myMethod()}
  103. a| Calls a method in the current ScreenController and gets the method's return String. The method can also be void and have a side effect, e.g. play a sound etc.
  104. a|${ENV.HOME}
  105. a| Returns the path to user's home directory.
  106. a|${ENV.key}
  107. a| Looks up `key` in the environment variables. Use it like Java's System.getEnv(“key).
  108. a|${PROP.key}
  109. a| looks up `key` in the Nifty properties. Use Nifty.setGlobalproperties(properties) and Nifty.getGlobalproperties(“key). Or SystemGetProperties(key);
  110. |===
  111. See also: link:https://github.com/nifty-gui/nifty-gui/raw/1.4/nifty-core/manual/nifty-gui-the-manual-1.3.2.pdf[Nifty GUI - the Manual: XML GUI (Special XML Markup)]
  112. == Use ScreenControllers for Mutally Exclusive Functionality
  113. Technically you are free to create one ScreenController class for each screen, or reuse the same ScreenController for all or some of them. In the end it may be best to create individual ScreenControllers for functionality that is mutually exclusive.
  114. For example, create a `MyHudScreen.java` for the `hud` screen, and a `MyStartScreen.java` for the `start` screen.
  115. * Include all user interface methods that are needed during the game (while the HUD is up) in `MyHudScreen.java`. Then make this class control all screens that can be up during the game (the HUD screen, a MiniMap screen, an Inventory screen, an Abilities or Skills screen, etc). All these screens possibly share data (game data, player data), so it makes sense to control them all with methods of the same `MyHudScreen.java` class.
  116. * The start screen, however, is mostly independent of the running game. Include all user interface methods that are needed outside the game (while you are on the start screen) in `MyStartScreen.java`. Then make this class control all screens that can be up outside the game (the Start screen, a Settings/Options screen, a HighScore screen, etc). All these classes need to read and write saved game data, so it makes sense to control them all with methods of the same `MyStartScreen.java` class.
  117. == Create a "Loading..." Screen
  118. Get the full <<jme3/advanced/loading_screen#,Loading Screen>> tutorial here.
  119. == Create a Popup Menu
  120. Get the full <<jme3/advanced/nifty_gui_popup_menu#,Nifty GUI PopUp Menu>> tutorial here.
  121. == Add Visual Effects
  122. You can register effects to screen elements.
  123. * Respond to element events such as onStartScreen, onEndScreen, onHover, onFocus, onActive,
  124. * Trigger effects that change movement, blending, size, color, fading, and much more.
  125. Here is an example that moves a panel when the startScreen opens. You place an &lt; effect &gt; tag inside the element that you want to be affected.
  126. [source,xml]
  127. ----
  128. <panel height="25%" width="35%" ...>
  129. <effect>
  130. <onStartScreen name="move" mode="in" direction="top" length="300" startDelay="0" inherit="true"/>
  131. </effect>
  132. </panel>
  133. ----
  134. Learn more from the NiftyGUI page:
  135. * link:https://github.com/nifty-gui/nifty-gui/raw/1.4/nifty-core/manual/nifty-gui-the-manual-1.3.2.pdf[Nifty GUI - the Manual: Effects]
  136. * link:https://github.com/nifty-gui/nifty-gui/wiki/Effects[Effects]
  137. == Add Sound Effects
  138. Playing sounds using Nifty is also possible with a `playSound` effect as trigger. Remember to first register the sound that you want to play:
  139. [source,xml]
  140. ----
  141. <registerSound id="myclick" filename="Interface/sounds/ButtonClick.ogg" />
  142. ...
  143. <label>
  144. <effect>
  145. <onClick name="playSound" sound="myclick"/>
  146. </effect>
  147. </label>
  148. ----
  149. == Pass ClickLoc From Nifty to Java
  150. After a mouse click, you may want to record the 2D clickLoc and send this info to your Java application. Typical ScreenController methods however only have a String argument. You'd have to convert the String to ints.
  151. To pass the clickLoc as two ints, you can use the special `(int x, int y)` syntax in the ScreenController:
  152. [source,java]
  153. ----
  154. public void clicked(int x, int y) {
  155. // here you can use the x and y of the clickLoc
  156. }
  157. ----
  158. In the Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++ screen code (e.g. XML file) you must call the `(int x, int y)` method _without_ any parameters!
  159. [source,xml]
  160. ----
  161. <interact onClick="clicked()"/>
  162. ----
  163. You can name the method (here `clicked`) what ever you like, as long as you keep the argument syntax.
  164. == Load Several XML Files
  165. The basic Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++ example showed how to use the `nifty.fromXML()` method to load one XML file containing all Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++ screens.
  166. The following code sample shows how you can load several XML files into one nifty object. Loading several files with `nifty.addXml()` allows you to split up each screen into one XML file, instead of all into one hard-to-read XML file.
  167. [source,java]
  168. ----
  169. NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, viewPort);
  170. Nifty nifty = niftyDisplay.getNifty();
  171. nifty.addXml("Interface/Screens/OptionsScreen.xml");
  172. nifty.addXml("Interface/Screens/StartScreen.xml");
  173. nifty.gotoScreen("startScreen");
  174. StartScreenControl screenControl = (StartScreenControl) nifty.getScreen("startScreen").getScreenController();
  175. OptionsScreenControl optionsControl = (OptionsScreenControl) nifty.getScreen("optionsScreen").getScreenController();
  176. stateManager.attach(screenControl);
  177. stateManager.attach(optionsControl);
  178. guiViewPort.addProcessor(niftyDisplay);
  179. ----
  180. == Register additional explicit screen controllers
  181. In addition to the `nifty.addXml()` methods to attach many nifty XML files, there exists a `nifty.registerScreenController()` method to explicitly attach more screen controllers.
  182. The following code sample shows how you can explicitly attach several screen controllers before adding the XML file to nifty, which would otherwise cause nifty to implicitly instantiate the screen controller class.
  183. [source,java]
  184. ----
  185. NiftyJmeDisplay niftyDisplay = NiftyJmeDisplay.newNiftyJmeDisplay(assetManager, inputManager, audioRenderer, viewPort);
  186. Nifty nifty = niftyDisplay.getNifty();
  187. nifty.registerScreenController(new OptionsScreenController(randomConstructorArgument));
  188. nifty.addXml("Interface/Screens/OptionsScreen.xml");
  189. ----
  190. == Design Your Own Styles
  191. By default, your Nifty XML screens use the built.in styles:
  192. [source,xml]
  193. ----
  194. <useStyles filename="nifty-default-styles.xml" />
  195. ----
  196. But you can switch to a set of custom styles in your game project's asset directory like this:
  197. [source,xml]
  198. ----
  199. <useStyles filename="Interface/Styles/myCustomStyles.xml" />
  200. ----
  201. Inside myCustomStyles.xml you define styles like this:
  202. [source,xml]
  203. ----
  204. <?xml version="1.0" encoding="UTF-8"?>
  205. <nifty-styles>
  206. <useStyles filename="Interface/Styles/Font/myCustomFontStyle.xml" />
  207. <useStyles filename="Interface/Styles/Button/myCustomButtonStyle.xml" />
  208. <useStyles filename="Interface/Styles/Label/myCustomLabelStyle.xml" />
  209. ...
  210. </nifty-styles>
  211. ----
  212. Learn more about how to create styles by looking at the link:https://github.com/nifty-gui/nifty-gui/wiki/Working-from-Source[Nifty GUI source code] for “nifty-style-black”. Copy it as a template and change it to create your own style.
  213. '''
  214. Learn more from the NiftyGUI page:
  215. * link:https://github.com/nifty-gui/nifty-gui/wiki/Effects[https://github.com/nifty-gui/nifty-gui/wiki/Effects]