script: add Sprite.selection

This commit is contained in:
David Capello 2016-05-06 20:13:32 -03:00
parent 34ebf07118
commit e03f34688e
5 changed files with 154 additions and 42 deletions

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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 }
};