2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2016-02-01 21:52:05 +08:00
|
|
|
// Copyright (C) 2001-2016 David Capello
|
2015-02-12 23:16:25 +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.
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/app.h"
|
2015-08-21 19:35:29 +08:00
|
|
|
#include "app/cmd/set_cel_opacity.h"
|
2015-12-11 05:34:25 +08:00
|
|
|
#include "app/cmd/set_user_data.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/command.h"
|
2015-08-21 19:35:29 +08:00
|
|
|
#include "app/console.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/context_access.h"
|
2014-05-02 23:10:29 +08:00
|
|
|
#include "app/document_api.h"
|
2015-08-21 22:01:49 +08:00
|
|
|
#include "app/document_range.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/modules/gui.h"
|
2015-01-19 09:05:33 +08:00
|
|
|
#include "app/transaction.h"
|
2015-08-21 22:01:49 +08:00
|
|
|
#include "app/ui/timeline.h"
|
2015-12-11 05:34:25 +08:00
|
|
|
#include "app/ui/user_data_popup.h"
|
2015-08-21 19:35:29 +08:00
|
|
|
#include "app/ui_context.h"
|
|
|
|
#include "base/bind.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "base/mem_utils.h"
|
2015-08-21 19:35:29 +08:00
|
|
|
#include "base/scoped_value.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/cel.h"
|
2015-08-21 22:01:49 +08:00
|
|
|
#include "doc/cels_range.h"
|
2015-08-21 19:35:29 +08:00
|
|
|
#include "doc/document_event.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/image.h"
|
|
|
|
#include "doc/layer.h"
|
|
|
|
#include "doc/sprite.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "ui/ui.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-09-23 03:33:49 +08:00
|
|
|
#include "cel_properties.xml.h"
|
2015-08-21 10:42:05 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
using namespace ui;
|
|
|
|
|
2015-08-21 19:35:29 +08:00
|
|
|
class CelPropertiesWindow;
|
|
|
|
static CelPropertiesWindow* g_window = nullptr;
|
|
|
|
|
|
|
|
class CelPropertiesWindow : public app::gen::CelProperties
|
|
|
|
, public doc::ContextObserver
|
|
|
|
, public doc::DocumentObserver {
|
2015-08-21 10:42:05 +08:00
|
|
|
public:
|
2015-08-21 19:35:29 +08:00
|
|
|
CelPropertiesWindow()
|
|
|
|
: m_timer(250, this)
|
2015-08-21 22:01:49 +08:00
|
|
|
, m_document(nullptr)
|
2015-08-21 19:35:29 +08:00
|
|
|
, m_cel(nullptr)
|
2016-02-02 05:09:02 +08:00
|
|
|
, m_selfUpdate(false)
|
|
|
|
, m_newUserData(false) {
|
2015-12-05 02:17:42 +08:00
|
|
|
opacity()->Change.connect(base::Bind<void>(&CelPropertiesWindow::onStartTimer, this));
|
2015-12-11 05:34:25 +08:00
|
|
|
userData()->Click.connect(base::Bind<void>(&CelPropertiesWindow::onPopupUserData, this));
|
2015-12-05 02:17:42 +08:00
|
|
|
m_timer.Tick.connect(base::Bind<void>(&CelPropertiesWindow::onCommitChange, this));
|
2015-08-21 19:35:29 +08:00
|
|
|
|
|
|
|
remapWindow();
|
|
|
|
centerWindow();
|
|
|
|
load_window_pos(this, "CelProperties");
|
|
|
|
|
2016-09-14 02:02:00 +08:00
|
|
|
UIContext::instance()->add_observer(this);
|
2015-08-21 19:35:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~CelPropertiesWindow() {
|
2016-09-14 02:02:00 +08:00
|
|
|
UIContext::instance()->remove_observer(this);
|
2015-08-21 19:35:29 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 22:01:49 +08:00
|
|
|
void setCel(Document* doc, Cel* cel) {
|
|
|
|
if (m_document) {
|
2016-09-14 02:02:00 +08:00
|
|
|
m_document->remove_observer(this);
|
2015-08-21 22:01:49 +08:00
|
|
|
m_document = nullptr;
|
2015-08-21 19:35:29 +08:00
|
|
|
m_cel = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_timer.stop();
|
2015-08-21 22:01:49 +08:00
|
|
|
m_document = doc;
|
|
|
|
m_cel = cel;
|
2016-04-23 00:19:06 +08:00
|
|
|
m_range = App::instance()->timeline()->range();
|
2015-08-21 19:35:29 +08:00
|
|
|
|
2015-08-21 22:01:49 +08:00
|
|
|
if (m_document)
|
2016-09-14 02:02:00 +08:00
|
|
|
m_document->add_observer(this);
|
2015-08-21 19:35:29 +08:00
|
|
|
|
2015-08-21 22:01:49 +08:00
|
|
|
updateFromCel();
|
2015-08-21 10:42:05 +08:00
|
|
|
}
|
2015-08-21 19:35:29 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
int opacityValue() const {
|
|
|
|
return opacity()->getValue();
|
|
|
|
}
|
|
|
|
|
2016-02-01 21:52:05 +08:00
|
|
|
int countCels(int* backgroundCount = nullptr) const {
|
|
|
|
if (backgroundCount)
|
|
|
|
*backgroundCount = 0;
|
|
|
|
|
2015-08-21 22:01:49 +08:00
|
|
|
if (!m_document)
|
|
|
|
return 0;
|
|
|
|
else if (m_cel &&
|
|
|
|
(!m_range.enabled() ||
|
|
|
|
(m_range.frames() == 1 &&
|
|
|
|
m_range.layers() == 1))) {
|
2016-02-01 21:52:05 +08:00
|
|
|
if (backgroundCount && m_cel->layer()->isBackground())
|
|
|
|
*backgroundCount = 1;
|
2015-08-21 22:01:49 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (m_range.enabled()) {
|
2016-02-02 05:05:40 +08:00
|
|
|
Sprite* sprite = m_document->sprite();
|
2015-08-21 22:01:49 +08:00
|
|
|
int count = 0;
|
2016-02-02 05:05:40 +08:00
|
|
|
for (Cel* cel : sprite->uniqueCels(m_range.frameBegin(),
|
|
|
|
m_range.frameEnd())) {
|
|
|
|
if (m_range.inRange(sprite->layerToIndex(cel->layer()))) {
|
2016-02-01 21:52:05 +08:00
|
|
|
if (backgroundCount && cel->layer()->isBackground())
|
|
|
|
++(*backgroundCount);
|
2015-08-21 22:01:49 +08:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-21 19:35:29 +08:00
|
|
|
bool onProcessMessage(ui::Message* msg) override {
|
|
|
|
switch (msg->type()) {
|
|
|
|
|
2015-08-22 01:53:31 +08:00
|
|
|
case kKeyDownMessage:
|
|
|
|
if (opacity()->hasFocus()) {
|
|
|
|
if (static_cast<KeyMessage*>(msg)->scancode() == kKeyEnter) {
|
|
|
|
onCommitChange();
|
|
|
|
closeWindow(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-08-21 19:35:29 +08:00
|
|
|
case kCloseMessage:
|
|
|
|
// Save changes before we close the window
|
2015-08-21 22:01:49 +08:00
|
|
|
setCel(nullptr, nullptr);
|
2015-08-21 19:35:29 +08:00
|
|
|
save_window_pos(this, "CelProperties");
|
|
|
|
|
|
|
|
deferDelete();
|
|
|
|
g_window = nullptr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return Window::onProcessMessage(msg);
|
|
|
|
}
|
|
|
|
|
2015-08-21 22:01:49 +08:00
|
|
|
void onStartTimer() {
|
2015-08-21 19:35:29 +08:00
|
|
|
if (m_selfUpdate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_timer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onCommitChange() {
|
|
|
|
base::ScopedValue<bool> switchSelf(m_selfUpdate, true, false);
|
|
|
|
|
|
|
|
m_timer.stop();
|
|
|
|
|
|
|
|
int newOpacity = opacityValue();
|
2015-08-21 22:01:49 +08:00
|
|
|
int count = countCels();
|
2015-08-21 19:35:29 +08:00
|
|
|
|
2015-08-21 22:01:49 +08:00
|
|
|
if ((count > 1) ||
|
2016-02-01 21:52:05 +08:00
|
|
|
(count == 1 && m_cel && (newOpacity != m_cel->opacity() ||
|
|
|
|
m_userData != m_cel->data()->userData()))) {
|
2015-08-21 19:35:29 +08:00
|
|
|
try {
|
|
|
|
ContextWriter writer(UIContext::instance());
|
2015-12-11 05:34:25 +08:00
|
|
|
Transaction transaction(writer.context(), "Set Cel Properties");
|
2015-08-21 19:35:29 +08:00
|
|
|
|
2016-02-01 21:52:05 +08:00
|
|
|
if (count == 1 && m_cel) {
|
|
|
|
if (!m_cel->layer()->isBackground() &&
|
|
|
|
newOpacity != m_cel->opacity()) {
|
2015-08-21 22:01:49 +08:00
|
|
|
transaction.execute(new cmd::SetCelOpacity(writer.cel(), newOpacity));
|
2016-02-01 21:52:05 +08:00
|
|
|
}
|
2015-12-11 05:34:25 +08:00
|
|
|
|
2015-12-23 00:18:08 +08:00
|
|
|
if (m_userData != m_cel->data()->userData()) {
|
2015-12-11 05:34:25 +08:00
|
|
|
transaction.execute(new cmd::SetUserData(writer.cel()->data(), m_userData));
|
2015-12-23 00:18:08 +08:00
|
|
|
|
|
|
|
// Redraw timeline because the cel's user data/color
|
|
|
|
// might have changed.
|
2016-04-23 00:19:06 +08:00
|
|
|
App::instance()->timeline()->invalidate();
|
2015-12-23 00:18:08 +08:00
|
|
|
}
|
2015-08-21 22:01:49 +08:00
|
|
|
}
|
2016-02-02 05:05:40 +08:00
|
|
|
else if (m_range.enabled()) {
|
|
|
|
Sprite* sprite = m_document->sprite();
|
|
|
|
for (Cel* cel : sprite->uniqueCels(m_range.frameBegin(),
|
|
|
|
m_range.frameEnd())) {
|
|
|
|
if (m_range.inRange(sprite->layerToIndex(cel->layer()))) {
|
|
|
|
if (!cel->layer()->isBackground() && newOpacity != cel->opacity()) {
|
2015-12-24 01:19:09 +08:00
|
|
|
transaction.execute(new cmd::SetCelOpacity(cel, newOpacity));
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:09:02 +08:00
|
|
|
if (m_newUserData &&
|
|
|
|
m_userData != cel->data()->userData()) {
|
2015-12-24 01:19:09 +08:00
|
|
|
transaction.execute(new cmd::SetUserData(cel->data(), m_userData));
|
|
|
|
|
|
|
|
// Redraw timeline because the cel's user data/color
|
|
|
|
// might have changed.
|
2016-04-23 00:19:06 +08:00
|
|
|
App::instance()->timeline()->invalidate();
|
2015-12-24 01:19:09 +08:00
|
|
|
}
|
2015-12-23 00:18:08 +08:00
|
|
|
}
|
2015-08-21 22:01:49 +08:00
|
|
|
}
|
|
|
|
}
|
2015-08-21 19:35:29 +08:00
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
}
|
|
|
|
catch (const std::exception& e) {
|
|
|
|
Console::showException(e);
|
|
|
|
}
|
|
|
|
|
2015-08-21 22:01:49 +08:00
|
|
|
update_screen_for_document(m_document);
|
2015-08-21 19:35:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 05:34:25 +08:00
|
|
|
void onPopupUserData() {
|
2016-02-01 21:52:05 +08:00
|
|
|
if (countCels() > 0) {
|
2016-02-02 05:09:02 +08:00
|
|
|
m_newUserData = false;
|
2016-02-01 21:52:05 +08:00
|
|
|
if (m_cel)
|
|
|
|
m_userData = m_cel->data()->userData();
|
|
|
|
else
|
|
|
|
m_userData = UserData();
|
|
|
|
|
2015-12-11 05:34:25 +08:00
|
|
|
if (show_user_data_popup(userData()->bounds(), m_userData)) {
|
2016-02-02 05:09:02 +08:00
|
|
|
m_newUserData = true;
|
2015-12-12 00:58:32 +08:00
|
|
|
onCommitChange();
|
2015-12-11 05:34:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 19:35:29 +08:00
|
|
|
// ContextObserver impl
|
|
|
|
void onActiveSiteChange(const Site& site) override {
|
|
|
|
if (isVisible())
|
2015-08-21 22:01:49 +08:00
|
|
|
setCel(static_cast<app::Document*>(const_cast<doc::Document*>(site.document())),
|
|
|
|
const_cast<Cel*>(site.cel()));
|
|
|
|
else if (m_document)
|
|
|
|
setCel(nullptr, nullptr);
|
2015-08-21 19:35:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DocumentObserver impl
|
|
|
|
void onCelOpacityChange(DocumentEvent& ev) override {
|
2015-08-21 22:01:49 +08:00
|
|
|
if (m_cel == ev.cel())
|
|
|
|
updateFromCel();
|
2015-08-21 19:35:29 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 22:01:49 +08:00
|
|
|
void updateFromCel() {
|
2015-08-21 19:35:29 +08:00
|
|
|
if (m_selfUpdate)
|
|
|
|
return;
|
|
|
|
|
2015-08-21 22:01:49 +08:00
|
|
|
m_timer.stop(); // Cancel current editions (just in case)
|
|
|
|
|
|
|
|
base::ScopedValue<bool> switchSelf(m_selfUpdate, true, false);
|
|
|
|
|
2016-02-01 21:52:05 +08:00
|
|
|
int bgCount = 0;
|
|
|
|
int count = countCels(&bgCount);
|
2015-08-21 22:01:49 +08:00
|
|
|
|
2015-12-11 05:34:25 +08:00
|
|
|
m_userData = UserData();
|
2016-02-02 05:09:02 +08:00
|
|
|
m_newUserData = false;
|
2015-12-11 05:34:25 +08:00
|
|
|
|
2015-08-21 22:01:49 +08:00
|
|
|
if (count > 0) {
|
|
|
|
if (m_cel) {
|
|
|
|
opacity()->setValue(m_cel->opacity());
|
2015-12-11 05:34:25 +08:00
|
|
|
m_userData = m_cel->data()->userData();
|
2015-08-21 22:01:49 +08:00
|
|
|
}
|
2016-02-01 21:52:05 +08:00
|
|
|
opacity()->setEnabled(bgCount < count);
|
2015-08-21 22:01:49 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
opacity()->setEnabled(false);
|
|
|
|
}
|
2015-08-21 19:35:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer m_timer;
|
2015-08-21 22:01:49 +08:00
|
|
|
Document* m_document;
|
2015-08-21 19:35:29 +08:00
|
|
|
Cel* m_cel;
|
2015-08-21 22:01:49 +08:00
|
|
|
DocumentRange m_range;
|
2015-08-21 19:35:29 +08:00
|
|
|
bool m_selfUpdate;
|
2015-12-11 05:34:25 +08:00
|
|
|
UserData m_userData;
|
2016-02-02 05:09:02 +08:00
|
|
|
bool m_newUserData;
|
2015-08-21 10:42:05 +08:00
|
|
|
};
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
class CelPropertiesCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
|
|
|
CelPropertiesCommand();
|
2014-08-15 10:07:47 +08:00
|
|
|
Command* clone() const override { return new CelPropertiesCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
protected:
|
2015-08-21 19:35:29 +08:00
|
|
|
bool onEnabled(Context* context) override;
|
|
|
|
void onExecute(Context* context) override;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
CelPropertiesCommand::CelPropertiesCommand()
|
|
|
|
: Command("CelProperties",
|
|
|
|
"Cel Properties",
|
|
|
|
CmdUIOnlyFlag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CelPropertiesCommand::onEnabled(Context* context)
|
|
|
|
{
|
|
|
|
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
|
|
|
|
ContextFlags::ActiveLayerIsImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CelPropertiesCommand::onExecute(Context* context)
|
|
|
|
{
|
2015-08-21 19:35:29 +08:00
|
|
|
ContextReader reader(context);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-08-21 19:35:29 +08:00
|
|
|
if (!g_window)
|
|
|
|
g_window = new CelPropertiesWindow;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-08-21 22:01:49 +08:00
|
|
|
g_window->setCel(reader.document(), reader.cel());
|
2015-08-21 19:35:29 +08:00
|
|
|
g_window->openWindow();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-08-21 19:35:29 +08:00
|
|
|
// Focus layer name
|
|
|
|
g_window->opacity()->requestFocus();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createCelPropertiesCommand()
|
|
|
|
{
|
|
|
|
return new CelPropertiesCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|