ImageControl.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "Base.h"
  2. #include "ImageControl.h"
  3. namespace gameplay
  4. {
  5. ImageControl::ImageControl() :
  6. _srcRegion(Rectangle::empty()), _dstRegion(Rectangle::empty()), _batch(NULL),
  7. _tw(0.0f), _th(0.0f), _uvs(Theme::UVs::full())
  8. {
  9. }
  10. ImageControl::~ImageControl()
  11. {
  12. SAFE_DELETE(_batch);
  13. }
  14. ImageControl* ImageControl::create(const char* id, Theme::Style* style)
  15. {
  16. GP_ASSERT(style);
  17. ImageControl* imageControl = new ImageControl();
  18. if (id)
  19. imageControl->_id = id;
  20. imageControl->setStyle(style);
  21. imageControl->_consumeInputEvents = false;
  22. imageControl->_focusIndex = -2;
  23. return imageControl;
  24. }
  25. ImageControl* ImageControl::create(Theme::Style* style, Properties* properties)
  26. {
  27. ImageControl* imageControl = new ImageControl();
  28. imageControl->initialize(style, properties);
  29. imageControl->_consumeInputEvents = false;
  30. imageControl->_focusIndex = -2;
  31. return imageControl;
  32. }
  33. void ImageControl::initialize(Theme::Style* style, Properties* properties)
  34. {
  35. GP_ASSERT(properties);
  36. Control::initialize(style, properties);
  37. const char* path = properties->getString("path");
  38. if (path)
  39. {
  40. setImage(path);
  41. }
  42. if (properties->exists("srcRegion"))
  43. {
  44. Vector4 region;
  45. properties->getVector4("srcRegion", &region);
  46. setRegionSrc(region.x, region.y, region.z, region.w);
  47. }
  48. if (properties->exists("dstRegion"))
  49. {
  50. Vector4 region;
  51. properties->getVector4("dstRegion", &region);
  52. setRegionDst(region.x, region.y, region.z, region.w);
  53. }
  54. }
  55. void ImageControl::setImage(const char* path)
  56. {
  57. SAFE_DELETE(_batch);
  58. Texture* texture = Texture::create(path);
  59. _batch = SpriteBatch::create(texture);
  60. _tw = 1.0f / texture->getWidth();
  61. _th = 1.0f / texture->getHeight();
  62. texture->release();
  63. }
  64. void ImageControl::setRegionSrc(float x, float y, float width, float height)
  65. {
  66. _srcRegion.set(x, y, width, height);
  67. _uvs.u1 = x * _tw;
  68. _uvs.u2 = (x + width) * _tw;
  69. _uvs.v1 = 1.0f - (y * _th);
  70. _uvs.v2 = 1.0f - ((y + height) * _tw);
  71. }
  72. void ImageControl::setRegionSrc(const Rectangle& region)
  73. {
  74. setRegionSrc(region.x, region.y, region.width, region.height);
  75. }
  76. const Rectangle& ImageControl::getRegionSrc() const
  77. {
  78. return _srcRegion;
  79. }
  80. void ImageControl::setRegionDst(float x, float y, float width, float height)
  81. {
  82. _dstRegion.set(x, y, width, height);
  83. }
  84. void ImageControl::setRegionDst(const Rectangle& region)
  85. {
  86. setRegionDst(region.x, region.y, region.width, region.height);
  87. }
  88. const Rectangle& ImageControl::getRegionDst() const
  89. {
  90. return _dstRegion;
  91. }
  92. void ImageControl::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  93. {
  94. spriteBatch->finish();
  95. // An ImageControl is not part of the texture atlas but should use the same projection matrix.
  96. _batch->setProjectionMatrix(spriteBatch->getProjectionMatrix());
  97. _batch->start();
  98. if (_dstRegion.isEmpty())
  99. {
  100. _batch->draw(_viewportBounds.x, _viewportBounds.y, _viewportBounds.width, _viewportBounds.height,
  101. _uvs.u1, _uvs.v1, _uvs.u2, _uvs.v2, Vector4::one(), _viewportClipBounds);
  102. }
  103. else
  104. {
  105. _batch->draw(_viewportBounds.x + _dstRegion.x, _viewportBounds.y + _dstRegion.y,
  106. _dstRegion.width, _dstRegion.height,
  107. _uvs.u1, _uvs.v1, _uvs.u2, _uvs.v2, Vector4::one(), _viewportClipBounds);
  108. }
  109. _batch->finish();
  110. spriteBatch->start();
  111. }
  112. }