hud.adoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. = Head-Up Display (HUD)
  2. :revnumber: 2.0
  3. :revdate: 2020/07/24
  4. :keywords: gui, display, documentation, hud
  5. ////
  6. image::http://www.jmonkeyengine.com/wp-content/uploads/2010/10/grapplinghook.jpg[grapplinghook.jpg,width="256",height="192",align="right"]
  7. ////
  8. A HUD (Head-Up Display) is part of a game's visual user interface. It's an overlay that displays additional information as (typically) 2-dimensional text or icons on the screen, on top of the 3D scene. Not all games have, or need a HUD. To avoid breaking the immersion and cluttering the screen, only use a HUD if it is the only way to convey certain information.
  9. HUDs are used to supply players with essential information about the game state.
  10. * Status: Score, minimap, points, stealth mode, …
  11. * Resources: Ammunition, lives/health, time, …
  12. * Vehicle instruments: Cockpit, speedometer, …
  13. * Navigational aides: Crosshairs, mouse pointer or hand, …
  14. You have multiple options how to create HUDs.
  15. [cols="3", options="header"]
  16. |===
  17. a|Option
  18. a|Pros
  19. a|Cons
  20. a|Attach elements to default guiNode:
  21. a|Easy to learn. jMonkeyEngine built-in +++<abbr title="Application Programming Interface">API</abbr>+++ for attaching plain images and bitmap text.
  22. a|Only basic features. +
  23. You will have to write custom controls / buttons / effects if you need them.
  24. a|Use advanced xref:gui/nifty_gui.adoc[Nifty GUI] integration:
  25. a|Full-featured interactive user interface. +
  26. Includes buttons, effects, controls. +
  27. Supports XML and Java layouts.
  28. a|Steeper learning curve.
  29. a|Use user contributed +++<abbr title="Graphical User Interface">GUI</abbr>+++ libraries such as xref:contributions:gui/tonegodgui/tonegodgui.adoc[tonegodgui] or link:http://hub.jmonkeyengine.org/t/lemur-api-documentation/27209[Lemur]:
  30. a|Both have many features that would be difficult to do with Nifty +
  31. Includes buttons, effects, controls. +
  32. New features are still being released
  33. a|Are not necessarily guaranteed future updates, not as well documented
  34. |===
  35. Using the +++<abbr title="Graphical User Interface">GUI</abbr>+++ Node is the default approach in jme3 to create simple HUDs. If you just quickly want to display a line of text, or a simple icon on the screen, use the no-frills +++<abbr title="Graphical User Interface">GUI</abbr>+++ Node, it's easier.
  36. == Simple HUD: GUI Node
  37. You already know the `rootNode` that holds the 3-dimensional scene graph. jME3 also offers a 2-dimension (orthogonal) node, the `guiNode`.
  38. This is how you use the guiNode for HUDs:
  39. * Create a +++<abbr title="Graphical User Interface">GUI</abbr>+++ element: a BitmapText or Picture object.
  40. * Attach the element to the guiNode.
  41. * Place the element in the orthogonal render queue using `setQueueBucket(Bucket.Gui)`.
  42. The BitmapTexts and Pictures appear as 2 dimensional element on the screen.
  43. [TIP]
  44. ====
  45. By default, the guiNode has some scene graph statistics attached. To clear the guiNode before you attach your own +++<abbr title="Graphical User Interface">GUI</abbr>+++ elements, use the following methods:
  46. [source,java]
  47. ----
  48. setDisplayStatView(false); setDisplayFps(false);
  49. ----
  50. ====
  51. === Displaying Pictures in the HUD
  52. A simple image can be displayed using `com.jme3.ui.Picture`.
  53. [source,java]
  54. ----
  55. Picture pic = new Picture("HUD Picture");
  56. pic.setImage(assetManager, "Textures/ColoredTex/Monkey.png", true);
  57. pic.setWidth(settings.getWidth()/2);
  58. pic.setHeight(settings.getHeight()/2);
  59. pic.setPosition(settings.getWidth()/4, settings.getHeight()/4);
  60. guiNode.attachChild(pic);
  61. ----
  62. When you set the last boolean in setImage() to true, the alpha channel of your image is rendered transparent/translucent.
  63. === Displaying Text in the HUD
  64. You use `com.jme3.font.BitmapText` to display text on the screen.
  65. [source,java]
  66. ----
  67. BitmapText hudText = new BitmapText(guiFont, false);
  68. hudText.setSize(guiFont.getCharSet().getRenderedSize()); // font size
  69. hudText.setColor(ColorRGBA.Blue); // font color
  70. hudText.setText("You can write any string here"); // the text
  71. hudText.setLocalTranslation(300, hudText.getLineHeight(), 0); // position
  72. guiNode.attachChild(hudText);
  73. ----
  74. The BitmapFont object `guiFont` is a default font provided by SimpleApplication. Copy your own fonts as .fnt plus .png files into the `assets/Interface/Fonts` directory and load them like this:
  75. [source]
  76. ----
  77. BitmapFont myFont = assetManager.loadFont("Interface/Fonts/Console.fnt");
  78. hudText = new BitmapText(myFont, false);
  79. ----
  80. === Positioning HUD Elements
  81. * When positioning +++<abbr title="Graphical User Interface">GUI</abbr>+++ text and images in 2D, the *bottom left corner* of the screen is `(0f,0f)`, and the *top right corner* is at `(settings.getWidth(),settings.getHeight())`.
  82. * If you have several 2D elements in the +++<abbr title="Graphical User Interface">GUI</abbr>+++ bucket that overlap, define their depth order by specifying a Z value. For example use `pic.move(x, y, -1)` to move the picture to the background, or `hudText.setLocalTranslation(x,y,1)` to move text to the foreground.
  83. * Size and length values in the orthogonal render queue are treated like pixels. A 20*20-wu big quad is rendered 20 pixels wide.
  84. === Displaying Geometries in the HUD
  85. It is technically possible to attach Quads and 3D Geometries to the HUD. They show up as flat, static +++<abbr title="Graphical User Interface">GUI</abbr>+++ elements. The size unit for the guiNode is pixels, not world units. If you attach a Geometry that uses a lit Material, you must add a light to the guiNode.
  86. [IMPORTANT]
  87. ====
  88. If you don't see an attached object in the +++<abbr title="Graphical User Interface">GUI</abbr>+++, check it's position and material (add a light to guiNode). Also verify whether it is not too tiny to be seen. For comparison: A 1 world-unit wide cube is only 1 pixel wide when attached to the guiNode! You may need to scale it bigger.
  89. ====
  90. === Keeping the HUD Up-To-Date
  91. Use the update loop to keep the content up-to-date.
  92. [source,java]
  93. ----
  94. public void simpleUpdate(float tpf) {
  95. ...
  96. hudText.setText("Score: " + score);
  97. ...
  98. picture.setImage(assetManager, "Interface/statechange.png", true);
  99. ...
  100. }
  101. ----
  102. == Advanced HUD: Nifty GUI
  103. The recommended approach to create HUDs is using xref:gui/nifty_gui.adoc[Nifty GUI].
  104. . Lay out the +++<abbr title="Graphical User Interface">GUI</abbr>+++ in one or several Nifty XML or Java files.
  105. . Write the controller classes in Java.
  106. . Load the XML file with the controller object in your game's simpleInit() method.
  107. The advantage of Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++ is that it is well integrated into jME and the jMonkeyEngine SDK, and that it offers all the features that you expect from a professional modern user interface.
  108. For HUDs, you basically follow the same instructions as for creating a normal xref:gui/nifty_gui.adoc[Nifty GUI], you just don't pause the game while the HUD is up.
  109. == See also
  110. * xref:sdk:plugin/fonts.adoc[Fonts]