2016-04-07 06:05:06 +08:00
|
|
|
// Aseprite
|
2018-05-07 11:11:50 +08:00
|
|
|
// Copyright (C) 2016-2018 David Capello
|
2016-04-07 06:05:06 +08:00
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
2016-04-07 06:05:06 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/script/sprite_wrap.h"
|
|
|
|
|
2018-05-07 11:11:50 +08:00
|
|
|
#include "app/app.h"
|
2016-04-07 06:05:06 +08:00
|
|
|
#include "app/cmd/set_sprite_size.h"
|
2018-05-07 11:11:50 +08:00
|
|
|
#include "app/context.h"
|
2018-07-07 22:54:44 +08:00
|
|
|
#include "app/doc.h"
|
2018-07-07 14:07:16 +08:00
|
|
|
#include "app/doc_api.h"
|
2016-04-07 06:05:06 +08:00
|
|
|
#include "app/script/image_wrap.h"
|
2018-07-07 13:47:42 +08:00
|
|
|
#include "app/site.h"
|
2016-04-07 06:05:06 +08:00
|
|
|
#include "app/transaction.h"
|
|
|
|
#include "app/ui/document_view.h"
|
|
|
|
#include "doc/sprite.h"
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
2018-07-07 22:54:44 +08:00
|
|
|
SpriteWrap::SpriteWrap(Doc* doc)
|
2016-04-07 06:05:06 +08:00
|
|
|
: m_doc(doc)
|
2018-05-07 11:11:50 +08:00
|
|
|
, m_view(App::instance()->context()->getFirstDocumentView(m_doc))
|
2016-04-07 06:05:06 +08:00
|
|
|
, m_transaction(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SpriteWrap::~SpriteWrap()
|
|
|
|
{
|
|
|
|
for (auto it : m_images)
|
|
|
|
delete it.second;
|
|
|
|
|
|
|
|
if (m_transaction)
|
|
|
|
delete m_transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
Transaction& SpriteWrap::transaction()
|
|
|
|
{
|
|
|
|
if (!m_transaction) {
|
2018-05-07 11:11:50 +08:00
|
|
|
m_transaction = new Transaction(App::instance()->context(),
|
2016-04-07 06:05:06 +08:00
|
|
|
"Script Execution",
|
|
|
|
ModifyDocument);
|
|
|
|
}
|
|
|
|
return *m_transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteWrap::commit()
|
|
|
|
{
|
2016-04-17 08:29:57 +08:00
|
|
|
commitImages();
|
2016-04-07 06:05:06 +08:00
|
|
|
|
|
|
|
if (m_transaction) {
|
|
|
|
m_transaction->commit();
|
|
|
|
delete m_transaction;
|
|
|
|
m_transaction = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 08:29:57 +08:00
|
|
|
void SpriteWrap::commitImages()
|
|
|
|
{
|
|
|
|
for (auto it : m_images)
|
|
|
|
it.second->commit();
|
|
|
|
}
|
|
|
|
|
2018-07-07 22:54:44 +08:00
|
|
|
Doc* SpriteWrap::document()
|
2016-04-07 06:05:06 +08:00
|
|
|
{
|
|
|
|
return m_doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
doc::Sprite* SpriteWrap::sprite()
|
|
|
|
{
|
|
|
|
return m_doc->sprite();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageWrap* SpriteWrap::activeImage()
|
|
|
|
{
|
2016-04-17 08:29:57 +08:00
|
|
|
if (!m_view) {
|
2018-05-07 11:11:50 +08:00
|
|
|
m_view = App::instance()->context()->getFirstDocumentView(m_doc);
|
2016-04-17 08:29:57 +08:00
|
|
|
if (!m_view)
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-04-07 06:05:06 +08:00
|
|
|
|
2018-05-07 11:11:50 +08:00
|
|
|
#ifdef ENABLE_UI
|
2018-07-07 13:47:42 +08:00
|
|
|
Site site;
|
2016-04-07 06:05:06 +08:00
|
|
|
m_view->getSite(&site);
|
|
|
|
return wrapImage(site.image());
|
2018-05-07 11:11:50 +08:00
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
2016-04-07 06:05:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageWrap* SpriteWrap::wrapImage(doc::Image* img)
|
|
|
|
{
|
|
|
|
auto it = m_images.find(img->id());
|
|
|
|
if (it != m_images.end())
|
|
|
|
return it->second;
|
|
|
|
else {
|
|
|
|
auto wrap = new ImageWrap(this, img);
|
|
|
|
m_images[img->id()] = wrap;
|
|
|
|
return wrap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace app
|