Merge branch 'scripting'

This commit is contained in:
David Capello 2016-05-06 16:35:49 -03:00
commit d21e7770d6
5 changed files with 109 additions and 8 deletions

View File

@ -12,6 +12,8 @@
#include "app/script/console_object.h" #include "app/script/console_object.h"
#include "app/document.h" #include "app/document.h"
#include "app/commands/commands.h"
#include "app/commands/params.h"
#include "app/script/app_scripting.h" #include "app/script/app_scripting.h"
#include "app/script/sprite_wrap.h" #include "app/script/sprite_wrap.h"
#include "app/ui_context.h" #include "app/ui_context.h"
@ -26,6 +28,30 @@ namespace app {
namespace { namespace {
script::result_t App_open(script::ContextHandle handle)
{
script::Context ctx(handle);
if (!ctx.isString(0) ||
!ctx.toString(0))
return 0;
const char* fn = ctx.toString(0);
app::Document* oldDoc = UIContext::instance()->activeDocument();
Command* openCommand = CommandsModule::instance()->getCommandByName(CommandId::OpenFile);
Params params;
params.set("filename", fn);
UIContext::instance()->executeCommand(openCommand, params);
app::Document* newDoc = UIContext::instance()->activeDocument();
if (newDoc != oldDoc)
ctx.pushObject(unwrap_engine(ctx)->wrapSprite(newDoc), "Sprite");
else
ctx.pushNull();
return 1;
}
script::result_t App_get_activeSprite(script::ContextHandle handle) script::result_t App_get_activeSprite(script::ContextHandle handle)
{ {
script::Context ctx(handle); script::Context ctx(handle);
@ -66,6 +92,7 @@ script::result_t App_get_version(script::ContextHandle handle)
} }
const script::FunctionEntry App_methods[] = { const script::FunctionEntry App_methods[] = {
{ "open", App_open, 1 },
{ nullptr, nullptr, 0 } { nullptr, nullptr, 0 }
}; };

View File

@ -39,6 +39,9 @@ void ImageWrap::commit()
m_backup.get(), m_backup.get(),
m_modifiedRegion, 0, 0, m_modifiedRegion, 0, 0,
true)); true));
m_backup.reset(nullptr);
m_modifiedRegion.clear();
} }
void ImageWrap::modifyRegion(const gfx::Region& rgn) void ImageWrap::modifyRegion(const gfx::Region& rgn)

View File

