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 };
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  std::array<Coordf, 4> get_corners() const;
32 } __attribute__((packed));
33 
35 public:
36  UUID uuid;
37  ObjectType type;
38  unsigned int vertex;
39  LayerRange layer;
40  SelectableRef(const UUID &uu, ObjectType ty, unsigned int v = 0, LayerRange la = 10000)
41  : uuid(uu), type(ty), vertex(v), layer(la)
42  {
43  }
44  bool operator<(const SelectableRef &other) const
45  {
46  if (type < other.type) {
47  return true;
48  }
49  if (type > other.type) {
50  return false;
51  }
52  if (uuid < other.uuid) {
53  return true;
54  }
55  else if (uuid > other.uuid) {
56  return false;
57  }
58  return vertex < other.vertex;
59  }
60  bool operator==(const SelectableRef &other) const
61  {
62  return (uuid == other.uuid) && (vertex == other.vertex) && (type == other.type);
63  }
64 };
65 
66 class Selectables {
67  friend class Canvas;
68  friend class CanvasGL;
69  friend class DragSelection;
70  friend class SelectablesRenderer;
71 
72 public:
73  Selectables(const class Canvas &ca);
74  void clear();
75  void append(const UUID &uu, ObjectType ot, const Coordf &center, const Coordf &a, const Coordf &b,
76  unsigned int vertex = 0, LayerRange layer = 10000, bool always = false);
77  void append(const UUID &uu, ObjectType ot, const Coordf &center, unsigned int vertex = 0, LayerRange layer = 10000,
78  bool always = false);
79  void append_angled(const UUID &uu, ObjectType ot, const Coordf &center, const Coordf &box_center,
80  const Coordf &box_dim, float angle, unsigned int vertex = 0, LayerRange layer = 10000,
81  bool always = false);
82  void append_line(const UUID &uu, ObjectType ot, const Coordf &p0, const Coordf &p1, float width,
83  unsigned int vertex = 0, LayerRange layer = 10000, bool always = false);
84  void append_arc(const UUID &uu, ObjectType ot, const Coordf &center, float r0, float r1, float a0, float a1,
85  unsigned int vertex = 0, LayerRange layer = 10000, bool always = false);
86  void update_preview(const std::set<SelectableRef> &sel);
87 
88  void group_begin();
89  void group_end();
90 
91  const auto &get_items() const
92  {
93  return items;
94  }
95 
96  const auto &get_items_ref() const
97  {
98  return items_ref;
99  }
100 
101 private:
102  const Canvas &ca;
103  std::vector<Selectable> items;
104  std::vector<SelectableRef> items_ref;
105  std::map<SelectableRef, unsigned int> items_map;
106  std::vector<int> items_group;
107 
108  int group_max = 0;
109  int group_current = -1;
110 };
111 } // namespace horizon
Definition: canvas_gl.hpp:18
Definition: canvas.hpp:24
Definition: drag_selection.hpp:8
Definition: layer_range.hpp:7
Definition: selectables.hpp:34
Definition: selectables.hpp:9
Definition: selectables_renderer.hpp:5
Definition: selectables.hpp:66
This class encapsulates a UUID and allows it to be uses as a value type.
Definition: uuid.hpp:16
zip_uint8_t uint8_t
zip_uint8_t typedef.
Definition: zip.hpp:78