script: Sprite.crop() works with selection or x,y,w,h params

This commit is contained in:
David Capello 2016-05-06 16:52:29 -03:00
parent 207bc6c98b
commit 76eca942f0

View File

@ -112,18 +112,32 @@ script::result_t Sprite_resize(script::ContextHandle handle)
script::result_t Sprite_crop(script::ContextHandle handle)
{
script::Context ctx(handle);
int x = ctx.requireInt(0);
int y = ctx.requireInt(1);
int w = ctx.requireInt(2);
int h = ctx.requireInt(3);
gfx::Rect bounds;
auto wrap = (SpriteWrap*)ctx.getThis();
if (wrap) {
wrap->commitImages();
Document* doc = wrap->document();
DocumentApi api(doc, wrap->transaction());
api.cropSprite(doc->sprite(), gfx::Rect(x, y, w, h));
// Use mask bounds
if (ctx.isUndefined(0)) {
if (doc->isMaskVisible())
bounds = doc->mask()->bounds();
else
bounds = doc->sprite()->bounds();
}
else {
bounds.x = ctx.requireInt(0);
bounds.y = ctx.requireInt(1);
bounds.w = ctx.requireInt(2);
bounds.h = ctx.requireInt(3);
}
if (!bounds.isEmpty()) {
DocumentApi api(doc, wrap->transaction());
api.cropSprite(doc->sprite(), bounds);
}
}
return 0;