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 "imp/in_tool_action.hpp"
9 #include <set>
10 #include <memory>
11 
12 namespace horizon {
13 using json = nlohmann::json;
14 
15 enum class ToolEventType { NONE, MOVE, ACTION, LAYER_CHANGE, DATA };
16 
17 enum class ToolID;
18 
23 class ToolArgs {
24 public:
25  ToolEventType type = ToolEventType::NONE;
26  Coordi coords;
27  std::set<SelectableRef> selection;
28  bool keep_selection = false;
29  InToolActionID action = InToolActionID::NONE;
30 
31  Target target;
32  int work_layer = 0;
33  std::unique_ptr<ToolData> data = nullptr;
34  ToolArgs()
35  {
36  }
37 };
38 
42 class ToolResponse {
43 public:
44  ToolID next_tool;
45  std::unique_ptr<ToolData> data = nullptr;
46  enum class Result { NOP, END, COMMIT, REVERT };
47  Result result = Result::NOP;
52  static ToolResponse end()
53  {
54  return ToolResponse(Result::END);
55  }
56 
57  static ToolResponse commit()
58  {
59  return ToolResponse(Result::COMMIT);
60  }
61 
62  static ToolResponse revert()
63  {
64  return ToolResponse(Result::REVERT);
65  }
66 
70  static ToolResponse next(Result res, ToolID t, std::unique_ptr<ToolData> data = nullptr)
71  {
72  ToolResponse r(res);
73  r.next_tool = t;
74  r.data = std::move(data);
75  return r;
76  };
77 
78  ToolResponse();
79 
80 private:
81  ToolResponse(Result r);
82 };
83 
84 class ToolSettings {
85 public:
86  virtual void load_from_json(const json &j) = 0;
87  virtual json serialize() const = 0;
88  virtual ~ToolSettings()
89  {
90  }
91 };
92 
93 class ToolSettingsProxy {
94 public:
95  ToolSettingsProxy(class ToolBase *t, ToolSettings *s) : tool(t), settings(s)
96  {
97  }
98  ToolSettings &operator*()
99  {
100  return *settings;
101  }
102  operator ToolSettings *()
103  {
104  return settings;
105  }
106  ToolSettings *operator->()
107  {
108  return settings;
109  }
110  ~ToolSettingsProxy();
111 
112 private:
113  ToolBase *tool;
114  ToolSettings *settings;
115 };
116 
117 
121 class ToolBase {
122 public:
123  ToolBase(class IDocument *c, ToolID tid);
124  void set_imp_interface(class ImpInterface *i);
125  void set_transient();
126  virtual ToolID get_tool_id_for_settings() const
127  {
128  return tool_id;
129  }
130  virtual const ToolSettings *get_settings_const() const
131  {
132  return nullptr;
133  }
134  ToolSettingsProxy get_settings_proxy()
135  {
136  return ToolSettingsProxy(this, get_settings());
137  }
138  virtual void apply_settings()
139  {
140  }
141 
142  virtual std::set<InToolActionID> get_actions() const
143  {
144  return {};
145  }
146 
153  virtual ToolResponse begin(const ToolArgs &args) = 0;
154 
158  virtual ToolResponse update(const ToolArgs &args) = 0;
159 
163  virtual bool can_begin()
164  {
165  return false;
166  }
167 
171  virtual bool is_specific()
172  {
173  return false;
174  }
175 
176  std::set<SelectableRef> selection;
177 
178  virtual ~ToolBase()
179  {
180  }
181 
182 protected:
183  Documents doc;
184  class ImpInterface *imp = nullptr;
185  ToolID tool_id;
186  bool is_transient = false;
187  virtual ToolSettings *get_settings()
188  {
189  return nullptr;
190  }
191 };
192 } // namespace horizon
horizon::ToolBase::can_begin
virtual bool can_begin()
Definition: tool.hpp:163
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.
horizon::ToolBase::is_specific
virtual bool is_specific()
Definition: tool.hpp:171
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:166
horizon::ToolBase
Common interface for all Tools.
Definition: tool.hpp:121
nlohmann::json
basic_json<> json
default JSON class
Definition: json_fwd.hpp:61
horizon::ToolResponse
To signal back to the core what the Tool did, a Tool returns a ToolResponse.
Definition: tool.hpp:42
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:70
horizon::ToolArgs
This is what a Tool receives when the user did something.
Definition: tool.hpp:23
horizon::ToolResponse::end
static ToolResponse end()
Use this if you're done.
Definition: tool.hpp:52