| 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 |
|
| 15 |
|
| 16 |
namespace GLWindow |
| 17 |
{ |
| 18 |
|
| 19 |
|
| 20 |
struct All : public CVD::Exceptions::All |
| 21 |
{ |
| 22 |
using CVD::Exceptions::All::All; |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
struct CreationError : public All |
| 28 |
{ |
| 29 |
CreationError(std::string w); |
| 30 |
}; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
struct RuntimeError : public All |
| 35 |
{ |
| 36 |
RuntimeError(std::string w); |
| 37 |
}; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
class GLWindow |
| 43 |
{ |
| 44 |
public: |
| 45 |
|
| 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 |
|
| 57 |
enum EventType |
| 58 |
{ |
| 59 |
EVENT_CLOSE, |
| 60 |
EVENT_EXPOSE |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
class EventHandler |
| 65 |
{ |
| 66 |
public: |
| 67 |
virtual ~EventHandler() { } |
| 68 |
|
| 69 |
virtual void on_key_down(GLWindow&, unsigned int ) { } |
| 70 |
|
| 71 |
virtual void on_key_up(GLWindow& , unsigned int ) { } |
| 72 |
|
| 73 |
virtual void on_mouse_move(GLWindow& , ImageRef , unsigned int ) { } |
| 74 |
|
| 75 |
virtual void on_mouse_down(GLWindow& , ImageRef , unsigned int , unsigned int ) { } |
| 76 |
|
| 77 |
virtual void on_mouse_up(GLWindow& , ImageRef , unsigned int , unsigned int ) { } |
| 78 |
|
| 79 |
virtual void on_resize(GLWindow& , ImageRef ) { } |
| 80 |
|
| 81 |
virtual void on_event(GLWindow& , unsigned int ) { } |
| 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 |
|
| 102 |
struct EventSummary |
| 103 |
{ |
| 104 |
EventSummary() |
| 105 |
: cursor(-1, -1) |
| 106 |
, cursor_moved(false) |
| 107 |
{ |
| 108 |
} |
| 109 |
|
| 110 |
std::map<int, int> key_down, key_up; |
| 111 |
typedef std::map<int, int>::const_iterator key_iterator; |
| 112 |
|
| 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 |
|
| 116 |
std::map<int, int> events; |
| 117 |
|
| 118 |
void clear() { *this = EventSummary(); } |
| 119 |
|
| 120 |
bool should_quit() const; |
| 121 |
|
| 122 |
ImageRef cursor; |
| 123 |
|
| 124 |
bool cursor_moved; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 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 |
|
| 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 |
|
| 145 |
ImageRef size() const; |
| 146 |
|
| 147 |
void set_size(const ImageRef&); |
| 148 |
|
| 149 |
ImageRef position() const; |
| 150 |
|
| 151 |
void set_position(const ImageRef&); |
| 152 |
|
| 153 |
void set_cursor_position(const ImageRef& where); |
| 154 |
|
| 155 |
ImageRef cursor_position() const; |
| 156 |
|
| 157 |
void show_cursor(bool show = true); |
| 158 |
|
| 159 |
void hide_cursor() { show_cursor(false); } |
| 160 |
|
| 161 |
std::string title() const; |
| 162 |
|
| 163 |
void set_title(const std::string& title); |
| 164 |
|
| 165 |
void swap_buffers(); |
| 166 |
|
| 167 |
void handle_events(EventHandler& handler); |
| 168 |
|
| 169 |
void get_events(std::vector<Event>& events); |
| 170 |
|
| 171 |
void get_events(EventSummary& summary); |
| 172 |
|
| 173 |
bool has_events() const; |
| 174 |
|
| 175 |
void activate(); |
| 176 |
|
| 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 |