2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
|
|
|
|
// Copyright (C) 2001-2015 David Capello
|
|
|
|
|
//
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
// published by the Free Software Foundation.
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/app.h"
|
2014-01-29 10:56:44 +08:00
|
|
|
#include "app/color.h"
|
|
|
|
|
#include "app/color_picker.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/command.h"
|
|
|
|
|
#include "app/commands/params.h"
|
|
|
|
|
#include "app/modules/editors.h"
|
2014-01-29 10:56:44 +08:00
|
|
|
#include "app/settings/settings.h"
|
|
|
|
|
#include "app/tools/tool.h"
|
|
|
|
|
#include "app/tools/tool_box.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/color_bar.h"
|
|
|
|
|
#include "app/ui/editor/editor.h"
|
2014-01-29 10:56:44 +08:00
|
|
|
#include "app/ui_context.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/image.h"
|
2015-04-21 03:27:09 +08:00
|
|
|
#include "doc/site.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/sprite.h"
|
2014-01-29 10:56:44 +08:00
|
|
|
#include "ui/manager.h"
|
|
|
|
|
#include "ui/system.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2012-06-18 09:02:54 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
using namespace ui;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
class EyedropperCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
/**
|
|
|
|
|
* True means "pick background color", false the foreground color.
|
|
|
|
|
*/
|
|
|
|
|
bool m_background;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
EyedropperCommand();
|
2014-08-15 10:07:47 +08:00
|
|
|
Command* clone() const override { return new EyedropperCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
|
protected:
|
2015-03-12 02:40:22 +08:00
|
|
|
void onLoadParams(const Params& params) override;
|
|
|
|
|
void onExecute(Context* context) override;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EyedropperCommand::EyedropperCommand()
|
|
|
|
|
: Command("Eyedropper",
|
|
|
|
|
"Eyedropper",
|
|
|
|
|
CmdUIOnlyFlag)
|
|
|
|
|
{
|
|
|
|
|
m_background = false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-12 02:40:22 +08:00
|
|
|
void EyedropperCommand::onLoadParams(const Params& params)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2015-03-12 02:40:22 +08:00
|
|
|
std::string target = params.get("target");
|
2012-01-06 06:45:03 +08:00
|
|
|
if (target == "foreground") m_background = false;
|
|
|
|
|
else if (target == "background") m_background = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EyedropperCommand::onExecute(Context* context)
|
|
|
|
|
{
|
2012-06-18 09:02:54 +08:00
|
|
|
Widget* widget = ui::Manager::getDefault()->getMouse();
|
2012-01-06 06:45:03 +08:00
|
|
|
if (!widget || widget->type != editor_type())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Editor* editor = static_cast<Editor*>(widget);
|
2014-07-30 12:28:15 +08:00
|
|
|
Sprite* sprite = editor->sprite();
|
2012-01-06 06:45:03 +08:00
|
|
|
if (!sprite)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// pixel position to get
|
2014-11-24 11:09:22 +08:00
|
|
|
gfx::Point pixelPos = editor->screenToEditor(ui::get_mouse_position());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2014-01-29 10:56:44 +08:00
|
|
|
// Check if we've to grab alpha channel or the merged color.
|
|
|
|
|
ISettings* settings = UIContext::instance()->settings();
|
|
|
|
|
bool grabAlpha = settings->getGrabAlpha();
|
|
|
|
|
|
|
|
|
|
ColorPicker picker;
|
2015-04-21 03:27:09 +08:00
|
|
|
picker.pickColor(editor->getSite(),
|
2014-11-24 11:09:22 +08:00
|
|
|
pixelPos,
|
2014-01-29 10:56:44 +08:00
|
|
|
grabAlpha ?
|
|
|
|
|
ColorPicker::FromActiveLayer:
|
|
|
|
|
ColorPicker::FromComposition);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2014-01-29 10:56:44 +08:00
|
|
|
if (grabAlpha) {
|
|
|
|
|
tools::ToolBox* toolBox = App::instance()->getToolBox();
|
|
|
|
|
for (tools::ToolIterator it=toolBox->begin(), end=toolBox->end(); it!=end; ++it) {
|
|
|
|
|
settings->getToolSettings(*it)->setOpacity(picker.alpha());
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
|
if (m_background)
|
2014-01-29 10:56:44 +08:00
|
|
|
settings->setBgColor(picker.color());
|
2012-01-06 06:45:03 +08:00
|
|
|
else
|
2014-01-29 10:56:44 +08:00
|
|
|
settings->setFgColor(picker.color());
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command* CommandFactory::createEyedropperCommand()
|
|
|
|
|
{
|
|
|
|
|
return new EyedropperCommand;
|
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
|
} // namespace app
|