ImageControl.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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->_focusIndex = -2;
  22. return imageControl;
  23. }
  24. Control* ImageControl::create(Theme::Style* style, Properties* properties)
  25. {
  26. ImageControl* imageControl = new ImageControl();
  27. imageControl->initialize(style, properties);
  28. imageControl->_focusIndex = -2;
  29. return imageControl;
  30. }
  31. void ImageControl::initialize(Theme::Style* style, Properties* properties)
  32. {
  33. GP_ASSERT(properties);
  34. Control::initialize(style, properties);
  35. std::string path;
  36. if (properties->getPath("path", &path))
  37. {
  38. setImage(path.c_str());
  39. }
  40. if (properties->exists("srcRegion"))
  41. {
  42. Vector4 region;
  43. properties->getVector4("srcRegion", &region);
  44. setRegionSrc(region.x, region.y, region.z, region.w);
  45. }
  46. if (properties->exists("dstRegion"))
  47. {
  48. Vector4 region;
  49. properties->getVector4("dstRegion", &region);
  50. setRegionDst(region.x, region.y, region.z, region.w);
  51. }
  52. }
  53. void ImageControl::setImage(const char* path)
  54. {
  55. SAFE_DELETE(_batch);
  56. Texture* texture = Texture::create(path);
  57. _batch = SpriteBatch::create(texture);
  58. _tw = 1.0f / texture->getWidth();
  59. _th = 1.0f / texture->getHeight();
  60. texture->release();
  61. _dirty = true;
  62. }
  63. void ImageControl::setRegionSrc(float x, float y, float width, float height)
  64. {
  65. _srcRegion.set(x, y, width, height);
  66. _uvs.u1 = x * _tw;
  67. _uvs.u2 = (x + width) * _tw;
  68. _uvs.v1 = 1.0f - (y * _th);
  69. _uvs.v2 = 1.0f - ((y + height) * _th);
  70. _dirty = true;
  71. }
  72. void ImageControl::setRegionSrc(const Rectangle& region)
  73. {
  74. setRegionSrc(region.x, region.y, region.width, region.height);
  75. _dirty = true;
  76. }
  77. const Rectangle& ImageControl::getRegionSrc() const
  78. {
  79. return _srcRegion;
  80. }
  81. void ImageControl::setRegionDst(float x, float y, float width, float height)
  82. {
  83. _dstRegion.set(x, y, width, height);
  84. _dirty = true;
  85. }
  86. void ImageControl::setRegionDst(const Rectangle& region)
  87. {
  88. setRegionDst(region.x, region.y, region.width, region.height);
  89. _dirty = true;
  90. }
  91. const Rectangle& ImageControl::getRegionDst() const
  92. {
  93. return _dstRegion;
  94. }
  95. const char* ImageControl::getType() const
  96. {
  97. return "image";
  98. }
  99. void ImageControl::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  100. {
  101. spriteBatch->finish();
  102. // An ImageControl is not part of the texture atlas but should use the same projection matrix.
  103. _batch->setProjectionMatrix(spriteBatch->getProjectionMatrix());
  104. Vector4 color = Vector4::one();
  105. color.w *= _opacity;
  106. _batch->start();
  107. if (_dstRegion.isEmpty())
  108. {
  109. _batch->draw(_viewportBounds.x, _viewportBounds.y, _viewportBounds.width, _viewportBounds.height,
  110. _uvs.u1, _uvs.v1, _uvs.u2, _uvs.v2, color, _viewportClipBounds);
  111. }
  112. else
  113. {
  114. _batch->draw(_viewportBounds.x + _dstRegion.x, _viewportBounds.y + _dstRegion.y,
  115. _dstRegion.width, _dstRegion.height,
  116. _uvs.u1, _uvs.v1, _uvs.u2, _uvs.v2, color, _viewportClipBounds);
  117. }
  118. _batch->finish();
  119. spriteBatch->start();
  120. }
  121. void ImageControl::update(const Control* container, const Vector2& offset)
  122. {
  123. Control::update(container, offset);
  124. if (_batch)
  125. {
  126. if (_autoWidth == Control::AUTO_SIZE_FIT)
  127. {
  128. setWidth(_batch->getSampler()->getTexture()->getWidth());
  129. }
  130. if (_autoHeight == Control::AUTO_SIZE_FIT)
  131. {
  132. setHeight(_batch->getSampler()->getTexture()->getWidth());
  133. }
  134. }
  135. }
  136. }