ImageControl.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "Base.h"
  2. #include "ImageControl.h"
  3. namespace gameplay
  4. {
  5. ImageControl::ImageControl() : _image(NULL), _texture(NULL), _batch(NULL)
  6. {
  7. }
  8. ImageControl::~ImageControl()
  9. {
  10. SAFE_RELEASE(_image);
  11. SAFE_RELEASE(_texture);
  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. return imageControl;
  22. }
  23. ImageControl* ImageControl::create(Theme::Style* style, Properties* properties)
  24. {
  25. ImageControl* imageControl = new ImageControl();
  26. imageControl->initialize(style, properties);
  27. return imageControl;
  28. }
  29. void ImageControl::initialize(Theme::Style* style, Properties* properties)
  30. {
  31. GP_ASSERT(properties);
  32. Control::initialize(style, properties);
  33. const char* path = properties->getString("path");
  34. if (path)
  35. {
  36. setImage(path);
  37. }
  38. }
  39. void ImageControl::setImage(const char* path)
  40. {
  41. SAFE_RELEASE(_image);
  42. SAFE_RELEASE(_texture);
  43. SAFE_DELETE(_batch);
  44. _image = Image::create(path);
  45. _texture = Texture::create(_image);
  46. _batch = SpriteBatch::create(_texture);
  47. }
  48. void ImageControl::setImage(Image* image)
  49. {
  50. SAFE_RELEASE(_image);
  51. SAFE_RELEASE(_texture);
  52. SAFE_DELETE(_batch);
  53. image->addRef();
  54. _image = image;
  55. _texture = Texture::create(_image);
  56. _batch = SpriteBatch::create(_texture);
  57. }
  58. void ImageControl::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  59. {
  60. spriteBatch->finish();
  61. // An ImageControl is not part of the texture atlas but should use the same projection matrix.
  62. _batch->setProjectionMatrix(spriteBatch->getProjectionMatrix());
  63. _batch->start();
  64. _batch->draw(_viewportBounds.x, _viewportBounds.y, _viewportBounds.width, _viewportBounds.height,
  65. 0.0f, 0.0f, 1.0f, 1.0f, Vector4::one(), _viewportClipBounds);
  66. _batch->finish();
  67. spriteBatch->start();
  68. }
  69. }