Panda3D
virtualMouse.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 virtualMouse.cxx
10  * @author drose
11  * @date 2002-03-12
12  */
13 
14 #include "virtualMouse.h"
15 #include "dataNodeTransmit.h"
16 
17 TypeHandle VirtualMouse::_type_handle;
18 
19 /**
20  *
21  */
22 VirtualMouse::
23 VirtualMouse(const std::string &name) :
24  DataNode(name)
25 {
26  _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type());
27  _pixel_size_output = define_output("pixel_size", EventStoreVec2::get_class_type());
28  _xy_output = define_output("xy", EventStoreVec2::get_class_type());
29  _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
30 
31  _pixel_xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
32  _pixel_size = new EventStoreVec2(LPoint2(0.0f, 0.0f));
33  _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
34  _button_events = new ButtonEventList;
35  _next_button_events = new ButtonEventList;
36 
37  _mouse_x = 0;
38  _mouse_y = 0;
39  _win_width = 100;
40  _win_height = 100;
41  _mouse_on = false;
42 }
43 
44 /**
45  * Sets the current mouse pixel location, where (0,0) is the upper left, and
46  * (width-1, height-1) is the lower right pixel of the virtual window.
47  */
49 set_mouse_pos(int x, int y) {
50  _mouse_x = x;
51  _mouse_y = y;
52 }
53 
54 /**
55  * Sets the size of the "window" in which the mouse rolls. This changes the
56  * meaning of the values passed to set_mouse_pos().
57  */
59 set_window_size(int width, int height) {
60  _win_width = width;
61  _win_height = height;
62 }
63 
64 /**
65  * Sets whether the mouse should appear to be within the window or not. If
66  * this is true, the mouse is within the window; if false, the mouse is not
67  * within the window (and set_mouse_pos() means nothing).
68  */
70 set_mouse_on(bool flag) {
71  _mouse_on = flag;
72 }
73 
74 /**
75  * Simulates a mouse or keyboard button being depressed. This should be
76  * followed up by a call to release_button() sometime later (possibly
77  * immediately).
78  */
80 press_button(ButtonHandle button) {
81  _next_button_events->add_event(ButtonEvent(button, ButtonEvent::T_down));
82 }
83 
84 /**
85  * Simulates the button being released. This should follow a previous call to
86  * press_button().
87  */
90  _next_button_events->add_event(ButtonEvent(button, ButtonEvent::T_up));
91 }
92 
93 /**
94  * The virtual implementation of transmit_data(). This function receives an
95  * array of input parameters and should generate an array of output
96  * parameters. The input parameters may be accessed with the index numbers
97  * returned by the define_input() calls that were made earlier (presumably in
98  * the constructor); likewise, the output parameters should be set with the
99  * index numbers returned by the define_output() calls.
100  */
101 void VirtualMouse::
102 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &,
103  DataNodeTransmit &output) {
104  // Swap in the button events, and clear them for next time.
105  PT(ButtonEventList) events = _button_events;
106  _button_events = _next_button_events;
107  _next_button_events = events;
108  _next_button_events->clear();
109  output.set_data(_button_events_output, EventParameter(_button_events));
110 
111  _pixel_size->set_value(LPoint2(_win_width, _win_height));
112  output.set_data(_pixel_size_output, EventParameter(_pixel_size));
113 
114  if (_mouse_on) {
115  // The mouse is within the window.
116  _pixel_xy->set_value(LPoint2(_mouse_x, _mouse_y));
117  output.set_data(_pixel_xy_output, EventParameter(_pixel_xy));
118 
119  // Normalize pixel motion to range [-1,1].
120  PN_stdfloat xf = (2.0f * (PN_stdfloat)_mouse_x) / (PN_stdfloat)_win_width - 1.0f;
121  PN_stdfloat yf = 1.0f - (2.0f * (PN_stdfloat)_mouse_y) / (PN_stdfloat)_win_height;
122  _xy->set_value(LPoint2(xf, yf));
123  output.set_data(_xy_output, EventParameter(_xy));
124  }
125 }
Records a set of button events that happened recently.
Records a button event of some kind.
Definition: buttonEvent.h:46
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:26
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...
Encapsulates the data generated from (or sent into) any particular DataNode.
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
The fundamental type of node for the data graph.
Definition: dataNode.h:52
An optional parameter associated with an event.
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
Definition: paramValue.h:103
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
void set_window_size(int width, int height)
Sets the size of the "window" in which the mouse rolls.
void release_button(ButtonHandle button)
Simulates the button being released.
void set_mouse_pos(int x, int y)
Sets the current mouse pixel location, where (0,0) is the upper left, and (width-1,...
void set_mouse_on(bool flag)
Sets whether the mouse should appear to be within the window or not.
void press_button(ButtonHandle button)
Simulates a mouse or keyboard button being depressed.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.