@ -16,11 +16,13 @@
#include "app/commands/params.h" #include "app/commands/params.h"
#include "app/document.h" #include "app/document.h"
#include "app/document_api.h" #include "app/document_api.h"
#include "app/file/palette_file.h"
#include "app/script/app_scripting.h" #include "app/script/app_scripting.h"
#include "app/script/sprite_wrap.h" #include "app/script/sprite_wrap.h"
#include "app/transaction.h" #include "app/transaction.h"
#include "app/ui/document_view.h" #include "app/ui/document_view.h"
#include "app/ui_context.h" #include "app/ui_context.h"
#include "doc/palette.h"
#include "doc/site.h" #include "doc/site.h"
#include "doc/sprite.h" #include "doc/sprite.h"
#include "script/engine.h" #include "script/engine.h"
@ -56,6 +58,8 @@ script::result_t Sprite_resize(script::ContextHandle handle)
auto wrap = (SpriteWrap*)ctx.getThis(); auto wrap = (SpriteWrap*)ctx.getThis();
if (wrap) { if (wrap) {
wrap->commitImages();
Document* doc = wrap->document(); Document* doc = wrap->document();
DocumentApi api(doc, wrap->transaction()); DocumentApi api(doc, wrap->transaction());
api.setSpriteSize(doc->sprite(), w, h); api.setSpriteSize(doc->sprite(), w, h);
@ -74,6 +78,8 @@ script::result_t Sprite_crop(script::ContextHandle handle)
auto wrap = (SpriteWrap*)ctx.getThis(); auto wrap = (SpriteWrap*)ctx.getThis();
if (wrap) { if (wrap) {
wrap->commitImages();
Document* doc = wrap->document(); Document* doc = wrap->document();
DocumentApi api(doc, wrap->transaction()); DocumentApi api(doc, wrap->transaction());
api.cropSprite(doc->sprite(), gfx::Rect(x, y, w, h)); api.cropSprite(doc->sprite(), gfx::Rect(x, y, w, h));
@ -82,28 +88,81 @@ script::result_t Sprite_crop(script::ContextHandle handle)
return 0; return 0;
} }
script::result_t Sprite_save(script::ContextHandle handle)
{
script::Context ctx(handle);
auto wrap = (SpriteWrap*)ctx.getThis();
if (wrap) {
wrap->commit();
auto doc = wrap->document();
auto uiCtx = UIContext::instance();
uiCtx->setActiveDocument(doc);
Command* saveCommand = CommandsModule::instance()->getCommandByName(CommandId::SaveFile);
uiCtx->executeCommand(saveCommand);
}
return 0;
}
script::result_t Sprite_saveAs(script::ContextHandle handle) script::result_t Sprite_saveAs(script::ContextHandle handle)
{ {
script::Context ctx(handle); script::Context ctx(handle);
const char* fn = ctx.requireString(0); const char* fn = ctx.requireString(0);
bool asCopy = ctx.getBool(1);
auto wrap = (SpriteWrap*)ctx.getThis(); auto wrap = (SpriteWrap*)ctx.getThis();
if (fn && wrap) { if (fn && wrap) {
Document* doc = wrap->document(); wrap->commit();
auto doc = wrap->document();
auto uiCtx = UIContext::instance(); auto uiCtx = UIContext::instance();
uiCtx->setActiveDocument(doc); uiCtx->setActiveDocument(doc);
Command* saveAsCommand = CommandsModule::instance()->getCommandByName(CommandId::SaveFileCopyAs); Command* saveCommand =
CommandsModule::instance()->getCommandByName(
(asCopy ? CommandId::SaveFileCopyAs:
CommandId::SaveFile));
Params params; Params params;
params.set("filename", fn); if (asCopy)
uiCtx->executeCommand(saveAsCommand, params); params.set("filename", fn);
else
doc->setFilename(fn);
uiCtx->executeCommand(saveCommand, params);
} }
return 0; return 0;
} }
script::result_t Sprite_loadPalette(script::ContextHandle handle)
{
script::Context ctx(handle);
const char* fn = ctx.requireString(0);
auto wrap = (SpriteWrap*)ctx.getThis();
if (fn && wrap) {
auto doc = wrap->document();
base::UniquePtr<doc::Palette> palette(load_palette(fn));
if (palette) {
// TODO Merge this with the code in LoadPaletteCommand
doc->getApi(wrap->transaction()).setPalette(
wrap->sprite(), 0, palette);
}
}
return 0;
}
script::result_t Sprite_get_filename(script::ContextHandle handle)
{
script::Context ctx(handle);
auto wrap = (SpriteWrap*)ctx.getThis();
ctx.pushString(wrap->document()->filename().c_str());
return 1;
}
script::result_t Sprite_get_width(script::ContextHandle handle) script::result_t Sprite_get_width(script::ContextHandle handle)
{ {
script::Context ctx(handle); script::Context ctx(handle);
@ -151,11 +210,14 @@ script::result_t Sprite_set_height(script::ContextHandle handle)
const script::FunctionEntry Sprite_methods[] = { const script::FunctionEntry Sprite_methods[] = {
{ "resize", Sprite_resize, 2 }, { "resize", Sprite_resize, 2 },
{ "crop", Sprite_crop, 4 }, { "crop", Sprite_crop, 4 },
{ "save", Sprite_save, 2 },
{ "saveAs", Sprite_saveAs, 2 }, { "saveAs", Sprite_saveAs, 2 },
{ "loadPalette", Sprite_loadPalette, 1 },
{ nullptr, nullptr, 0 } { nullptr, nullptr, 0 }
}; };
const script::PropertyEntry Sprite_props[] = { const script::PropertyEntry Sprite_props[] = {
{ "filename", Sprite_get_filename, nullptr },
{ "width", Sprite_get_width, Sprite_set_width }, { "width", Sprite_get_width, Sprite_set_width },
{ "height", Sprite_get_height, Sprite_set_height }, { "height", Sprite_get_height, Sprite_set_height },
{ nullptr, nullptr, 0 } { nullptr, nullptr, 0 }

View File

@ -51,8 +51,7 @@ Transaction& SpriteWrap::transaction()
void SpriteWrap::commit() void SpriteWrap::commit()
{ {
for (auto it : m_images) commitImages();
it.second->commit();
if (m_transaction) { if (m_transaction) {
m_transaction->commit(); m_transaction->commit();
@ -61,6 +60,12 @@ void SpriteWrap::commit()
} }
} }
void SpriteWrap::commitImages()
{
for (auto it : m_images)
it.second->commit();
}
app::Document* SpriteWrap::document() app::Document* SpriteWrap::document()
{ {
return m_doc; return m_doc;
@ -73,8 +78,11 @@ doc::Sprite* SpriteWrap::sprite()
ImageWrap* SpriteWrap::activeImage() ImageWrap* SpriteWrap::activeImage()
{ {
if (!m_view) if (!m_view) {
return nullptr; m_view = UIContext::instance()->getFirstDocumentView(m_doc);
if (!m_view)
return nullptr;
}
doc::Site site; doc::Site site;
m_view->getSite(&site); m_view->getSite(&site);

View File

@ -32,6 +32,7 @@ namespace app {
~SpriteWrap(); ~SpriteWrap();
void commit(); void commit();
void commitImages();
Transaction& transaction(); Transaction& transaction();
app::Document* document(); app::Document* document();