1// Aseprite
2// Copyright (C) 2021-2022 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/ui/editor/zooming_state.h"
13
14#include "app/app.h"
15#include "app/ui/editor/editor.h"
16#include "app/ui/skin/skin_theme.h"
17#include "app/ui/status_bar.h"
18#include "doc/sprite.h"
19#include "gfx/rect.h"
20#include "os/window.h"
21#include "ui/manager.h"
22#include "ui/message.h"
23#include "ui/system.h"
24#include "ui/theme.h"
25#include "ui/view.h"
26
27#include <cmath>
28
29namespace app {
30
31using namespace ui;
32
33ZoomingState::ZoomingState()
34 : m_startZoom(1, 1)
35 , m_moved(false)
36{
37}
38
39bool ZoomingState::onMouseDown(Editor* editor, MouseMessage* msg)
40{
41 m_startPos = msg->position();
42 m_startZoom = editor->zoom();
43
44 editor->captureMouse();
45 return true;
46}
47
48bool ZoomingState::onMouseUp(Editor* editor, MouseMessage* msg)
49{
50 if (!m_moved) {
51 render::Zoom zoom = editor->zoom();
52
53 if (msg->left())
54 zoom.in();
55 else if (msg->right())
56 zoom.out();
57
58 editor->setZoomAndCenterInMouse(
59 zoom, msg->position(), Editor::ZoomBehavior::MOUSE);
60 }
61
62 editor->backToPreviousState();
63 editor->releaseMouse();
64 return true;
65}
66
67bool ZoomingState::onMouseMove(Editor* editor, MouseMessage* msg)
68{
69 gfx::Point pt = (msg->position() - m_startPos);
70 int threshold = 8 * guiscale() * editor->display()->nativeWindow()->scale();
71
72 if (m_moved || std::sqrt(pt.x*pt.x + pt.y*pt.y) > threshold) {
73 m_moved = true;
74
75 int newScale = m_startZoom.linearScale() + pt.x / threshold;
76 render::Zoom newZoom = render::Zoom::fromLinearScale(newScale);
77
78 editor->setZoomAndCenterInMouse(
79 newZoom, m_startPos, Editor::ZoomBehavior::MOUSE);
80 }
81 return true;
82}
83
84bool ZoomingState::onSetCursor(Editor* editor, const gfx::Point& mouseScreenPos)
85{
86 auto theme = skin::SkinTheme::get(editor);
87 editor->showMouseCursor(
88 kCustomCursor, theme->cursors.magnifier());
89 return true;
90}
91
92bool ZoomingState::onKeyDown(Editor* editor, KeyMessage* msg)
93{
94 return false;
95}
96
97bool ZoomingState::onKeyUp(Editor* editor, KeyMessage* msg)
98{
99 return false;
100}
101
102bool ZoomingState::onUpdateStatusBar(Editor* editor)
103{
104 return false;
105}
106
107} // namespace app
108