Panda3D
graphicsWindowInputDevice.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file graphicsWindowInputDevice.cxx
10  * @author drose
11  * @date 2000-05-24
12  */
13 
15 #include "graphicsWindow.h"
16 #include "mouseButton.h"
17 #include "keyboardButton.h"
18 
19 TypeHandle GraphicsWindowInputDevice::_type_handle;
20 
21 using std::string;
22 
23 /**
24  * Defines a new InputDevice for the window. Most windows will have exactly
25  * one InputDevice: a keyboard/mouse pair. Some may also add joystick data,
26  * or additional mice or something.
27  *
28  * This private constructor is only used internally by the named constructors,
29  * below.
30  */
31 GraphicsWindowInputDevice::
32 GraphicsWindowInputDevice(GraphicsWindow *host, const string &name, bool pointer, bool keyboard) :
33  InputDevice(name, DeviceClass::virtual_device)
34 {
35  if (pointer) {
36  enable_feature(Feature::pointer);
37  add_pointer(PointerType::mouse, 0);
38  }
39  if (keyboard) {
40  enable_feature(Feature::keyboard);
41  }
42 }
43 
44 /**
45  * This named constructor returns an input device that only has a pointing
46  * device, no keyboard.
47  */
48 PT(GraphicsWindowInputDevice) GraphicsWindowInputDevice::
49 pointer_only(GraphicsWindow *host, const string &name) {
50  return new GraphicsWindowInputDevice(host, name, true, false);
51 }
52 
53 /**
54  * This named constructor returns an input device that only has a keyboard, no
55  * pointing device.
56  */
57 PT(GraphicsWindowInputDevice) GraphicsWindowInputDevice::
58 keyboard_only(GraphicsWindow *host, const string &name) {
59  return new GraphicsWindowInputDevice(host, name, false, true);
60 }
61 
62 /**
63  * This named constructor returns an input device that has both a keyboard and
64  * pointer.
65  */
66 PT(GraphicsWindowInputDevice) GraphicsWindowInputDevice::
67 pointer_and_keyboard(GraphicsWindow *host, const string &name) {
68  return new GraphicsWindowInputDevice(host, name, true, true);
69 }
70 
71 /**
72  * Records that the indicated button has been depressed.
73  */
75 button_down(ButtonHandle button, double time) {
76  LightMutexHolder holder(_lock);
77  _button_events->add_event(ButtonEvent(button, ButtonEvent::T_down, time));
78  _buttons_held.insert(button);
79 }
80 
81 /**
82  * Records that the indicated button was depressed earlier, and we only just
83  * detected the event after the fact. This is mainly useful for tracking the
84  * state of modifier keys.
85  */
87 button_resume_down(ButtonHandle button, double time) {
88  LightMutexHolder holder(_lock);
89  _button_events->add_event(ButtonEvent(button, ButtonEvent::T_resume_down, time));
90  _buttons_held.insert(button);
91 }
92 
93 /**
94  * Records that the indicated button has been released.
95  */
97 button_up(ButtonHandle button, double time) {
98  LightMutexHolder holder(_lock);
99  _button_events->add_event(ButtonEvent(button, ButtonEvent::T_up, time));
100  _buttons_held.erase(button);
101 }
102 
103 /**
104  * Records that the indicated keystroke has been generated.
105  */
107 keystroke(int keycode, double time) {
108  LightMutexHolder holder(_lock);
109  _button_events->add_event(ButtonEvent(keycode, time));
110 }
111 
112 /**
113  * Records that the indicated candidate string has been highlighted. This is
114  * used to implement IME support for typing in international languages,
115  * especially Chinese/Japanese/Korean.
116  */
118 candidate(const std::wstring &candidate_string, size_t highlight_start,
119  size_t highlight_end, size_t cursor_pos) {
120  LightMutexHolder holder(_lock);
121  _button_events->add_event(ButtonEvent(candidate_string,
122  highlight_start, highlight_end,
123  cursor_pos));
124 }
125 
126 /**
127  * This should be called when the window focus is lost, so that we may miss
128  * upcoming button events (especially "up" events) for the next period of
129  * time. It generates keyboard and mouse "up" events for those buttons that
130  * we previously sent unpaired "down" events, so that the Panda application
131  * will believe all buttons are now released.
132  */
134 focus_lost(double time) {
135  LightMutexHolder holder(_lock);
136  ButtonsHeld::iterator bi;
137  for (bi = _buttons_held.begin(); bi != _buttons_held.end(); ++bi) {
138  _button_events->add_event(ButtonEvent(*bi, ButtonEvent::T_up, time));
139  }
140  _buttons_held.clear();
141 }
142 
143 /**
144  * Records that the indicated button has been depressed.
145  */
147 raw_button_down(ButtonHandle button, double time) {
148  LightMutexHolder holder(_lock);
149  _button_events->add_event(ButtonEvent(button, ButtonEvent::T_raw_down, time));
150 }
151 
152 /**
153  * Records that the indicated button has been released.
154  */
156 raw_button_up(ButtonHandle button, double time) {
157  LightMutexHolder holder(_lock);
158  _button_events->add_event(ButtonEvent(button, ButtonEvent::T_raw_up, time));
159 }
160 
161 /**
162  * To be called by a particular kind of GraphicsWindow to indicate that the
163  * pointer is within the window, at the given pixel coordinates.
164  */
166 set_pointer_in_window(double x, double y, double time) {
167  LightMutexHolder holder(_lock);
168  PointerData data = _pointers[0];
169  data._id = 0;
170  data._type = PointerType::mouse;
171  data._xpos = x;
172  data._ypos = y;
173  data._in_window = true;
174  InputDevice::update_pointer(data, time);
175 }
176 
177 /**
178  * To be called by a particular kind of GraphicsWindow to indicate that the
179  * pointer is no longer within the window.
180  */
183  LightMutexHolder holder(_lock);
184  if (_pointers.empty()) {
185  return;
186  }
187 
188  _pointers[0]._in_window = false;
189  _pointers[0]._pressure = 0.0;
190 
191  if (_enable_pointer_events) {
192  int seq = _event_sequence++;
193  if (_pointer_events.is_null()) {
194  _pointer_events = new PointerEventList();
195  }
196  _pointer_events->add_event(_pointers[0]._in_window,
197  _pointers[0]._xpos,
198  _pointers[0]._ypos,
199  seq, time);
200  }
201 }
void button_down(ButtonHandle button, double time=ClockObject::get_global_clock() ->get_frame_time())
Records that the indicated button has been depressed.
void keystroke(int keycode, double time=ClockObject::get_global_clock() ->get_frame_time())
Records that the indicated keystroke has been generated.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_pointer_in_window(double x, double y, double time=ClockObject::get_global_clock() ->get_frame_time())
To be called by a particular kind of GraphicsWindow to indicate that the pointer is within the window...
Records a set of pointer events that happened recently.
Records a button event of some kind.
Definition: buttonEvent.h:46
A window, fullscreen or on a desktop, into which a graphics device sends its output for interactive d...
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:26
void candidate(const std::wstring &candidate_string, size_t highlight_start, size_t highlight_end, size_t cursor_pos)
Records that the indicated candidate string has been highlighted.
This is a structure representing a single input device.
Definition: inputDevice.h:53
Similar to MutexHolder, but for a light mutex.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Holds the data that might be generated by a 2-d pointer input device, such as the mouse in the Graphi...
Definition: pointerData.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void button_up(ButtonHandle button, double time=ClockObject::get_global_clock() ->get_frame_time())
Records that the indicated button has been released.
void set_pointer_out_of_window(double time=ClockObject::get_global_clock() ->get_frame_time())
To be called by a particular kind of GraphicsWindow to indicate that the pointer is no longer within ...
void raw_button_down(ButtonHandle button, double time=ClockObject::get_global_clock() ->get_frame_time())
Records that the indicated button has been depressed.
This is a virtual input device that represents the keyboard and mouse pair that is associated with a ...
PT(GraphicsWindowInputDevice) GraphicsWindowInputDevice
This named constructor returns an input device that only has a pointing device, no keyboard.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void button_resume_down(ButtonHandle button, double time=ClockObject::get_global_clock() ->get_frame_time())
Records that the indicated button was depressed earlier, and we only just detected the event after th...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
void focus_lost(double time=ClockObject::get_global_clock() ->get_frame_time())
This should be called when the window focus is lost, so that we may miss upcoming button events (espe...
void raw_button_up(ButtonHandle button, double time=ClockObject::get_global_clock() ->get_frame_time())
Records that the indicated button has been released.