From 82f5345ace605cbc99bd0f65e5e21e6ad6055cea Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 1 Apr 2011 22:48:58 -0300 Subject: [PATCH] Add ToolLoopManager::Pointer abstraction to avoid using JMessage in src/tools/ dir. --- src/tools/tool.cpp | 20 ++++++++++---------- src/tools/tool.h | 31 ++++++++++++++++++++++++------- src/widgets/editor/editor.cpp | 24 +++++++++++++++++------- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/tools/tool.cpp b/src/tools/tool.cpp index b7f9b7703..12606bc34 100644 --- a/src/tools/tool.cpp +++ b/src/tools/tool.cpp @@ -115,7 +115,7 @@ ToolLoopManager::~ToolLoopManager() delete m_toolLoop; } -void ToolLoopManager::prepareLoop(JMessage msg) +void ToolLoopManager::prepareLoop(const Pointer& pointer) { // Start with no points at all m_points.clear(); @@ -133,24 +133,24 @@ void ToolLoopManager::prepareLoop(JMessage msg) m_toolLoop->getDstImage()); } -void ToolLoopManager::releaseLoop(JMessage msg) +void ToolLoopManager::releaseLoop(const Pointer& pointer) { // No more preview image RenderEngine::setPreviewImage(NULL, NULL); } -void ToolLoopManager::pressButton(JMessage msg) +void ToolLoopManager::pressButton(const Pointer& pointer) { // If the user pressed the other mouse button... - if ((m_toolLoop->getMouseButton() == 0 && msg->mouse.right) || - (m_toolLoop->getMouseButton() == 1 && msg->mouse.left)) { + if ((m_toolLoop->getMouseButton() == 0 && pointer.getButton() == Pointer::Right) || + (m_toolLoop->getMouseButton() == 1 && pointer.getButton() == Pointer::Left)) { // Cancel the tool-loop (the destination image should be completelly discarded) m_toolLoop->cancel(); return; } // Convert the screen point to a sprite point - Point spritePoint = m_toolLoop->screenToSprite(Point(msg->mouse.x, msg->mouse.y)); + Point spritePoint = m_toolLoop->screenToSprite(Point(pointer.getX(), pointer.getY())); m_toolLoop->setSpeed(Point(0, 0)); m_oldPoint = spritePoint; snapToGrid(true, spritePoint); @@ -164,9 +164,9 @@ void ToolLoopManager::pressButton(JMessage msg) doLoopStep(false); } -bool ToolLoopManager::releaseButton(JMessage msg) +bool ToolLoopManager::releaseButton(const Pointer& pointer) { - Point spritePoint = m_toolLoop->screenToSprite(Point(msg->mouse.x, msg->mouse.y)); + Point spritePoint = m_toolLoop->screenToSprite(Point(pointer.getX(), pointer.getY())); snapToGrid(true, spritePoint); bool res = m_toolLoop->getController()->releaseButton(m_points, spritePoint); @@ -180,10 +180,10 @@ bool ToolLoopManager::releaseButton(JMessage msg) return res; } -void ToolLoopManager::movement(JMessage msg) +void ToolLoopManager::movement(const Pointer& pointer) { // Convert the screen point to a sprite point - Point spritePoint = m_toolLoop->screenToSprite(Point(msg->mouse.x, msg->mouse.y)); + Point spritePoint = m_toolLoop->screenToSprite(Point(pointer.getX(), pointer.getY())); // Calculate the speed (new sprite point - old sprite point) m_toolLoop->setSpeed(spritePoint - m_oldPoint); m_oldPoint = spritePoint; diff --git a/src/tools/tool.h b/src/tools/tool.h index bd61d72e7..7f2cd9869 100644 --- a/src/tools/tool.h +++ b/src/tools/tool.h @@ -26,8 +26,6 @@ #include "filters/tiled_mode.h" #include "gfx/point.h" #include "gfx/rect.h" -#include "gui/message.h" -#include "gui/rect.h" class Context; class Document; @@ -363,6 +361,25 @@ class ToolLoopManager public: + // Simple container of mouse events information. + class Pointer + { + public: + enum Button { Left, Middle, Right }; + + Pointer(int x, int y, Button button) + : m_x(x), m_y(y), m_button(button) { } + + int getX() const { return m_x; } + int getY() const { return m_y; } + Button getButton() const { return m_button; } + + private: + int m_x, m_y; + Button m_button; + }; + + // Contructs a manager for the IToolLoop delegate. ToolLoopManager(IToolLoop* toolLoop); virtual ~ToolLoopManager(); @@ -370,22 +387,22 @@ public: // Should be called when the user start a tool-trace (pressing the // left or right button for first time in the editor). - void prepareLoop(JMessage msg); + void prepareLoop(const Pointer& pointer); // Called when the loop is over. - void releaseLoop(JMessage msg); + void releaseLoop(const Pointer& pointer); // Should be called each time the user presses a mouse button. - void pressButton(JMessage msg); + void pressButton(const Pointer& pointer); // Should be called each time the user releases a mouse button. // // Returns true if the tool-loop should continue, or false // if the editor should release the mouse capture. - bool releaseButton(JMessage msg); + bool releaseButton(const Pointer& pointer); // Should be called each time the user moves the mouse inside the editor. - void movement(JMessage msg); + void movement(const Pointer& pointer); private: void doLoopStep(bool last_step); diff --git a/src/widgets/editor/editor.cpp b/src/widgets/editor/editor.cpp index 876b26cd6..d40d39e0c 100644 --- a/src/widgets/editor/editor.cpp +++ b/src/widgets/editor/editor.cpp @@ -69,6 +69,16 @@ using namespace gfx; static bool editor_view_msg_proc(JWidget widget, JMessage msg); +static ToolLoopManager::Pointer pointer_from_msg(JMessage msg) +{ + ToolLoopManager::Pointer::Button button = + (msg->mouse.right ? ToolLoopManager::Pointer::Right: + (msg->mouse.middle ? ToolLoopManager::Pointer::Middle: + ToolLoopManager::Pointer::Left)); + + return ToolLoopManager::Pointer(msg->mouse.x, msg->mouse.y, button); +} + View* editor_view_new() { View* widget = new View(); @@ -926,11 +936,11 @@ bool Editor::onProcessMessage(JMessage msg) if (m_state == EDITOR_STATE_DRAWING) { ASSERT(m_toolLoopManager != NULL); - m_toolLoopManager->pressButton(msg); + m_toolLoopManager->pressButton(pointer_from_msg(msg)); // Cancel drawing loop if (m_toolLoopManager->isCanceled()) { - m_toolLoopManager->releaseLoop(msg); + m_toolLoopManager->releaseLoop(pointer_from_msg(msg)); delete m_toolLoopManager; m_toolLoopManager = NULL; @@ -1071,8 +1081,8 @@ bool Editor::onProcessMessage(JMessage msg) m_state = EDITOR_STATE_DRAWING; - m_toolLoopManager->prepareLoop(msg); - m_toolLoopManager->pressButton(msg); + m_toolLoopManager->prepareLoop(pointer_from_msg(msg)); + m_toolLoopManager->pressButton(pointer_from_msg(msg)); // Redraw it (without pen preview) if (thick) @@ -1173,7 +1183,7 @@ bool Editor::onProcessMessage(JMessage msg) // notify mouse movement to the tool ASSERT(m_toolLoopManager != NULL); - m_toolLoopManager->movement(msg); + m_toolLoopManager->movement(pointer_from_msg(msg)); // draw the cursor again if (thick) @@ -1210,10 +1220,10 @@ bool Editor::onProcessMessage(JMessage msg) if (m_state == EDITOR_STATE_DRAWING) { ASSERT(m_toolLoopManager != NULL); - if (m_toolLoopManager->releaseButton(msg)) + if (m_toolLoopManager->releaseButton(pointer_from_msg(msg))) return true; - m_toolLoopManager->releaseLoop(msg); + m_toolLoopManager->releaseLoop(pointer_from_msg(msg)); delete m_toolLoopManager; m_toolLoopManager = NULL;