nifty_gui_overlay.adoc 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. = Integrating Nifty GUI: Overlay
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :keywords: gui, documentation, nifty, hud
  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. . *Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++ Overlay* or <<jme3/advanced/nifty_gui_projection#,Nifty GUI Projection>>
  14. . <<jme3/advanced/nifty_gui_java_interaction#,Interact with the GUI from Java>>
  15. image::jme3/advanced/nifty-gui-example.png[nifty-gui-example.png,width="300",height="200",align="left"]
  16. Typically, you define a key (for example escape) that switches the +++<abbr title="Graphical User Interface">GUI</abbr>+++ on and off. The +++<abbr title="Graphical User Interface">GUI</abbr>+++ can be a StartScreen, OptionsScreen, CharacterCreationScreen, etc. While the +++<abbr title="Graphical User Interface">GUI</abbr>+++ is up, you pause the running game, and then overlay the +++<abbr title="Graphical User Interface">GUI</abbr>+++. You also must switch to a different set of user inputs while the game is paused, so the player can use the mouse pointer and keyboard to interact with the +++<abbr title="Graphical User Interface">GUI</abbr>+++.
  17. You can also <<jme3/advanced/nifty_gui_projection#,project>> the +++<abbr title="Graphical User Interface">GUI</abbr>+++ as a texture onto a mesh texture (but then you cannot click to select).
  18. On this page, we look at the overlay variant, which is more commonly used in games.
  19. == Sample Code
  20. * link:https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-examples/src/main/java/jme3test/niftygui/TestNiftyGui.java[TestNiftyGui.java]
  21. [TIP]
  22. ====
  23. The jme3-niftygui library is included in jMonkeyEngine. If you installed jMonkeyEngine using one of the <<documentation#install,optional methods>>, it will be added to your projects Library folder as part of the installation. If you're using the jMonkeyEngine SDK, you add it to any project by btn:[RMB] selecting your projects `Library` folder, choosing `menu:Add Library[jme-niftygui]` followed by `Add Library`.
  24. ====
  25. == Overlaying the User Interface Over the Screen
  26. This code shows you how to overlay anything on the screen with the +++<abbr title="Graphical User Interface">GUI</abbr>+++. This is the most common usecase.
  27. [source,java]
  28. ----
  29. NiftyJmeDisplay niftyDisplay = NiftyJmeDisplay.newNiftyJmeDisplay(
  30. assetManager, inputManager, audioRenderer, guiViewPort);
  31. /** Create a new NiftyGUI object */
  32. Nifty nifty = niftyDisplay.getNifty();
  33. /** Read your XML and initialize your custom ScreenController */
  34. nifty.fromXml("Interface/tutorial/step2/screen.xml", "start");
  35. // nifty.fromXml("Interface/helloworld.xml", "start", new MySettingsScreen(data));
  36. // attach the Nifty display to the gui view port as a processor
  37. guiViewPort.addProcessor(niftyDisplay);
  38. // disable the fly cam
  39. flyCam.setDragToRotate(true);
  40. ----
  41. Currently you do not have a ScreenController – we will create one in the next exercise. As soon as you have a screen controller, you will use the commented variant of the XML loading method:
  42. [source,java]
  43. ----
  44. nifty.fromXml("Interface/helloworld.xml", "start", new MySettingsScreen());
  45. ----
  46. The `MySettingsScreen` class is a custom de.lessvoid.nifty.screen.ScreenController in which you will implement your +++<abbr title="Graphical User Interface">GUI</abbr>+++ behaviour.
  47. If you have many screens or you want to keep them organized in separate files there is a method available that will just load an additional XML file. The content of the files are
  48. simply added to whatever XML data has been loaded before.
  49. [source,java]
  50. ----
  51. nifty.addXml("Interface/mysecondscreen.xml");
  52. ----
  53. == Next Steps
  54. Now that you have layed out and integrated the +++<abbr title="Graphical User Interface">GUI</abbr>+++ in your app, you want to respond to user input and display the current game. Time to create a ScreenController!
  55. * <<jme3/advanced/nifty_gui_java_interaction#,Interact with the GUI from Java>>