Form.h 6.9 KB

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