hud.adoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. = Head-Up Display (HUD)
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :keywords: gui, display, documentation, hud
  6. :relfileprefix: ../../
  7. :imagesdir: ../..
  8. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  9. ////
  10. image::http://www.jmonkeyengine.com/wp-content/uploads/2010/10/grapplinghook.jpg[grapplinghook.jpg,width="256",height="192",align="right"]
  11. ////
  12. 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.
  13. HUDs are used to supply players with essential information about the game state.
  14. * Status: Score, minimap, points, stealth mode, …
  15. * Resources: Ammunition, lives/health, time, …
  16. * Vehicle instruments: Cockpit, speedometer, …
  17. * Navigational aides: Crosshairs, mouse pointer or hand, …
  18. You have multiple options how to create HUDs.
  19. [cols="3", options="header"]
  20. |===
  21. a|Option
  22. a|Pros
  23. a|Cons
  24. a|Attach elements to default guiNode:
  25. a|Easy to learn. jMonkeyEngine built-in +++<abbr title="Application Programming Interface">API</abbr>+++ for attaching plain images and bitmap text.
  26. a|Only basic features. +
  27. You will have to write custom controls / buttons / effects if you need them.
  28. a|Use advanced <<jme3/advanced/nifty_gui#,Nifty GUI>> integration:
  29. a|Full-featured interactive user interface. +
  30. Includes buttons, effects, controls. +
  31. Supports XML and Java layouts.
  32. a|Steeper learning curve.
  33. a|Use user contributed +++<abbr title="Graphical User Interface">GUI</abbr>+++ libraries such as <<jme3/contributions/tonegodgui#,tonegodgui>> or link:http://hub.jmonkeyengine.org/t/lemur-api-documentation/27209[Lemur]:
  34. a|Both have many features that would be difficult to do with Nifty +
  35. Includes buttons, effects, controls. +
  36. New features are still being released
  37. a|Are not necessarily guaranteed future updates, not as well documented
  38. |===
  39. 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.
  40. == Simple HUD: GUI Node
  41. You already know the `rootNode` that holds the 3-dimensional scene graph. jME3 also offers a 2-dimension (orthogonal) node, the `guiNode`.
  42. This is how you use the guiNode for HUDs:
  43. * Create a +++<abbr title="Graphical User Interface">GUI</abbr>+++ element: a BitmapText or Picture object.
  44. * Attach the element to the guiNode.
  45. * Place the element in the orthogonal render queue using `setQueueBucket(Bucket.Gui)`.
  46. The BitmapTexts and Pictures appear as 2 dimensional element on the screen.
  47. [TIP]
  48. ====
  49. 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:
  50. [source,java]
  51. ----
  52. setDisplayStatView(false); setDisplayFps(false);
  53. ----
  54. ====
  55. === Displaying Pictures in the HUD
  56. A simple image can be displayed using `com.jme3.ui.Picture`.
  57. [source,java]
  58. ----
  59. Picture pic = new Picture("HUD Picture");
  60. pic.setImage(assetManager, "Textures/ColoredTex/Monkey.png", true);
  61. pic.setWidth(settings.getWidth()/2);
  62. pic.setHeight(settings.getHeight()/2);
  63. pic.setPosition(settings.getWidth()/4, settings.getHeight()/4);
  64. guiNode.attachChild(pic);
  65. ----
  66. When you set the last boolean in setImage() to true, the alpha channel of your image is rendered transparent/translucent.
  67. === Displaying Text in the HUD
  68. You use `com.jme3.font.BitmapText` to display text on the screen.
  69. [source,java]
  70. ----
  71. BitmapText hudText = new BitmapText(guiFont, false);
  72. hudText.setSize(guiFont.getCharSet().getRenderedSize()); // font size
  73. hudText.setColor(ColorRGBA.Blue); // font color
  74. hudText.setText("You can write any string here"); // the text
  75. hudText.setLocalTranslation(300, hudText.getLineHeight(), 0); // position
  76. guiNode.attachChild(hudText);
  77. ----
  78. The BitmapFont object `guiFont` is a default font provided by SimpleApplication. Copy you own fonts as .fnt plus .png files into the `assets/Interface/Fonts` directory and load them like this:
  79. [source]
  80. ----
  81. BitmapFont myFont = assetManager.loadFont("Interface/Fonts/Console.fnt");
  82. hudText = new BitmapText(myFont, false);
  83. ----
  84. === Positioning HUD Elements
  85. * 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())`.
  86. * If you have several 2D elements in the +++<abbr title="Graphical User Interface">GUI</abbr>+++ bucket that overlap, define their depth order by specifing 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.
  87. * Size and length values in the orthogonal render queue are treated like pixels. A 20*20-wu big quad is rendered 20 pixels wide.
  88. === Displaying Geometries in the HUD
  89. 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.
  90. [IMPORTANT]
  91. ====
  92. 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.
  93. ====
  94. === Keeping the HUD Up-To-Date
  95. Use the update loop to keep the content up-to-date.
  96. [source,java]
  97. ----
  98. public void simpleUpdate(float tpf) {
  99. ...
  100. hudText.setText("Score: " + score);
  101. ...
  102. picture.setImage(assetManager, "Interface/statechange.png", true);
  103. ...
  104. }
  105. ----
  106. == Advanced HUD: Nifty GUI
  107. The recommended approach to create HUDs is using <<jme3/advanced/nifty_gui#,Nifty GUI>>.
  108. . Lay out the +++<abbr title="Graphical User Interface">GUI</abbr>+++ in one or several Nifty XML or Java files.
  109. . Write the controller classes in Java.
  110. . Load the XML file with the controller object in your game's simpleInit() method.
  111. 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.
  112. For HUDs, you basically follow the same instructions as for creating a normal <<jme3/advanced/nifty_gui#,Nifty GUI>>, you just don't pause the game while the HUD is up.
  113. == See also
  114. * <<jme3/external/fonts#,Fonts>>