Skip to content

Commit

Permalink
Fix eyedropper to use the real mouse position received in messages
Browse files Browse the repository at this point in the history
Instead of the global mouse position, we can use the more proper
mouse position receive in editor messages.
  • Loading branch information
dacap committed Mar 16, 2018
1 parent 7b49bf2 commit 072e223
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
27 changes: 19 additions & 8 deletions src/app/commands/cmd_eyedropper.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<Editor*>(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;
Expand All @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion src/app/commands/cmd_eyedropper.h
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -10,6 +10,7 @@

#include "app/color.h"
#include "app/commands/command.h"
#include "gfx/point.h"

namespace doc {
class Site;
Expand All @@ -20,6 +21,7 @@ namespace render {
}

namespace app {
class Editor;

class EyedropperCommand : public Command {
public:
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/app/ui/editor/moving_pixels_state.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
18 changes: 8 additions & 10 deletions src/app/ui/editor/standby_state.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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<tools::PickInk*>(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)
Expand Down
4 changes: 2 additions & 2 deletions src/app/ui/editor/standby_state.h
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 072e223

Please sign in to comment.