Allolib  1.0
C++ Components For Interactive Multimedia
al_PickableManager.hpp
1 
2 #ifndef __PICKABLEMANAGER_HPP__
3 #define __PICKABLEMANAGER_HPP__
4 
5 #include <vector>
6 // #include <map>
7 
8 #include "al/graphics/al_Graphics.hpp"
9 #include "al/io/al_Window.hpp"
10 #include "al/math/al_Ray.hpp"
11 #include "al/ui/al_Pickable.hpp"
12 
13 namespace al {
14 
18 public:
19  PickableManager() {}
20 
21  PickableManager &registerPickable(Pickable &p);
22  PickableManager &operator<<(Pickable &p) { return registerPickable(p); }
23  PickableManager &operator<<(Pickable *p) { return registerPickable(*p); }
24 
25  std::vector<Pickable *> pickables() { return mPickables; }
26 
27  Hit intersect(Rayd r);
28 
29  void event(PickEvent e);
30 
31  void unhighlightAll();
32 
33  void onMouseMove(Graphics &g, const Mouse &m, int w, int h);
34  void onMouseDown(Graphics &g, const Mouse &m, int w, int h);
35  void onMouseDrag(Graphics &g, const Mouse &m, int w, int h);
36  void onMouseUp(Graphics &g, const Mouse &m, int w, int h);
37 
38  void onKeyDown(const Keyboard &k);
39  void onKeyUp(const Keyboard &k);
40 
41  Hit lastPoint() { return mLastPoint; }
42  Hit lastPick() { return mLastPick; }
43 
44 protected:
45  std::vector<Pickable *> mPickables;
46  // std::map<int, Hit> mHover;
47  // std::map<int, Hit> mSelect;
48 
49  Hit mLastPoint;
50  Hit mLastPick;
51  Vec3d selectOffset;
52  int x, y;
53 
54  bool ctrlKey{false}; // true if control key
55 
56  Vec3d unproject(Graphics &g, Vec3d screenPos, bool view = true) {
57  auto v = Matrix4d::identity();
58  if (view)
59  v = g.viewMatrix();
60  auto mvp = g.projMatrix() * v * g.modelMatrix();
61  Matrix4d invprojview = Matrix4d::inverse(mvp);
62  Vec4d worldPos4 = invprojview.transform(screenPos);
63  return worldPos4.sub<3>(0) / worldPos4.w;
64  }
65 
66  Rayd getPickRay(Graphics &g, int screenX, int screenY, int width, int height,
67  bool view = true) {
68  Rayd r;
69  Vec3d screenPos;
70  screenPos.x = (screenX * 1. / width) * 2. - 1.;
71  screenPos.y = ((height - screenY) * 1. / height) * 2. - 1.;
72  screenPos.z = -1.;
73  Vec3d worldPos = unproject(g, screenPos, view);
74  r.origin().set(worldPos);
75 
76  screenPos.z = 1.;
77  worldPos = unproject(g, screenPos, view);
78  r.direction().set(worldPos);
79  r.direction() -= r.origin();
80  r.direction().normalize();
81  return r;
82  }
83 };
84 
85 } // namespace al
86 
87 #endif
Interface for loading fonts and rendering text.
Definition: al_Graphics.hpp:63
static Mat identity()
Get identity matrix.
Definition: al_Mat.hpp:128
static Matrix4 inverse(const Mat< 4, T > &m)
Get the inverse of a matrix.
Definition: al_Matrix4.hpp:428
Vec< 4, T > transform(const Vec< 4, T > &vCol) const
Computes product of matrix multiplied by column vector, r = m * vCol.
Definition: al_Matrix4.hpp:420
Vec & normalize(T scale=T(1))
Set magnitude to one without changing direction.
Definition: al_Vec.hpp:729
Vec & set(const Vec< N, T2 > &v)
Set elements from another vector.
Definition: al_Vec.hpp:301
Vec< M, T > sub(int begin=0) const
Get a subvector.
Definition: al_Vec.hpp:240
Definition: al_App.hpp:23