Form.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #ifndef FORM_H_
  2. #define FORM_H_
  3. #include "Ref.h"
  4. #include "Container.h"
  5. #include "Mesh.h"
  6. #include "Node.h"
  7. #include "FrameBuffer.h"
  8. #include "Touch.h"
  9. #include "Keyboard.h"
  10. #include "Mouse.h"
  11. namespace gameplay
  12. {
  13. class Theme;
  14. /**
  15. * Top-level container of UI controls. The following properties are available for forms:
  16. @verbatim
  17. form <formID>
  18. {
  19. // Form properties.
  20. theme = <Path to .theme File> // See Theme.h.
  21. layout = <Layout::Type> // A value from the Layout::Type enum. e.g.: LAYOUT_VERTICAL
  22. style = <styleID> // A style from the referenced theme.
  23. position = <x, y> // Position of the form on-screen, measured in pixels.
  24. alignment = <Control::Alignment> // Note: 'position' will be ignored.
  25. autoWidth = <bool> // Will result in a form the width of the display.
  26. autoHeight = <bool> // Will result in a form the height of the display.
  27. size = <width, height> // Size of the form, measured in pixels.
  28. width = <width> // Can be used in place of 'size', e.g. with 'autoHeight = true'
  29. height = <height> // Can be used in place of 'size', e.g. with 'autoWidth = true'
  30. consumeEvents = <bool> // Whether the form propogates input events to the Game's input event handler. Default is false
  31. // All the nested controls within this form.
  32. container { }
  33. label { }
  34. textBox { }
  35. button { }
  36. checkBox { }
  37. radioButton { }
  38. slider { }
  39. }
  40. @endverbatim
  41. */
  42. class Form : public Container
  43. {
  44. friend class Platform;
  45. public:
  46. /**
  47. * Creates a form using the data from the Properties object defined at the specified URL,
  48. * where the URL is of the format "<file-path>.<extension>#<namespace-id>/<namespace-id>/.../<namespace-id>"
  49. * (and "#<namespace-id>/<namespace-id>/.../<namespace-id>" is optional).
  50. *
  51. * @param url The URL pointing to the Properties object defining the form.
  52. * @script{create}
  53. */
  54. static Form* create(const char* url);
  55. /**
  56. * Create a new Form.
  57. *
  58. * @param id The Form's ID.
  59. * @param style The Form's style.
  60. * @param layoutType The form's layout type.
  61. *
  62. * @return The new Form.
  63. * @script{create}
  64. */
  65. static Form* create(const char* id, Theme::Style* style, Layout::Type layoutType = Layout::LAYOUT_ABSOLUTE);
  66. /**
  67. * Get a form from its ID.
  68. *
  69. * @param id The ID of the form to search for.
  70. *
  71. * @return A form with the given ID, or null if one was not found.
  72. */
  73. static Form* getForm(const char* id);
  74. /**
  75. * Gets the theme for the form.
  76. *
  77. * @return The theme for the form.
  78. */
  79. Theme* getTheme() const;
  80. /**
  81. * Set the desired size of this form.
  82. *
  83. * @param width The width.
  84. * @param height The height.
  85. */
  86. virtual void setSize(float width, float height);
  87. /**
  88. * Set the bounds of this form.
  89. *
  90. * @param bounds The new bounds to set.
  91. */
  92. virtual void setBounds(const Rectangle& bounds);
  93. /**
  94. * Set the desired width of the form.
  95. *
  96. * @param width The width.
  97. */
  98. virtual void setWidth(float width);
  99. /**
  100. * Set the desired height of the form.
  101. *
  102. * @param height The height.
  103. */
  104. virtual void setHeight(float height);
  105. /**
  106. * Set this form's width to that of the display.
  107. *
  108. * @param autoWidth Whether to set this form's width to that of the display.
  109. */
  110. virtual void setAutoWidth(bool autoWidth);
  111. /**
  112. * Set this form's height to that of the display.
  113. *
  114. * @param autoHeight Whether to set this form's height to that of the display.
  115. */
  116. virtual void setAutoHeight(bool autoHeight);
  117. /**
  118. * Attach this form to a node.
  119. *
  120. * A form can be drawn as part of the 3-dimensional world if it is attached to a node.
  121. * The form's contents will be rendered into a framebuffer which will be used to texture a quad.
  122. * This quad will be given the same dimensions as the form and must be transformed appropriately.
  123. * Alternatively, a quad can be set explicitly on a form with the setQuad() methods.
  124. *
  125. * @param node The node to attach this form to.
  126. */
  127. void setNode(Node* node);
  128. /**
  129. * Updates each control within this form, and positions them according to its layout.
  130. */
  131. void update(float elapsedTime);
  132. /**
  133. * Draws this form.
  134. */
  135. void draw();
  136. /**
  137. * @see Control::getType
  138. */
  139. const char* getType() const;
  140. private:
  141. /**
  142. * Constructor.
  143. */
  144. Form();
  145. /**
  146. * Constructor.
  147. */
  148. Form(const Form& copy);
  149. /**
  150. * Destructor.
  151. */
  152. virtual ~Form();
  153. /**
  154. * Initialize a quad for this form in order to draw it in 3D.
  155. *
  156. * @param mesh The mesh to create a model from.
  157. */
  158. void initializeQuad(Mesh* mesh);
  159. /**
  160. * Propagate touch events to enabled forms.
  161. *
  162. * @return Whether the touch event was consumed by a form.
  163. */
  164. static bool touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  165. /**
  166. * Propagate key events to enabled forms.
  167. *
  168. * @return Whether the key event was consumed by a form.
  169. */
  170. static bool keyEventInternal(Keyboard::KeyEvent evt, int key);
  171. /**
  172. * Propagate mouse events to enabled forms.
  173. *
  174. * @return True if the mouse event is consumed or false if it is not consumed.
  175. *
  176. * @see Mouse::MouseEvent
  177. */
  178. static bool mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
  179. /**
  180. * Get the next highest power of two of an integer. Used when creating framebuffers.
  181. *
  182. * @param x The number to start with.
  183. *
  184. * @return The next highest power of two after x, or x if it is already a power of two.
  185. */
  186. static unsigned int nextPowerOfTwo(unsigned int x);
  187. /**
  188. * Unproject a point (from a mouse or touch event) into the scene and then project it onto the form.
  189. *
  190. * @param x The x coordinate of the mouse/touch point.
  191. * @param y The y coordinate of the mouse/touch point.
  192. * @param point A destination vector to populate with the projected point, in the form's plane.
  193. *
  194. * @return True if the projected point lies within the form's plane, false otherwise.
  195. */
  196. bool projectPoint(int x, int y, Vector3* point);
  197. Theme* _theme; // The Theme applied to this Form.
  198. FrameBuffer* _frameBuffer; // FBO the Form is rendered into for texturing the quad.
  199. SpriteBatch* _spriteBatch;
  200. Node* _node; // Node for transforming this Form in world-space.
  201. Model* _nodeQuad; // Quad for rendering this Form in 3d space.
  202. Material* _nodeMaterial; // Material for rendering this Form in 3d space.
  203. float _u2;
  204. float _v1;
  205. Matrix _projectionMatrix; // Orthographic projection matrix to be set on SpriteBatch objects when rendering into the FBO.
  206. Matrix _defaultProjectionMatrix;
  207. };
  208. }
  209. #endif