quickstart.adoc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. = quickstart
  2. :revnumber: 2.0
  3. :revdate: 2020/07/27
  4. == Quick Start Guide:
  5. For anyone who just wants to jump right in and read as you go, here are the basics for getting a UI up and running in minutes.
  6. === Step 1: Creating the Screen Control
  7. This can be done one of two ways.
  8. *Method 1* - Using the provided default style information:
  9. [source,java]
  10. ----
  11. // this = any JME Application
  12. Screen screen = new Screen(this);
  13. guiNode.addControl(screen);
  14. ----
  15. *Method 2* - Providing a path to another Style map:
  16. [source,java]
  17. ----
  18. // this = any JME Application
  19. Screen screen = new Screen(this, "tonegod/gui/style/def/style_map.xml");
  20. guiNode.addControl(screen);
  21. ----
  22. [NOTE]
  23. ====
  24. style_map.xml consists of a list of xml documents containing varied styles for varied use. You can copy the default style map and replace one, many, all with project specific style (Covered later in this tutorial).
  25. ====
  26. === Step 2: Adding a Control to the Screen
  27. Might as well start with something interesting as all control constructors follow the same format. Let go with a window and then we’ll add a button to it.
  28. *Constructor 1:* +
  29. Here are the three constructor choices for creating the window:
  30. [source,java]
  31. ----
  32. /** Parameters:
  33. * Screen screen,
  34. * String UID,
  35. * Vector2f position
  36. */
  37. Window win = new Window(screen, "win", new Vector2f(15, 15));
  38. screen.addElement(win);
  39. ----
  40. And… boom! You have a fancy new resizable, movable window as part of you’re UI. Now, let’s take a look at the two additional constructors.
  41. *Constructor 2:* +
  42. The second adds a 4th parameter to specify the windows dimensions, like such:
  43. [source,java]
  44. ----
  45. /** Additional Parameter:
  46. * Vector2f dimensions */
  47. Window win = new Window(screen, "win", new Vector2f(15, 15),
  48. new Vector2f(400, 300)
  49. );
  50. screen.addElement(win);
  51. ----
  52. *Constructor 3:* +
  53. The third option adds 2 more parameters and looks like this:
  54. [source,java]
  55. ----
  56. /** Additional Parameters:
  57. * Vector4f resizeBorders,
  58. * String defaultImg
  59. */
  60. Window win = new Window(screen, "win", new Vector2f(15, 15), new Vector2f(400, 300),
  61. new Vector4f(14,14,14,14),
  62. "tonegod/gui/style/def/Window/panel_x.png"
  63. );
  64. screen.addElement(win);
  65. ----
  66. [NOTE]
  67. ====
  68. 3 new constructor options have been added recently. They are the same as the 3 above minus the UID parameter.
  69. ====
  70. Any parameters not specified are derived from the defaults specified in the Window style information.
  71. [NOTE]
  72. ====
  73. The occasional control extends this constructor format, adding an additional Orientation parameter or possibly a boolean flag for controls that provide multiple configurable layouts.
  74. ====
  75. === Step 3: Adding a Button to the Window
  76. Now we can add a button to the window that will create even MORE windows!
  77. The Button class is one of the only +++<abbr title="Graphical User Interface">GUI</abbr>+++ control classes that implements JME’s Control interface. The Control only becomes active if setInterval is called because the Button requires the use of stillPressed events. This and much more will be cover in later documentation. Again, you ave the three options above for creating an instance of the button control.
  78. [NOTE]
  79. ====
  80. The button control (like many controls) is abstract and provides methods for handling user input.
  81. ====
  82. First, lets setup a method to create new windows:
  83. [source,java]
  84. ----
  85. private int winCount = 0;
  86. public final void createNewWindow(String someWindowTitle) {
  87. Window nWin = new Window(
  88. screen,
  89. "Window" + winCount,
  90. new Vector2f( (screen.getWidth()/2)-175, (screen.getHeight()/2)-100 )
  91. );
  92. nWin.setWindowTitle(someWindowTitle);
  93. screen.addElement(nWin);
  94. winCount++;
  95. }
  96. ----
  97. Now we can add the button will call our create window method.
  98. [source,java]
  99. ----
  100. /** Parameters:
  101. * Screen screen,
  102. * String UID,
  103. * Vector2f position
  104. */
  105. ButtonAdapter makeWindow = new ButtonAdapter( screen, "Btn1", new Vector2f(15, 55) ) {
  106. @Override
  107. public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
  108. createNewWindow("New Window " + winCount);
  109. }
  110. };
  111. // Add it to out initial window
  112. win.addChild(makeWindow);
  113. ----
  114. [TIP]
  115. ====
  116. For layout purposes, it is suggested that you add all child Elements to a control PRIOR to adding the control to the screen… so, create a window, add a button, add window to screen.
  117. ====
  118. *A Bit More Info:* +
  119. All controls are based on the Element class which has access to all default behaviors. Behaviors can be enabled disabled on ANY control or primitive Element.
  120. == A Few of the Common Behaviors:
  121. [source,java]
  122. ----
  123. // Makes control resizable from defined borders
  124. element.setIsResizable(boolean);
  125. // Makes the control movable
  126. element.setIsMovable(boolean);
  127. // Constrained to parent dimensions
  128. element.setLockToParentBounds(boolean);
  129. // On interaction effects direct parent instead of self
  130. element.setEffectParent(boolean);
  131. // On interaction effects absolute parent (screen lvl) instead of self
  132. element.setEffectAbsoluteParent(boolean);
  133. // allows the control to scale north/south from any encapsulating parent resize
  134. element.setScaleNS(boolean);
  135. // allows the control to scale east/west from any encapsulating parent resize
  136. element.setScaleEW(boolean);
  137. element.setDockN(boolean); // also enables/disables dock south
  138. element.setDockS(boolean); // also enables/disables dock north
  139. element.setDockE(boolean); // also enables/disables dock west
  140. element.setDockW(boolean); // also enables/disables dock east
  141. // Forcing the element to ignore the mouse
  142. element.setIgnoreMouse(boolean);
  143. ----
  144. [NOTE]
  145. ====
  146. There are more behaviors, however, these are the most critical when creating custom controls to ensure that nested Elements react as you would like when a parent Element is altered.
  147. ====
  148. == Quick Start Example In Full
  149. [source,java]
  150. ----
  151. public int winCount = 0;
  152. private Screen screen;
  153. public final void createNewWindow(String someWindowTitle) {
  154. Window nWin = new Window(
  155. screen,
  156. "Window" + winCount,
  157. new Vector2f( (screen.getWidth()/2)-175, (screen.getHeight()/2)-100 )
  158. );
  159. nWin.setWindowTitle(someWindowTitle);
  160. screen.addElement(nWin);
  161. winCount++;
  162. }
  163. public void simpleInitApp() {
  164. screen = new Screen(this, "tonegod/gui/style/def/style_map.xml");
  165. screen.initialize();
  166. guiNode.addControl(screen);
  167. // Add window
  168. Window win = new Window(screen, "win", new Vector2f(15, 15));
  169. // create button and add to window
  170. ButtonAdapter makeWindow = new ButtonAdapter( screen, "Btn1", new Vector2f(15, 55) ) {
  171. @Override
  172. public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
  173. createNewWindow("New Window " + winCount);
  174. }
  175. };
  176. // Add it to our initial window
  177. win.addChild(makeWindow);
  178. // Add window to the screen
  179. screen.addElement(win);
  180. }
  181. ----