00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "graphicsWindowInputDevice.h"
00017 #include "mouseButton.h"
00018 #include "keyboardButton.h"
00019
00020 #define EXPCL EXPCL_PANDA_DISPLAY
00021 #define EXPTP EXPTP_PANDA_DISPLAY
00022 #define TYPE GraphicsWindowInputDevice
00023 #define NAME vector_GraphicsWindowInputDevice
00024
00025 #include "vector_src.cxx"
00026
00027
00028 #ifdef __GNUC__
00029 #pragma implementation
00030 #endif
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 GraphicsWindowInputDevice::
00044 GraphicsWindowInputDevice(GraphicsWindow *host, const string &name, int flags) :
00045 _host(host),
00046 _name(name),
00047 _flags(flags),
00048 _device_index(0),
00049 _event_sequence(0),
00050 _pointer_mode_enable(false),
00051 _pointer_speed(1.0),
00052 _pointer_true_x(0.0),
00053 _pointer_true_y(0.0),
00054 _enable_pointer_events(false)
00055 {
00056 }
00057
00058
00059
00060
00061
00062
00063
00064 GraphicsWindowInputDevice GraphicsWindowInputDevice::
00065 pointer_only(GraphicsWindow *host, const string &name) {
00066 return GraphicsWindowInputDevice(host, name, IDF_has_pointer);
00067 }
00068
00069
00070
00071
00072
00073
00074
00075 GraphicsWindowInputDevice GraphicsWindowInputDevice::
00076 keyboard_only(GraphicsWindow *host, const string &name) {
00077 return GraphicsWindowInputDevice(host, name, IDF_has_keyboard);
00078 }
00079
00080
00081
00082
00083
00084
00085
00086 GraphicsWindowInputDevice GraphicsWindowInputDevice::
00087 pointer_and_keyboard(GraphicsWindow *host, const string &name) {
00088 return
00089 GraphicsWindowInputDevice(host, name, IDF_has_pointer | IDF_has_keyboard);
00090 }
00091
00092
00093
00094
00095
00096
00097 GraphicsWindowInputDevice::
00098 GraphicsWindowInputDevice(const GraphicsWindowInputDevice ©)
00099 {
00100 *this = copy;
00101 }
00102
00103
00104
00105
00106
00107
00108 void GraphicsWindowInputDevice::
00109 operator = (const GraphicsWindowInputDevice ©)
00110 {
00111 LightMutexHolder holder(_lock);
00112 LightMutexHolder holder1(copy._lock);
00113 _host = copy._host;
00114 _name = copy._name;
00115 _flags = copy._flags;
00116 _device_index = copy._device_index;
00117 _event_sequence = copy._event_sequence;
00118 _pointer_mode_enable = copy._pointer_mode_enable;
00119 _pointer_speed = copy._pointer_speed;
00120 _pointer_true_x = copy._pointer_true_x;
00121 _pointer_true_y = copy._pointer_true_y;
00122 _enable_pointer_events = copy._enable_pointer_events;
00123 _mouse_data = copy._mouse_data;
00124 _true_mouse_data = copy._true_mouse_data;
00125 _button_events = copy._button_events;
00126 _pointer_events = copy._pointer_events;
00127 }
00128
00129
00130
00131
00132
00133
00134 GraphicsWindowInputDevice::
00135 ~GraphicsWindowInputDevice() {
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 bool GraphicsWindowInputDevice::
00148 has_button_event() const {
00149 LightMutexHolder holder(_lock);
00150 return !_button_events.empty();
00151 }
00152
00153
00154
00155
00156
00157
00158
00159 ButtonEvent GraphicsWindowInputDevice::
00160 get_button_event() {
00161 LightMutexHolder holder(_lock);
00162 ButtonEvent be = _button_events.front();
00163 _button_events.pop_front();
00164 return be;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 bool GraphicsWindowInputDevice::
00176 has_pointer_event() const {
00177 LightMutexHolder holder(_lock);
00178 return (_pointer_events != 0);
00179 }
00180
00181
00182
00183
00184
00185
00186
00187 PT(PointerEventList) GraphicsWindowInputDevice::
00188 get_pointer_events() {
00189 LightMutexHolder holder(_lock);
00190 PT(PointerEventList) result = _pointer_events;
00191 _pointer_events = 0;
00192 return result;
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 void GraphicsWindowInputDevice::
00214 enable_pointer_mode(double speed) {
00215 LightMutexHolder holder(_lock);
00216 nassertv(_device_index != 0);
00217 _pointer_mode_enable = true;
00218 _pointer_speed = speed;
00219 _pointer_true_x = _host->get_x_size() * 0.5;
00220 _pointer_true_y = _host->get_y_size() * 0.5;
00221 _mouse_data._in_window = true;
00222 _mouse_data._xpos = int(_pointer_true_x + 0.5);
00223 _mouse_data._ypos = int(_pointer_true_y + 0.5);
00224 }
00225
00226
00227
00228
00229
00230
00231 void GraphicsWindowInputDevice::
00232 disable_pointer_mode() {
00233 LightMutexHolder holder(_lock);
00234 nassertv(_device_index != 0);
00235 _pointer_mode_enable = false;
00236 _pointer_speed = 1.0;
00237 _pointer_true_x = 0.0;
00238 _pointer_true_y = 0.0;
00239 _mouse_data = _true_mouse_data;
00240 }
00241
00242
00243
00244
00245
00246
00247 void GraphicsWindowInputDevice::
00248 set_pointer(bool inwin, int x, int y, double time) {
00249 LightMutexHolder holder(_lock);
00250
00251 int delta_x = x - _true_mouse_data._xpos;
00252 int delta_y = y - _true_mouse_data._ypos;
00253 _true_mouse_data._in_window = inwin;
00254 _true_mouse_data._xpos = x;
00255 _true_mouse_data._ypos = y;
00256
00257 if (_pointer_mode_enable) {
00258 _pointer_true_x += (delta_x * _pointer_speed);
00259 _pointer_true_y += (delta_y * _pointer_speed);
00260 double xhi = _host->get_x_size();
00261 double yhi = _host->get_y_size();
00262 if (_pointer_true_x < 0.0) _pointer_true_x = 0.0;
00263 if (_pointer_true_y < 0.0) _pointer_true_y = 0.0;
00264 if (_pointer_true_x > xhi) _pointer_true_x = xhi;
00265 if (_pointer_true_y > yhi) _pointer_true_y = yhi;
00266 _mouse_data._in_window = true;
00267 _mouse_data._xpos = int(_pointer_true_x + 0.5);
00268 _mouse_data._ypos = int(_pointer_true_y + 0.5);
00269 } else {
00270 _mouse_data = _true_mouse_data;
00271 }
00272
00273 if (_enable_pointer_events) {
00274 int seq = _event_sequence++;
00275 if (_pointer_events == 0) {
00276 _pointer_events = new PointerEventList();
00277 }
00278 _pointer_events->add_event(_mouse_data._in_window,
00279 _mouse_data._xpos,
00280 _mouse_data._ypos,
00281 seq, time);
00282 }
00283 }
00284
00285
00286
00287
00288
00289
00290 void GraphicsWindowInputDevice::
00291 button_down(ButtonHandle button, double time) {
00292 LightMutexHolder holder(_lock);
00293 _button_events.push_back(ButtonEvent(button, ButtonEvent::T_down, time));
00294 _buttons_held.insert(button);
00295 }
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 void GraphicsWindowInputDevice::
00306 button_resume_down(ButtonHandle button, double time) {
00307 LightMutexHolder holder(_lock);
00308 _button_events.push_back(ButtonEvent(button, ButtonEvent::T_resume_down, time)
00309 );
00310 _buttons_held.insert(button);
00311 }
00312
00313
00314
00315
00316
00317
00318 void GraphicsWindowInputDevice::
00319 button_up(ButtonHandle button, double time) {
00320 LightMutexHolder holder(_lock);
00321 _button_events.push_back(ButtonEvent(button, ButtonEvent::T_up, time));
00322 _buttons_held.erase(button);
00323 }
00324
00325
00326
00327
00328
00329
00330
00331 void GraphicsWindowInputDevice::
00332 keystroke(int keycode, double time) {
00333 LightMutexHolder holder(_lock);
00334 _button_events.push_back(ButtonEvent(keycode, time));
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 void GraphicsWindowInputDevice::
00346 candidate(const wstring &candidate_string, size_t highlight_start,
00347 size_t highlight_end, size_t cursor_pos) {
00348 LightMutexHolder holder(_lock);
00349 _button_events.push_back(ButtonEvent(candidate_string,
00350 highlight_start, highlight_end,
00351 cursor_pos));
00352 }
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 void GraphicsWindowInputDevice::
00366 focus_lost(double time) {
00367 LightMutexHolder holder(_lock);
00368 ButtonsHeld::iterator bi;
00369 for (bi = _buttons_held.begin(); bi != _buttons_held.end(); ++bi) {
00370 _button_events.push_back(ButtonEvent(*bi, ButtonEvent::T_up, time));
00371 }
00372 _buttons_held.clear();
00373 }