diff --git a/src/app/scripting/app_scripting.cpp b/src/app/scripting/app_scripting.cpp index 97f64fb7c..bb181009c 100644 --- a/src/app/scripting/app_scripting.cpp +++ b/src/app/scripting/app_scripting.cpp @@ -22,157 +22,8 @@ namespace app { -static scripting::result_t rgba(scripting::ContextHandle handle) -{ - scripting::Context ctx(handle); - int r = ctx.requireInt(0); - int g = ctx.requireInt(1); - int b = ctx.requireInt(2); - int a = (ctx.isUndefined(3) ? 255: ctx.requireInt(3)); - ctx.pushUInt(doc::rgba(r, g, b, a)); - return 1; -} - -static scripting::result_t rgbaR(scripting::ContextHandle handle) -{ - scripting::Context ctx(handle); - ctx.pushUInt(doc::rgba_getr(ctx.requireUInt(0))); - return 1; -} - -static scripting::result_t rgbaG(scripting::ContextHandle handle) -{ - scripting::Context ctx(handle); - ctx.pushUInt(doc::rgba_getg(ctx.requireUInt(0))); - return 1; -} - -static scripting::result_t rgbaB(scripting::ContextHandle handle) -{ - scripting::Context ctx(handle); - ctx.pushUInt(doc::rgba_getb(ctx.requireUInt(0))); - return 1; -} - -static scripting::result_t rgbaA(scripting::ContextHandle handle) -{ - scripting::Context ctx(handle); - ctx.pushUInt(doc::rgba_geta(ctx.requireUInt(0))); - return 1; -} - -static scripting::result_t Sprite_ctor(scripting::ContextHandle handle) -{ - scripting::Context ctx(handle); - if (ctx.isConstructorCall()) { - int w = ctx.requireInt(0); - int h = ctx.requireInt(1); - int colorMode = (ctx.isUndefined(2) ? IMAGE_RGB: ctx.requireInt(2)); - - base::UniquePtr sprite( - Sprite::createBasicSprite((doc::PixelFormat)colorMode, w, h, 256)); - base::UniquePtr doc(new Document(sprite)); - sprite.release(); - - doc->setContext(UIContext::instance()); - ctx.pushThis(doc.release()); - } - return 0; -} - -static scripting::result_t Sprite_putPixel(scripting::ContextHandle handle) -{ - scripting::Context ctx(handle); - int x = ctx.requireInt(0); - int y = ctx.requireInt(1); - doc::color_t color = ctx.requireUInt(2); - - Document* doc = (Document*)ctx.getThis(); - DocumentView* docView = UIContext::instance()->getFirstDocumentView(doc); - if (!docView) - return 0; - - doc::Site site; - docView->getSite(&site); - - int celX, celY; - doc::Image* image = site.image(&celX, &celY, nullptr); - if (image) - image->putPixel(x-celX, y-celY, color); - - return 0; -} - -static scripting::result_t Sprite_getPixel(scripting::ContextHandle handle) -{ - scripting::Context ctx(handle); - int x = ctx.requireInt(0); - int y = ctx.requireInt(1); - - Document* doc = (Document*)ctx.getThis(); - DocumentView* docView = UIContext::instance()->getFirstDocumentView(doc); - if (!docView) - return 0; - - doc::Site site; - docView->getSite(&site); - - int celX, celY; - doc::Image* image = site.image(&celX, &celY, nullptr); - if (image) { - doc::color_t color = image->getPixel(x-celX, y-celY); - ctx.pushUInt(color); - return 1; - } - else - return 0; -} - -static scripting::result_t Sprite_resize(scripting::ContextHandle handle) -{ - scripting::Context ctx(handle); - int w = ctx.requireInt(0); - int h = ctx.requireInt(1); - - Document* doc = (Document*)ctx.getThis(); - { - Transaction transaction(UIContext::instance(), "Script Execution", ModifyDocument); - DocumentApi api(doc, transaction); - api.setSpriteSize(doc->sprite(), w, h); - transaction.commit(); - } - - return 0; -} - -static scripting::result_t Sprite_get_width(scripting::ContextHandle handle) -{ - scripting::Context ctx(handle); - Document* doc = (Document*)ctx.getThis(); - ctx.pushInt(doc->sprite()->width()); - return 1; -} - -static scripting::result_t Sprite_get_height(scripting::ContextHandle handle) -{ - scripting::Context ctx(handle); - Document* doc = (Document*)ctx.getThis(); - ctx.pushInt(doc->sprite()->height()); - return 1; -} - -const scripting::FunctionEntry Sprite_methods[] = { - { "putPixel", Sprite_putPixel, 3 }, - { "getPixel", Sprite_getPixel, 2 }, - { "resize", Sprite_resize, 2 }, - { nullptr, nullptr, 0 } -}; - -const scripting::PropertyEntry Sprite_props[] = { - { "width", Sprite_get_width, nullptr }, - { "height", Sprite_get_height, nullptr }, - { nullptr, nullptr, 0 } -}; +#include "app/scripting/raw_color.h" +#include "app/scripting/sprite.h" AppScripting::AppScripting(scripting::EngineDelegate* delegate) : scripting::Engine(delegate) diff --git a/src/app/scripting/raw_color.h b/src/app/scripting/raw_color.h new file mode 100644 index 000000000..1bfbb1c51 --- /dev/null +++ b/src/app/scripting/raw_color.h @@ -0,0 +1,49 @@ +// Aseprite +// Copyright (C) 2001-2015 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. + +namespace { + +scripting::result_t rgba(scripting::ContextHandle handle) +{ + scripting::Context ctx(handle); + int r = ctx.requireInt(0); + int g = ctx.requireInt(1); + int b = ctx.requireInt(2); + int a = (ctx.isUndefined(3) ? 255: ctx.requireInt(3)); + ctx.pushUInt(doc::rgba(r, g, b, a)); + return 1; +} + +scripting::result_t rgbaR(scripting::ContextHandle handle) +{ + scripting::Context ctx(handle); + ctx.pushUInt(doc::rgba_getr(ctx.requireUInt(0))); + return 1; +} + +scripting::result_t rgbaG(scripting::ContextHandle handle) +{ + scripting::Context ctx(handle); + ctx.pushUInt(doc::rgba_getg(ctx.requireUInt(0))); + return 1; +} + +scripting::result_t rgbaB(scripting::ContextHandle handle) +{ + scripting::Context ctx(handle); + ctx.pushUInt(doc::rgba_getb(ctx.requireUInt(0))); + return 1; +} + +scripting::result_t rgbaA(scripting::ContextHandle handle) +{ + scripting::Context ctx(handle); + ctx.pushUInt(doc::rgba_geta(ctx.requireUInt(0))); + return 1; +} + +} // anonymous namespace diff --git a/src/app/scripting/sprite.h b/src/app/scripting/sprite.h new file mode 100644 index 000000000..c0ec39231 --- /dev/null +++ b/src/app/scripting/sprite.h @@ -0,0 +1,123 @@ +// Aseprite +// Copyright (C) 2001-2015 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. + +namespace { + +scripting::result_t Sprite_ctor(scripting::ContextHandle handle) +{ + scripting::Context ctx(handle); + if (ctx.isConstructorCall()) { + int w = ctx.requireInt(0); + int h = ctx.requireInt(1); + int colorMode = (ctx.isUndefined(2) ? IMAGE_RGB: ctx.requireInt(2)); + + base::UniquePtr sprite( + Sprite::createBasicSprite((doc::PixelFormat)colorMode, w, h, 256)); + base::UniquePtr doc(new Document(sprite)); + sprite.release(); + + doc->setContext(UIContext::instance()); + ctx.pushThis(doc.release()); + } + return 0; +} + +scripting::result_t Sprite_putPixel(scripting::ContextHandle handle) +{ + scripting::Context ctx(handle); + int x = ctx.requireInt(0); + int y = ctx.requireInt(1); + doc::color_t color = ctx.requireUInt(2); + + Document* doc = (Document*)ctx.getThis(); + DocumentView* docView = UIContext::instance()->getFirstDocumentView(doc); + if (!docView) + return 0; + + doc::Site site; + docView->getSite(&site); + + int celX, celY; + doc::Image* image = site.image(&celX, &celY, nullptr); + if (image) + image->putPixel(x-celX, y-celY, color); + + return 0; +} + +scripting::result_t Sprite_getPixel(scripting::ContextHandle handle) +{ + scripting::Context ctx(handle); + int x = ctx.requireInt(0); + int y = ctx.requireInt(1); + + Document* doc = (Document*)ctx.getThis(); + DocumentView* docView = UIContext::instance()->getFirstDocumentView(doc); + if (!docView) + return 0; + + doc::Site site; + docView->getSite(&site); + + int celX, celY; + doc::Image* image = site.image(&celX, &celY, nullptr); + if (image) { + doc::color_t color = image->getPixel(x-celX, y-celY); + ctx.pushUInt(color); + return 1; + } + else + return 0; +} + +scripting::result_t Sprite_resize(scripting::ContextHandle handle) +{ + scripting::Context ctx(handle); + int w = ctx.requireInt(0); + int h = ctx.requireInt(1); + + Document* doc = (Document*)ctx.getThis(); + { + Transaction transaction(UIContext::instance(), "Script Execution", ModifyDocument); + DocumentApi api(doc, transaction); + api.setSpriteSize(doc->sprite(), w, h); + transaction.commit(); + } + + return 0; +} + +scripting::result_t Sprite_get_width(scripting::ContextHandle handle) +{ + scripting::Context ctx(handle); + Document* doc = (Document*)ctx.getThis(); + ctx.pushInt(doc->sprite()->width()); + return 1; +} + +scripting::result_t Sprite_get_height(scripting::ContextHandle handle) +{ + scripting::Context ctx(handle); + Document* doc = (Document*)ctx.getThis(); + ctx.pushInt(doc->sprite()->height()); + return 1; +} + +const scripting::FunctionEntry Sprite_methods[] = { + { "putPixel", Sprite_putPixel, 3 }, + { "getPixel", Sprite_getPixel, 2 }, + { "resize", Sprite_resize, 2 }, + { nullptr, nullptr, 0 } +}; + +const scripting::PropertyEntry Sprite_props[] = { + { "width", Sprite_get_width, nullptr }, + { "height", Sprite_get_height, nullptr }, + { nullptr, nullptr, 0 } +}; + +} // anonymous namespace