Debian Codegraph @

cvd/glwindow.h

raw symbols SCIP
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 {
14 /// %Exceptions specific to CVD::GLWindow
15 /// @ingroup gException
16 namespace GLWindow
17 {
18 /// Base class for all CVD::GLWindow exceptions
19 /// @ingroup gException
20 struct All : public CVD::Exceptions::All
21 {
22 using CVD::Exceptions::All::All;
23 };
24
25 /// An exception occurred during initialisation
26 /// @ingroup gException
27 struct CreationError : public All
28 {
29 CreationError(std::string w); ///< Construct from error string
30 };
31
32 /// An exception occurred during run-time
33 /// @ingroup gException
34 struct RuntimeError : public All
35 {
36 RuntimeError(std::string w); ///< Construct from error string
37 };
38 }
39 }
40
41 /// An object that creates a window and a GL context attached to that window, and manages its events.
42 class GLWindow
43 {
44 public:
45 /// Symbols for mouse buttons and modifiers
46 enum MouseButton
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 };
56 /// Symbols for window events
57 enum EventType
58 {
59 EVENT_CLOSE,
60 EVENT_EXPOSE
61 };
62
63 /// Abstract base class for event handlers. Subclass this and override to implement a handler.
64 class EventHandler
65 {
66 public:
67 virtual ~EventHandler() { }
68 /// Called for key press events
69 virtual void on_key_down(GLWindow&, unsigned int /*key*/) { }
70 /// Called for key release events
71 virtual void on_key_up(GLWindow& /*win*/, unsigned int /*key*/) { }
72 /// Called for mouse movement events
73 virtual void on_mouse_move(GLWindow& /*win*/, ImageRef /*where*/, unsigned int /*state*/) { }
74 /// Called for mouse button press events
75 virtual void on_mouse_down(GLWindow& /*win*/, ImageRef /*where*/, unsigned int /*state*/, unsigned int /*button*/) { }
76 /// Called for mouse button release events
77 virtual void on_mouse_up(GLWindow& /*win*/, ImageRef /*where*/, unsigned int /*state*/, unsigned int /*button*/) { }
78 /// Called for window resize events
79 virtual void on_resize(GLWindow& /*win*/, ImageRef /*size*/) { }
80 /// Called for general window events (such as EVENT_CLOSE)
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
101 /// A summary of multiple events
102 struct EventSummary
103 {
104 EventSummary()
105 : cursor(-1, -1)
106 , cursor_moved(false)
107 {
108 }
109 /// key->frequency mapping for key presses and releases
110 std::map<int, int> key_down, key_up;
111 typedef std::map<int, int>::const_iterator key_iterator;
112 /// button->frequency mapping for mouse presses and releases
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;
115 /// Generic window events -> frequency
116 std::map<int, int> events;
117 /// Reset the summary
118 void clear() { *this = EventSummary(); }
119 /// Has escape been pressed or the close button pressed?
120 bool should_quit() const;
121 /// last seen cursor position from mouse_move
122 ImageRef cursor;
123 /// was the cursor moved during the recording of the history
124 bool cursor_moved;
125 };
126
127 /// Construct a GLWindow of the given size and colour depth, with the given title.
128 /// A double-buffered GL context is associated with the window.
129 /// @param size Window size
130 /// @param bpp Colour depth
131 /// @param title Window title
132 /// @param display X11 display string, passed to XOpenDisplay. "" Is used to indicate NULL. This is ignored for non X11 platforms.
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 }
137 ///@overload
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();
144 /// Get the size
145 ImageRef size() const;
146 /// Set the size
147 void set_size(const ImageRef&);
148 /// Get the position
149 ImageRef position() const;
150 /// Set the position
151 void set_position(const ImageRef&);
152 /// Set the mouse cursor position
153 void set_cursor_position(const ImageRef& where);
154 /// Get the mouse cursor position
155 ImageRef cursor_position() const;
156 /// Show (or hide) the cursor
157 void show_cursor(bool show = true);
158 /// Hide the cursor
159 void hide_cursor() { show_cursor(false); }
160 /// Get the title
161 std::string title() const;
162 /// Set the title
163 void set_title(const std::string& title);
164 /// Swap the front and back buffers
165 void swap_buffers();
166 /// Handle events in the event queue by calling back to the specified handler.
167 void handle_events(EventHandler& handler);
168 /// Store all events in the event queue into Event objects.
169 void get_events(std::vector<Event>& events);
170 /// Make a summary of the events in the queue.
171 void get_events(EventSummary& summary);
172 /// @returns true if events are pending
173 bool has_events() const;
174 /// Make this GL context active
175 void activate();
176 /// Make this GL context active
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