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 
96 class ToolBase {
97 public:
98  ToolBase(class IDocument *c, ToolID tid);
99  void set_imp_interface(class ImpInterface *i);
100  void set_transient();
101  virtual ToolID get_tool_id_for_settings() const
102  {
103  return tool_id;
104  }
105  virtual ToolSettings *get_settings()
106  {
107  return nullptr;
108  }
109  virtual void apply_settings()
110  {
111  }
112 
113  virtual std::set<InToolActionID> get_actions() const
114  {
115  return {};
116  }
117 
124  virtual ToolResponse begin(const ToolArgs &args) = 0;
125 
129  virtual ToolResponse update(const ToolArgs &args) = 0;
130 
134  virtual bool can_begin()
135  {
136  return false;
137  }
138 
142  virtual bool is_specific()
143  {
144  return false;
145  }
146 
147  std::set<SelectableRef> selection;
148 
149  virtual ~ToolBase()
150  {
151  }
152 
153 protected:
154  Documents doc;
155  class ImpInterface *imp = nullptr;
156  ToolID tool_id;
157  bool is_transient = false;
158 };
159 } // 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.hpp:23
Common interface for all Tools.
Definition: tool.hpp:96
virtual bool can_begin()
Definition: tool.hpp:134
virtual bool is_specific()
Definition: tool.hpp:142
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.hpp:42
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
static ToolResponse end()
Use this if you're done.
Definition: tool.hpp:52
Definition: tool.hpp:84
a class to store JSON values
Definition: json.hpp:170
basic_json<> json
default JSON class
Definition: json_fwd.hpp:62