ImageControl.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. ImageControl* imageControl = new ImageControl();
  17. imageControl->_id = id ? id : "";
  18. imageControl->initialize("Image", style, NULL);
  19. return imageControl;
  20. }
  21. Control* ImageControl::create(Theme::Style* style, Properties* properties)
  22. {
  23. ImageControl* imageControl = new ImageControl();
  24. imageControl->initialize("Image", style, properties);
  25. return imageControl;
  26. }
  27. void ImageControl::initialize(const char* typeName, Theme::Style* style, Properties* properties)
  28. {
  29. Control::initialize(typeName, style, properties);
  30. if (properties)
  31. {
  32. std::string path;
  33. if (properties->getPath("path", &path))
  34. {
  35. setImage(path.c_str());
  36. }
  37. if (properties->exists("srcRegion"))
  38. {
  39. Vector4 region;
  40. properties->getVector4("srcRegion", &region);
  41. setRegionSrc(region.x, region.y, region.z, region.w);
  42. }
  43. if (properties->exists("dstRegion"))
  44. {
  45. Vector4 region;
  46. properties->getVector4("dstRegion", &region);
  47. setRegionDst(region.x, region.y, region.z, region.w);
  48. }
  49. }
  50. }
  51. const char* ImageControl::getTypeName() const
  52. {
  53. return "ImageControl";
  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. if (_autoSize != AUTO_SIZE_NONE)
  64. setDirty(DIRTY_BOUNDS);
  65. }
  66. void ImageControl::setRegionSrc(float x, float y, float width, float height)
  67. {
  68. _srcRegion.set(x, y, width, height);
  69. _uvs.u1 = x * _tw;
  70. _uvs.u2 = (x + width) * _tw;
  71. _uvs.v1 = 1.0f - (y * _th);
  72. _uvs.v2 = 1.0f - ((y + height) * _th);
  73. }
  74. void ImageControl::setRegionSrc(const Rectangle& region)
  75. {
  76. setRegionSrc(region.x, region.y, region.width, region.height);
  77. }
  78. const Rectangle& ImageControl::getRegionSrc() const
  79. {
  80. return _srcRegion;
  81. }
  82. void ImageControl::setRegionDst(float x, float y, float width, float height)
  83. {
  84. _dstRegion.set(x, y, width, height);
  85. }
  86. void ImageControl::setRegionDst(const Rectangle& region)
  87. {
  88. setRegionDst(region.x, region.y, region.width, region.height);
  89. }
  90. const Rectangle& ImageControl::getRegionDst() const
  91. {
  92. return _dstRegion;
  93. }
  94. unsigned int ImageControl::drawImages(Form* form, const Rectangle& clip)
  95. {
  96. if (!_batch)
  97. return 0;
  98. startBatch(form, _batch);
  99. Vector4 color = Vector4::one();
  100. color.w *= _opacity;
  101. if (_dstRegion.isEmpty())
  102. {
  103. _batch->draw(_viewportBounds.x, _viewportBounds.y, _viewportBounds.width, _viewportBounds.height,
  104. _uvs.u1, _uvs.v1, _uvs.u2, _uvs.v2, color, _viewportClipBounds);
  105. }
  106. else
  107. {
  108. _batch->draw(_viewportBounds.x + _dstRegion.x, _viewportBounds.y + _dstRegion.y,
  109. _dstRegion.width, _dstRegion.height,
  110. _uvs.u1, _uvs.v1, _uvs.u2, _uvs.v2, color, _viewportClipBounds);
  111. }
  112. finishBatch(form, _batch);
  113. return 1;
  114. }
  115. void ImageControl::updateBounds()
  116. {
  117. if (_batch)
  118. {
  119. if (_autoSize & AUTO_SIZE_WIDTH)
  120. {
  121. setWidthInternal(_batch->getSampler()->getTexture()->getWidth());
  122. }
  123. if (_autoSize & AUTO_SIZE_HEIGHT)
  124. {
  125. setHeightInternal(_batch->getSampler()->getTexture()->getWidth());
  126. }
  127. }
  128. Control::updateBounds();
  129. }
  130. }