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