2022-01-07 03:54:39 +08:00
|
|
|
// Aseprite
|
2024-02-22 05:53:18 +08:00
|
|
|
// Copyright (C) 2022-2024 Igara Studio S.A.
|
2022-01-07 03:54:39 +08:00
|
|
|
//
|
|
|
|
// 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/editor/delayed_mouse_move.h"
|
|
|
|
|
|
|
|
#include "app/ui/editor/editor.h"
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
|
|
|
DelayedMouseMove::DelayedMouseMove(DelayedMouseMoveDelegate* delegate,
|
|
|
|
Editor* editor,
|
|
|
|
const int interval)
|
|
|
|
: m_delegate(delegate)
|
|
|
|
, m_editor(editor)
|
|
|
|
, m_timer(interval)
|
2024-02-22 05:53:18 +08:00
|
|
|
, m_spritePos(std::numeric_limits<float>::min(),
|
|
|
|
std::numeric_limits<float>::min())
|
2022-01-07 03:54:39 +08:00
|
|
|
{
|
|
|
|
ASSERT(m_delegate);
|
|
|
|
m_timer.Tick.connect([this] { commitMouseMove(); });
|
|
|
|
}
|
|
|
|
|
2022-01-11 02:59:36 +08:00
|
|
|
void DelayedMouseMove::initSpritePos(const gfx::PointF& pos)
|
|
|
|
{
|
|
|
|
m_spritePos = pos;
|
|
|
|
}
|
|
|
|
|
2022-01-07 03:54:39 +08:00
|
|
|
void DelayedMouseMove::onMouseDown(const ui::MouseMessage* msg)
|
|
|
|
{
|
|
|
|
updateSpritePos(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DelayedMouseMove::onMouseMove(const ui::MouseMessage* msg)
|
|
|
|
{
|
|
|
|
if (!updateSpritePos(msg))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!m_timer.isRunning()) {
|
|
|
|
if (m_timer.interval() > 0) {
|
|
|
|
m_timer.start();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Commit immediately
|
|
|
|
commitMouseMove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DelayedMouseMove::onMouseUp(const ui::MouseMessage* msg)
|
|
|
|
{
|
2022-04-13 06:11:20 +08:00
|
|
|
if (updateSpritePos(msg))
|
|
|
|
commitMouseMove();
|
2022-01-07 03:54:39 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 21:51:55 +08:00
|
|
|
void DelayedMouseMove::stopTimer()
|
|
|
|
{
|
|
|
|
if (m_timer.isRunning())
|
|
|
|
m_timer.stop();
|
|
|
|
}
|
|
|
|
|
2022-01-07 03:54:39 +08:00
|
|
|
void DelayedMouseMove::commitMouseMove()
|
|
|
|
{
|
|
|
|
if (m_timer.isRunning())
|
|
|
|
m_timer.stop();
|
|
|
|
|
2023-12-30 00:18:09 +08:00
|
|
|
try {
|
|
|
|
m_delegate->onCommitMouseMove(m_editor, spritePos());
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex) {
|
|
|
|
m_editor->showUnhandledException(ex, nullptr);
|
|
|
|
}
|
2022-01-07 03:54:39 +08:00
|
|
|
}
|
|
|
|
|
2022-01-07 05:43:12 +08:00
|
|
|
const gfx::PointF& DelayedMouseMove::spritePos() const
|
2022-01-07 03:54:39 +08:00
|
|
|
{
|
2024-02-22 05:53:18 +08:00
|
|
|
ASSERT(m_spritePos.x != std::numeric_limits<float>::min() &&
|
|
|
|
m_spritePos.y != std::numeric_limits<float>::min());
|
2022-01-07 03:54:39 +08:00
|
|
|
return m_spritePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DelayedMouseMove::updateSpritePos(const ui::MouseMessage* msg)
|
|
|
|
{
|
|
|
|
// The autoScroll() function controls the "infinite scroll" when we
|
|
|
|
// touch the viewport borders.
|
|
|
|
const gfx::Point mousePos = m_editor->autoScroll(msg, AutoScroll::MouseDir);
|
2022-01-07 05:43:12 +08:00
|
|
|
const gfx::PointF spritePos = m_editor->screenToEditorF(mousePos);
|
2022-01-07 03:54:39 +08:00
|
|
|
|
|
|
|
// Avoid redrawing everything if the position in the canvas didn't
|
|
|
|
// change.
|
|
|
|
if (m_spritePos != spritePos) {
|
|
|
|
m_spritePos = spritePos;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace app
|