mirror of
https://github.com/aseprite/aseprite.git
synced 2025-04-01 01:20:25 +00:00
Refactor removing duplicated code with new WindowWithHand
FilterWindow and DialogWindow (for scripts) share this code to enable the Hand tool in the active Editor.
This commit is contained in:
parent
699342fe9d
commit
4d7b28bfb6
@ -673,6 +673,7 @@ target_sources(app-lib PRIVATE
|
|||||||
ui/timeline/timeline.cpp
|
ui/timeline/timeline.cpp
|
||||||
ui/toolbar.cpp
|
ui/toolbar.cpp
|
||||||
ui/user_data_view.cpp
|
ui/user_data_view.cpp
|
||||||
|
ui/window_with_hand.cpp
|
||||||
ui/workspace.cpp
|
ui/workspace.cpp
|
||||||
ui/workspace_panel.cpp
|
ui/workspace_panel.cpp
|
||||||
ui/workspace_tabs.cpp
|
ui/workspace_tabs.cpp
|
||||||
|
@ -17,10 +17,6 @@
|
|||||||
#include "app/ini_file.h"
|
#include "app/ini_file.h"
|
||||||
#include "app/modules/gui.h"
|
#include "app/modules/gui.h"
|
||||||
#include "app/pref/preferences.h"
|
#include "app/pref/preferences.h"
|
||||||
#include "app/tools/tool_box.h"
|
|
||||||
#include "app/ui/editor/editor.h"
|
|
||||||
#include "app/ui/context_bar.h"
|
|
||||||
#include "app/ui/toolbar.h"
|
|
||||||
|
|
||||||
namespace app {
|
namespace app {
|
||||||
|
|
||||||
@ -32,7 +28,7 @@ FilterWindow::FilterWindow(const char* title, const char* cfgSection,
|
|||||||
WithChannels withChannels,
|
WithChannels withChannels,
|
||||||
WithTiled withTiled,
|
WithTiled withTiled,
|
||||||
TiledMode tiledMode)
|
TiledMode tiledMode)
|
||||||
: Window(WithTitleBar, title)
|
: WindowWithHand(WithTitleBar, title)
|
||||||
, m_cfgSection(cfgSection)
|
, m_cfgSection(cfgSection)
|
||||||
, m_filterMgr(filterMgr)
|
, m_filterMgr(filterMgr)
|
||||||
, m_hbox(HORIZONTAL)
|
, m_hbox(HORIZONTAL)
|
||||||
@ -46,8 +42,6 @@ FilterWindow::FilterWindow(const char* title, const char* cfgSection,
|
|||||||
, m_tiledCheck(withTiled == WithTiledCheckBox ?
|
, m_tiledCheck(withTiled == WithTiledCheckBox ?
|
||||||
new CheckBox(Strings::filters_tiled()) :
|
new CheckBox(Strings::filters_tiled()) :
|
||||||
nullptr)
|
nullptr)
|
||||||
, m_editor(nullptr)
|
|
||||||
, m_oldTool(nullptr)
|
|
||||||
{
|
{
|
||||||
m_okButton.processMnemonicFromText();
|
m_okButton.processMnemonicFromText();
|
||||||
m_cancelButton.processMnemonicFromText();
|
m_cancelButton.processMnemonicFromText();
|
||||||
@ -91,20 +85,12 @@ FilterWindow::FilterWindow(const char* title, const char* cfgSection,
|
|||||||
// OK is magnetic (the default button)
|
// OK is magnetic (the default button)
|
||||||
m_okButton.setFocusMagnet(true);
|
m_okButton.setFocusMagnet(true);
|
||||||
|
|
||||||
if (Editor::activeEditor()) {
|
// Enable the Hand tool in the active editor.
|
||||||
m_editor = Editor::activeEditor();
|
enableHandTool(true);
|
||||||
m_editor->add_observer(this);
|
|
||||||
m_oldTool = m_editor->getCurrentEditorTool();
|
|
||||||
tools::Tool* hand = App::instance()->toolBox()->getToolById(tools::WellKnownTools::Hand);
|
|
||||||
ToolBar::instance()->selectTool(hand);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FilterWindow::~FilterWindow()
|
FilterWindow::~FilterWindow()
|
||||||
{
|
{
|
||||||
if (m_oldTool)
|
|
||||||
ToolBar::instance()->selectTool(m_oldTool);
|
|
||||||
|
|
||||||
// Save window configuration
|
// Save window configuration
|
||||||
save_window_pos(this, m_cfgSection);
|
save_window_pos(this, m_cfgSection);
|
||||||
|
|
||||||
@ -113,9 +99,6 @@ FilterWindow::~FilterWindow()
|
|||||||
|
|
||||||
// Save cels target button
|
// Save cels target button
|
||||||
Preferences::instance().filters.celsTarget(m_targetButton.celsTarget());
|
Preferences::instance().filters.celsTarget(m_targetButton.celsTarget());
|
||||||
|
|
||||||
if (m_editor)
|
|
||||||
m_editor->remove_observer(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FilterWindow::doModal()
|
bool FilterWindow::doModal()
|
||||||
@ -150,18 +133,6 @@ bool FilterWindow::doModal()
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FilterWindow::onBroadcastMouseMessage(const gfx::Point& screenPos,
|
|
||||||
ui::WidgetsList& targets) {
|
|
||||||
// Add the Filter Window as receptor of mouse events.
|
|
||||||
targets.push_back(this);
|
|
||||||
// Also add the editor
|
|
||||||
if (m_editor)
|
|
||||||
targets.push_back(ui::View::getView(m_editor));
|
|
||||||
// and add the context bar.
|
|
||||||
if (App::instance()->contextBar())
|
|
||||||
targets.push_back(App::instance()->contextBar());
|
|
||||||
}
|
|
||||||
|
|
||||||
void FilterWindow::restartPreview()
|
void FilterWindow::restartPreview()
|
||||||
{
|
{
|
||||||
bool state = m_showPreview.isSelected();
|
bool state = m_showPreview.isSelected();
|
||||||
|
@ -11,21 +11,21 @@
|
|||||||
|
|
||||||
#include "app/commands/filters/filter_preview.h"
|
#include "app/commands/filters/filter_preview.h"
|
||||||
#include "app/commands/filters/filter_target_buttons.h"
|
#include "app/commands/filters/filter_target_buttons.h"
|
||||||
#include "app/ui/editor/editor.h"
|
#include "app/ui/window_with_hand.h"
|
||||||
#include "filters/tiled_mode.h"
|
#include "filters/tiled_mode.h"
|
||||||
#include "ui/box.h"
|
#include "ui/box.h"
|
||||||
#include "ui/button.h"
|
#include "ui/button.h"
|
||||||
#include "ui/window.h"
|
#include "ui/window.h"
|
||||||
|
|
||||||
namespace app {
|
namespace app {
|
||||||
|
class Editor;
|
||||||
class FilterManagerImpl;
|
class FilterManagerImpl;
|
||||||
|
|
||||||
using namespace filters;
|
using namespace filters;
|
||||||
|
|
||||||
// A generic window to show parameters for a Filter with integrated
|
// A generic window to show parameters for a Filter with integrated
|
||||||
// preview in the current editor.
|
// preview in the current editor.
|
||||||
class FilterWindow : public ui::Window,
|
class FilterWindow : public WindowWithHand {
|
||||||
public EditorObserver {
|
|
||||||
public:
|
public:
|
||||||
enum WithChannels { WithChannelsSelector, WithoutChannelsSelector };
|
enum WithChannels { WithChannelsSelector, WithoutChannelsSelector };
|
||||||
enum WithTiled { WithTiledCheckBox, WithoutTiledCheckBox };
|
enum WithTiled { WithTiledCheckBox, WithoutTiledCheckBox };
|
||||||
@ -51,9 +51,6 @@ namespace app {
|
|||||||
// which specified different targets for each matrix.
|
// which specified different targets for each matrix.
|
||||||
void setNewTarget(Target target);
|
void setNewTarget(Target target);
|
||||||
|
|
||||||
void onBroadcastMouseMessage(const gfx::Point& screenPos,
|
|
||||||
ui::WidgetsList& targets) override;
|
|
||||||
|
|
||||||
// Returns the container where derived classes should put controls.
|
// Returns the container where derived classes should put controls.
|
||||||
ui::Widget* getContainer() { return &m_container; }
|
ui::Widget* getContainer() { return &m_container; }
|
||||||
|
|
||||||
@ -87,8 +84,6 @@ namespace app {
|
|||||||
FilterTargetButtons m_targetButton;
|
FilterTargetButtons m_targetButton;
|
||||||
ui::CheckBox m_showPreview;
|
ui::CheckBox m_showPreview;
|
||||||
ui::CheckBox* m_tiledCheck;
|
ui::CheckBox* m_tiledCheck;
|
||||||
Editor* m_editor;
|
|
||||||
tools::Tool* m_oldTool;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace app
|
} // namespace app
|
||||||
|
@ -22,13 +22,11 @@
|
|||||||
#include "app/ui/button_set.h"
|
#include "app/ui/button_set.h"
|
||||||
#include "app/ui/color_button.h"
|
#include "app/ui/color_button.h"
|
||||||
#include "app/ui/color_shades.h"
|
#include "app/ui/color_shades.h"
|
||||||
|
#include "app/ui/editor/editor.h"
|
||||||
#include "app/ui/expr_entry.h"
|
#include "app/ui/expr_entry.h"
|
||||||
#include "app/ui/filename_field.h"
|
#include "app/ui/filename_field.h"
|
||||||
#include "app/ui/main_window.h"
|
#include "app/ui/main_window.h"
|
||||||
#include "app/ui/editor/editor.h"
|
#include "app/ui/window_with_hand.h"
|
||||||
#include "app/ui/toolbar.h"
|
|
||||||
#include "app/ui/context_bar.h"
|
|
||||||
#include "app/tools/tool_box.h"
|
|
||||||
#include "base/paths.h"
|
#include "base/paths.h"
|
||||||
#include "base/remove_from_container.h"
|
#include "base/remove_from_container.h"
|
||||||
#include "ui/box.h"
|
#include "ui/box.h"
|
||||||
@ -47,7 +45,6 @@
|
|||||||
#include "ui/slider.h"
|
#include "ui/slider.h"
|
||||||
#include "ui/system.h"
|
#include "ui/system.h"
|
||||||
#include "ui/view.h"
|
#include "ui/view.h"
|
||||||
#include "ui/window.h"
|
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
@ -63,24 +60,14 @@ using namespace ui;
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
class DialogWindow : public ui::Window,
|
class DialogWindow : public WindowWithHand {
|
||||||
public EditorObserver {
|
|
||||||
public:
|
public:
|
||||||
DialogWindow(Type type, const std::string& text)
|
DialogWindow(Type type, const std::string& text)
|
||||||
: Window(type, text)
|
: WindowWithHand(type, text)
|
||||||
, m_editor(nullptr)
|
|
||||||
, m_oldTool(nullptr)
|
|
||||||
, m_handTool(false)
|
, m_handTool(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
~DialogWindow() {
|
|
||||||
if (m_editor)
|
|
||||||
m_editor->remove_observer(this);
|
|
||||||
if (m_oldTool)
|
|
||||||
ToolBar::instance()->selectTool(m_oldTool);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enables the Hand tool in the active editor.
|
// Enables the Hand tool in the active editor.
|
||||||
void setHandTool(const bool flag) {
|
void setHandTool(const bool flag) {
|
||||||
m_handTool = flag;
|
m_handTool = flag;
|
||||||
@ -89,47 +76,16 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
void onOpen(Event& ev) override {
|
void onOpen(Event& ev) override {
|
||||||
if (m_handTool && Editor::activeEditor()) {
|
if (m_handTool && Editor::activeEditor()) {
|
||||||
m_editor = Editor::activeEditor();
|
enableHandTool(true);
|
||||||
m_editor->add_observer(this);
|
|
||||||
m_oldTool = m_editor->getCurrentEditorTool();
|
|
||||||
tools::Tool* hand = App::instance()->toolBox()->getToolById(tools::WellKnownTools::Hand);
|
|
||||||
ToolBar::instance()->selectTool(hand);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void onBeforeClose(CloseEvent& ev) override {
|
void onBeforeClose(CloseEvent& ev) override {
|
||||||
// unset references in case the same dialog is opened again
|
if (isHandToolEnabled())
|
||||||
if (m_editor) {
|
enableHandTool(false);
|
||||||
m_editor->remove_observer(this);
|
|
||||||
m_editor = nullptr;
|
|
||||||
}
|
|
||||||
if (m_oldTool) {
|
|
||||||
ToolBar::instance()->selectTool(m_oldTool);
|
|
||||||
m_oldTool = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void onBroadcastMouseMessage(const gfx::Point& screenPos, ui::WidgetsList& targets) override {
|
|
||||||
if (m_handTool) {
|
|
||||||
// Same impl as in FilterWindow::onBroadcastMouseMessage():
|
|
||||||
|
|
||||||
// Add this Window as receptor of mouse events.
|
|
||||||
targets.push_back(this);
|
|
||||||
// Add also the editor as receptor of mouse events.
|
|
||||||
if (m_editor)
|
|
||||||
targets.push_back(ui::View::getView(m_editor));
|
|
||||||
// and add the context bar.
|
|
||||||
if (App::instance()->contextBar())
|
|
||||||
targets.push_back(App::instance()->contextBar());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Window::onBroadcastMouseMessage(screenPos, targets);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Editor* m_editor;
|
|
||||||
tools::Tool* m_oldTool;
|
|
||||||
bool m_handTool;
|
bool m_handTool;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
70
src/app/ui/window_with_hand.cpp
Normal file
70
src/app/ui/window_with_hand.cpp
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
// Aseprite
|
||||||
|
// Copyright (C) 2024 Igara Studio S.A.
|
||||||
|
//
|
||||||
|
// This program is distributed under the terms of
|
||||||
|
// the End-User License Agreement for Aseprite.
|
||||||
|
|
||||||
|
#ifdef HAVE_CONFIG_H
|
||||||
|
#include "config.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "app/ui/window_with_hand.h"
|
||||||
|
|
||||||
|
#include "app/app.h"
|
||||||
|
#include "app/tools/tool_box.h"
|
||||||
|
#include "app/ui/context_bar.h"
|
||||||
|
#include "app/ui/editor/editor.h"
|
||||||
|
#include "app/ui/toolbar.h"
|
||||||
|
|
||||||
|
namespace app {
|
||||||
|
|
||||||
|
WindowWithHand::WindowWithHand(Type type, const std::string& text)
|
||||||
|
: Window(type, text)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
WindowWithHand::~WindowWithHand()
|
||||||
|
{
|
||||||
|
enableHandTool(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWithHand::enableHandTool(const bool state)
|
||||||
|
{
|
||||||
|
if (m_editor) {
|
||||||
|
m_editor->remove_observer(this);
|
||||||
|
m_editor = nullptr;
|
||||||
|
}
|
||||||
|
if (m_oldTool) {
|
||||||
|
ToolBar::instance()->selectTool(m_oldTool);
|
||||||
|
m_oldTool = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto* editor = Editor::activeEditor();
|
||||||
|
if (state && editor) {
|
||||||
|
m_editor = editor;
|
||||||
|
m_editor->add_observer(this);
|
||||||
|
m_oldTool = m_editor->getCurrentEditorTool();
|
||||||
|
tools::Tool* hand = App::instance()->toolBox()->getToolById(tools::WellKnownTools::Hand);
|
||||||
|
ToolBar::instance()->selectTool(hand);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWithHand::onBroadcastMouseMessage(const gfx::Point& screenPos,
|
||||||
|
ui::WidgetsList& targets)
|
||||||
|
{
|
||||||
|
if (m_editor) {
|
||||||
|
// Add this window as receptor of mouse events.
|
||||||
|
targets.push_back(this);
|
||||||
|
// Also add the editor
|
||||||
|
if (m_editor)
|
||||||
|
targets.push_back(ui::View::getView(m_editor));
|
||||||
|
// and add the context bar.
|
||||||
|
if (App::instance()->contextBar())
|
||||||
|
targets.push_back(App::instance()->contextBar());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Window::onBroadcastMouseMessage(screenPos, targets);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace app
|
43
src/app/ui/window_with_hand.h
Normal file
43
src/app/ui/window_with_hand.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// Aseprite
|
||||||
|
// Copyright (C) 2024 Igara Studio S.A.
|
||||||
|
//
|
||||||
|
// This program is distributed under the terms of
|
||||||
|
// the End-User License Agreement for Aseprite.
|
||||||
|
|
||||||
|
#ifndef APP_UI_WINDOW_WITH_HAND_H_INCLUDED
|
||||||
|
#define APP_UI_WINDOW_WITH_HAND_H_INCLUDED
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "app/ui/editor/editor_observer.h"
|
||||||
|
#include "ui/window.h"
|
||||||
|
|
||||||
|
namespace app {
|
||||||
|
namespace tools {
|
||||||
|
class Tool;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A Window that enables the possibility to scroll/zoom the active
|
||||||
|
// editor with the Hand tool.
|
||||||
|
class WindowWithHand : public ui::Window,
|
||||||
|
public EditorObserver {
|
||||||
|
public:
|
||||||
|
WindowWithHand(Type type, const std::string& text);
|
||||||
|
~WindowWithHand();
|
||||||
|
|
||||||
|
// Enables the Hand tool in the active editor.
|
||||||
|
void enableHandTool(bool state);
|
||||||
|
|
||||||
|
bool isHandToolEnabled() const { return m_editor != nullptr; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void onBroadcastMouseMessage(const gfx::Point& screenPos,
|
||||||
|
ui::WidgetsList& targets) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Editor* m_editor = nullptr;
|
||||||
|
tools::Tool* m_oldTool = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace app
|
||||||
|
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user