1// Aseprite
2// Copyright (C) 2020-2022 Igara Studio S.A.
3// Copyright (C) 2015-2016 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/moving_symmetry_state.h"
13
14#include "app/ui/editor/editor.h"
15#include "app/ui/status_bar.h"
16#include "fmt/format.h"
17#include "ui/message.h"
18
19#include <cmath>
20
21namespace app {
22
23using namespace ui;
24
25MovingSymmetryState::MovingSymmetryState(Editor* editor, MouseMessage* msg,
26 app::gen::SymmetryMode mode,
27 Option<double>& symmetryAxis)
28 : m_symmetryMode(mode)
29 , m_symmetryAxis(symmetryAxis)
30 , m_symmetryAxisStart(symmetryAxis())
31{
32 m_mouseStart = editor->screenToEditorF(msg->position());
33 editor->captureMouse();
34}
35
36bool MovingSymmetryState::onMouseUp(Editor* editor, MouseMessage* msg)
37{
38 editor->backToPreviousState();
39 editor->releaseMouse();
40 return true;
41}
42
43bool MovingSymmetryState::onMouseMove(Editor* editor, MouseMessage* msg)
44{
45 gfx::PointF newCursorPos = editor->screenToEditorF(msg->position());
46 gfx::PointF delta = newCursorPos - m_mouseStart;
47 double pos = 0.0;
48
49 switch (m_symmetryMode) {
50 case app::gen::SymmetryMode::HORIZONTAL:
51 pos = m_symmetryAxisStart + delta.x;
52 pos = std::round(pos*2.0)/2.0;
53 pos = std::clamp(pos, 1.0, editor->sprite()->width()-1.0);
54 break;
55 case app::gen::SymmetryMode::VERTICAL:
56 pos = m_symmetryAxisStart + delta.y;
57 pos = std::round(pos*2.0)/2.0;
58 pos = std::clamp(pos, 1.0, editor->sprite()->height()-1.0);
59 break;
60 }
61 m_symmetryAxis(pos);
62
63 // Redraw the editor.
64 editor->invalidate();
65
66 // Use StandbyState implementation
67 return StandbyState::onMouseMove(editor, msg);
68}
69
70bool MovingSymmetryState::onUpdateStatusBar(Editor* editor)
71{
72 if (m_symmetryMode == app::gen::SymmetryMode::HORIZONTAL)
73 StatusBar::instance()->setStatusText(
74 0, fmt::format("Left {:3.1f} Right {:3.1f}",
75 m_symmetryAxis(),
76 double(editor->sprite()->width()) - m_symmetryAxis()));
77 else
78 StatusBar::instance()->setStatusText(
79 0, fmt::format("Top {:3.1f} Bottom {:3.1f}",
80 m_symmetryAxis(),
81 double(editor->sprite()->height()) - m_symmetryAxis()));
82
83 return true;
84}
85
86} // namespace app
87