|
|
@@ -4,14 +4,14 @@
|
|
|
namespace gameplay
|
|
|
{
|
|
|
|
|
|
-ImageControl::ImageControl() : _image(NULL), _texture(NULL), _batch(NULL)
|
|
|
+ImageControl::ImageControl() :
|
|
|
+ _srcRegion(Rectangle::empty()), _dstRegion(Rectangle::empty()), _batch(NULL),
|
|
|
+ _tw(0.0f), _th(0.0f), _uvs(Theme::UVs::full())
|
|
|
{
|
|
|
}
|
|
|
|
|
|
ImageControl::~ImageControl()
|
|
|
{
|
|
|
- SAFE_RELEASE(_image);
|
|
|
- SAFE_RELEASE(_texture);
|
|
|
SAFE_DELETE(_batch);
|
|
|
}
|
|
|
|
|
|
@@ -45,29 +45,57 @@ void ImageControl::initialize(Theme::Style* style, Properties* properties)
|
|
|
{
|
|
|
setImage(path);
|
|
|
}
|
|
|
+
|
|
|
+ Vector4 region;
|
|
|
+ properties->getVector4("srcRegion", ®ion);
|
|
|
+ setSrcRegion(region.x, region.y, region.z, region.w);
|
|
|
+ properties->getVector4("dstRegion", ®ion);
|
|
|
+ setDstRegion(region.x, region.y, region.z, region.w);
|
|
|
}
|
|
|
|
|
|
void ImageControl::setImage(const char* path)
|
|
|
{
|
|
|
- SAFE_RELEASE(_image);
|
|
|
- SAFE_RELEASE(_texture);
|
|
|
SAFE_DELETE(_batch);
|
|
|
-
|
|
|
- _image = Image::create(path);
|
|
|
- _texture = Texture::create(_image);
|
|
|
- _batch = SpriteBatch::create(_texture);
|
|
|
+ Texture* texture = Texture::create(path);
|
|
|
+ _batch = SpriteBatch::create(texture);
|
|
|
+ _tw = 1.0f / texture->getWidth();
|
|
|
+ _th = 1.0f / texture->getHeight();
|
|
|
+ texture->release();
|
|
|
}
|
|
|
|
|
|
void ImageControl::setImage(Image* image)
|
|
|
{
|
|
|
- SAFE_RELEASE(_image);
|
|
|
- SAFE_RELEASE(_texture);
|
|
|
SAFE_DELETE(_batch);
|
|
|
+ Texture* texture = Texture::create(image);
|
|
|
+ _batch = SpriteBatch::create(texture);
|
|
|
+ _tw = 1.0f / texture->getWidth();
|
|
|
+ _th = 1.0f / texture->getHeight();
|
|
|
+ texture->release();
|
|
|
+}
|
|
|
+
|
|
|
+void ImageControl::setSrcRegion(float x, float y, float width, float height)
|
|
|
+{
|
|
|
+ _srcRegion.set(x, y, width, height);
|
|
|
+
|
|
|
+ _uvs.u1 = x * _tw;
|
|
|
+ _uvs.u2 = (x + width) * _tw;
|
|
|
+ _uvs.v1 = 1.0f - (y * _th);
|
|
|
+ _uvs.v2 = 1.0f - ((y + height) * _tw);
|
|
|
+}
|
|
|
+
|
|
|
+const Rectangle& ImageControl::getSrcRegion() const
|
|
|
+{
|
|
|
+ return _srcRegion;
|
|
|
+}
|
|
|
|
|
|
- image->addRef();
|
|
|
- _image = image;
|
|
|
- _texture = Texture::create(_image);
|
|
|
- _batch = SpriteBatch::create(_texture);
|
|
|
+void ImageControl::setDstRegion(float x, float y, float width, float height)
|
|
|
+{
|
|
|
+ _dstRegion.set(x, y, width, height);
|
|
|
+}
|
|
|
+
|
|
|
+const Rectangle& ImageControl::getDstRegion() const
|
|
|
+{
|
|
|
+ return _dstRegion;
|
|
|
}
|
|
|
|
|
|
void ImageControl::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
|
|
|
@@ -78,8 +106,17 @@ void ImageControl::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
|
|
|
_batch->setProjectionMatrix(spriteBatch->getProjectionMatrix());
|
|
|
|
|
|
_batch->start();
|
|
|
- _batch->draw(_viewportBounds.x, _viewportBounds.y, _viewportBounds.width, _viewportBounds.height,
|
|
|
- 0.0f, 0.0f, 1.0f, 1.0f, Vector4::one(), _viewportClipBounds);
|
|
|
+ if (_dstRegion.isEmpty())
|
|
|
+ {
|
|
|
+ _batch->draw(_viewportBounds.x, _viewportBounds.y, _viewportBounds.width, _viewportBounds.height,
|
|
|
+ _uvs.u1, _uvs.v1, _uvs.u2, _uvs.v2, Vector4::one(), _viewportClipBounds);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ _batch->draw(_viewportBounds.x + _dstRegion.x, _viewportBounds.y + _dstRegion.y,
|
|
|
+ _dstRegion.width, _dstRegion.height,
|
|
|
+ _uvs.u1, _uvs.v1, _uvs.u2, _uvs.v2, Vector4::one(), _viewportClipBounds);
|
|
|
+ }
|
|
|
_batch->finish();
|
|
|
|
|
|
spriteBatch->start();
|