nifty_gui_java_layout.adoc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. = Laying Out the GUI in Java
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :keywords: gui, documentation, nifty, hud
  6. :relfileprefix: ../../
  7. :imagesdir: ../..
  8. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  9. . <<jme3/advanced/nifty_gui#,Nifty GUI Concepts>>
  10. . <<jme3/advanced/nifty_gui_best_practices#,Nifty GUI Best Practices>>
  11. . <<jme3/advanced/nifty_gui_xml_layout#,Nifty GUI XML Layout>> or *Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++ Java Layout*
  12. . <<jme3/advanced/nifty_gui_overlay#,Nifty GUI Overlay>> or <<jme3/advanced/nifty_gui_projection#,Nifty GUI Projection>>
  13. . <<jme3/advanced/nifty_gui_java_interaction#,Interact with the GUI from Java>>
  14. You can "`draw`" the +++<abbr title="Graphical User Interface">GUI</abbr>+++ to the screen by writing Java code – alternatively to using XML. Typically you lay out the static base +++<abbr title="Graphical User Interface">GUI</abbr>+++ in XML, and use Java commands if you need to change the +++<abbr title="Graphical User Interface">GUI</abbr>+++ dynamically at runtime. In theory, you can also lay out the whole +++<abbr title="Graphical User Interface">GUI</abbr>+++ in Java (but we don't cover that here).
  15. == Sample Code
  16. Sample project
  17. * *Original Source Code:* link:https://github.com/nifty-gui/nifty-gui/tree/1.4/nifty-examples/src/main/java/de/lessvoid/nifty/examples/defaultcontrols[/nifty-examples/src/main/java/de/lessvoid/nifty/examples/defaultcontrols/].
  18. //* *Download demo project:* link:http://files.seapegasus.org/NiftyGuiDemo.zip[http://files.seapegasus.org/NiftyGuiDemo.zip] (jme3-ready) +
  19. //The full demo ZIP is based on `de.lessvoid.nifty.examples.controls.ControlsDemo.java`.
  20. //.. The demo is a SimpleApplication-based game (use e.g. the BasicGame template in the jMonkeyEngine SDK).
  21. //.. Copy images and sound files into your project's `assets/Interface/` directory. (In this example, I copied them from `nifty-default-controls-examples/trunk/src/main/resources/` to `assets/Interface/`).
  22. //.. Make sure to use paths relative to your project's `assets/` directory.
  23. //*** E.g. for .fnt/.png/.jpg files use filename `Interface/yang.png` not filename `yang.png`.
  24. //*** E.g. for .wav/.ogg files use filename `Interface/sounds/gong.wav` not filename `sounds/gong.wav`.
  25. Just so you get a quick picture what Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++'s Java Syntax looks like, here is the most basic example. It creates a screen with a layer and a panel that contains a button.
  26. [source,java]
  27. ----
  28. package mygame;
  29. import com.jme3.app.Application;
  30. import com.jme3.app.SimpleApplication;
  31. import com.jme3.app.state.BaseAppState;
  32. import com.jme3.niftygui.NiftyJmeDisplay;
  33. import de.lessvoid.nifty.Nifty;
  34. import de.lessvoid.nifty.builder.LayerBuilder;
  35. import de.lessvoid.nifty.builder.PanelBuilder;
  36. import de.lessvoid.nifty.builder.ScreenBuilder;
  37. import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
  38. import de.lessvoid.nifty.screen.DefaultScreenController;
  39. public class MyStartScreen extends BaseAppState {
  40. @Override
  41. protected void initialize(Application app) {
  42. //It is technically safe to do all initialization and cleanup in the
  43. //onEnable()/onDisable() methods. Choosing to use initialize() and
  44. //cleanup() for this is a matter of performance specifics for the
  45. //implementor.
  46. //TODO: initialize your AppState, e.g. attach spatials to rootNode
  47. }
  48. @Override
  49. protected void cleanup(Application app) {
  50. //TODO: clean up what you initialized in the initialize method,
  51. //e.g. remove all spatials from rootNode
  52. }
  53. //onEnable()/onDisable() can be used for managing things that should
  54. //only exist while the state is enabled. Prime examples would be scene
  55. //graph attachment or input listener attachment.
  56. @Override
  57. protected void onEnable() {
  58. NiftyJmeDisplay niftyDisplay = NiftyJmeDisplay.newNiftyJmeDisplay(
  59. getApplication().getAssetManager(),
  60. getApplication().getInputManager(),
  61. getApplication().getAudioRenderer(),
  62. getApplication().getGuiViewPort());
  63. Nifty nifty = niftyDisplay.getNifty();
  64. getApplication().getGuiViewPort().addProcessor(niftyDisplay);
  65. ((SimpleApplication) getApplication()).getFlyByCamera().setDragToRotate(true);
  66. nifty.loadStyleFile("nifty-default-styles.xml");
  67. nifty.loadControlFile("nifty-default-controls.xml");
  68. // <screen>
  69. nifty.addScreen("Screen_ID", new ScreenBuilder("Hello Nifty Screen"){{
  70. controller(new DefaultScreenController()); // Screen properties
  71. // <layer>
  72. layer(new LayerBuilder("Layer_ID") {{
  73. childLayoutVertical(); // layer properties, add more...
  74. // <panel>
  75. panel(new PanelBuilder("Panel_ID") {{
  76. childLayoutCenter(); // panel properties, add more...
  77. // GUI elements
  78. control(new ButtonBuilder("Button_ID", "Hello Nifty"){{
  79. alignCenter();
  80. valignCenter();
  81. height("5%");
  82. width("15%");
  83. }});
  84. //.. add more GUI elements here
  85. }});
  86. // </panel>
  87. }});
  88. // </layer>
  89. }}.build(nifty));
  90. // </screen>
  91. nifty.gotoScreen("Screen_ID"); // start the screen
  92. }
  93. @Override
  94. protected void onDisable() {
  95. //Called when the state was previously enabled but is now disabled
  96. //either because setEnabled(false) was called or the state is being
  97. //cleaned up.
  98. }
  99. @Override
  100. public void update(float tpf) {
  101. //TODO: implement behavior during runtime
  102. }
  103. }
  104. ----
  105. == Implement Your GUI Layout
  106. image::jme3/advanced/gui-layout-draft.png[gui-layout-draft.png,width="",height="",align="left"]
  107. In this tutorial, you recreate the same screen as in the <<jme3/advanced/nifty_gui_xml_layout#, Laying out the GUI in XML>> example.
  108. Create an Screen.Java file that extends BaseAppState in the mygame directory of your project. One Java file can contain several, or even all screens. As a reminder: Nifty displays one screen at a time; a screen contains several layers on top of one another; each layer contains panels that are embedded into another; the panels contain the actual content (text, images, or controls).
  109. Next, setup your Nifty display.
  110. [source, java]
  111. ----
  112. NiftyJmeDisplay niftyDisplay = NiftyJmeDisplay.newNiftyJmeDisplay(
  113. getApplication().getAssetManager(),
  114. getApplication().getInputManager(),
  115. getApplication().getAudioRenderer(),
  116. getApplication().getGuiViewPort());
  117. Nifty nifty = niftyDisplay.getNifty();
  118. getApplication().getGuiViewPort().addProcessor(niftyDisplay);
  119. ((SimpleApplication) getApplication()).getFlyByCamera().setDragToRotate(true);
  120. nifty.loadStyleFile("nifty-default-styles.xml");
  121. nifty.loadControlFile("nifty-default-controls.xml");
  122. // <!-- ... -->
  123. nifty.gotoScreen("start"); // start the screen
  124. ----
  125. === Make Screens
  126. The following minimal Java file contains a start screen and a HUD screen. (Neither has been defined yet.)
  127. [source,java]
  128. ----
  129. nifty.addScreen("start", new ScreenBuilder("start") {{
  130. controller(new DefaultScreenController());
  131. // <!-- ... -->
  132. }}.build(nifty));
  133. nifty.addScreen("hud", new ScreenBuilder("hud") {{
  134. controller(new DefaultScreenController());
  135. // <!-- ... -->
  136. }}.build(nifty));
  137. ----
  138. Every Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++ must have a start screen. The others (in this example, the HUD screen) are optional.
  139. === Make Layers
  140. The following Java code shows how we add layers to the start screen and HUD screen. Add the following to your `Screen.java` file:
  141. [source,java]
  142. ----
  143. nifty.addScreen("start", new ScreenBuilder("start") {{
  144. controller(new DefaultScreenController());
  145. // layer added
  146. layer(new LayerBuilder("background") {{
  147. childLayoutCenter();
  148. backgroundColor("#000f");
  149. // <!-- ... -->
  150. }});
  151. layer(new LayerBuilder("foreground") {{
  152. childLayoutVertical();
  153. backgroundColor("#0000");
  154. // <!-- ... -->
  155. }});
  156. // layer added
  157. }}.build(nifty));
  158. ----
  159. Repeat the same, but use
  160. [source]
  161. ----
  162. nifty.addScreen("hud", new ScreenBuilder("hud"){{
  163. ----
  164. for the HUD screen.
  165. In a layer, you can now add panels and arrange them. Panels are containers that mark the areas where you want to display text, images, or controls (buttons etc) later.
  166. === Make Panels
  167. A panel is the inner-most container (that will contain the actual content: text, images, or controls). You place panels inside layers. The following panels go into in the `start` screen `foreground` layer:
  168. [source,java]
  169. ----
  170. nifty.addScreen("start", new ScreenBuilder("start") {{
  171. controller(new DefaultScreenController());
  172. // layer added
  173. layer(new LayerBuilder("background") {{
  174. childLayoutCenter();
  175. backgroundColor("#000f");
  176. // <!-- ... -->
  177. }});
  178. layer(new LayerBuilder("foreground") {{
  179. childLayoutVertical();
  180. backgroundColor("#0000");
  181. // panel added
  182. panel(new PanelBuilder("panel_top") {{
  183. childLayoutCenter();
  184. alignCenter();
  185. backgroundColor("#f008");
  186. height("25%");
  187. width("75%");
  188. }});
  189. panel(new PanelBuilder("panel_mid") {{
  190. childLayoutCenter();
  191. alignCenter();
  192. backgroundColor("#0f08");
  193. height("50%");
  194. width("75%");
  195. }});
  196. panel(new PanelBuilder("panel_bottom") {{
  197. childLayoutHorizontal();
  198. alignCenter();
  199. backgroundColor("#00f8");
  200. height("25%");
  201. width("75%");
  202. panel(new PanelBuilder("panel_bottom_left") {{
  203. childLayoutCenter();
  204. valignCenter();
  205. backgroundColor("#44f8");
  206. height("50%");
  207. width("50%");
  208. }});
  209. panel(new PanelBuilder("panel_bottom_right") {{
  210. childLayoutCenter();
  211. valignCenter();
  212. backgroundColor("#88f8");
  213. height("50%");
  214. width("50%");
  215. }});
  216. }}); // panel added
  217. }});
  218. // layer added
  219. }}.build(nifty));
  220. ----
  221. The following panels go into in the `hud` screen:
  222. [source,Java]
  223. ----
  224. nifty.addScreen("hud", new ScreenBuilder("hud") {{
  225. controller(new DefaultScreenController());
  226. layer(new LayerBuilder("background") {{
  227. childLayoutCenter();
  228. backgroundColor("#000f");
  229. // <!-- ... -->
  230. }});
  231. layer(new LayerBuilder("foreground") {{
  232. childLayoutHorizontal();
  233. backgroundColor("#0000");
  234. // panel added
  235. panel(new PanelBuilder("panel_left") {{
  236. childLayoutVertical();
  237. backgroundColor("#0f08");
  238. height("100%");
  239. width("80%");
  240. // <!-- spacer -->
  241. }});
  242. panel(new PanelBuilder("panel_right") {{
  243. childLayoutVertical();
  244. backgroundColor("#00f8");
  245. height("100%");
  246. width("20%");
  247. panel(new PanelBuilder("panel_top_right1") {{
  248. childLayoutCenter();
  249. backgroundColor("#00f8");
  250. height("15%");
  251. width("100%");
  252. }});
  253. panel(new PanelBuilder("panel_top_right2") {{
  254. childLayoutCenter();
  255. backgroundColor("#44f8");
  256. height("15%");
  257. width("100%");
  258. }});
  259. panel(new PanelBuilder("panel_bot_right") {{
  260. childLayoutCenter();
  261. valignCenter();
  262. backgroundColor("#88f8");
  263. height("70%");
  264. width("100%");
  265. }});
  266. }}); // panel added
  267. }});
  268. }}.build(nifty));
  269. ----
  270. Try the sample. Remember to activate a screen using `nifty.gotoScreen("start");` or `hud` respectively.
  271. The result should look as follows:
  272. image::jme3/advanced/nifty-gui-panels.png[nifty-gui-panels.png,width="",height="",align="center"]
  273. == Adding Content to Panels
  274. 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: Layouts] on the Nifty +++<abbr title="Graphical User Interface">GUI</abbr>+++ site.
  275. === Add Images
  276. The `start-background.png` image is a fullscreen background picture. In the `start` screen, add the following image element:
  277. [source,java]
  278. ----
  279. nifty.addScreen("start", new ScreenBuilder("start") {{
  280. controller(new DefaultScreenController());
  281. // layer added
  282. layer(new LayerBuilder("background") {{
  283. childLayoutCenter();
  284. backgroundColor("#000f");
  285. // add image
  286. image(new ImageBuilder() {{
  287. filename("Interface/start-background.png");
  288. }});
  289. }});
  290. // <!-- ... -->
  291. }}.build(nifty));
  292. ----
  293. The `hud-frame.png` image is a transparent frame that we use as HUD decoration. In the `hud` screen, add the following image element:
  294. [source,java]
  295. ----
  296. nifty.addScreen("hud", new ScreenBuilder("hud") {{
  297. controller(new DefaultScreenController());
  298. layer(new LayerBuilder("background") {{
  299. childLayoutCenter();
  300. backgroundColor("#000f");
  301. // add image
  302. image(new ImageBuilder() {{
  303. filename("Interface/hud-frame.png");
  304. }});
  305. }});
  306. // <!-- ... -->
  307. }}.build(nifty));
  308. ----
  309. The `face1.png` image is an image that you want to use as a status icon.
  310. In the `hud` screens `foreground` layer, add the following image element:
  311. [source,java]
  312. ----
  313. panel(new PanelBuilder("panel_top_right2") {{
  314. childLayoutCenter();
  315. backgroundColor("#44f8");
  316. height("15%");
  317. width("100%");
  318. // add image
  319. image(new ImageBuilder() {{
  320. filename("Interface/face1.png");
  321. valignCenter();
  322. alignCenter();
  323. height("50%");
  324. width("30%");
  325. }});
  326. }});
  327. ----
  328. This image is scaled to use 50% of the height and 30% of the width of its container.
  329. === Add Static Text
  330. The game title is a typical example of static text. In the `start` screen `foreground` layer, add the following text element:
  331. [source,java]
  332. ----
  333. // panel added
  334. panel(new PanelBuilder("panel_top") {{
  335. childLayoutCenter();
  336. alignCenter();
  337. backgroundColor("#f008");
  338. height("25%");
  339. width("75%");
  340. text(new TextBuilder() {{
  341. text("My Cool Game");
  342. font("Interface/Fonts/Default.fnt");
  343. height("100%");
  344. width("100%");
  345. }});
  346. }});
  347. ----
  348. For longer pieces of static text, such as an introduction, you can use `wrap="true"`. Setting `wrap="true"` will only work when you set a width for the text element, so that Nifty knows when to wrap a line. Add the following text element to the `Start` screen `foreground` layer:
  349. [source,java]
  350. ----
  351. panel(new PanelBuilder("panel_mid") {{
  352. childLayoutCenter();
  353. alignCenter();
  354. backgroundColor("#0f08");
  355. height("50%");
  356. width("75%");
  357. // add text
  358. text(new TextBuilder() {{
  359. text("Here goes some text describing the game and the rules and stuff. "
  360. + "Incidentally, the text is quite long and needs to wrap at the end of lines.");
  361. font("Interface/Fonts/Default.fnt");
  362. wrap(true);
  363. height("100%");
  364. width("100%");
  365. }});
  366. }});
  367. ----
  368. The font used is jME3's default font "`Interface/Fonts/Default.fnt`" which is included in the jMonkeyEngine.JAR. You can add your own fonts to your own `assets/Interface` directory. Set your font to the one you created during the start of the <<jme3/advanced/nifty_gui_xml_layout#implement-your-gui-layout#, Gui Implementation>> phase of your game.
  369. === Add Controls
  370. Before you can use any control, you must load a Control Definition first. Thats why we add the following two lines _before_ the screen definitions:
  371. [source,java]
  372. ----
  373. nifty.loadStyleFile("nifty-default-styles.xml");
  374. nifty.loadControlFile("nifty-default-controls.xml");
  375. ----
  376. ==== Label Control
  377. Use label controls for text that you want to edit dynamically from Java. One example for this is the score display.
  378. In the `hud` screen's `foreground` layer, add the following text element:
  379. [source,java]
  380. ----
  381. panel(new PanelBuilder("panel_top_right1") {{
  382. childLayoutCenter();
  383. backgroundColor("#00f8");
  384. height("15%");
  385. width("100%");
  386. control(new LabelBuilder(){{
  387. color("#000");
  388. text("123");
  389. width("100%");
  390. height("100%");
  391. }});
  392. }});
  393. ----
  394. Note that the width and height do not scale the bitmap font, but make indirectly certain it is centered. If you want a different size for the font, you need to provide an extra bitmap font (they come with fixes sizes and don't scale well).
  395. ==== Button Control
  396. Our +++<abbr title="Graphical User Interface">GUI</abbr>+++ plan asks for two buttons on the start screen. You add the Start and Quit buttons to the bottom panel of the `start` screen using the `<control>` element:
  397. [source,java]
  398. ----
  399. panel(new PanelBuilder("panel_bottom_left") {{
  400. childLayoutCenter();
  401. valignCenter();
  402. backgroundColor("#44f8");
  403. height("50%");
  404. width("50%");
  405. // add control
  406. control(new ButtonBuilder("StartButton", "Start") {{
  407. alignCenter();
  408. valignCenter();
  409. height("50%");
  410. width("50%");
  411. }});
  412. }});
  413. panel(new PanelBuilder("panel_bottom_right") {{
  414. childLayoutCenter();
  415. valignCenter();
  416. backgroundColor("#88f8");
  417. height("50%");
  418. width("50%");
  419. // add control
  420. control(new ButtonBuilder("QuitButton", "Quit") {{
  421. alignCenter();
  422. valignCenter();
  423. height("50%");
  424. width("50%");
  425. }});
  426. }});
  427. ----
  428. Note that these controls don't do anything yet – we'll get to that soon.
  429. ==== Other Controls
  430. Nifty additionally offers many customizable controls such as check boxes, text fields, menus, chats, tabs, …
  431. See also:
  432. * 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]
  433. * link:https://github.com/nifty-gui/nifty-gui/wiki/Controls[Controls]
  434. == Intermediate Result
  435. When you preview this code in the jMonkeyEngine SDK, our tutorial demo should looks as follows: A start screen with two buttons, and a game screen with a simple HUD frame and a blue cube (which stands for any jME3 game content).
  436. TIP: Remove all lines that set background colors, you only needed them to see the arrangement.
  437. image::jme3/advanced/nifty-gui-simple-demo.png[nifty-gui-simple-demo.png,width="",height="",align="center"]
  438. Your `Screen.java` file should look like this:
  439. [source, java]
  440. ----
  441. package mygame;
  442. import com.jme3.app.Application;
  443. import com.jme3.app.SimpleApplication;
  444. import com.jme3.app.state.BaseAppState;
  445. import com.jme3.niftygui.NiftyJmeDisplay;
  446. import de.lessvoid.nifty.Nifty;
  447. import de.lessvoid.nifty.builder.ImageBuilder;
  448. import de.lessvoid.nifty.builder.LayerBuilder;
  449. import de.lessvoid.nifty.builder.PanelBuilder;
  450. import de.lessvoid.nifty.builder.ScreenBuilder;
  451. import de.lessvoid.nifty.builder.TextBuilder;
  452. import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
  453. import de.lessvoid.nifty.controls.label.builder.LabelBuilder;
  454. import de.lessvoid.nifty.screen.DefaultScreenController;
  455. public class Screen extends BaseAppState {
  456. @Override
  457. protected void initialize(Application app) {
  458. //It is technically safe to do all initialization and cleanup in the
  459. //onEnable()/onDisable() methods. Choosing to use initialize() and
  460. //cleanup() for this is a matter of performance specifics for the
  461. //implementor.
  462. //TODO: initialize your AppState, e.g. attach spatials to rootNode
  463. }
  464. @Override
  465. protected void cleanup(Application app) {
  466. //TODO: clean up what you initialized in the initialize method,
  467. //e.g. remove all spatials from rootNode
  468. }
  469. //onEnable()/onDisable() can be used for managing things that should
  470. //only exist while the state is enabled. Prime examples would be scene
  471. //graph attachment or input listener attachment.
  472. @Override
  473. protected void onEnable() {
  474. NiftyJmeDisplay niftyDisplay = NiftyJmeDisplay.newNiftyJmeDisplay(
  475. getApplication().getAssetManager(),
  476. getApplication().getInputManager(),
  477. getApplication().getAudioRenderer(),
  478. getApplication().getGuiViewPort());
  479. Nifty nifty = niftyDisplay.getNifty();
  480. getApplication().getGuiViewPort().addProcessor(niftyDisplay);
  481. ((SimpleApplication) getApplication()).getFlyByCamera().setDragToRotate(true);
  482. nifty.loadStyleFile("nifty-default-styles.xml");
  483. nifty.loadControlFile("nifty-default-controls.xml");
  484. nifty.addScreen("start", new ScreenBuilder("start") {{
  485. controller(new DefaultScreenController());
  486. // layer added
  487. layer(new LayerBuilder("background") {{
  488. childLayoutCenter();
  489. // backgroundColor("#000f");
  490. // add image
  491. image(new ImageBuilder() {{
  492. filename("Interface/start-background.png");
  493. }});
  494. }});
  495. layer(new LayerBuilder("foreground") {{
  496. childLayoutVertical();
  497. // backgroundColor("#0000");
  498. // panel added
  499. panel(new PanelBuilder("panel_top") {{
  500. childLayoutCenter();
  501. alignCenter();
  502. // backgroundColor("#f008");
  503. height("25%");
  504. width("75%");
  505. text(new TextBuilder() {{
  506. text("My Cool Game");
  507. font("Interface/Fonts/Arial.fnt");
  508. height("100%");
  509. width("100%");
  510. }});
  511. }});
  512. panel(new PanelBuilder("panel_mid") {{
  513. childLayoutCenter();
  514. alignCenter();
  515. // backgroundColor("#0f08");
  516. height("50%");
  517. width("75%");
  518. // add text
  519. text(new TextBuilder() {{
  520. text("Here goes some text describing the game and the rules and stuff. "
  521. + "Incidentally, the text is quite long and needs to wrap at the end of lines. ");
  522. font("Interface/Fonts/Arial.fnt");
  523. wrap(true);
  524. height("100%");
  525. width("100%");
  526. }});
  527. }});
  528. panel(new PanelBuilder("panel_bottom") {{
  529. childLayoutHorizontal();
  530. alignCenter();
  531. // backgroundColor("#00f8");
  532. height("25%");
  533. width("75%");
  534. panel(new PanelBuilder("panel_bottom_left") {{
  535. childLayoutCenter();
  536. valignCenter();
  537. // backgroundColor("#44f8");
  538. height("50%");
  539. width("50%");
  540. // add control
  541. control(new ButtonBuilder("StartButton", "Start") {{
  542. alignCenter();
  543. valignCenter();
  544. height("50%");
  545. width("50%");
  546. }});
  547. }});
  548. panel(new PanelBuilder("panel_bottom_right") {{
  549. childLayoutCenter();
  550. valignCenter();
  551. // backgroundColor("#88f8");
  552. height("50%");
  553. width("50%");
  554. // add control
  555. control(new ButtonBuilder("QuitButton", "Quit") {{
  556. alignCenter();
  557. valignCenter();
  558. height("50%");
  559. width("50%");
  560. }});
  561. }});
  562. }}); // panel added
  563. }});
  564. // layer added
  565. }}.build(nifty));
  566. nifty.addScreen("hud", new ScreenBuilder("hud") {{
  567. controller(new DefaultScreenController());
  568. layer(new LayerBuilder("background") {{
  569. childLayoutCenter();
  570. // backgroundColor("#000f");
  571. // add image
  572. image(new ImageBuilder() {{
  573. filename("Interface/hud-frame.png");
  574. }});
  575. }});
  576. layer(new LayerBuilder("foreground") {{
  577. childLayoutHorizontal();
  578. // backgroundColor("#0000");
  579. // panel added
  580. panel(new PanelBuilder("panel_left") {{
  581. childLayoutVertical();
  582. // backgroundColor("#0f08");
  583. height("100%");
  584. width("80%");
  585. // <!-- spacer -->
  586. }});
  587. panel(new PanelBuilder("panel_right") {{
  588. childLayoutVertical();
  589. // backgroundColor("#00f8");
  590. height("100%");
  591. width("20%");
  592. panel(new PanelBuilder("panel_top_right1") {{
  593. childLayoutCenter();
  594. // backgroundColor("#00f8");
  595. height("15%");
  596. width("100%");
  597. control(new LabelBuilder(){{
  598. color("#000");
  599. text("123");
  600. width("100%");
  601. height("100%");
  602. }});
  603. }});
  604. panel(new PanelBuilder("panel_top_right2") {{
  605. childLayoutCenter();
  606. // backgroundColor("#44f8");
  607. height("15%");
  608. width("100%");
  609. // add image
  610. image(new ImageBuilder() {{
  611. filename("Interface/face1.png");
  612. valignCenter();
  613. alignCenter();
  614. height("50%");
  615. width("30%");
  616. }});
  617. }});
  618. panel(new PanelBuilder("panel_bot_right") {{
  619. childLayoutCenter();
  620. valignCenter();
  621. // backgroundColor("#88f8");
  622. height("70%");
  623. width("100%");
  624. }});
  625. }}); // panel added
  626. }});
  627. }}.build(nifty));
  628. nifty.gotoScreen("hud"); // start the screen
  629. }
  630. @Override
  631. protected void onDisable() {
  632. //Called when the state was previously enabled but is now disabled
  633. //either because setEnabled(false) was called or the state is being
  634. //cleaned up.
  635. }
  636. @Override
  637. public void update(float tpf) {
  638. //TODO: implement behavior during runtime
  639. }
  640. }
  641. ----
  642. == Nifty Java Settings
  643. Before initializing the nifty screens, you set up properties and register media.
  644. [cols="2", options="header"]
  645. |===
  646. a| Nifty Method
  647. a| Description
  648. a| registerSound("mysound", "Interface/abc.wav");
  649. a|
  650. a| registerMusic("mymusic", "Interface/xyz.ogg");
  651. a|
  652. a| registerMouseCursor("mypointer", "Interface/abc.png", 5, 4);
  653. a|
  654. a| registerEffect(?);
  655. a| ?
  656. a| setDebugOptionPanelColors(true);
  657. a| Highlight all panels, makes it easier to arrange them.
  658. |===
  659. Example:
  660. [source,java]
  661. ----
  662. nifty.registerMouseCursor("hand", "Interface/mouse-cursor-hand.png", 5, 4);
  663. ----
  664. == Next Steps
  665. Integrate the +++<abbr title="Graphical User Interface">GUI</abbr>+++ into the game. Typically, you will overlay the +++<abbr title="Graphical User Interface">GUI</abbr>+++.
  666. * <<jme3/advanced/nifty_gui_overlay#,Nifty GUI Overlay>> (recommended)
  667. * <<jme3/advanced/nifty_gui_projection#,Nifty GUI Projection>> (optional)