Panda3D
mouseAndKeyboard.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 mouseAndKeyboard.cxx
10  * @author drose
11  * @date 2002-03-12
12  */
13 
14 #include "mouseAndKeyboard.h"
15 #include "mouseData.h"
16 #include "buttonHandle.h"
17 #include "buttonEvent.h"
18 #include "dataNodeTransmit.h"
19 #include "graphicsWindow.h"
20 
21 TypeHandle MouseAndKeyboard::_type_handle;
22 
23 /**
24  *
25  */
26 MouseAndKeyboard::
27 MouseAndKeyboard(GraphicsWindow *window, int device, const std::string &name) :
28  DataNode(name),
29  _window(window),
30  _device(window->get_input_device(device))
31 {
32  _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type());
33  _pixel_size_output = define_output("pixel_size", EventStoreVec2::get_class_type());
34  _xy_output = define_output("xy", EventStoreVec2::get_class_type());
35  _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
36  _pointer_events_output = define_output("pointer_events", PointerEventList::get_class_type());
37 
38  _pixel_xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
39  _pixel_size = new EventStoreVec2(LPoint2(0.0f, 0.0f));
40  _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
41 
42  _device->enable_pointer_events();
43 }
44 
45 /**
46  * Redirects the class to get the data from the mouse and keyboard associated
47  * with a different window and/or device number.
48  */
50 set_source(GraphicsWindow *window, int device) {
51  _window = window;
52  _device = window->get_input_device(device);
53 
54  //_device->enable_pointer_events();
55 }
56 
57 /**
58  * The virtual implementation of transmit_data(). This function receives an
59  * array of input parameters and should generate an array of output
60  * parameters. The input parameters may be accessed with the index numbers
61  * returned by the define_input() calls that were made earlier (presumably in
62  * the constructor); likewise, the output parameters should be set with the
63  * index numbers returned by the define_output() calls.
64  */
65 void MouseAndKeyboard::
66 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &,
67  DataNodeTransmit &output) {
68 
70 
71  if (device->has_button_event()) {
72  PT(ButtonEventList) bel = device->get_button_events();
73  output.set_data(_button_events_output, EventParameter(bel));
74  }
75  if (device->has_pointer_event()) {
76  PT(PointerEventList) pel = device->get_pointer_events();
77  output.set_data(_pointer_events_output, EventParameter(pel));
78  }
79 
80  // Get the window size.
81  WindowProperties properties = _window->get_properties();
82  if (properties.has_size()) {
83  int w = properties.get_x_size();
84  int h = properties.get_y_size();
85 
86  _pixel_size->set_value(LPoint2(w, h));
87  output.set_data(_pixel_size_output, EventParameter(_pixel_size));
88 
89  if (device->has_pointer()) {
90  PointerData mdata = device->get_pointer();
91 
92  if (mdata._in_window) {
93  // Get mouse motion in pixels.
94  _pixel_xy->set_value(LPoint2(mdata._xpos, mdata._ypos));
95  output.set_data(_pixel_xy_output, EventParameter(_pixel_xy));
96 
97  // Normalize pixel motion to range [-1,1].
98  PN_stdfloat xf = (PN_stdfloat)(2 * mdata._xpos) / (PN_stdfloat)w - 1.0f;
99  PN_stdfloat yf = 1.0f - (PN_stdfloat)(2 * mdata._ypos) / (PN_stdfloat)h;
100 
101  _xy->set_value(LPoint2(xf, yf));
102  output.set_data(_xy_output, EventParameter(_xy));
103  }
104  }
105  }
106 }
get_input_device
Returns the nth input device associated with the window.
The fundamental type of node for the data graph.
Definition: dataNode.h:52
void set_source(GraphicsWindow *window, int device)
Redirects the class to get the data from the mouse and keyboard associated with a different window an...
An optional parameter associated with an event.
bool has_pointer() const
Returns true if this is a pointing device.
Definition: inputDevice.I:107
Records a set of pointer events that happened recently.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
Definition: paramValue.h:103
Records a set of button events that happened recently.
A window, fullscreen or on a desktop, into which a graphics device sends its output for interactive d...
A container for the various kinds of properties we might ask to have on a graphics window before we o...
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
bool has_pointer_event() const
Returns true if this device has a pending pointer event (a mouse movement), or false otherwise.
Definition: inputDevice.cxx:82
This is a virtual input device that represents the keyboard and mouse pair that is associated with a ...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PointerData get_pointer() const
Returns the PointerData associated with the input device's pointer.
bool has_button_event() const
Returns true if this device has a pending button event (a mouse button or keyboard button down/up),...
Definition: inputDevice.cxx:59
int get_y_size() const
Returns size in pixels in the y dimension of the useful part of the window, not including decorations...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_x_size() const
Returns size in pixels in the x dimension of the useful part of the window, not including decorations...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
has_size
Returns true if the window size has been specified, false otherwise.
Encapsulates the data generated from (or sent into) any particular DataNode.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...