libcvd
glwindow.h
1 #ifndef GLWINDOW_H
2 #define GLWINDOW_H
3 
4 #include <cvd/image_ref.h>
5 #include <map>
6 #include <string>
7 #include <vector>
8 
9 namespace CVD
10 {
11 
12 namespace Exceptions
13 {
16  namespace GLWindow
17  {
20  struct All : public CVD::Exceptions::All
21  {
22  using CVD::Exceptions::All::All;
23  };
24 
27  struct CreationError : public All
28  {
29  CreationError(std::string w);
30  };
31 
34  struct RuntimeError : public All
35  {
36  RuntimeError(std::string w);
37  };
38  }
39 }
40 
42 class GLWindow
43 {
44  public:
47  {
48  BUTTON_LEFT = 1,
49  BUTTON_MIDDLE = 2,
50  BUTTON_RIGHT = 4,
51  BUTTON_MOD_CTRL = 8,
52  BUTTON_MOD_SHIFT = 0x10,
53  BUTTON_WHEEL_UP = 0x20,
54  BUTTON_WHEEL_DOWN = 0x40
55  };
57  enum EventType
58  {
59  EVENT_CLOSE,
60  EVENT_EXPOSE
61  };
62 
65  {
66  public:
67  virtual ~EventHandler() { }
69  virtual void on_key_down(GLWindow&, unsigned int /*key*/) { }
71  virtual void on_key_up(GLWindow& /*win*/, unsigned int /*key*/) { }
73  virtual void on_mouse_move(GLWindow& /*win*/, ImageRef /*where*/, unsigned int /*state*/) { }
75  virtual void on_mouse_down(GLWindow& /*win*/, ImageRef /*where*/, unsigned int /*state*/, unsigned int /*button*/) { }
77  virtual void on_mouse_up(GLWindow& /*win*/, ImageRef /*where*/, unsigned int /*state*/, unsigned int /*button*/) { }
79  virtual void on_resize(GLWindow& /*win*/, ImageRef /*size*/) { }
81  virtual void on_event(GLWindow& /*win*/, unsigned int /*event*/) { }
82  };
83 
84  struct Event
85  {
86  enum Type
87  {
88  KEY_DOWN,
89  KEY_UP,
90  MOUSE_MOVE,
91  MOUSE_DOWN,
92  MOUSE_UP,
93  RESIZE,
94  EVENT
95  };
96  Type type;
97  int which, state;
98  ImageRef where, size;
99  };
100 
103  {
104  EventSummary()
105  : cursor(-1, -1)
106  , cursor_moved(false)
107  {
108  }
110  std::map<int, int> key_down, key_up;
111  typedef std::map<int, int>::const_iterator key_iterator;
113  std::map<int, std::pair<ImageRef, int>> mouse_down, mouse_up;
114  typedef std::map<int, std::pair<ImageRef, int>>::const_iterator mouse_iterator;
116  std::map<int, int> events;
118  void clear() { *this = EventSummary(); }
120  bool should_quit() const;
125  };
126 
133  GLWindow(const ImageRef& size, int bpp = 24, const std::string& title = "GLWindow", const std::string& display = "")
134  {
135  init(size, bpp, title, display);
136  }
138  GLWindow(const ImageRef& size, const std::string& title, int bpp = 24, const std::string& display = "")
139  {
140  init(size, bpp, title, display);
141  }
142 
143  ~GLWindow();
145  ImageRef size() const;
147  void set_size(const ImageRef&);
149  ImageRef position() const;
151  void set_position(const ImageRef&);
153  void set_cursor_position(const ImageRef& where);
155  ImageRef cursor_position() const;
157  void show_cursor(bool show = true);
159  void hide_cursor() { show_cursor(false); }
161  std::string title() const;
163  void set_title(const std::string& title);
165  void swap_buffers();
167  void handle_events(EventHandler& handler);
169  void get_events(std::vector<Event>& events);
171  void get_events(EventSummary& summary);
173  bool has_events() const;
175  void activate();
177  void make_current() { activate(); }
178 
179  struct State;
180 
181  private:
182  State* state;
183  void init(const ImageRef& size, int bpp, const std::string& title, const std::string& display);
184 };
185 
186 }
187 
188 #endif
An exception occurred during run-time.
Definition: glwindow.h:34
virtual void on_key_down(GLWindow &, unsigned int)
Called for key press events.
Definition: glwindow.h:69
std::map< int, int > events
Generic window events -> frequency.
Definition: glwindow.h:116
virtual void on_key_up(GLWindow &, unsigned int)
Called for key release events.
Definition: glwindow.h:71
void hide_cursor()
Hide the cursor.
Definition: glwindow.h:159
All classes and functions are within the CVD namespace.
Definition: argb.h:6
void make_current()
Make this GL context active.
Definition: glwindow.h:177
bool cursor_moved
was the cursor moved during the recording of the history
Definition: glwindow.h:124
virtual void on_mouse_move(GLWindow &, ImageRef, unsigned int)
Called for mouse movement events.
Definition: glwindow.h:73
An exception occurred during initialisation.
Definition: glwindow.h:27
An object that creates a window and a GL context attached to that window, and manages its events...
Definition: glwindow.h:42
virtual void on_mouse_up(GLWindow &, ImageRef, unsigned int, unsigned int)
Called for mouse button release events.
Definition: glwindow.h:77
GLWindow(const ImageRef &size, const std::string &title, int bpp=24, const std::string &display="")
Definition: glwindow.h:138
Base class for all CVD::GLWindow exceptions.
Definition: glwindow.h:20
A summary of multiple events.
Definition: glwindow.h:102
Definition: glwindow.cc:20
void clear()
Reset the summary.
Definition: glwindow.h:118
Base class for all CVD exceptions.
Definition: exceptions.h:15
Definition: image_ref.h:29
ImageRef cursor
last seen cursor position from mouse_move
Definition: glwindow.h:122
virtual void on_event(GLWindow &, unsigned int)
Called for general window events (such as EVENT_CLOSE)
Definition: glwindow.h:81
std::map< int, int > key_down
key->frequency mapping for key presses and releases
Definition: glwindow.h:110
GLWindow(const ImageRef &size, int bpp=24, const std::string &title="GLWindow", const std::string &display="")
Construct a GLWindow of the given size and colour depth, with the given title.
Definition: glwindow.h:133
Definition: glwindow.h:84
std::map< int, std::pair< ImageRef, int > > mouse_down
button->frequency mapping for mouse presses and releases
Definition: glwindow.h:113
MouseButton
Symbols for mouse buttons and modifiers.
Definition: glwindow.h:46
virtual void on_mouse_down(GLWindow &, ImageRef, unsigned int, unsigned int)
Called for mouse button press events.
Definition: glwindow.h:75
virtual void on_resize(GLWindow &, ImageRef)
Called for window resize events.
Definition: glwindow.h:79
Abstract base class for event handlers. Subclass this and override to implement a handler...
Definition: glwindow.h:64
EventType
Symbols for window events.
Definition: glwindow.h:57