Horizon
selectables.hpp
1 #pragma once
2 #include "common/common.hpp"
3 #include "util/uuid.hpp"
4 #include <epoxy/gl.h>
5 #include <map>
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 };
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  std::array<Coordf, 4> get_corners() const;
27 } __attribute__((packed));
28 
30 public:
31  const UUID uuid;
32  const ObjectType type;
33  const unsigned int vertex;
34  const int layer;
35  SelectableRef(const UUID &uu, ObjectType ty, unsigned int v = 0, int la = 10000)
36  : uuid(uu), type(ty), vertex(v), layer(la)
37  {
38  }
39  bool operator<(const SelectableRef &other) const
40  {
41  if (type < other.type) {
42  return true;
43  }
44  if (type > other.type) {
45  return false;
46  }
47  if (uuid < other.uuid) {
48  return true;
49  }
50  else if (uuid > other.uuid) {
51  return false;
52  }
53  return vertex < other.vertex;
54  }
55  bool operator==(const SelectableRef &other) const
56  {
57  return (uuid == other.uuid) && (vertex == other.vertex) && (type == other.type);
58  }
59 };
60 
61 class Selectables {
62  friend class Canvas;
63  friend class CanvasGL;
64  friend class DragSelection;
65  friend class SelectablesRenderer;
66 
67 public:
68  Selectables(class Canvas *ca);
69  void clear();
70  void append(const UUID &uu, ObjectType ot, const Coordf &center, const Coordf &a, const Coordf &b,
71  unsigned int vertex = 0, int layer = 10000, bool always = false);
72  void append(const UUID &uu, ObjectType ot, const Coordf &center, unsigned int vertex = 0, int layer = 10000,
73  bool always = false);
74  void append_angled(const UUID &uu, ObjectType ot, const Coordf &center, const Coordf &box_center,
75  const Coordf &box_dim, float angle, unsigned int vertex = 0, int layer = 10000,
76  bool always = false);
77 
78 private:
79  Canvas *ca;
80  std::vector<Selectable> items;
81  std::vector<SelectableRef> items_ref;
82  std::map<SelectableRef, unsigned int> items_map;
83 };
84 
86 public:
87  SelectablesRenderer(class CanvasGL *ca, Selectables *sel);
88  void realize();
89  void render();
90  void push();
91 
92 private:
93  CanvasGL *ca;
94  Selectables *sel;
95 
96  GLuint program;
97  GLuint vao;
98  GLuint vbo;
99 
100  GLuint screenmat_loc;
101  GLuint scale_loc;
102  GLuint offset_loc;
103 };
104 } // namespace horizon
Definition: selectables.hpp:85
Definition: selectables.hpp:29
Definition: canvas_gl.hpp:10
Definition: canvas.hpp:17
Definition: drag_selection.hpp:8
This class encapsulates a UUID and allows it to be uses as a value type.
Definition: uuid.hpp:16
Definition: block.cpp:7
Definition: selectables.hpp:61
Definition: selectables.hpp:8