Horizon
tool_pub.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, ACTION, LAYER_CHANGE, DATA };
15 
16 enum class ToolID;
17 enum class InToolActionID;
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;
30 
31  Target target;
32  int work_layer = 0;
33  std::unique_ptr<ToolData> data = nullptr;
34  ToolArgs();
35 };
36 
40 class ToolResponse {
41 public:
42  ToolID next_tool;
43  std::unique_ptr<ToolData> data = nullptr;
44  enum class Result { NOP, END, COMMIT, REVERT };
45  Result result = Result::NOP;
50  static ToolResponse end()
51  {
52  return ToolResponse(Result::END);
53  }
54 
55  static ToolResponse commit()
56  {
57  return ToolResponse(Result::COMMIT);
58  }
59 
60  static ToolResponse revert()
61  {
62  return ToolResponse(Result::REVERT);
63  }
64 
68  static ToolResponse next(Result res, ToolID t, std::unique_ptr<ToolData> data = nullptr)
69  {
70  ToolResponse r(res);
71  r.next_tool = t;
72  r.data = std::move(data);
73  return r;
74  };
75 
76  ToolResponse();
77 
78 private:
79  ToolResponse(Result r);
80 };
81 
82 class ToolSettings {
83 public:
84  virtual void load_from_json(const json &j) = 0;
85  virtual json serialize() const = 0;
86  virtual ~ToolSettings()
87  {
88  }
89 };
90 
94 class ToolBase {
95 public:
96  ToolBase(class IDocument *c, ToolID tid);
97  void set_imp_interface(class ImpInterface *i);
98  void set_transient();
99 
100  virtual void apply_settings()
101  {
102  }
103 
104  virtual std::map<ToolID, ToolSettings *> get_all_settings()
105  {
106  if (auto s = get_settings())
107  return {{tool_id, s}};
108  else
109  return {};
110  }
111 
112  virtual std::set<InToolActionID> get_actions() const
113  {
114  return {};
115  }
116 
123  virtual ToolResponse begin(const ToolArgs &args) = 0;
124 
128  virtual ToolResponse update(const ToolArgs &args) = 0;
129 
133  virtual bool can_begin()
134  {
135  return false;
136  }
137 
141  virtual bool is_specific()
142  {
143  return false;
144  }
145 
146  std::set<SelectableRef> selection;
147 
148  virtual ~ToolBase()
149  {
150  }
151 
152 protected:
153  virtual ToolSettings *get_settings()
154  {
155  return nullptr;
156  }
157 
158  Documents doc;
159  class ImpInterface *imp = nullptr;
160  const ToolID tool_id;
161  bool is_transient = false;
162 };
163 } // namespace horizon
Definition: idocument.hpp:5
Definition: imp_interface.hpp:12
Definition: target.hpp:7
This is what a Tool receives when the user did something.
Definition: tool_pub.hpp:23
Common interface for all Tools.
Definition: tool_pub.hpp:94
virtual bool can_begin()
Definition: tool_pub.hpp:133
virtual bool is_specific()
Definition: tool_pub.hpp:141
virtual ToolResponse begin(const ToolArgs &args)=0
Gets called right after the constructor has finished.
virtual ToolResponse update(const ToolArgs &args)=0
Gets called whenever the user generated some sort of input.
To signal back to the core what the Tool did, a Tool returns a ToolResponse.
Definition: tool_pub.hpp:40
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_pub.hpp:68
static ToolResponse end()
Use this if you're done.
Definition: tool_pub.hpp:50
Definition: tool_pub.hpp:82
a class to store JSON values
Definition: json.hpp:177
basic_json<> json
default JSON class
Definition: json_fwd.hpp:62