ImageControl.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. std::string path;
  38. if (properties->getPath("path", &path))
  39. {
  40. setImage(path.c_str());
  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. _dirty = true;
  64. }
  65. void ImageControl::setRegionSrc(float x, float y, float width, float height)
  66. {
  67. _srcRegion.set(x, y, width, height);
  68. _uvs.u1 = x * _tw;
  69. _uvs.u2 = (x + width) * _tw;
  70. _uvs.v1 = 1.0f - (y * _th);
  71. _uvs.v2 = 1.0f - ((y + height) * _th);
  72. _dirty = true;
  73. }
  74. void ImageControl::setRegionSrc(const Rectangle& region)
  75. {
  76. setRegionSrc(region.x, region.y, region.width, region.height);
  77. _dirty = true;
  78. }
  79. const Rectangle& ImageControl::getRegionSrc() const
  80. {
  81. return _srcRegion;
  82. }
  83. void ImageControl::setRegionDst(float x, float y, float width, float height)
  84. {
  85. _dstRegion.set(x, y, width, height);
  86. _dirty = true;
  87. }
  88. void ImageControl::setRegionDst(const Rectangle& region)
  89. {
  90. setRegionDst(region.x, region.y, region.width, region.height);
  91. _dirty = true;
  92. }
  93. const Rectangle& ImageControl::getRegionDst() const
  94. {
  95. return _dstRegion;
  96. }
  97. const char* ImageControl::getType() const
  98. {
  99. return "image";
  100. }
  101. void ImageControl::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  102. {
  103. spriteBatch->finish();
  104. // An ImageControl is not part of the texture atlas but should use the same projection matrix.
  105. _batch->setProjectionMatrix(spriteBatch->getProjectionMatrix());
  106. Vector4 color = Vector4::one();
  107. color.w *= _opacity;
  108. _batch->start();
  109. if (_dstRegion.isEmpty())
  110. {
  111. _batch->draw(_viewportBounds.x, _viewportBounds.y, _viewportBounds.width, _viewportBounds.height,
  112. _uvs.u1, _uvs.v1, _uvs.u2, _uvs.v2, color, _viewportClipBounds);
  113. }
  114. else
  115. {
  116. _batch->draw(_viewportBounds.x + _dstRegion.x, _viewportBounds.y + _dstRegion.y,
  117. _dstRegion.width, _dstRegion.height,
  118. _uvs.u1, _uvs.v1, _uvs.u2, _uvs.v2, color, _viewportClipBounds);
  119. }
  120. _batch->finish();
  121. spriteBatch->start();
  122. }
  123. }