2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2021-06-11 21:45:49 +08:00
|
|
|
// Copyright (C) 2020-2021 Igara Studio S.A.
|
2018-07-07 13:47:42 +08:00
|
|
|
// Copyright (C) 2001-2018 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"
|
2018-07-07 13:47:42 +08:00
|
|
|
#include "app/doc_event.h"
|
2018-07-07 21:07:21 +08:00
|
|
|
#include "app/doc_range.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/modules/gui.h"
|
2018-08-21 03:00:59 +08:00
|
|
|
#include "app/tx.h"
|
2017-03-27 00:33:12 +08:00
|
|
|
#include "app/ui/timeline/timeline.h"
|
2020-09-11 02:49:08 +08:00
|
|
|
#include "app/ui/user_data_view.h"
|
2015-08-21 19:35:29 +08:00
|
|
|
#include "app/ui_context.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"
|
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;
|
|
|
|
|
2018-07-07 13:47:42 +08:00
|
|
|
class CelPropertiesWindow : public app::gen::CelProperties,
|
|
|
|
public ContextObserver,
|
|
|
|
public DocObserver {
|
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)
|
2020-09-11 02:49:08 +08:00
|
|
|
, m_userDataView(new gen::UserData(), &Preferences::instance().cels.userDataVisibility)
|
|
|
|
{
|
2020-07-04 08:51:46 +08:00
|
|
|
opacity()->Change.connect([this]{ onStartTimer(); });
|
2020-09-11 02:49:08 +08:00
|
|
|
userData()->Click.connect([this]{ onToggleUserData(); });
|
2020-07-04 08:51:46 +08:00
|
|
|
m_timer.Tick.connect([this]{ onCommitChange(); });
|
2015-08-21 19:35:29 +08:00
|
|
|
|
2020-09-11 02:49:08 +08:00
|
|
|
m_userDataView.entry()->Change.connect([this]{ onStartTimer(); });
|
|
|
|
m_userDataView.color()->Change.connect([this]{ onStartTimer(); });
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-07 22:54:44 +08:00
|
|
|
void setCel(Doc* doc, Cel* cel) {
|
2015-08-21 22:01:49 +08:00
|
|
|
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
|
|
|
|
2020-09-11 02:49:08 +08:00
|
|
|
if (countCels() > 0 && m_cel) {
|
|
|
|
ui::Grid* mainGrid = g_window->propertiesGrid();
|
|
|
|
m_userDataView.configureAndSet(m_cel->data()->userData(), mainGrid);
|
|
|
|
}
|
|
|
|
else if (!m_cel)
|
|
|
|
m_userDataView.setVisible(false, false);
|
|
|
|
g_window->remapWindow();
|
|
|
|
manager()->invalidate();
|
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-08-25 23:31:00 +08:00
|
|
|
for (Cel* cel : sprite->uniqueCels(m_range.selectedFrames())) {
|
|
|
|
if (m_range.contains(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()) {
|
2016-10-24 05:46:21 +08:00
|
|
|
KeyScancode scancode = static_cast<KeyMessage*>(msg)->scancode();
|
|
|
|
if (scancode == kKeyEnter ||
|
|
|
|
scancode == kKeyEsc) {
|
2015-08-22 01:53:31 +08:00
|
|
|
onCommitChange();
|
|
|
|
closeWindow(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-08-21 19:35:29 +08:00
|
|
|
case kCloseMessage:
|
|
|
|
// Save changes before we close the window
|
2020-09-11 02:49:08 +08:00
|
|
|
if (m_cel)
|
|
|
|
save_window_pos(this, "CelProperties");
|
2015-08-21 22:01:49 +08:00
|
|
|
setCel(nullptr, nullptr);
|
2015-08-21 19:35:29 +08:00
|
|
|
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();
|
|
|
|
|
2017-04-06 03:01:18 +08:00
|
|
|
const int newOpacity = opacityValue();
|
2020-09-11 02:49:08 +08:00
|
|
|
const UserData newUserData= m_userDataView.userData();
|
2017-04-06 03:01:18 +08:00
|
|
|
const 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() ||
|
2020-09-11 02:49:08 +08:00
|
|
|
newUserData != m_cel->data()->userData()))) {
|
2015-08-21 19:35:29 +08:00
|
|
|
try {
|
|
|
|
ContextWriter writer(UIContext::instance());
|
2018-08-21 03:00:59 +08:00
|
|
|
Tx tx(writer.context(), "Set Cel Properties");
|
2015-08-21 19:35:29 +08:00
|
|
|
|
2018-07-07 21:07:21 +08:00
|
|
|
DocRange range;
|
2016-08-26 07:19:29 +08:00
|
|
|
if (m_range.enabled())
|
|
|
|
range = m_range;
|
|
|
|
else {
|
2018-07-07 21:07:21 +08:00
|
|
|
range.startRange(m_cel->layer(), m_cel->frame(), DocRange::kCels);
|
2016-08-26 07:19:29 +08:00
|
|
|
range.endRange(m_cel->layer(), m_cel->frame());
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite* sprite = m_document->sprite();
|
|
|
|
for (Cel* cel : sprite->uniqueCels(range.selectedFrames())) {
|
|
|
|
if (range.contains(cel->layer())) {
|
|
|
|
if (!cel->layer()->isBackground() && newOpacity != cel->opacity()) {
|
2018-08-21 03:00:59 +08:00
|
|
|
tx(new cmd::SetCelOpacity(cel, newOpacity));
|
2016-08-26 07:19:29 +08:00
|
|
|
}
|
2015-12-11 05:34:25 +08:00
|
|
|
|
2021-06-11 21:45:49 +08:00
|
|
|
if (newUserData != cel->data()->userData()) {
|
2020-09-11 02:49:08 +08:00
|
|
|
tx(new cmd::SetUserData(cel->data(), newUserData, m_document));
|
2015-12-23 00:18:08 +08:00
|
|
|
|
2016-08-26 07:19:29 +08:00
|
|
|
// Redraw timeline because the cel's user data/color
|
|
|
|
// might have changed.
|
2021-06-11 21:45:49 +08:00
|
|
|
if (newUserData.color() != cel->data()->userData().color()) {
|
|
|
|
App::instance()->timeline()->invalidate();
|
|
|
|
}
|
2015-12-23 00:18:08 +08:00
|
|
|
}
|
2015-08-21 22:01:49 +08:00
|
|
|
}
|
|
|
|
}
|
2015-08-21 19:35:29 +08:00
|
|
|
|
2018-08-21 03:00:59 +08:00
|
|
|
tx.commit();
|
2015-08-21 19:35:29 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 02:49:08 +08:00
|
|
|
void onToggleUserData() {
|
|
|
|
if (m_cel) {
|
|
|
|
m_userDataView.toggleVisibility();
|
|
|
|
g_window->remapWindow();
|
|
|
|
manager()->invalidate();
|
2015-12-11 05:34:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 19:35:29 +08:00
|
|
|
// ContextObserver impl
|
|
|
|
void onActiveSiteChange(const Site& site) override {
|
2020-11-06 01:25:45 +08:00
|
|
|
onCommitChange();
|
2015-08-21 19:35:29 +08:00
|
|
|
if (isVisible())
|
2018-07-07 22:54:44 +08:00
|
|
|
setCel(const_cast<Doc*>(site.document()),
|
2015-08-21 22:01:49 +08:00
|
|
|
const_cast<Cel*>(site.cel()));
|
|
|
|
else if (m_document)
|
|
|
|
setCel(nullptr, nullptr);
|
2015-08-21 19:35:29 +08:00
|
|
|
}
|
|
|
|
|
2018-07-07 08:06:03 +08:00
|
|
|
// DocObserver impl
|
2020-08-04 21:40:42 +08:00
|
|
|
void onBeforeRemoveCel(DocEvent& ev) override {
|
|
|
|
if (m_cel == ev.cel())
|
|
|
|
setCel(m_document, nullptr);
|
|
|
|
}
|
|
|
|
|
2018-07-07 08:06:03 +08:00
|
|
|
void onCelOpacityChange(DocEvent& ev) override {
|
2015-08-21 22:01:49 +08:00
|
|
|
if (m_cel == ev.cel())
|
|
|
|
updateFromCel();
|
2015-08-21 19:35:29 +08:00
|
|
|
}
|
|
|
|
|
2020-09-11 02:49:08 +08:00
|
|
|
void onUserDataChange(DocEvent& ev) override {
|
|
|
|
if (m_cel && m_cel->data() == ev.withUserData())
|
|
|
|
updateFromCel();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
if (m_cel) {
|
|
|
|
opacity()->setValue(m_cel->opacity());
|
2020-09-11 02:49:08 +08:00
|
|
|
color_t c = m_cel->data()->userData().color();
|
|
|
|
m_userDataView.color()->setColor(Color::fromRgb(rgba_getr(c), rgba_getg(c), rgba_getb(c), rgba_geta(c)));
|
|
|
|
m_userDataView.entry()->setText(m_cel->data()->userData().text());
|
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);
|
2020-09-11 02:49:08 +08:00
|
|
|
m_userDataView.setVisible(false, false);
|
2015-08-21 22:01:49 +08:00
|
|
|
}
|
2015-08-21 19:35:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer m_timer;
|
2018-07-07 22:54:44 +08:00
|
|
|
Doc* m_document;
|
2015-08-21 19:35:29 +08:00
|
|
|
Cel* m_cel;
|
2018-07-07 21:07:21 +08:00
|
|
|
DocRange m_range;
|
2015-08-21 19:35:29 +08:00
|
|
|
bool m_selfUpdate;
|
2020-09-11 02:49:08 +08:00
|
|
|
UserDataView m_userDataView;
|
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();
|
|
|
|
|
|
|
|
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()
|
2017-12-02 02:10:21 +08:00
|
|
|
: Command(CommandId::CelProperties(), CmdUIOnlyFlag)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|