2015-02-12 23:16:25 +08:00
|
|
|
// 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.
|
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"
|
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"
|
2012-06-16 10:37:59 +08:00
|
|
|
#include "app/find_widget.h"
|
|
|
|
#include "app/load_widget.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 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 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-08-21 10:42:05 +08:00
|
|
|
#include "generated_cel_properties.h"
|
|
|
|
|
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)
|
|
|
|
, m_cel(nullptr)
|
|
|
|
, m_selfUpdate(false) {
|
|
|
|
opacity()->Change.connect(Bind<void>(&CelPropertiesWindow::onShowChange, this));
|
|
|
|
m_timer.Tick.connect(Bind<void>(&CelPropertiesWindow::onCommitChange, this));
|
|
|
|
|
|
|
|
remapWindow();
|
|
|
|
centerWindow();
|
|
|
|
load_window_pos(this, "CelProperties");
|
|
|
|
|
|
|
|
UIContext::instance()->addObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~CelPropertiesWindow() {
|
|
|
|
UIContext::instance()->removeObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setCel(Cel* cel) {
|
|
|
|
// Save uncommited changes
|
|
|
|
if (m_cel) {
|
|
|
|
if (m_timer.isRunning())
|
|
|
|
onCommitChange();
|
|
|
|
|
|
|
|
document()->removeObserver(this);
|
|
|
|
m_cel = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_timer.stop();
|
|
|
|
m_cel = const_cast<Cel*>(cel);
|
|
|
|
|
|
|
|
base::ScopedValue<bool> switchSelf(m_selfUpdate, true, false);
|
|
|
|
|
|
|
|
if (m_cel) {
|
|
|
|
document()->addObserver(this);
|
|
|
|
|
|
|
|
m_oldOpacity = cel->opacity();
|
|
|
|
|
|
|
|
opacity()->setValue(cel->opacity());
|
|
|
|
opacity()->setEnabled(!cel->layer()->isBackground());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
opacity()->setEnabled(false);
|
|
|
|
}
|
2015-08-21 10:42:05 +08:00
|
|
|
}
|
2015-08-21 19:35:29 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
app::Document* document() {
|
|
|
|
ASSERT(m_cel);
|
|
|
|
if (m_cel)
|
|
|
|
return static_cast<app::Document*>(m_cel->document());
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int opacityValue() const {
|
|
|
|
return opacity()->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onProcessMessage(ui::Message* msg) override {
|
|
|
|
switch (msg->type()) {
|
|
|
|
|
|
|
|
case kCloseMessage:
|
|
|
|
// Save changes before we close the window
|
|
|
|
setCel(nullptr);
|
|
|
|
save_window_pos(this, "CelProperties");
|
|
|
|
|
|
|
|
deferDelete();
|
|
|
|
g_window = nullptr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return Window::onProcessMessage(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onShowChange() {
|
|
|
|
if (m_selfUpdate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_cel->setOpacity(opacityValue());
|
|
|
|
m_timer.start();
|
|
|
|
|
|
|
|
update_screen_for_document(document());
|
|
|
|
}
|
|
|
|
|
|
|
|
void onCommitChange() {
|
|
|
|
base::ScopedValue<bool> switchSelf(m_selfUpdate, true, false);
|
|
|
|
|
|
|
|
m_timer.stop();
|
|
|
|
|
|
|
|
int newOpacity = opacityValue();
|
|
|
|
|
2015-08-21 20:38:18 +08:00
|
|
|
m_cel->setOpacity(m_oldOpacity);
|
|
|
|
|
2015-08-21 19:35:29 +08:00
|
|
|
if (newOpacity != m_oldOpacity) {
|
|
|
|
try {
|
|
|
|
ContextWriter writer(UIContext::instance());
|
|
|
|
Transaction transaction(writer.context(), "Cel Opacity Change");
|
|
|
|
|
2015-08-21 20:38:18 +08:00
|
|
|
if (newOpacity != m_oldOpacity)
|
2015-08-21 19:35:29 +08:00
|
|
|
transaction.execute(new cmd::SetCelOpacity(writer.cel(), newOpacity));
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
}
|
|
|
|
catch (const std::exception& e) {
|
|
|
|
Console::showException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
update_screen_for_document(document());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContextObserver impl
|
|
|
|
void onActiveSiteChange(const Site& site) override {
|
|
|
|
if (isVisible())
|
|
|
|
setCel(const_cast<Cel*>(site.cel()));
|
|
|
|
else if (m_cel)
|
|
|
|
setCel(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DocumentObserver impl
|
|
|
|
void onCelOpacityChange(DocumentEvent& ev) override {
|
|
|
|
updateFromCel(ev.cel());
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateFromCel(Cel* cel) {
|
|
|
|
if (m_selfUpdate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Cancel current editions (just in case)
|
|
|
|
m_timer.stop();
|
|
|
|
setCel(cel);
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer m_timer;
|
|
|
|
Cel* m_cel;
|
|
|
|
int m_oldOpacity;
|
|
|
|
bool m_selfUpdate;
|
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);
|
|
|
|
Cel* cel = reader.cel();
|
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 19:35:29 +08:00
|
|
|
g_window->setCel(cel);
|
|
|
|
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
|