From 072e223ec573bed298a28380816f939afcea27c8 Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 16 Mar 2018 12:26:38 -0300 Subject: [PATCH] Fix eyedropper to use the real mouse position received in messages Instead of the global mouse position, we can use the more proper mouse position receive in editor messages. --- src/app/commands/cmd_eyedropper.cpp | 27 ++++++++++++++++------- src/app/commands/cmd_eyedropper.h | 9 +++++++- src/app/ui/editor/moving_pixels_state.cpp | 4 ++-- src/app/ui/editor/standby_state.cpp | 18 +++++++-------- src/app/ui/editor/standby_state.h | 4 ++-- 5 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/app/commands/cmd_eyedropper.cpp b/src/app/commands/cmd_eyedropper.cpp index 88c7eeb42..79efb0ff5 100644 --- a/src/app/commands/cmd_eyedropper.cpp +++ b/src/app/commands/cmd_eyedropper.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2017 David Capello +// Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -183,11 +183,22 @@ void EyedropperCommand::onLoadParams(const Params& params) void EyedropperCommand::onExecute(Context* context) { - Widget* widget = ui::Manager::getDefault()->getMouse(); + gfx::Point mousePos = ui::get_mouse_position(); + Widget* widget = ui::Manager::getDefault()->pick(mousePos); if (!widget || widget->type() != editor_type()) return; Editor* editor = static_cast(widget); + executeOnMousePos(context, editor, mousePos, !m_background); +} + +void EyedropperCommand::executeOnMousePos(Context* context, + Editor* editor, + const gfx::Point& mousePos, + const bool foreground) +{ + ASSERT(editor); + Sprite* sprite = editor->sprite(); if (!sprite) return; @@ -199,24 +210,24 @@ void EyedropperCommand::onExecute(Context* context) } // Pixel position to get - gfx::PointF pixelPos = editor->screenToEditorF(ui::get_mouse_position()); + gfx::PointF pixelPos = editor->screenToEditorF(mousePos); // Start with fg/bg color DisableColorBarEditMode disable; Preferences& pref = Preferences::instance(); app::Color color = - m_background ? pref.colorBar.bgColor(): - pref.colorBar.fgColor(); + foreground ? pref.colorBar.fgColor(): + pref.colorBar.bgColor(); pickSample(editor->getSite(), pixelPos, editor->projection(), color); - if (m_background) - pref.colorBar.bgColor(color); - else + if (foreground) pref.colorBar.fgColor(color); + else + pref.colorBar.bgColor(color); } Command* CommandFactory::createEyedropperCommand() diff --git a/src/app/commands/cmd_eyedropper.h b/src/app/commands/cmd_eyedropper.h index 5747a9fe8..ee3cf1145 100644 --- a/src/app/commands/cmd_eyedropper.h +++ b/src/app/commands/cmd_eyedropper.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2016 David Capello +// Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -10,6 +10,7 @@ #include "app/color.h" #include "app/commands/command.h" +#include "gfx/point.h" namespace doc { class Site; @@ -20,6 +21,7 @@ namespace render { } namespace app { + class Editor; class EyedropperCommand : public Command { public: @@ -32,6 +34,11 @@ namespace app { const render::Projection& proj, app::Color& color); + void executeOnMousePos(Context* context, + Editor* editor, + const gfx::Point& mousePos, + const bool foreground); + protected: void onLoadParams(const Params& params) override; void onExecute(Context* context) override; diff --git a/src/app/ui/editor/moving_pixels_state.cpp b/src/app/ui/editor/moving_pixels_state.cpp index 55b043960..5f441da8f 100644 --- a/src/app/ui/editor/moving_pixels_state.cpp +++ b/src/app/ui/editor/moving_pixels_state.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2017 David Capello +// Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -245,7 +245,7 @@ bool MovingPixelsState::onMouseDown(Editor* editor, MouseMessage* msg) // Call the eyedropper command tools::Ink* clickedInk = editor->getCurrentEditorInk(); if (clickedInk->isEyedropper()) { - callEyedropper(editor); + callEyedropper(editor, msg); return true; } diff --git a/src/app/ui/editor/standby_state.cpp b/src/app/ui/editor/standby_state.cpp index 73c0acbab..ee9a5e369 100644 --- a/src/app/ui/editor/standby_state.cpp +++ b/src/app/ui/editor/standby_state.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2017 David Capello +// Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -209,7 +209,7 @@ bool StandbyState::onMouseDown(Editor* editor, MouseMessage* msg) // Call the eyedropper command if (clickedInk->isEyedropper()) { editor->captureMouse(); - callEyedropper(editor); + callEyedropper(editor, msg); return true; } @@ -360,7 +360,7 @@ bool StandbyState::onMouseMove(Editor* editor, MouseMessage* msg) tools::Ink* clickedInk = editor->getCurrentEditorInk(); if (clickedInk->isEyedropper() && editor->hasCapture()) { - callEyedropper(editor); + callEyedropper(editor, msg); } } @@ -733,20 +733,18 @@ void StandbyState::transformSelection(Editor* editor, MouseMessage* msg, HandleT } } -void StandbyState::callEyedropper(Editor* editor) +void StandbyState::callEyedropper(Editor* editor, const ui::MouseMessage* msg) { tools::Ink* clickedInk = editor->getCurrentEditorInk(); if (!clickedInk->isEyedropper()) return; - Command* eyedropper_cmd = - Commands::instance()->byId(CommandId::Eyedropper()); + EyedropperCommand* eyedropper = + (EyedropperCommand*)Commands::instance()->byId(CommandId::Eyedropper()); bool fg = (static_cast(clickedInk)->target() == tools::PickInk::Fg); - Params params; - params.set("target", fg ? "foreground": "background"); - - UIContext::instance()->executeCommand(eyedropper_cmd, params); + eyedropper->executeOnMousePos(UIContext::instance(), editor, + msg->position(), fg); } void StandbyState::onPivotChange(Editor* editor) diff --git a/src/app/ui/editor/standby_state.h b/src/app/ui/editor/standby_state.h index dabbcc763..85a94c614 100644 --- a/src/app/ui/editor/standby_state.h +++ b/src/app/ui/editor/standby_state.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2017 David Capello +// Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -49,7 +49,7 @@ namespace app { void startSelectionTransformation(Editor* editor, const gfx::Point& move, double angle); protected: - void callEyedropper(Editor* editor); + void callEyedropper(Editor* editor, const ui::MouseMessage* msg); bool checkStartDrawingStraightLine(Editor* editor, const ui::MouseMessage* msg); class Decorator : public EditorDecorator {