mirror of
https://github.com/aseprite/aseprite.git
synced 2025-02-06 12:39:57 +00:00
Merge branch 'scripting'
This commit is contained in:
commit
d21e7770d6
@ -12,6 +12,8 @@
|
||||
#include "app/script/console_object.h"
|
||||
|
||||
#include "app/document.h"
|
||||
#include "app/commands/commands.h"
|
||||
#include "app/commands/params.h"
|
||||
#include "app/script/app_scripting.h"
|
||||
#include "app/script/sprite_wrap.h"
|
||||
#include "app/ui_context.h"
|
||||
@ -26,6 +28,30 @@ namespace app {
|
||||
|
||||
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::Context ctx(handle);
|
||||
@ -66,6 +92,7 @@ script::result_t App_get_version(script::ContextHandle handle)
|
||||
}
|
||||
|
||||
const script::FunctionEntry App_methods[] = {
|
||||
{ "open", App_open, 1 },
|
||||
{ nullptr, nullptr, 0 }
|
||||
};
|
||||
|
||||
|
@ -39,6 +39,9 @@ void ImageWrap::commit()
|
||||
m_backup.get(),
|
||||
m_modifiedRegion, 0, 0,
|
||||
true));
|
||||
|
||||
m_backup.reset(nullptr);
|
||||
m_modifiedRegion.clear();
|
||||
}
|
||||
|
||||
void ImageWrap::modifyRegion(const gfx::Region& rgn)
|
||||
|
@ -16,11 +16,13 @@
|
||||
#include "app/commands/params.h"
|
||||
#include "app/document.h"
|
||||
#include "app/document_api.h"
|
||||
#include "app/file/palette_file.h"
|
||||
#include "app/script/app_scripting.h"
|
||||
#include "app/script/sprite_wrap.h"
|
||||
#include "app/transaction.h"
|
||||
#include "app/ui/document_view.h"
|
||||
#include "app/ui_context.h"
|
||||
#include "doc/palette.h"
|
||||
#include "doc/site.h"
|
||||
#include "doc/sprite.h"
|
||||
#include "script/engine.h"
|
||||
@ -56,6 +58,8 @@ script::result_t Sprite_resize(script::ContextHandle handle)
|
||||
|
||||
auto wrap = (SpriteWrap*)ctx.getThis();
|
||||
if (wrap) {
|
||||
wrap->commitImages();
|
||||
|
||||
Document* doc = wrap->document();
|
||||
DocumentApi api(doc, wrap->transaction());
|
||||
api.setSpriteSize(doc->sprite(), w, h);
|
||||
@ -74,6 +78,8 @@ script::result_t Sprite_crop(script::ContextHandle handle)
|
||||
|
||||
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));
|
||||
@ -82,28 +88,81 @@ script::result_t Sprite_crop(script::ContextHandle handle)
|
||||
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::Context ctx(handle);
|
||||
const char* fn = ctx.requireString(0);
|
||||
bool asCopy = ctx.getBool(1);
|
||||
|
||||
auto wrap = (SpriteWrap*)ctx.getThis();
|
||||
if (fn && wrap) {
|
||||
Document* doc = wrap->document();
|
||||
wrap->commit();
|
||||
|
||||
auto doc = wrap->document();
|
||||
auto uiCtx = UIContext::instance();
|
||||
uiCtx->setActiveDocument(doc);
|
||||
|
||||
Command* saveAsCommand = CommandsModule::instance()->getCommandByName(CommandId::SaveFileCopyAs);
|
||||
Command* saveCommand =
|
||||
CommandsModule::instance()->getCommandByName(
|
||||
(asCopy ? CommandId::SaveFileCopyAs:
|
||||
CommandId::SaveFile));
|
||||
|
||||
Params params;
|
||||
params.set("filename", fn);
|
||||
uiCtx->executeCommand(saveAsCommand, params);
|
||||
if (asCopy)
|
||||
params.set("filename", fn);
|
||||
else
|
||||
doc->setFilename(fn);
|
||||
uiCtx->executeCommand(saveCommand, params);
|
||||
}
|
||||
|
||||
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::Context ctx(handle);
|
||||
@ -151,11 +210,14 @@ script::result_t Sprite_set_height(script::ContextHandle handle)
|
||||
const script::FunctionEntry Sprite_methods[] = {
|
||||
{ "resize", Sprite_resize, 2 },
|
||||
{ "crop", Sprite_crop, 4 },
|
||||
{ "save", Sprite_save, 2 },
|
||||
{ "saveAs", Sprite_saveAs, 2 },
|
||||
{ "loadPalette", Sprite_loadPalette, 1 },
|
||||
{ nullptr, nullptr, 0 }
|
||||
};
|
||||
|
||||
const script::PropertyEntry Sprite_props[] = {
|
||||
{ "filename", Sprite_get_filename, nullptr },
|
||||
{ "width", Sprite_get_width, Sprite_set_width },
|
||||
{ "height", Sprite_get_height, Sprite_set_height },
|
||||
{ nullptr, nullptr, 0 }
|
||||
|
@ -51,8 +51,7 @@ Transaction& SpriteWrap::transaction()
|
||||
|
||||
void SpriteWrap::commit()
|
||||
{
|
||||
for (auto it : m_images)
|
||||
it.second->commit();
|
||||
commitImages();
|
||||
|
||||
if (m_transaction) {
|
||||
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()
|
||||
{
|
||||
return m_doc;
|
||||
@ -73,8 +78,11 @@ doc::Sprite* SpriteWrap::sprite()
|
||||
|
||||
ImageWrap* SpriteWrap::activeImage()
|
||||
{
|
||||
if (!m_view)
|
||||
return nullptr;
|
||||
if (!m_view) {
|
||||
m_view = UIContext::instance()->getFirstDocumentView(m_doc);
|
||||
if (!m_view)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
doc::Site site;
|
||||
m_view->getSite(&site);
|
||||
|
@ -32,6 +32,7 @@ namespace app {
|
||||
~SpriteWrap();
|
||||
|
||||
void commit();
|
||||
void commitImages();
|
||||
Transaction& transaction();
|
||||
|
||||
app::Document* document();
|
||||
|
Loading…
x
Reference in New Issue
Block a user