2016-03-19 22:33:05 +08:00
|
|
|
// Aseprite
|
|
|
|
|
// Copyright (C) 2015, 2016 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.
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "app/cmd/set_mask.h"
|
|
|
|
|
#include "app/commands/command.h"
|
|
|
|
|
#include "app/context_access.h"
|
|
|
|
|
#include "app/document.h"
|
|
|
|
|
#include "app/modules/editors.h"
|
|
|
|
|
#include "app/modules/gui.h"
|
|
|
|
|
#include "app/pref/preferences.h"
|
|
|
|
|
#include "app/snap_to_grid.h"
|
|
|
|
|
#include "app/transaction.h"
|
|
|
|
|
#include "app/ui/editor/editor.h"
|
|
|
|
|
#include "doc/mask.h"
|
|
|
|
|
#include "ui/system.h"
|
|
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
|
|
|
|
|
using namespace doc;
|
|
|
|
|
|
|
|
|
|
class SelectTileCommand : public Command {
|
|
|
|
|
public:
|
|
|
|
|
SelectTileCommand();
|
|
|
|
|
Command* clone() const override { return new SelectTileCommand(*this); }
|
|
|
|
|
|
|
|
|
|
protected:
|
2016-03-19 23:09:03 +08:00
|
|
|
void onLoadParams(const Params& params) override;
|
|
|
|
|
bool onEnabled(Context* ctx) override;
|
|
|
|
|
void onExecute(Context* ctx) override;
|
|
|
|
|
std::string onGetFriendlyName() const override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
tools::SelectionMode m_mode;
|
2016-03-19 22:33:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SelectTileCommand::SelectTileCommand()
|
|
|
|
|
: Command("SelectTile",
|
|
|
|
|
"Select Tile",
|
|
|
|
|
CmdRecordableFlag)
|
2016-03-19 23:09:03 +08:00
|
|
|
, m_mode(tools::SelectionMode::DEFAULT)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SelectTileCommand::onLoadParams(const Params& params)
|
2016-03-19 22:33:05 +08:00
|
|
|
{
|
2016-03-19 23:09:03 +08:00
|
|
|
std::string mode = params.get("mode");
|
|
|
|
|
if (mode == "add")
|
|
|
|
|
m_mode = tools::SelectionMode::ADD;
|
|
|
|
|
else if (mode == "subtract")
|
|
|
|
|
m_mode = tools::SelectionMode::SUBTRACT;
|
|
|
|
|
else
|
|
|
|
|
m_mode = tools::SelectionMode::DEFAULT;
|
2016-03-19 22:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
2016-03-19 23:09:03 +08:00
|
|
|
bool SelectTileCommand::onEnabled(Context* ctx)
|
2016-03-19 22:33:05 +08:00
|
|
|
{
|
2016-03-19 23:09:03 +08:00
|
|
|
return ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable);
|
2016-03-19 22:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SelectTileCommand::onExecute(Context* ctx)
|
|
|
|
|
{
|
|
|
|
|
if (!current_editor ||
|
|
|
|
|
!current_editor->hasMouse())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Lock sprite
|
|
|
|
|
ContextWriter writer(ctx);
|
|
|
|
|
Document* doc(writer.document());
|
|
|
|
|
auto& docPref = Preferences::instance().document(doc);
|
|
|
|
|
|
|
|
|
|
base::UniquePtr<Mask> mask(new Mask());
|
2016-03-19 23:09:03 +08:00
|
|
|
|
|
|
|
|
if (m_mode != tools::SelectionMode::DEFAULT)
|
|
|
|
|
mask->copyFrom(doc->mask());
|
|
|
|
|
|
2016-03-19 22:33:05 +08:00
|
|
|
{
|
2016-03-19 23:09:03 +08:00
|
|
|
gfx::Rect gridBounds = docPref.grid.bounds();
|
2016-03-19 22:33:05 +08:00
|
|
|
gfx::Point pos = current_editor->screenToEditor(ui::get_mouse_position());
|
|
|
|
|
pos = snap_to_grid(gridBounds, pos, PreferSnapTo::BoxOrigin);
|
2016-03-19 23:09:03 +08:00
|
|
|
gridBounds.setOrigin(pos);
|
2016-03-19 22:33:05 +08:00
|
|
|
|
2016-03-19 23:09:03 +08:00
|
|
|
if (m_mode != tools::SelectionMode::SUBTRACT)
|
|
|
|
|
mask->add(gridBounds);
|
|
|
|
|
else
|
|
|
|
|
mask->subtract(gridBounds);
|
2016-03-19 22:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the new mask
|
|
|
|
|
Transaction transaction(writer.context(),
|
|
|
|
|
"Select Tile",
|
|
|
|
|
DoesntModifyDocument);
|
|
|
|
|
transaction.execute(new cmd::SetMask(doc, mask));
|
|
|
|
|
transaction.commit();
|
|
|
|
|
|
|
|
|
|
doc->generateMaskBoundaries();
|
|
|
|
|
update_screen_for_document(doc);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 23:09:03 +08:00
|
|
|
std::string SelectTileCommand::onGetFriendlyName() const
|
|
|
|
|
{
|
|
|
|
|
std::string text = "Select Tile";
|
|
|
|
|
|
|
|
|
|
switch (m_mode) {
|
|
|
|
|
case tools::SelectionMode::ADD: text += " (Add)"; break;
|
|
|
|
|
case tools::SelectionMode::SUBTRACT: text += " (Subtract)"; break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 22:33:05 +08:00
|
|
|
Command* CommandFactory::createSelectTileCommand()
|
|
|
|
|
{
|
|
|
|
|
return new SelectTileCommand;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace app
|