1// Aseprite
2// Copyright (C) 2001-2017 David Capello
3//
4// This program is distributed under the terms of
5// the End-User License Agreement for Aseprite.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include <string>
12
13#include "app/app.h"
14#include "app/commands/command.h"
15#include "app/commands/params.h"
16#include "app/context.h"
17#include "app/i18n/strings.h"
18#include "app/pref/preferences.h"
19#include "app/tools/active_tool.h"
20#include "app/tools/tool.h"
21#include "app/ui/context_bar.h"
22#include "base/convert_to.h"
23#include "doc/brush.h"
24#include "fmt/format.h"
25
26namespace app {
27
28class ChangeBrushCommand : public Command {
29 enum Change {
30 None,
31 IncrementSize,
32 DecrementSize,
33 IncrementAngle,
34 DecrementAngle,
35 CustomBrush,
36 };
37
38public:
39 ChangeBrushCommand();
40
41protected:
42 bool onNeedsParams() const override { return true; }
43 void onLoadParams(const Params& params) override;
44 void onExecute(Context* context) override;
45 std::string onGetFriendlyName() const override;
46
47private:
48 Change m_change;
49 int m_slot;
50};
51
52ChangeBrushCommand::ChangeBrushCommand()
53 : Command(CommandId::ChangeBrush(), CmdUIOnlyFlag)
54{
55 m_change = None;
56 m_slot = 0;
57}
58
59void ChangeBrushCommand::onLoadParams(const Params& params)
60{
61 std::string change = params.get("change");
62 if (change == "increment-size") m_change = IncrementSize;
63 else if (change == "decrement-size") m_change = DecrementSize;
64 else if (change == "increment-angle") m_change = IncrementAngle;
65 else if (change == "decrement-angle") m_change = DecrementAngle;
66 else if (change == "custom") m_change = CustomBrush;
67
68 if (m_change == CustomBrush)
69 m_slot = params.get_as<int>("slot");
70 else
71 m_slot = 0;
72}
73
74void ChangeBrushCommand::onExecute(Context* context)
75{
76 // Change the brush of the selected tool in the toolbar (not the
77 // active tool which might be different, e.g. the quick tool)
78 tools::Tool* tool = App::instance()->activeToolManager()->activeTool();
79 ToolPreferences::Brush& brush =
80 Preferences::instance().tool(tool).brush;
81
82 switch (m_change) {
83 case None:
84 // Do nothing
85 break;
86 case IncrementSize:
87 if (brush.size() < doc::Brush::kMaxBrushSize)
88 brush.size(brush.size()+1);
89 break;
90 case DecrementSize:
91 if (brush.size() > doc::Brush::kMinBrushSize)
92 brush.size(brush.size()-1);
93 break;
94 case IncrementAngle:
95 if (brush.angle() < 180)
96 brush.angle(brush.angle()+1);
97 break;
98 case DecrementAngle:
99 if (brush.angle() > 0)
100 brush.angle(brush.angle()-1);
101 break;
102 case CustomBrush:
103 App::instance()->contextBar()
104 ->setActiveBrushBySlot(tool, m_slot);
105 break;
106 }
107}
108
109std::string ChangeBrushCommand::onGetFriendlyName() const
110{
111 std::string change;
112 switch (m_change) {
113 case None: break;
114 case IncrementSize: change = Strings::commands_ChangeBrush_IncrementSize(); break;
115 case DecrementSize: change = Strings::commands_ChangeBrush_DecrementSize(); break;
116 case IncrementAngle: change = Strings::commands_ChangeBrush_IncrementAngle(); break;
117 case DecrementAngle: change = Strings::commands_ChangeBrush_DecrementAngle(); break;
118 case CustomBrush:
119 change = fmt::format(Strings::commands_ChangeBrush_CustomBrush(), m_slot);
120 break;
121 }
122 return fmt::format(getBaseFriendlyName(), change);
123}
124
125Command* CommandFactory::createChangeBrushCommand()
126{
127 return new ChangeBrushCommand;
128}
129
130} // namespace app
131