ImageControl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef IMAGECONTROL_H_
  2. #define IMAGECONTROL_H_
  3. #include "Control.h"
  4. #include "Theme.h"
  5. #include "Image.h"
  6. #include "SpriteBatch.h"
  7. #include "Rectangle.h"
  8. namespace gameplay
  9. {
  10. /**
  11. * An ImageControl allows forms to display images from arbitrary files not specified in the theme.
  12. *
  13. * The following properties are available for image controls:
  14. @verbatim
  15. image <control ID>
  16. {
  17. style = <styleID>
  18. alignment = <Control::Alignment constant> // Note: 'position' will be ignored.
  19. position = <x, y>
  20. autoWidth = <bool>
  21. autoHeight = <bool>
  22. size = <width, height>
  23. width = <width> // Can be used in place of 'size', e.g. with 'autoHeight = true'
  24. height = <height> // Can be used in place of 'size', e.g. with 'autoWidth = true'
  25. consumeEvents = <bool> // Whether the label propagates input events to the Game's input event handler. Default is true.
  26. path = <string> // Path to image or texture atlas.
  27. srcRegion = <x, y, width, height> // Region within file to create UVs from.
  28. dstRegion = <x, y, width, height> // Region of control's viewport to draw into.
  29. }
  30. @endverbatim
  31. */
  32. class ImageControl : public Control
  33. {
  34. friend class Container;
  35. friend class ControlFactory;
  36. public:
  37. /**
  38. * Create a new ImageControl.
  39. *
  40. * @param id The control's ID.
  41. * @param style The control's style.
  42. *
  43. * @return The new ImageControl.
  44. * @script{create}
  45. */
  46. static ImageControl* create(const char* id, Theme::Style* style);
  47. /**
  48. * Set the path of the image for this ImageControl to display.
  49. *
  50. * @param path The path to the image.
  51. */
  52. void setImage(const char* path);
  53. /**
  54. * Set the source region of this ImageControl. This is the region of the file,
  55. * in pixels, to use when drawing.
  56. *
  57. * @param x The x coordinate of the source region.
  58. * @param y The y coordinate of the source region.
  59. * @param width The width of the source region.
  60. * @param height The height of the source region.
  61. */
  62. void setRegionSrc(float x, float y, float width, float height);
  63. /**
  64. * Set the source region of this ImageControl. This is the region of the file,
  65. * in pixels, to use when drawing.
  66. *
  67. * @param region The new source region.
  68. */
  69. void setRegionSrc(const Rectangle& region);
  70. /**
  71. * Get the source region of this ImageControl.
  72. *
  73. * @return The source region of this ImageControl.
  74. */
  75. const Rectangle& getRegionSrc() const;
  76. /**
  77. * Sets the destination region of this ImageControl. This is the region
  78. * within the control's viewport to draw the image.
  79. *
  80. * @param x The x coordinate of the destination region.
  81. * @param y The y coordinate of the destination region.
  82. * @param width The width of the destination region.
  83. * @param height The height of the destination region.
  84. */
  85. void setRegionDst(float x, float y, float width, float height);
  86. /**
  87. * Sets the destination region of this ImageControl. This is the region
  88. * within the control's viewport to draw the image.
  89. *
  90. * @param region The new destination region.
  91. */
  92. void setRegionDst(const Rectangle& region);
  93. /**
  94. * Get the destination region of this ImageControl.
  95. *
  96. * @return The destination region of this ImageControl.
  97. */
  98. const Rectangle& getRegionDst() const;
  99. const char* getType() const;
  100. protected:
  101. ImageControl();
  102. virtual ~ImageControl();
  103. static Control* create(Theme::Style* style, Properties* properties);
  104. virtual void initialize(Theme::Style* style, Properties* properties);
  105. void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
  106. /**
  107. * @see Control#update(const Control*, const Vector2&)
  108. */
  109. void update(const Control* container, const Vector2& offset);
  110. // Source region.
  111. Rectangle _srcRegion;
  112. // Destination region.
  113. Rectangle _dstRegion;
  114. SpriteBatch* _batch;
  115. // One over texture width and height, for use when calculating UVs from a new source region.
  116. float _tw;
  117. float _th;
  118. // Calculated UVs.
  119. Theme::UVs _uvs;
  120. private:
  121. ImageControl(const ImageControl& copy);
  122. };
  123. }
  124. #endif