Panda3D
 All Classes Functions Variables Enumerations
mouseAndKeyboard.cxx
1 // Filename: mouseAndKeyboard.cxx
2 // Created by: drose (12Mar02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "mouseAndKeyboard.h"
16 #include "mouseData.h"
17 #include "buttonHandle.h"
18 #include "buttonEvent.h"
19 #include "dataNodeTransmit.h"
20 #include "graphicsWindow.h"
21 
22 TypeHandle MouseAndKeyboard::_type_handle;
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: MouseAndKeyboard::Constructor
26 // Access: Published
27 // Description:
28 ////////////////////////////////////////////////////////////////////
29 MouseAndKeyboard::
30 MouseAndKeyboard(GraphicsWindow *window, int device, const string &name) :
31  DataNode(name),
32  _window(window),
33  _device(device)
34 {
35  _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type());
36  _pixel_size_output = define_output("pixel_size", EventStoreVec2::get_class_type());
37  _xy_output = define_output("xy", EventStoreVec2::get_class_type());
38  _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
39  _pointer_events_output = define_output("pointer_events", PointerEventList::get_class_type());
40 
41  _pixel_xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
42  _pixel_size = new EventStoreVec2(LPoint2(0.0f, 0.0f));
43  _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
44  _button_events = new ButtonEventList;
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: MouseAndKeyboard::set_source
49 // Access: Published
50 // Description: Redirects the class to get the data from the mouse
51 // and keyboard associated with a different window
52 // and/or device number.
53 ////////////////////////////////////////////////////////////////////
55 set_source(GraphicsWindow *window, int device) {
56  _window = window;
57  _device = device;
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: MouseAndKeyboard::get_source_window
62 // Access: Published
63 // Description: Returns the associated source window.
64 ////////////////////////////////////////////////////////////////////
66 get_source_window() const {
67  return _window;
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: MouseAndKeyboard::get_source_device
72 // Access: Published
73 // Description: Returns the associated source device.
74 ////////////////////////////////////////////////////////////////////
75 int MouseAndKeyboard::
76 get_source_device() const {
77  return _device;
78 }
79 
80 ////////////////////////////////////////////////////////////////////
81 // Function: MouseAndKeyboard::do_transmit_data
82 // Access: Protected, Virtual
83 // Description: The virtual implementation of transmit_data(). This
84 // function receives an array of input parameters and
85 // should generate an array of output parameters. The
86 // input parameters may be accessed with the index
87 // numbers returned by the define_input() calls that
88 // were made earlier (presumably in the constructor);
89 // likewise, the output parameters should be set with
90 // the index numbers returned by the define_output()
91 // calls.
92 ////////////////////////////////////////////////////////////////////
93 void MouseAndKeyboard::
94 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &,
95  DataNodeTransmit &output) {
96  if (_window->has_button_event(_device)) {
97  // Fill up the button events.
98  _button_events->clear();
99  while (_window->has_button_event(_device)) {
100  ButtonEvent be = _window->get_button_event(_device);
101  _button_events->add_event(be);
102  }
103  output.set_data(_button_events_output, EventParameter(_button_events));
104  }
105  if (_window->has_pointer_event(_device)) {
106  PT(PointerEventList) pel = _window->get_pointer_events(_device);
107  output.set_data(_pointer_events_output, EventParameter(pel));
108  }
109 
110  // Get the window size.
111  WindowProperties properties = _window->get_properties();
112  if (properties.has_size()) {
113  int w = properties.get_x_size();
114  int h = properties.get_y_size();
115 
116  _pixel_size->set_value(LPoint2(w, h));
117  output.set_data(_pixel_size_output, EventParameter(_pixel_size));
118 
119  if (_window->has_pointer(_device)) {
120  const MouseData &mdata = _window->get_pointer(_device);
121 
122  if (mdata._in_window) {
123  // Get mouse motion in pixels.
124  _pixel_xy->set_value(LPoint2(mdata._xpos, mdata._ypos));
125  output.set_data(_pixel_xy_output, EventParameter(_pixel_xy));
126 
127  // Normalize pixel motion to range [-1,1].
128  PN_stdfloat xf = (PN_stdfloat)(2 * mdata._xpos) / (PN_stdfloat)w - 1.0f;
129  PN_stdfloat yf = 1.0f - (PN_stdfloat)(2 * mdata._ypos) / (PN_stdfloat)h;
130 
131  _xy->set_value(LPoint2(xf, yf));
132  output.set_data(_xy_output, EventParameter(_xy));
133  }
134  }
135  }
136 }
The fundamental type of node for the data graph.
Definition: dataNode.h:64
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.
Reads the mouse and/or keyboard data sent from a GraphicsWindow, and transmits it down the data graph...
Records a set of pointer events that happened recently.
Records a button event of some kind.
Definition: buttonEvent.h:53
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
Definition: paramValue.h:109
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.
Holds the data that might be generated by a 2-d pointer input device, such as the mouse in the Graphi...
Definition: mouseData.h:28
int get_x_size() const
Returns size in pixels in the x dimension of the useful part of the window, not including decorations...
This is a two-component point in space.
Definition: lpoint2.h:92
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
Encapsulates the data generated from (or sent into) any particular DataNode.
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...