Form.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. namespace gameplay
  11. {
  12. class Theme;
  13. /**
  14. * Top-level container of UI controls.
  15. */
  16. class Form : public Container
  17. {
  18. friend class Platform;
  19. public:
  20. /**
  21. * Create from properties file.
  22. * The top-most namespace in the file must be named 'form'. The following properties are available for forms:
  23. *
  24. * form <Form ID>
  25. * {
  26. * // Form properties.
  27. * theme = <Path to Theme File> // See Theme.h.
  28. * layout = <Layout Type> // A value from the Layout::Type enum. E.g.: LAYOUT_VERTICAL
  29. * style = <Style ID> // A style from the referenced theme.
  30. * position = <x, y> // Position of the form on-screen, measured in pixels.
  31. * size = <width, height> // Size of the form, measured in pixels.
  32. *
  33. * // All the controls within this form.
  34. * container{}
  35. * label{}
  36. * textBox{}
  37. * button{}
  38. * checkBox{}
  39. * radioButton{}
  40. * slider{}
  41. * }
  42. *
  43. * @param path Path to the properties file to create a new form from.
  44. */
  45. static Form* create(const char* path);
  46. /**
  47. * Get a form from its ID.
  48. *
  49. * @param id The ID of the form to search for.
  50. *
  51. * @return A form with the given ID, or null if one was not found.
  52. */
  53. static Form* getForm(const char* id);
  54. /**
  55. * Create a 3D quad to texture with this Form.
  56. *
  57. * The specified points should describe a triangle strip with the first 3 points
  58. * forming a triangle wound in counter-clockwise direction, with the second triangle
  59. * formed from the last three points in clockwise direction.
  60. *
  61. * @param p1 The first point.
  62. * @param p2 The second point.
  63. * @param p3 The third point.
  64. * @param p4 The fourth point.
  65. */
  66. void setQuad(const Vector3& p1, const Vector3& p2, const Vector3& p3, const Vector3& p4);
  67. /**
  68. * Create a 2D quad to texture with this Form.
  69. *
  70. * @param x The x coordinate.
  71. * @param y The y coordinate.
  72. * @param width The width of the quad.
  73. * @param height The height of the quad.
  74. */
  75. void setQuad(float x, float y, float width, float height);
  76. /**
  77. * Attach this form to a node.
  78. *
  79. * A form can be drawn as part of the 3-dimensional world if it is attached to a node.
  80. * The form's contents will be rendered into a framebuffer which will be used to texture a quad.
  81. * This quad will be given the same dimensions as the form and must be transformed appropriately.
  82. * Alternatively, a quad can be set explicitly on a form with the setQuad() methods.
  83. *
  84. * @param node The node to attach this form to.
  85. */
  86. void setNode(Node* node);
  87. /**
  88. * Updates each control within this form, and positions them according to its layout.
  89. */
  90. void update();
  91. /**
  92. * Draws this form.
  93. */
  94. void draw();
  95. protected:
  96. Form();
  97. virtual ~Form();
  98. static Form* create(const char* textureFile, Layout::Type type);
  99. /**
  100. * Initialize a quad for this form in order to draw it in 3D.
  101. *
  102. * @param mesh The mesh to create a model from.
  103. */
  104. void initializeQuad(Mesh* mesh);
  105. /**
  106. * Draw this form into the current framebuffer.
  107. *
  108. * @param spriteBatch The sprite batch containing this form's theme texture.
  109. * @param clip The form's clipping rectangle.
  110. */
  111. void draw(SpriteBatch* spriteBatch, const Rectangle& clip);
  112. /**
  113. * Propagate touch events to enabled forms.
  114. *
  115. * @return Whether the touch event was consumed by a form.
  116. */
  117. static bool touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  118. /**
  119. * Propagate key events to enabled forms.
  120. */
  121. static void keyEventInternal(Keyboard::KeyEvent evt, int key);
  122. Theme* _theme; // The Theme applied to this Form.
  123. Model* _quad; // Quad for rendering this Form in world-space.
  124. Node* _node; // Node for transforming this Form in world-space.
  125. FrameBuffer* _frameBuffer; // FBO the Form is rendered into for texturing the quad.
  126. Matrix _projectionMatrix; // Orthographic projection matrix to be set on SpriteBatch objects when rendering into the FBO.
  127. private:
  128. Form(const Form& copy);
  129. };
  130. }
  131. #endif