2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2025-01-07 07:21:51 +08:00
|
|
|
// Copyright (C) 2018-2025 Igara Studio S.A.
|
2018-03-30 03:13:10 +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.
|
2013-01-21 05:40:37 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2013-01-21 05:40:37 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
2013-01-21 05:40:37 +08:00
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
#include "app/ui/doc_view.h"
|
2013-01-21 05:40:37 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/app.h"
|
2015-02-23 08:18:53 +08:00
|
|
|
#include "app/app_menus.h"
|
2015-05-10 06:55:33 +08:00
|
|
|
#include "app/cmd/clear_mask.h"
|
|
|
|
#include "app/cmd/deselect_mask.h"
|
2016-05-04 23:32:39 +08:00
|
|
|
#include "app/cmd/trim_cel.h"
|
2015-02-20 09:14:06 +08:00
|
|
|
#include "app/commands/commands.h"
|
2015-04-15 20:58:41 +08:00
|
|
|
#include "app/console.h"
|
2015-05-10 06:55:33 +08:00
|
|
|
#include "app/context_access.h"
|
2018-07-15 09:47:03 +08:00
|
|
|
#include "app/doc_access.h"
|
2019-09-06 02:03:13 +08:00
|
|
|
#include "app/doc_event.h"
|
2017-10-18 05:00:45 +08:00
|
|
|
#include "app/i18n/strings.h"
|
2014-07-07 09:10:30 +08:00
|
|
|
#include "app/modules/palettes.h"
|
2015-12-23 04:49:21 +08:00
|
|
|
#include "app/pref/preferences.h"
|
2018-08-21 03:00:59 +08:00
|
|
|
#include "app/tx.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/editor/editor.h"
|
|
|
|
#include "app/ui/editor/editor_customization_delegate.h"
|
|
|
|
#include "app/ui/editor/editor_view.h"
|
2022-08-03 04:23:58 +08:00
|
|
|
#include "app/ui/editor/navigate_state.h"
|
2014-10-29 22:58:03 +08:00
|
|
|
#include "app/ui/keyboard_shortcuts.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/main_window.h"
|
2015-02-23 08:18:53 +08:00
|
|
|
#include "app/ui/status_bar.h"
|
2017-03-30 08:18:29 +08:00
|
|
|
#include "app/ui/timeline/timeline.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/workspace.h"
|
2015-02-20 09:14:06 +08:00
|
|
|
#include "app/ui_context.h"
|
2015-05-10 06:55:33 +08:00
|
|
|
#include "app/util/clipboard.h"
|
2025-03-08 05:03:49 +08:00
|
|
|
#include "app/util/slice_utils.h"
|
2016-11-02 06:14:05 +08:00
|
|
|
#include "base/fs.h"
|
2020-12-16 01:00:44 +08:00
|
|
|
#include "doc/color.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/layer.h"
|
2025-03-08 05:03:49 +08:00
|
|
|
#include "doc/slice.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/sprite.h"
|
2017-10-18 05:00:45 +08:00
|
|
|
#include "fmt/format.h"
|
2015-02-23 08:18:53 +08:00
|
|
|
#include "ui/alert.h"
|
2025-08-01 11:29:24 +08:00
|
|
|
#include "ui/display.h"
|
2015-02-23 08:18:53 +08:00
|
|
|
#include "ui/menu.h"
|
2013-01-21 05:40:37 +08:00
|
|
|
#include "ui/message.h"
|
2025-01-07 07:21:51 +08:00
|
|
|
#include "ui/shortcut.h"
|
2014-11-09 08:09:29 +08:00
|
|
|
#include "ui/system.h"
|
2013-01-21 05:40:37 +08:00
|
|
|
#include "ui/view.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
|
2015-04-15 20:58:41 +08:00
|
|
|
#include <typeinfo>
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2013-01-21 05:40:37 +08:00
|
|
|
|
|
|
|
using namespace ui;
|
|
|
|
|
2021-12-08 02:45:52 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Used to show a view temporarily (the one with the file to be
|
|
|
|
// closed) and restore the previous view. E.g. When we close the
|
|
|
|
// non-active sprite pressing the cross button in a sprite tab.
|
|
|
|
class SetRestoreDocView {
|
|
|
|
public:
|
|
|
|
SetRestoreDocView(UIContext* ctx, DocView* newView) : m_ctx(ctx), m_oldView(ctx->activeView())
|
|
|
|
{
|
|
|
|
if (newView != m_oldView)
|
|
|
|
m_ctx->setActiveView(newView);
|
|
|
|
else
|
|
|
|
m_oldView = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
~SetRestoreDocView()
|
|
|
|
{
|
|
|
|
if (m_oldView)
|
|
|
|
m_ctx->setActiveView(m_oldView);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
UIContext* m_ctx;
|
|
|
|
DocView* m_oldView;
|
|
|
|
};
|
|
|
|
|
2013-01-21 05:40:37 +08:00
|
|
|
class AppEditor : public Editor,
|
|
|
|
public EditorObserver,
|
2015-02-10 23:59:43 +08:00
|
|
|
public EditorCustomizationDelegate {
|
2013-01-21 05:40:37 +08:00
|
|
|
public:
|
2018-07-15 10:24:49 +08:00
|
|
|
AppEditor(Doc* document, DocViewPreviewDelegate* previewDelegate)
|
2016-02-12 08:09:31 +08:00
|
|
|
: Editor(document)
|
|
|
|
, m_previewDelegate(previewDelegate)
|
|
|
|
{
|
2016-09-14 02:02:00 +08:00
|
|
|
add_observer(this);
|
2013-01-21 05:40:37 +08:00
|
|
|
setCustomizationDelegate(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AppEditor()
|
|
|
|
{
|
2016-09-14 02:02:00 +08:00
|
|
|
remove_observer(this);
|
2013-01-21 05:40:37 +08:00
|
|
|
setCustomizationDelegate(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditorObserver implementation
|
2016-02-12 08:09:31 +08:00
|
|
|
void dispose() override { m_previewDelegate->onDisposeOtherEditor(this); }
|
2013-01-21 05:40:37 +08:00
|
|
|
|
2014-08-15 10:07:47 +08:00
|
|
|
void onScrollChanged(Editor* editor) override
|
|
|
|
{
|
2016-02-12 08:09:31 +08:00
|
|
|
m_previewDelegate->onScrollOtherEditor(this);
|
2015-08-15 06:46:48 +08:00
|
|
|
|
|
|
|
if (isActive())
|
|
|
|
StatusBar::instance()->updateFromEditor(this);
|
2013-01-21 05:40:37 +08:00
|
|
|
}
|
|
|
|
|
2014-08-28 20:36:34 +08:00
|
|
|
void onAfterFrameChanged(Editor* editor) override
|
|
|
|
{
|
2016-02-12 08:09:31 +08:00
|
|
|
m_previewDelegate->onPreviewOtherEditor(this);
|
2014-07-07 09:10:30 +08:00
|
|
|
|
2015-05-19 08:34:30 +08:00
|
|
|
if (isActive())
|
2018-03-30 03:13:10 +08:00
|
|
|
set_current_palette(editor->sprite()->palette(editor->frame()), false);
|
2013-03-15 08:40:37 +08:00
|
|
|
}
|
|
|
|
|
2015-04-27 02:59:28 +08:00
|
|
|
void onAfterLayerChanged(Editor* editor) override
|
|
|
|
{
|
2016-02-12 08:09:31 +08:00
|
|
|
m_previewDelegate->onPreviewOtherEditor(this);
|
2015-04-27 02:59:28 +08:00
|
|
|
}
|
|
|
|
|
2013-01-21 05:40:37 +08:00
|
|
|
// EditorCustomizationDelegate implementation
|
2014-08-15 10:07:47 +08:00
|
|
|
tools::Tool* getQuickTool(tools::Tool* currentTool) override
|
|
|
|
{
|
2014-10-29 22:58:03 +08:00
|
|
|
return KeyboardShortcuts::instance()->getCurrentQuicktool(currentTool);
|
2013-01-21 05:40:37 +08:00
|
|
|
}
|
|
|
|
|
2015-10-01 03:34:43 +08:00
|
|
|
KeyAction getPressedKeyAction(KeyContext context) override
|
|
|
|
{
|
2015-08-26 00:29:19 +08:00
|
|
|
return KeyboardShortcuts::instance()->getCurrentActionModifiers(context);
|
2014-11-17 05:33:31 +08:00
|
|
|
}
|
|
|
|
|
2017-03-30 08:18:29 +08:00
|
|
|
TagProvider* getTagProvider() override { return App::instance()->mainWindow()->getTimeline(); }
|
|
|
|
|
2014-11-09 08:09:29 +08:00
|
|
|
protected:
|
|
|
|
bool onProcessMessage(Message* msg) override
|
|
|
|
{
|
|
|
|
switch (msg->type()) {
|
|
|
|
case kKeyDownMessage:
|
|
|
|
case kKeyUpMessage:
|
|
|
|
if (static_cast<KeyMessage*>(msg)->repeat() == 0) {
|
2018-07-24 02:15:04 +08:00
|
|
|
KeyboardShortcuts* keys = KeyboardShortcuts::instance();
|
2020-09-19 06:29:43 +08:00
|
|
|
KeyPtr lmb = keys->action(KeyAction::LeftMouseButton, KeyContext::Any);
|
|
|
|
KeyPtr rmb = keys->action(KeyAction::RightMouseButton, KeyContext::Any);
|
2014-11-09 08:09:29 +08:00
|
|
|
|
|
|
|
// Convert action keys into mouse messages.
|
2025-01-07 07:21:51 +08:00
|
|
|
if (lmb->isPressed(msg) || rmb->isPressed(msg)) {
|
2014-11-09 08:09:29 +08:00
|
|
|
MouseMessage mouseMsg(
|
|
|
|
(msg->type() == kKeyDownMessage ? kMouseDownMessage : kMouseUpMessage),
|
2016-04-21 09:33:10 +08:00
|
|
|
PointerType::Unknown,
|
2025-01-07 07:21:51 +08:00
|
|
|
(lmb->isPressed(msg) ? kButtonLeft : kButtonRight),
|
2015-10-15 03:42:49 +08:00
|
|
|
msg->modifiers(),
|
2021-03-03 00:50:49 +08:00
|
|
|
mousePosInDisplay());
|
2014-11-09 08:09:29 +08:00
|
|
|
|
|
|
|
sendMessage(&mouseMsg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-04-15 20:58:41 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
return Editor::onProcessMessage(msg);
|
|
|
|
}
|
2015-04-15 21:06:55 +08:00
|
|
|
catch (const std::exception& ex) {
|
2023-12-30 00:18:09 +08:00
|
|
|
showUnhandledException(ex, msg);
|
2015-04-15 20:58:41 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-09 08:09:29 +08:00
|
|
|
}
|
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
private:
|
2018-07-15 10:24:49 +08:00
|
|
|
DocViewPreviewDelegate* m_previewDelegate;
|
2013-01-21 05:40:37 +08:00
|
|
|
};
|
|
|
|
|
2017-03-30 08:18:29 +08:00
|
|
|
class PreviewEditor : public Editor,
|
|
|
|
public EditorCustomizationDelegate {
|
2015-02-10 23:59:43 +08:00
|
|
|
public:
|
2018-07-07 22:54:44 +08:00
|
|
|
PreviewEditor(Doc* document)
|
2022-08-03 04:23:58 +08:00
|
|
|
: Editor(document,
|
|
|
|
Editor::kShowOutside, // Don't show grid/mask in preview preview
|
|
|
|
std::make_shared<NavigateState>())
|
|
|
|
{
|
2017-03-30 08:18:29 +08:00
|
|
|
setCustomizationDelegate(this);
|
|
|
|
}
|
|
|
|
|
2017-04-15 02:28:46 +08:00
|
|
|
~PreviewEditor()
|
|
|
|
{
|
|
|
|
// As we are destroying this instance, we have to remove it as the
|
|
|
|
// customization delegate. Editor::~Editor() will call
|
|
|
|
// setCustomizationDelegate(nullptr) too which triggers a
|
|
|
|
// EditorCustomizationDelegate::dispose() if the customization
|
|
|
|
// isn't nullptr.
|
|
|
|
setCustomizationDelegate(nullptr);
|
2017-04-15 02:21:17 +08:00
|
|
|
}
|
|
|
|
|
2017-03-30 08:18:29 +08:00
|
|
|
// EditorCustomizationDelegate implementation
|
|
|
|
void dispose() override
|
|
|
|
{
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
tools::Tool* getQuickTool(tools::Tool* currentTool) override { return nullptr; }
|
|
|
|
|
|
|
|
KeyAction getPressedKeyAction(KeyContext context) override { return KeyAction::None; }
|
|
|
|
|
|
|
|
TagProvider* getTagProvider() override { return App::instance()->mainWindow()->getTimeline(); }
|
2015-02-10 23:59:43 +08:00
|
|
|
};
|
|
|
|
|
2021-12-08 02:45:52 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
DocView::DocView(Doc* document, Type type, DocViewPreviewDelegate* previewDelegate)
|
2015-06-24 01:00:00 +08:00
|
|
|
: Box(VERTICAL)
|
2015-04-07 03:12:28 +08:00
|
|
|
, m_type(type)
|
2013-01-21 05:40:37 +08:00
|
|
|
, m_document(document)
|
|
|
|
, m_view(
|
|
|
|
new EditorView(type == Normal ? EditorView::CurrentEditorMode : EditorView::AlwaysSelected))
|
2016-02-12 08:09:31 +08:00
|
|
|
, m_previewDelegate(previewDelegate)
|
|
|
|
, m_editor((type == Normal ? (Editor*)new AppEditor(document, previewDelegate) :
|
|
|
|
(Editor*)new PreviewEditor(document)))
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
|
|
|
addChild(m_view);
|
|
|
|
|
|
|
|
m_view->attachToView(m_editor);
|
|
|
|
m_view->setExpansive(true);
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
m_editor->setDocView(this);
|
2016-09-14 02:02:00 +08:00
|
|
|
m_document->add_observer(this);
|
2013-01-21 05:40:37 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
DocView::~DocView()
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
2016-09-14 02:02:00 +08:00
|
|
|
m_document->remove_observer(this);
|
2013-01-21 05:40:37 +08:00
|
|
|
delete m_editor;
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::getSite(Site* site) const
|
2013-03-12 07:29:45 +08:00
|
|
|
{
|
2015-04-21 03:27:09 +08:00
|
|
|
m_editor->getSite(site);
|
2013-03-12 07:29:45 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
std::string DocView::getTabText()
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
2015-02-20 00:13:25 +08:00
|
|
|
return m_document->name();
|
2013-01-21 05:40:37 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
TabIcon DocView::getTabIcon()
|
2015-02-20 08:44:22 +08:00
|
|
|
{
|
|
|
|
return TabIcon::NONE;
|
|
|
|
}
|
|
|
|
|
2020-12-16 01:00:44 +08:00
|
|
|
gfx::Color DocView::getTabColor()
|
|
|
|
{
|
|
|
|
color_t c = m_editor->sprite()->userData().color();
|
|
|
|
return gfx::rgba(doc::rgba_getr(c), doc::rgba_getg(c), doc::rgba_getb(c), doc::rgba_geta(c));
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
WorkspaceView* DocView::cloneWorkspaceView()
|
2013-03-28 08:19:35 +08:00
|
|
|
{
|
2018-07-15 10:24:49 +08:00
|
|
|
return new DocView(m_document, Normal, m_previewDelegate);
|
2013-03-28 08:19:35 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onWorkspaceViewSelected()
|
2014-05-09 12:01:59 +08:00
|
|
|
{
|
2021-01-16 01:46:45 +08:00
|
|
|
if (auto statusBar = StatusBar::instance())
|
|
|
|
statusBar->showDefaultText(m_document);
|
2014-05-09 12:01:59 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onClonedFrom(WorkspaceView* from)
|
2013-03-28 08:19:35 +08:00
|
|
|
{
|
2016-02-13 12:33:43 +08:00
|
|
|
Editor* newEditor = this->editor();
|
2018-07-15 10:24:49 +08:00
|
|
|
Editor* srcEditor = static_cast<DocView*>(from)->editor();
|
2013-03-28 08:19:35 +08:00
|
|
|
|
2014-07-30 12:28:15 +08:00
|
|
|
newEditor->setLayer(srcEditor->layer());
|
|
|
|
newEditor->setFrame(srcEditor->frame());
|
|
|
|
newEditor->setZoom(srcEditor->zoom());
|
2013-03-28 08:19:35 +08:00
|
|
|
|
Refactor several "getNoun()" getters to "noun()"
This is a work-in-progress to create a consistent API and finally
separate the whole Aseprite base/gfx/ui libs into a reusable C++ library.
Classes:
app::IFileItem, app::AppMenuItem, app::skin::SkinPart,
gfx::Rect, gfx::Border, she::FileDialog,
ui::IButtonIcon, ui::Graphics, ui::Overlay, ui::Widget,
ui::ScrollableViewDelegate, and UI events
2015-12-05 01:39:04 +08:00
|
|
|
View::getView(newEditor)->setViewScroll(View::getView(srcEditor)->viewScroll());
|
2013-03-28 08:19:35 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
bool DocView::onCloseView(Workspace* workspace, bool quitting)
|
2015-02-20 09:14:06 +08:00
|
|
|
{
|
2015-08-14 04:25:39 +08:00
|
|
|
if (m_editor->isMovingPixels())
|
2015-08-15 00:06:26 +08:00
|
|
|
m_editor->dropMovingPixels();
|
2015-08-14 04:25:39 +08:00
|
|
|
|
2015-04-06 23:22:20 +08:00
|
|
|
// If there is another view for this document, just close the view.
|
|
|
|
for (auto view : *workspace) {
|
2018-07-15 10:24:49 +08:00
|
|
|
DocView* docView = dynamic_cast<DocView*>(view);
|
2016-02-13 12:33:43 +08:00
|
|
|
if (docView && docView != this && docView->document() == document()) {
|
2015-04-06 23:22:20 +08:00
|
|
|
workspace->removeView(this);
|
|
|
|
delete this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:52:04 +08:00
|
|
|
UIContext* ctx = UIContext::instance();
|
2021-12-08 02:45:52 +08:00
|
|
|
SetRestoreDocView restoreView(ctx, this);
|
2015-02-23 08:18:53 +08:00
|
|
|
bool save_it;
|
|
|
|
bool try_again = true;
|
|
|
|
|
|
|
|
while (try_again) {
|
|
|
|
// This flag indicates if we have to sabe the sprite before to destroy it
|
|
|
|
save_it = false;
|
2021-12-08 02:45:52 +08:00
|
|
|
|
|
|
|
// See if the sprite has changes
|
|
|
|
while (m_document->isModified()) {
|
2025-08-01 11:29:24 +08:00
|
|
|
if (quitting) {
|
|
|
|
// Make sure the window is active so we can see the message when we close the app.
|
|
|
|
display()->nativeWindow()->activate();
|
|
|
|
}
|
|
|
|
|
2021-12-08 02:45:52 +08:00
|
|
|
// ask what want to do the user with the changes in the sprite
|
2024-06-21 07:14:29 +08:00
|
|
|
int ret = Alert::show(Strings::alerts_save_sprite_changes(
|
2021-12-08 02:45:52 +08:00
|
|
|
m_document->name(),
|
|
|
|
(quitting ? Strings::alerts_save_sprite_changes_quitting() :
|
|
|
|
Strings::alerts_save_sprite_changes_closing())));
|
|
|
|
|
|
|
|
if (ret == 1) {
|
|
|
|
// "save": save the changes
|
|
|
|
save_it = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ret != 2) {
|
|
|
|
// "cancel" or "ESC" */
|
|
|
|
return false; // we back doing nothing
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// "discard"
|
|
|
|
break;
|
2015-02-23 08:18:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does we need to save the sprite?
|
|
|
|
if (save_it) {
|
2015-02-23 21:52:04 +08:00
|
|
|
ctx->updateFlags();
|
|
|
|
|
2017-12-02 02:10:21 +08:00
|
|
|
Command* save_command = Commands::instance()->byId(CommandId::SaveFile());
|
2015-02-23 08:18:53 +08:00
|
|
|
ctx->executeCommand(save_command);
|
|
|
|
|
|
|
|
try_again = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
try_again = false;
|
|
|
|
}
|
|
|
|
|
2015-04-23 03:42:55 +08:00
|
|
|
try {
|
|
|
|
// Destroy the sprite (locking it as writer)
|
|
|
|
DocDestroyer destroyer(static_cast<app::Context*>(m_document->context()), m_document, 500);
|
2015-02-23 08:18:53 +08:00
|
|
|
|
2020-05-09 04:39:55 +08:00
|
|
|
StatusBar::instance()->setStatusText(0, fmt::format("Sprite '{}' closed.", m_document->name()));
|
2015-02-23 08:18:53 +08:00
|
|
|
|
2019-05-28 00:51:41 +08:00
|
|
|
// Just close the document (so we can reopen it with
|
|
|
|
// ReopenClosedFile command).
|
|
|
|
destroyer.closeDocument();
|
2015-02-23 08:18:53 +08:00
|
|
|
|
2015-04-23 03:42:55 +08:00
|
|
|
// At this point the view is already destroyed
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-15 09:47:03 +08:00
|
|
|
catch (const LockedDocException& ex) {
|
2015-04-23 03:42:55 +08:00
|
|
|
Console::showException(ex);
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-23 08:18:53 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onTabPopup(Workspace* workspace)
|
2015-02-23 08:18:53 +08:00
|
|
|
{
|
|
|
|
Menu* menu = AppMenus::instance()->getDocumentTabPopupMenu();
|
|
|
|
if (!menu)
|
|
|
|
return;
|
|
|
|
|
2015-02-23 21:52:04 +08:00
|
|
|
UIContext* ctx = UIContext::instance();
|
|
|
|
ctx->setActiveView(this);
|
|
|
|
ctx->updateFlags();
|
2015-02-20 09:14:06 +08:00
|
|
|
|
2021-03-20 05:57:56 +08:00
|
|
|
menu->showPopup(mousePosInDisplay(), display());
|
2015-02-20 09:14:06 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
bool DocView::onProcessMessage(Message* msg)
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
2013-07-29 08:17:07 +08:00
|
|
|
switch (msg->type()) {
|
2013-04-05 08:53:29 +08:00
|
|
|
case kFocusEnterMessage:
|
2018-12-13 00:25:12 +08:00
|
|
|
if (msg->recipient() != m_editor)
|
|
|
|
m_editor->requestFocus();
|
2013-01-21 05:40:37 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Box::onProcessMessage(msg);
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onGeneralUpdate(DocEvent& ev)
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
|
|
|
if (m_editor->isVisible())
|
2020-01-14 02:01:45 +08:00
|
|
|
m_editor->updateEditor(true);
|
2013-01-21 05:40:37 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onSpritePixelsModified(DocEvent& ev)
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
2015-08-13 04:32:17 +08:00
|
|
|
if (m_editor->isVisible() && m_editor->frame() == ev.frame())
|
2013-01-21 05:40:37 +08:00
|
|
|
m_editor->drawSpriteClipped(ev.region());
|
|
|
|
}
|
2013-03-12 07:29:45 +08:00
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onLayerMergedDown(DocEvent& ev)
|
2013-03-12 07:29:45 +08:00
|
|
|
{
|
|
|
|
m_editor->setLayer(ev.targetLayer());
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onAddLayer(DocEvent& ev)
|
2013-03-12 07:29:45 +08:00
|
|
|
{
|
2022-10-20 23:31:22 +08:00
|
|
|
if (m_editor->isActive()) {
|
2013-03-12 07:29:45 +08:00
|
|
|
ASSERT(ev.layer() != NULL);
|
|
|
|
m_editor->setLayer(ev.layer());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onAddFrame(DocEvent& ev)
|
2013-03-12 07:29:45 +08:00
|
|
|
{
|
2022-10-20 23:31:22 +08:00
|
|
|
if (m_editor->isActive())
|
2013-03-12 07:29:45 +08:00
|
|
|
m_editor->setFrame(ev.frame());
|
2014-07-30 12:28:15 +08:00
|
|
|
else if (m_editor->frame() > ev.frame())
|
2014-12-29 07:39:11 +08:00
|
|
|
m_editor->setFrame(m_editor->frame() + 1);
|
2013-03-12 07:29:45 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onRemoveFrame(DocEvent& ev)
|
2013-03-12 07:29:45 +08:00
|
|
|
{
|
|
|
|
// Adjust current frame of all editors that are in a frame more
|
|
|
|
// advanced that the removed one.
|
2014-07-30 12:28:15 +08:00
|
|
|
if (m_editor->frame() > ev.frame()) {
|
2014-12-29 07:39:11 +08:00
|
|
|
m_editor->setFrame(m_editor->frame() - 1);
|
2013-03-12 07:29:45 +08:00
|
|
|
}
|
|
|
|
// If the editor was in the previous "last frame" (current value of
|
2014-07-30 12:28:15 +08:00
|
|
|
// totalFrames()), we've to adjust it to the new last frame
|
|
|
|
// (lastFrame())
|
|
|
|
else if (m_editor->frame() >= m_editor->sprite()->totalFrames()) {
|
|
|
|
m_editor->setFrame(m_editor->sprite()->lastFrame());
|
2013-03-12 07:29:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 05:17:48 +08:00
|
|
|
void DocView::onTagChange(DocEvent& ev)
|
|
|
|
{
|
|
|
|
if (m_previewDelegate)
|
|
|
|
m_previewDelegate->onTagChangeEditor(m_editor, ev);
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onAddCel(DocEvent& ev)
|
2015-12-12 01:27:30 +08:00
|
|
|
{
|
|
|
|
UIContext::instance()->notifyActiveSiteChanged();
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:40:42 +08:00
|
|
|
void DocView::onAfterRemoveCel(DocEvent& ev)
|
2015-12-12 01:27:30 +08:00
|
|
|
{
|
2018-09-18 20:37:00 +08:00
|
|
|
// This can happen when we apply a filter that clear the whole cel
|
|
|
|
// and then the cel is removed in a background/job
|
|
|
|
// thread. (e.g. applying a convolution matrix)
|
|
|
|
if (!ui::is_ui_thread())
|
|
|
|
return;
|
|
|
|
|
2015-12-12 01:27:30 +08:00
|
|
|
UIContext::instance()->notifyActiveSiteChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onTotalFramesChanged(DocEvent& ev)
|
2013-03-12 07:29:45 +08:00
|
|
|
{
|
2014-07-30 12:28:15 +08:00
|
|
|
if (m_editor->frame() >= m_editor->sprite()->totalFrames()) {
|
|
|
|
m_editor->setFrame(m_editor->sprite()->lastFrame());
|
2013-03-12 07:29:45 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onLayerRestacked(DocEvent& ev)
|
2014-02-03 10:57:30 +08:00
|
|
|
{
|
2024-02-27 00:11:26 +08:00
|
|
|
if (hasContentInActiveFrame(ev.layer()))
|
|
|
|
m_editor->invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocView::onAfterLayerVisibilityChange(DocEvent& ev)
|
|
|
|
{
|
|
|
|
// If there is no cel for this layer in the current frame, there is
|
|
|
|
// no need to redraw the editor
|
|
|
|
if (hasContentInActiveFrame(ev.layer()))
|
|
|
|
m_editor->invalidate();
|
2014-02-03 10:57:30 +08:00
|
|
|
}
|
|
|
|
|
2019-03-30 02:57:10 +08:00
|
|
|
void DocView::onTilesetChanged(DocEvent& ev)
|
|
|
|
{
|
2019-07-30 02:12:23 +08:00
|
|
|
// This can happen when a filter is applied to each tile in a
|
|
|
|
// background thread.
|
|
|
|
if (!ui::is_ui_thread())
|
|
|
|
return;
|
|
|
|
|
2019-03-30 02:57:10 +08:00
|
|
|
m_editor->invalidate();
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onNewInputPriority(InputChainElement* element, const ui::Message* msg)
|
2015-05-10 06:55:33 +08:00
|
|
|
{
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
bool DocView::onCanCut(Context* ctx)
|
2015-05-10 06:55:33 +08:00
|
|
|
{
|
2016-05-20 00:22:58 +08:00
|
|
|
if (ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable | ContextFlags::ActiveLayerIsVisible |
|
|
|
|
ContextFlags::ActiveLayerIsEditable | ContextFlags::HasVisibleMask |
|
2018-06-30 03:36:01 +08:00
|
|
|
ContextFlags::HasActiveImage) &&
|
|
|
|
!ctx->checkFlags(ContextFlags::ActiveLayerIsReference))
|
2016-05-20 00:22:58 +08:00
|
|
|
return true;
|
|
|
|
else if (m_editor->isMovingPixels())
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2015-05-10 06:55:33 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
bool DocView::onCanCopy(Context* ctx)
|
2015-05-10 06:55:33 +08:00
|
|
|
{
|
2015-08-14 04:25:39 +08:00
|
|
|
if (ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable | ContextFlags::ActiveLayerIsVisible |
|
|
|
|
ContextFlags::HasVisibleMask | ContextFlags::HasActiveImage) &&
|
2018-06-30 03:36:01 +08:00
|
|
|
!ctx->checkFlags(ContextFlags::ActiveLayerIsReference))
|
2015-08-14 04:25:39 +08:00
|
|
|
return true;
|
|
|
|
else if (m_editor->isMovingPixels())
|
|
|
|
return true;
|
2025-03-08 05:03:49 +08:00
|
|
|
else if (m_editor->hasSelectedSlices())
|
|
|
|
return true;
|
2015-08-14 04:25:39 +08:00
|
|
|
else
|
|
|
|
return false;
|
2015-05-10 06:55:33 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
bool DocView::onCanPaste(Context* ctx)
|
2015-05-10 06:55:33 +08:00
|
|
|
{
|
2020-10-03 06:03:53 +08:00
|
|
|
if (ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable | ContextFlags::ActiveLayerIsVisible |
|
|
|
|
ContextFlags::ActiveLayerIsEditable | ContextFlags::ActiveLayerIsImage) &&
|
|
|
|
!ctx->checkFlags(ContextFlags::ActiveLayerIsReference)) {
|
|
|
|
auto format = ctx->clipboard()->format();
|
|
|
|
if (format == ClipboardFormat::Image) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (format == ClipboardFormat::Tilemap &&
|
|
|
|
ctx->checkFlags(ContextFlags::ActiveLayerIsTilemap)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2025-03-08 05:03:49 +08:00
|
|
|
|
|
|
|
if (ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable) &&
|
|
|
|
ctx->clipboard()->format() == ClipboardFormat::Slices) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-03 06:03:53 +08:00
|
|
|
return false;
|
2015-05-10 06:55:33 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
bool DocView::onCanClear(Context* ctx)
|
2015-05-10 06:55:33 +08:00
|
|
|
{
|
2015-08-14 04:25:39 +08:00
|
|
|
if (ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable | ContextFlags::ActiveLayerIsVisible |
|
|
|
|
ContextFlags::ActiveLayerIsEditable | ContextFlags::ActiveLayerIsImage) &&
|
2018-06-30 03:36:01 +08:00
|
|
|
!ctx->checkFlags(ContextFlags::ActiveLayerIsReference)) {
|
2015-08-14 04:25:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (m_editor->isMovingPixels()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2015-05-10 06:55:33 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
bool DocView::onCut(Context* ctx)
|
2015-05-10 06:55:33 +08:00
|
|
|
{
|
|
|
|
ContextWriter writer(ctx);
|
2020-09-25 22:13:52 +08:00
|
|
|
ctx->clipboard()->cut(writer);
|
2015-05-10 06:55:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
bool DocView::onCopy(Context* ctx)
|
2015-05-10 06:55:33 +08:00
|
|
|
{
|
|
|
|
const ContextReader reader(ctx);
|
2023-05-31 23:02:33 +08:00
|
|
|
if (reader.site().document() &&
|
|
|
|
static_cast<const Doc*>(reader.site().document())->isMaskVisible() && reader.site().image()) {
|
2020-09-25 22:13:52 +08:00
|
|
|
ctx->clipboard()->copy(reader);
|
2015-05-10 06:55:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
2025-03-08 05:03:49 +08:00
|
|
|
|
|
|
|
std::vector<Slice*> selectedSlices = get_selected_slices(reader.site());
|
|
|
|
if (!selectedSlices.empty()) {
|
|
|
|
ctx->clipboard()->copySlices(selectedSlices);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-05-10 06:55:33 +08:00
|
|
|
}
|
|
|
|
|
2024-09-05 04:37:24 +08:00
|
|
|
bool DocView::onPaste(Context* ctx, const gfx::Point* position)
|
2015-05-10 06:55:33 +08:00
|
|
|
{
|
2020-09-25 22:13:52 +08:00
|
|
|
auto clipboard = ctx->clipboard();
|
2020-10-03 06:03:53 +08:00
|
|
|
if (clipboard->format() == ClipboardFormat::Image ||
|
2025-03-08 05:03:49 +08:00
|
|
|
clipboard->format() == ClipboardFormat::Tilemap ||
|
|
|
|
clipboard->format() == ClipboardFormat::Slices) {
|
2024-09-05 04:37:24 +08:00
|
|
|
clipboard->paste(ctx, true, position);
|
2015-05-10 06:55:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
bool DocView::onClear(Context* ctx)
|
2015-05-10 06:55:33 +08:00
|
|
|
{
|
2019-05-07 21:28:37 +08:00
|
|
|
// First we check if there is a selected slice, so we'll delete
|
|
|
|
// those slices.
|
|
|
|
Site site = ctx->activeSite();
|
|
|
|
if (!site.selectedSlices().empty()) {
|
|
|
|
Command* removeSlices = Commands::instance()->byId(CommandId::RemoveSlice());
|
|
|
|
ctx->executeCommand(removeSlices);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In other case we delete the mask or the cel.
|
2015-05-10 06:55:33 +08:00
|
|
|
ContextWriter writer(ctx);
|
2019-09-06 02:03:13 +08:00
|
|
|
Doc* document = site.document();
|
2015-05-10 06:55:33 +08:00
|
|
|
bool visibleMask = document->isMaskVisible();
|
|
|
|
|
2023-05-31 23:02:33 +08:00
|
|
|
CelList cels = site.selectedUniqueCelsToEditPixels();
|
2019-09-06 02:03:13 +08:00
|
|
|
if (cels.empty()) // No cels to modify
|
2015-05-10 06:55:33 +08:00
|
|
|
return false;
|
|
|
|
|
2019-03-30 02:57:10 +08:00
|
|
|
// TODO This code is similar to clipboard::cut()
|
2015-05-10 06:55:33 +08:00
|
|
|
{
|
2023-12-13 08:34:30 +08:00
|
|
|
Tx tx(writer, "Clear");
|
2019-09-06 02:03:13 +08:00
|
|
|
const bool deselectMask = (visibleMask &&
|
|
|
|
!Preferences::instance().selection.keepSelectionAfterClear());
|
2015-12-23 04:49:21 +08:00
|
|
|
|
2021-02-23 03:39:40 +08:00
|
|
|
ctx->clipboard()->clearMaskFromCels(tx, document, site, cels, deselectMask);
|
2015-12-23 04:49:21 +08:00
|
|
|
|
2018-08-21 03:00:59 +08:00
|
|
|
tx.commit();
|
2015-05-10 06:55:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (visibleMask)
|
|
|
|
document->generateMaskBoundaries();
|
|
|
|
|
|
|
|
document->notifyGeneralUpdate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void DocView::onCancel(Context* ctx)
|
2015-05-10 06:55:33 +08:00
|
|
|
{
|
2019-05-03 03:26:13 +08:00
|
|
|
if (m_editor)
|
|
|
|
m_editor->cancelSelections();
|
|
|
|
|
2015-05-10 06:55:33 +08:00
|
|
|
// Deselect mask
|
|
|
|
if (ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable | ContextFlags::HasVisibleMask)) {
|
2017-12-02 02:10:21 +08:00
|
|
|
Command* deselectMask = Commands::instance()->byId(CommandId::DeselectMask());
|
2015-05-10 06:55:33 +08:00
|
|
|
ctx->executeCommand(deselectMask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 00:11:26 +08:00
|
|
|
bool DocView::hasContentInActiveFrame(const doc::Layer* layer) const
|
|
|
|
{
|
|
|
|
if (!layer)
|
|
|
|
return false;
|
|
|
|
else if (layer->cel(m_editor->frame()))
|
|
|
|
return true;
|
|
|
|
else if (layer->isGroup()) {
|
|
|
|
for (const doc::Layer* child : static_cast<const doc::LayerGroup*>(layer)->layers()) {
|
|
|
|
if (hasContentInActiveFrame(child))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
} // namespace app
|