nifty_gui_java_interaction.adoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. = Interacting with the GUI from Java
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :keywords: gui, documentation, input, control, hud, nifty
  6. :relfileprefix: ../../
  7. :imagesdir: ../..
  8. :experimental:
  9. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  10. . <<jme3/advanced/nifty_gui#,Nifty GUI Concepts>>
  11. . <<jme3/advanced/nifty_gui_best_practices#,Nifty GUI Best Practices>>
  12. . <<jme3/advanced/nifty_gui_xml_layout#,Nifty GUI XML Layout>> or <<jme3/advanced/nifty_gui_java_layout#,Nifty GUI Java Layout>>
  13. . <<jme3/advanced/nifty_gui_overlay#,Nifty GUI Overlay>> or <<jme3/advanced/nifty_gui_projection#,Nifty GUI Projection>>
  14. . *Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++ Java Interaction*
  15. In the previous parts of the tutorial, you created a two-screen user interface. But it is still static, and when you click the buttons, nothing happens yet. The purpose of the +++<abbr title="Graphical User Interface">GUI</abbr>+++ is to communicate with your Java classes: Your game needs to know what the users clicked, which settings they chose, which values they entered into a field, etc. Similarly, the user needs to know what the currently game state is (score, health, etc).
  16. == Connect GUI to Java Controller
  17. To let a Nifty screen communicate with the Java application, you register a `ScreenController` to every NiftyGUI screen. You create a ScreenController by creating a Java class that implements the `de.lessvoid.nifty.screen.ScreenController` interface and its abstract methods.
  18. *Pro Tip:* Since you are writing a jME3 application, you can additionally make the ScreenController class extend the <<jme3/advanced/application_states#,BaseAppState>> class! This gives the ScreenController access to the application object and to the update loop!
  19. Create an AppState *MyStartScreen*.java file in your package. ( btn:[RMB] click on your package and select `menu:New[Other>JME3 Classes>New BaseAppState]`)
  20. Now add *implements ScreenController* to _public class MyStartScreen extends BaseAppState{_ and add *import de.lessvoid.nifty.screen.ScreenController;*
  21. Continue with adding:
  22. [source,java]
  23. ----
  24. import de.lessvoid.nifty.screen.Screen;
  25. ...
  26. public void bind(Nifty nifty, Screen screen) {
  27. throw new UnsupportedOperationException("Not supported yet.");
  28. }
  29. public void onStartScreen() {
  30. throw new UnsupportedOperationException("Not supported yet.");
  31. }
  32. public void onEndScreen() {
  33. throw new UnsupportedOperationException("Not supported yet.");
  34. }
  35. ----
  36. .Complete BaseAppState file
  37. [source,java]
  38. ----
  39. package mygame;
  40. import com.jme3.app.Application;
  41. import com.jme3.app.state.BaseAppState;
  42. import de.lessvoid.nifty.Nifty;
  43. import de.lessvoid.nifty.screen.Screen;
  44. import de.lessvoid.nifty.screen.ScreenController;
  45. public class MyStartScreen extends BaseAppState implements ScreenController {
  46. @Override
  47. protected void initialize(Application app) {
  48. //It is technically safe to do all initialization and cleanup in the
  49. //onEnable()/onDisable() methods. Choosing to use initialize() and
  50. //cleanup() for this is a matter of performance specifics for the
  51. //implementor.
  52. //TODO: initialize your AppState, e.g. attach spatials to rootNode
  53. }
  54. @Override
  55. protected void cleanup(Application app) {
  56. //TODO: clean up what you initialized in the initialize method,
  57. //e.g. remove all spatials from rootNode
  58. }
  59. //onEnable()/onDisable() can be used for managing things that should
  60. //only exist while the state is enabled. Prime examples would be scene
  61. //graph attachment or input listener attachment.
  62. @Override
  63. protected void onEnable() {
  64. //Called when the state is fully enabled, ie: is attached and
  65. //isEnabled() is true or when the setEnabled() status changes after the
  66. //state is attached.
  67. }
  68. @Override
  69. protected void onDisable() {
  70. //Called when the state was previously enabled but is now disabled
  71. //either because setEnabled(false) was called or the state is being
  72. //cleaned up.
  73. }
  74. @Override
  75. public void update(float tpf) {
  76. //TODO: implement behavior during runtime
  77. }
  78. /**
  79. * Bind this ScreenController to a screen. This happens right before the
  80. * onStartScreen STARTED and only exactly once for a screen!
  81. * @param nifty nifty
  82. * @param screen screen
  83. */
  84. @Override
  85. public void bind(Nifty nifty, Screen screen) {
  86. throw new UnsupportedOperationException("Not supported yet.");
  87. }
  88. /**
  89. * called right after the onStartScreen event ENDED.
  90. */
  91. @Override
  92. public void onStartScreen() {
  93. throw new UnsupportedOperationException("Not supported yet.");
  94. }
  95. /**
  96. * called right after the onEndScreen event ENDED.
  97. */
  98. @Override
  99. public void onEndScreen() {
  100. throw new UnsupportedOperationException("Not supported yet.");
  101. }
  102. }
  103. ----
  104. The name and package of your custom ScreenController class (here `mygame.MyStartScreen`) goes into the controller parameter of the respective XML screen it belongs to. For example:
  105. [source,xml]
  106. ----
  107. <nifty>
  108. <screen id="start" controller="mygame.MyStartScreen">
  109. <!-- layer and panel code ... -->
  110. </screen>
  111. </nifty>
  112. ----
  113. Or the same in a Java syntax, respectively:
  114. [source,java]
  115. ----
  116. nifty.addScreen("start", new ScreenBuilder("start") {{
  117. controller(new mygame.MyStartScreen());
  118. }}.build(nifty));
  119. ----
  120. Now the Java class `MyStartScreen` and this +++<abbr title="Graphical User Interface">GUI</abbr>+++ screen (`start`) are connected. For this example you can also connect the `hud` screen to MyStartScreen.
  121. 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: Elements (Screen Controller)]
  122. == Make GUI and Java Interact
  123. In most cases, you will want to pass game data in and out of the ScreenController. Note that you can pass any custom arguments from your Java class into your ScreenController constructor (`public MyStartScreen(GameData data) {}`).
  124. Use any combination of the three following approaches to make Java classes interact with the +++<abbr title="Graphical User Interface">GUI</abbr>+++.
  125. === GUI Calls a Void Java Method
  126. This is how you respond to an +++<abbr title="Graphical User Interface">GUI</abbr>+++ interaction such as clicks in XML GUIs:
  127. . Add `visibleToMouse="true"` to the parent element!
  128. . Embed the `<interact />` element into the parent element.
  129. . Specify the Java methods that you want to call when the users performs certain actions, such as clicking. +
  130. Example: `<interact onClick="startGame(hud)" />`
  131. Or this is how you respond to an +++<abbr title="Graphical User Interface">GUI</abbr>+++ interaction such as clicks in Java GUIs:
  132. . Add `visibleToMouse(true);` to the parent element!
  133. . Embed one of the `interact…()` elements into the parent element.
  134. . Specify the Java method that you want to call after the interaction. +
  135. Example: `interactOnClick("startGame(hud)");`
  136. In the following example, we call the `startGame()` method when the player clicks the Start button, and `quitGame()` when the player clicks the Quit button.
  137. [source,xml]
  138. ----
  139. <panel id="panel_bottom_left" height="50%" width="50%" valign="center" childLayout="center">
  140. <control name="button" label="Start" id="StartButton" align="center" valign="center"
  141. visibleToMouse="true" >
  142. <interact onClick="startGame(hud)"/>
  143. </control>
  144. </panel>
  145. <panel id="panel_bottom_right" height="50%" width="50%" valign="center" childLayout="center">
  146. <control name="button" label="Quit" id="QuitButton" align="center" valign="center"
  147. visibleToMouse="true" >
  148. <interact onClick="quitGame()"/>
  149. </control>
  150. </panel>
  151. ----
  152. Or the same in a Java syntax, respectively:
  153. [source,java]
  154. ----
  155. control(new ButtonBuilder("StartButton", "Start") {{
  156. alignCenter();
  157. valignCenter();
  158. height("50%");
  159. width("50%");
  160. visibleToMouse(true);
  161. interactOnClick("startGame(hud)");
  162. }});
  163. ...
  164. control(new ButtonBuilder("QuitButton", "Quit") {{
  165. alignCenter();
  166. valignCenter();
  167. height("50%");
  168. width("50%");
  169. visibleToMouse(true);
  170. interactOnClick("quitGame()");
  171. }});
  172. ----
  173. Back in the MyStartScreen class, you specify what the `startGame()` and `quitGame()` methods do. As you see, you can pass String arguments (here `hud`) in the method call. You also see that you have access to the Application object.
  174. [source,java]
  175. ----
  176. public class MyStartScreen extends BaseAppState implements ScreenController {
  177. ...
  178. /** custom methods */
  179. public void startGame(String nextScreen) {
  180. nifty.gotoScreen(nextScreen); // switch to another screen
  181. // start the game and do some more stuff...
  182. }
  183. public void quitGame() {
  184. getApplication().stop();
  185. }
  186. ...
  187. }
  188. ----
  189. The startGame() example simply switches the +++<abbr title="Graphical User Interface">GUI</abbr>+++ to the `hud` screen when the user clicks Start. Of course, in a real game, you would perform more steps here: Load the game level, switch to in-game input and navigation handling, set a custom `running` boolean to true, attach custom in-game AppStates – and lots more.
  190. The quitGame() example shows that you have access to the Application object because you made the ScreenController extend BaseAppState.
  191. === GUI Gets Return Value from Java Method
  192. When the Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++ is initialized, you can get data from Java. In this example, the Java class `getPlayerName()` in `MyStartScreen` defines the Text that is displayed in the textfield before the words `'s Cool Game`.
  193. First define a Java method in the screen controller, in this example, `getPlayerName()`.
  194. [source,java]
  195. ----
  196. public class MySettingsScreen implements ScreenController {
  197. ...
  198. public String getPlayerName(){
  199. return System.getProperty("user.name");
  200. }
  201. }
  202. ----
  203. Nifty uses `${CALL.getPlayerName()}` to get the return value of the getPlayerName() method from your ScreenController Java class.
  204. [source,xml]
  205. ----
  206. <text text="${CALL.getPlayerName()}'s Cool Game" font="Interface/Fonts/Default.fnt" width="100%" height="100%" />
  207. ----
  208. Or the same in a Java syntax, respectively:
  209. [source,java]
  210. ----
  211. text(new TextBuilder() {{
  212. text("${CALL.getPlayerName()}'s Cool Game");
  213. font("Interface/Fonts/Default.fnt");
  214. height("100%");
  215. width("100%");
  216. }});
  217. ----
  218. You can use this for Strings and numeric values (e.g. when you read settings from a file, you display the results in the +++<abbr title="Graphical User Interface">GUI</abbr>+++) and also for methods with side effects.
  219. === Java Modifies Nifty Elements and Events
  220. You can also alter the appearance and functions of your nifty elements from Java. Make certain that the element that you want to alter has its `id="name"` attribute set, so you can identy and address it.
  221. Here's an example of how to change an image called `playerhealth`:
  222. [source,java]
  223. ----
  224. // load or create new image
  225. NiftyImage img = nifty.getRenderEngine().createImage("Interface/Images/face2.png", false);
  226. // find old image
  227. Element niftyElement = nifty.getCurrentScreen().findElementByName("playerhealth");
  228. // swap old with new image
  229. niftyElement.getRenderer(ImageRenderer.class).setImage(img);
  230. ----
  231. The same is valid for other elements, for example a text label "`score`":
  232. [source,java]
  233. ----
  234. // find old text
  235. Element niftyElement = nifty.getCurrentScreen().findElementByName("score");
  236. // swap old with new text
  237. niftyElement.getRenderer(TextRenderer.class).setText("124");
  238. ----
  239. Similarly, to change the onClick() event of an element, create an `ElementInteraction` object:
  240. [source,java]
  241. ----
  242. Element niftyElement = nifty.getCurrentScreen().findElementByName("myElement");
  243. niftyElement.getElementInteraction().getPrimary().setOnMouseOver(new NiftyMethodInvoker(nifty, "myCustomMethod()", this));
  244. ----
  245. For this to work, there already needs to be a (possibly inactive) `<interact />` tag inside your xml element:
  246. [source,xml]
  247. ----
  248. <interact onClick="doNothing()"/>
  249. ----
  250. == Next Steps
  251. You're done with the basic Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++ for jME3 tutorial. You can proceed to advanced topics and learn how add controls and effects:
  252. * <<jme3/advanced/nifty_gui_scenarios#, Nifty GUI Scenarios>>
  253. * 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]
  254. * link:https://github.com/nifty-gui/nifty-gui/wiki/Controls[Controls]