From e03f34688e8f3325df468dfe708073e93381ce4d Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 6 May 2016 20:13:32 -0300 Subject: [PATCH] script: add Sprite.selection --- src/app/CMakeLists.txt | 1 + src/app/script/app_scripting.cpp | 2 + src/app/script/selection_class.cpp | 122 +++++++++++++++++++++++++++++ src/app/script/selection_class.h | 20 +++++ src/app/script/sprite_class.cpp | 51 +++--------- 5 files changed, 154 insertions(+), 42 deletions(-) create mode 100644 src/app/script/selection_class.cpp create mode 100644 src/app/script/selection_class.h diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 1ed0b0143..89c2a52c6 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -324,6 +324,7 @@ add_library(app-lib script/console_object.cpp script/image_class.cpp script/image_wrap.cpp + script/selection_class.cpp script/sprite_class.cpp script/sprite_wrap.cpp send_crash.cpp diff --git a/src/app/script/app_scripting.cpp b/src/app/script/app_scripting.cpp index 637fb1962..346acf40b 100644 --- a/src/app/script/app_scripting.cpp +++ b/src/app/script/app_scripting.cpp @@ -16,6 +16,7 @@ #include "app/script/console_object.h" #include "app/script/image_class.h" #include "app/script/image_wrap.h" +#include "app/script/selection_class.h" #include "app/script/sprite_class.h" #include "app/script/sprite_wrap.h" @@ -31,6 +32,7 @@ AppScripting::AppScripting(script::EngineDelegate* delegate) ctx.pushGlobalObject(); register_image_class(-1, ctx); register_sprite_class(-1, ctx); + register_selection_class(-1, ctx); ctx.pushPointer(this); ctx.setProp(-2, script::kPtrId); diff --git a/src/app/script/selection_class.cpp b/src/app/script/selection_class.cpp new file mode 100644 index 000000000..6705207f8 --- /dev/null +++ b/src/app/script/selection_class.cpp @@ -0,0 +1,122 @@ +// Aseprite +// Copyright (C) 2015-2016 David Capello +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "app/script/selection_class.h" + +#include "app/cmd/deselect_mask.h" +#include "app/cmd/set_mask.h" +#include "app/document.h" +#include "app/script/sprite_wrap.h" +#include "app/transaction.h" +#include "doc/mask.h" + +namespace app { + +using namespace doc; + +namespace { + +script::result_t Selection_ctor(script::ContextHandle handle) +{ + return 0; +} + +script::result_t Selection_select(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); + + auto wrap = (SpriteWrap*)ctx.getThis(); + if (wrap) { + Document* doc = wrap->document(); + + Mask newMask; + if (w > 0 && h > 0) + newMask.replace(gfx::Rect(x, y, w, h)); + + wrap->transaction().execute(new cmd::SetMask(doc, &newMask)); + } + + return 0; +} + +script::result_t Selection_selectAll(script::ContextHandle handle) +{ + script::Context ctx(handle); + + auto wrap = (SpriteWrap*)ctx.getThis(); + if (wrap) { + Document* doc = wrap->document(); + + Mask newMask; + newMask.replace(doc->sprite()->bounds()); + + wrap->transaction().execute(new cmd::SetMask(doc, &newMask)); + } + + return 0; +} + +script::result_t Selection_deselect(script::ContextHandle handle) +{ + script::Context ctx(handle); + + auto wrap = (SpriteWrap*)ctx.getThis(); + if (wrap) { + Document* doc = wrap->document(); + wrap->transaction().execute(new cmd::DeselectMask(doc)); + } + + return 0; +} + +script::result_t Selection_get_bounds(script::ContextHandle handle) +{ + script::Context ctx(handle); + auto wrap = (SpriteWrap*)ctx.getThis(); + if (wrap) { + Document* doc = wrap->document(); + if (doc->isMaskVisible()) { + gfx::Rect bounds = doc->mask()->bounds(); + script::index_t obj = ctx.pushObject(); + ctx.pushNumber(bounds.x); ctx.setProp(obj, "x"); + ctx.pushNumber(bounds.y); ctx.setProp(obj, "y"); + ctx.pushNumber(bounds.w); ctx.setProp(obj, "width"); + ctx.pushNumber(bounds.h); ctx.setProp(obj, "height"); + return 1; + } + } + return 0; +} + +const script::FunctionEntry Selection_methods[] = { + { "select", Selection_select, 4 }, + { "selectAll", Selection_selectAll, 0 }, + { "deselect", Selection_deselect, 1 }, + { nullptr, nullptr, 0 } +}; + +const script::PropertyEntry Selection_props[] = { + { "bounds", Selection_get_bounds, nullptr }, + { nullptr, nullptr, 0 } +}; + +} // anonymous namespace + +void register_selection_class(script::index_t idx, script::Context& ctx) +{ + ctx.registerClass(idx, "Selection", Selection_ctor, 3, Selection_methods, Selection_props); +} + +} // namespace app diff --git a/src/app/script/selection_class.h b/src/app/script/selection_class.h new file mode 100644 index 000000000..37b749764 --- /dev/null +++ b/src/app/script/selection_class.h @@ -0,0 +1,20 @@ +// Aseprite +// Copyright (C) 2015-2016 David Capello +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. + +#ifndef APP_SCRIPT_SELECTION_CLASS_H_INCLUDED +#define APP_SCRIPT_SELECTION_CLASS_H_INCLUDED +#pragma once + +#include "script/engine.h" + +namespace app { + + void register_selection_class(script::index_t idx, script::Context& ctx); + +} // namespace app + +#endif diff --git a/src/app/script/sprite_class.cpp b/src/app/script/sprite_class.cpp index 5fa353c77..00b8be159 100644 --- a/src/app/script/sprite_class.cpp +++ b/src/app/script/sprite_class.cpp @@ -11,7 +11,6 @@ #include "app/script/sprite_class.h" -#include "app/cmd/set_mask.h" #include "app/cmd/set_sprite_size.h" #include "app/commands/commands.h" #include "app/commands/params.h" @@ -52,45 +51,6 @@ script::result_t Sprite_ctor(script::ContextHandle handle) return 0; } -script::result_t Sprite_select(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); - - auto wrap = (SpriteWrap*)ctx.getThis(); - if (wrap) { - Document* doc = wrap->document(); - - Mask newMask; - if (w > 0 && h > 0) - newMask.replace(gfx::Rect(x, y, w, h)); - - wrap->transaction().execute(new cmd::SetMask(doc, &newMask)); - } - - return 0; -} - -script::result_t Sprite_selectAll(script::ContextHandle handle) -{ - script::Context ctx(handle); - - auto wrap = (SpriteWrap*)ctx.getThis(); - if (wrap) { - Document* doc = wrap->document(); - - Mask newMask; - newMask.replace(doc->sprite()->bounds()); - - wrap->transaction().execute(new cmd::SetMask(doc, &newMask)); - } - - return 0; -} - script::result_t Sprite_resize(script::ContextHandle handle) { script::Context ctx(handle); @@ -262,9 +222,15 @@ script::result_t Sprite_set_height(script::ContextHandle handle) return 0; } +script::result_t Sprite_get_selection(script::ContextHandle handle) +{ + script::Context ctx(handle); + auto wrap = (SpriteWrap*)ctx.getThis(); + ctx.pushObject(wrap, "Selection"); + return 1; +} + const script::FunctionEntry Sprite_methods[] = { - { "select", Sprite_select, 4 }, - { "selectAll", Sprite_selectAll, 0 }, { "resize", Sprite_resize, 2 }, { "crop", Sprite_crop, 4 }, { "save", Sprite_save, 2 }, @@ -277,6 +243,7 @@ const script::PropertyEntry Sprite_props[] = { { "filename", Sprite_get_filename, nullptr }, { "width", Sprite_get_width, Sprite_set_width }, { "height", Sprite_get_height, Sprite_set_height }, + { "selection", Sprite_get_selection, nullptr }, { nullptr, nullptr, 0 } };