Horizon
selectables.hpp
1 #pragma once
2 #include "common/common.hpp"
3 #include "util/uuid.hpp"
4 #include "util/layer_range.hpp"
5 #include <map>
6 #include <set>
7 
8 namespace horizon {
9 class Selectable {
10 public:
11  float x;
12  float y;
13  float c_x;
14  float c_y;
15  float width;
16  float height;
17  float angle;
18  uint8_t flags;
19  enum class Flag { SELECTED = 1, PRELIGHT = 2, ALWAYS = 4, PREVIEW = 8, ARC_CENTER_IS_MIDPOINT = 16 };
20  bool get_flag(Flag f) const;
21  void set_flag(Flag f, bool v);
22 
23  Selectable(const Coordf &center, const Coordf &box_center, const Coordf &box_dim, float angle = 0,
24  bool always = false);
25  bool inside(const Coordf &c, float expand = 0) const;
26  float area() const;
27  bool is_line() const;
28  bool is_point() const;
29  bool is_box() const;
30  bool is_arc() const;
31  Coordf get_arc_center() const;
32  std::array<Coordf, 4> get_corners() const;
33 } __attribute__((packed));
34 
36 public:
37  UUID uuid;
38  ObjectType type;
39  unsigned int vertex;
40  LayerRange layer;
41  SelectableRef(const UUID &uu, ObjectType ty, unsigned int v = 0, LayerRange la = 10000)
42  : uuid(uu), type(ty), vertex(v), layer(la)
43  {
44  }
45  bool operator<(const SelectableRef &other) const
46  {
47  if (type < other.type) {
48  return true;
49  }
50  if (type > other.type) {
51  return false;
52  }
53  if (uuid < other.uuid) {
54  return true;
55  }
56  else if (uuid > other.uuid) {
57  return false;
58  }
59  return vertex < other.vertex;
60  }
61  bool operator==(const SelectableRef &other) const
62  {
63  return (uuid == other.uuid) && (vertex == other.vertex) && (type == other.type);
64  }
65 };
66 
67 class Selectables {
68  friend class Canvas;
69  friend class CanvasGL;
70  friend class DragSelection;
71  friend class SelectablesRenderer;
72 
73 public:
74  Selectables(const class Canvas &ca);
75  void clear();
76  void append(const UUID &uu, ObjectType ot, const Coordf &center, const Coordf &a, const Coordf &b,
77  unsigned int vertex = 0, LayerRange layer = 10000, bool always = false);
78  void append(const UUID &uu, ObjectType ot, const Coordf &center, unsigned int vertex = 0, LayerRange layer = 10000,
79  bool always = false);
80  void append_angled(const UUID &uu, ObjectType ot, const Coordf &center, const Coordf &box_center,
81  const Coordf &box_dim, float angle, unsigned int vertex = 0, LayerRange layer = 10000,
82  bool always = false);
83  void append_line(const UUID &uu, ObjectType ot, const Coordf &p0, const Coordf &p1, float width,
84  unsigned int vertex = 0, LayerRange layer = 10000, bool always = false);
85  void append_arc(const UUID &uu, ObjectType ot, const Coordf &center, float r0, float r1, float a0, float a1,
86  unsigned int vertex = 0, LayerRange layer = 10000, bool always = false);
87  void append_arc_midpoint(const UUID &uu, ObjectType ot, const Coordf &midpoint, float r0, float r1, float a0,
88  float a1, unsigned int vertex = 0, LayerRange layer = 10000, bool always = false);
89  void update_preview(const std::set<SelectableRef> &sel);
90 
91  void group_begin();
92  void group_end();
93 
94  const auto &get_items() const
95  {
96  return items;
97  }
98 
99  const auto &get_items_ref() const
100  {
101  return items_ref;
102  }
103 
104 private:
105  const Canvas &ca;
106  std::vector<Selectable> items;
107  std::vector<SelectableRef> items_ref;
108  std::map<SelectableRef, unsigned int> items_map;
109  std::vector<int> items_group;
110 
111  int group_max = 0;
112  int group_current = -1;
113 };
114 } // namespace horizon
Definition: canvas_gl.hpp:20
Definition: canvas.hpp:24
Definition: drag_selection.hpp:8
Definition: layer_range.hpp:7
Definition: selectables.hpp:35
Definition: selectables.hpp:9
Definition: selectables_renderer.hpp:5
Definition: selectables.hpp:67
This class encapsulates a UUID and allows it to be uses as a value type.
Definition: uuid.hpp:16