Horizon
tool.hpp
1 #pragma once
2 #include "common/common.hpp"
3 #include "canvas/selectables.hpp"
4 #include "canvas/target.hpp"
5 #include "tool_data.hpp"
6 #include "nlohmann/json_fwd.hpp"
7 #include "document/documents.hpp"
8 #include <set>
9 #include <memory>
10 
11 namespace horizon {
12 using json = nlohmann::json;
13 
14 enum class ToolEventType { NONE, MOVE, CLICK, CLICK_RELEASE, KEY, LAYER_CHANGE, DATA };
15 
16 enum class ToolID;
17 
22 class ToolArgs {
23 public:
24  ToolEventType type = ToolEventType::NONE;
25  Coordi coords;
26  std::set<SelectableRef> selection;
27  bool keep_selection = false;
28  unsigned int button = 0;
29  unsigned int key = 0;
30  enum Modifieres {
31  MOD_FINE = (1 << 0),
32  MOD_ALT = (1 << 1),
33  MOD_CTRL = (1 << 2),
34  };
35  unsigned int mod = 0;
36 
37  Target target;
38  int work_layer = 0;
39  std::unique_ptr<ToolData> data = nullptr;
40  ToolArgs()
41  {
42  }
43 };
44 
48 class ToolResponse {
49 public:
50  ToolID next_tool;
51  std::unique_ptr<ToolData> data = nullptr;
52  enum class Result { NOP, END, COMMIT, REVERT };
53  Result result = Result::NOP;
54  bool fast_draw = false;
55 
60  static ToolResponse end()
61  {
62  return ToolResponse(Result::END);
63  }
64 
65  static ToolResponse commit()
66  {
67  return ToolResponse(Result::COMMIT);
68  }
69 
70  static ToolResponse revert()
71  {
72  return ToolResponse(Result::REVERT);
73  }
74 
75  static ToolResponse fast()
76  {
77  ToolResponse r;
78  r.fast_draw = true;
79  return r;
80  }
81 
85  static ToolResponse next(Result res, ToolID t, std::unique_ptr<ToolData> data = nullptr)
86  {
87  ToolResponse r(res);
88  r.next_tool = t;
89  r.data = std::move(data);
90  return r;
91  };
92 
93  ToolResponse();
94 
95 private:
96  ToolResponse(Result r);
97 };
98 
99 class ToolSettings {
100 public:
101  virtual void load_from_json(const json &j) = 0;
102  virtual json serialize() const = 0;
103  virtual ~ToolSettings()
104  {
105  }
106 };
107 
109 public:
110  ToolSettingsProxy(class ToolBase *t, ToolSettings *s) : tool(t), settings(s)
111  {
112  }
113  ToolSettings &operator*()
114  {
115  return *settings;
116  }
117  operator ToolSettings *()
118  {
119  return settings;
120  }
121  ToolSettings *operator->()
122  {
123  return settings;
124  }
126 
127 private:
128  ToolBase *tool;
129  ToolSettings *settings;
130 };
131 
132 
136 class ToolBase {
137 public:
138  ToolBase(class IDocument *c, ToolID tid);
139  void set_imp_interface(class ImpInterface *i);
140  void set_transient();
141  virtual ToolID get_tool_id_for_settings() const
142  {
143  return tool_id;
144  }
145  virtual const ToolSettings *get_settings_const() const
146  {
147  return nullptr;
148  }
149  ToolSettingsProxy get_settings_proxy()
150  {
151  return ToolSettingsProxy(this, get_settings());
152  }
153  virtual void apply_settings()
154  {
155  }
156 
157 
164  virtual ToolResponse begin(const ToolArgs &args) = 0;
165 
169  virtual ToolResponse update(const ToolArgs &args) = 0;
170 
174  virtual bool can_begin()
175  {
176  return false;
177  }
178 
182  virtual bool is_specific()
183  {
184  return false;
185  }
186 
190  virtual bool handles_esc()
191  {
192  return false;
193  }
194 
195  std::set<SelectableRef> selection;
196 
197  virtual ~ToolBase()
198  {
199  }
200 
201 protected:
202  Documents doc;
203  class ImpInterface *imp = nullptr;
204  ToolID tool_id;
205  bool is_transient = false;
206  virtual ToolSettings *get_settings()
207  {
208  return nullptr;
209  }
210 };
211 } // namespace horizon
horizon::ToolSettings
Definition: tool.hpp:99
horizon::ToolBase::can_begin
virtual bool can_begin()
Definition: tool.hpp:174
horizon::IDocument
Definition: idocument.hpp:5
horizon::ToolBase::update
virtual ToolResponse update(const ToolArgs &args)=0
Gets called whenever the user generated some sort of input.
horizon::ToolBase::begin
virtual ToolResponse begin(const ToolArgs &args)=0
Gets called right after the constructor has finished.
nlohmann::json
basic_json<> json
default JSON class
Definition: json_fwd.hpp:61
horizon::ImpInterface
Definition: imp_interface.hpp:7
horizon::ToolBase::handles_esc
virtual bool handles_esc()
Definition: tool.hpp:190
horizon::ToolBase::is_specific
virtual bool is_specific()
Definition: tool.hpp:182
horizon::ToolSettingsProxy
Definition: tool.hpp:108
horizon::Coord< int64_t >
horizon::Target
Definition: target.hpp:6
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:165
horizon::ToolBase
Common interface for all Tools.
Definition: tool.hpp:136
horizon::ToolResponse
To signal back to the core what the Tool did, a Tool returns a ToolResponse.
Definition: tool.hpp:48
horizon::ToolResponse::next
static ToolResponse next(Result res, ToolID t, std::unique_ptr< ToolData > data=nullptr)
If you want another Tool to be launched you've finished, use this one.
Definition: tool.hpp:85
horizon::ToolArgs
This is what a Tool receives when the user did something.
Definition: tool.hpp:22
horizon::ToolResponse::end
static ToolResponse end()
Use this if you're done.
Definition: tool.hpp:60