ImageControl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * Defines an image control.
  12. *
  13. * This allows forms to display seperate images from arbitrary files not specified in the theme.
  14. *
  15. * @see http://gameplay3d.github.io/GamePlay/docs/file-formats.html#wiki-UI_Forms
  16. */
  17. class ImageControl : public Control
  18. {
  19. friend class Container;
  20. friend class ControlFactory;
  21. public:
  22. /**
  23. * Creates a new ImageControl.
  24. *
  25. * @param id The image control ID.
  26. * @param style The image control style (optional).
  27. *
  28. * @return The new image control.
  29. * @script{create}
  30. */
  31. static ImageControl* create(const char* id, Theme::Style* style = NULL);
  32. /**
  33. * Extends ScriptTarget::getTypeName() to return the type name of this class.
  34. *
  35. * Child controls should override this function to return the correct type name.
  36. *
  37. * @return The type name of this class: "ImageControl"
  38. * @see ScriptTarget::getTypeName()
  39. */
  40. const char* getTypeName() const;
  41. /**
  42. * Set the path of the image for this ImageControl to display.
  43. *
  44. * @param path The path to the image.
  45. */
  46. void setImage(const char* path);
  47. /**
  48. * Set the source region of this ImageControl. This is the region of the file,
  49. * in pixels, to use when drawing.
  50. *
  51. * @param x The x coordinate of the source region.
  52. * @param y The y coordinate of the source region.
  53. * @param width The width of the source region.
  54. * @param height The height of the source region.
  55. */
  56. void setRegionSrc(float x, float y, float width, float height);
  57. /**
  58. * Set the source region of this ImageControl. This is the region of the file,
  59. * in pixels, to use when drawing.
  60. *
  61. * @param region The new source region.
  62. */
  63. void setRegionSrc(const Rectangle& region);
  64. /**
  65. * Get the source region of this ImageControl.
  66. *
  67. * @return The source region of this ImageControl.
  68. */
  69. const Rectangle& getRegionSrc() const;
  70. /**
  71. * Sets the destination region of this ImageControl. This is the region
  72. * within the control's viewport to draw the image.
  73. *
  74. * @param x The x coordinate of the destination region.
  75. * @param y The y coordinate of the destination region.
  76. * @param width The width of the destination region.
  77. * @param height The height of the destination region.
  78. */
  79. void setRegionDst(float x, float y, float width, float height);
  80. /**
  81. * Sets the destination region of this ImageControl. This is the region
  82. * within the control's viewport to draw the image.
  83. *
  84. * @param region The new destination region.
  85. */
  86. void setRegionDst(const Rectangle& region);
  87. /**
  88. * Get the destination region of this ImageControl.
  89. *
  90. * @return The destination region of this ImageControl.
  91. */
  92. const Rectangle& getRegionDst() const;
  93. protected:
  94. ImageControl();
  95. virtual ~ImageControl();
  96. /**
  97. * Creates a new ImageControl.
  98. *
  99. * @param style The control's custom style.
  100. * @param properties A properties object containing a definition of the ImageControl (optional).
  101. *
  102. * @return The new ImageControl.
  103. * @script{create}
  104. *
  105. */
  106. static Control* create(Theme::Style* style, Properties* properties = NULL);
  107. void initialize(const char* typeName, Theme::Style* style, Properties* properties);
  108. /**
  109. * @see Control::drawImages
  110. */
  111. unsigned int drawImages(Form* form, const Rectangle& clip);
  112. /**
  113. * @see Control::updateBounds
  114. */
  115. void updateBounds();
  116. private:
  117. ImageControl(const ImageControl& copy);
  118. // Source region.
  119. Rectangle _srcRegion;
  120. // Destination region.
  121. Rectangle _dstRegion;
  122. SpriteBatch* _batch;
  123. // One over texture width and height, for use when calculating UVs from a new source region.
  124. float _tw;
  125. float _th;
  126. // Calculated UVs.
  127. Theme::UVs _uvs;
  128. };
  129. }
  130. #endif