From 76eca942f007ef577ff2b71d54627cd6aa757293 Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 6 May 2016 16:52:29 -0300 Subject: [PATCH] script: Sprite.crop() works with selection or x,y,w,h params --- src/app/script/sprite_class.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/app/script/sprite_class.cpp b/src/app/script/sprite_class.cpp index 856fb273c..5fa353c77 100644 --- a/src/app/script/sprite_class.cpp +++ b/src/app/script/sprite_class.cpp @@ -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;