Panda3D
 All Classes Functions Variables Enumerations
virtualMouse.cxx
1 // Filename: virtualMouse.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 "virtualMouse.h"
16 #include "dataNodeTransmit.h"
17 
18 TypeHandle VirtualMouse::_type_handle;
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: VirtualMouse::Constructor
22 // Access: Published
23 // Description:
24 ////////////////////////////////////////////////////////////////////
25 VirtualMouse::
26 VirtualMouse(const string &name) :
27  DataNode(name)
28 {
29  _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type());
30  _pixel_size_output = define_output("pixel_size", EventStoreVec2::get_class_type());
31  _xy_output = define_output("xy", EventStoreVec2::get_class_type());
32  _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
33 
34  _pixel_xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
35  _pixel_size = new EventStoreVec2(LPoint2(0.0f, 0.0f));
36  _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
37  _button_events = new ButtonEventList;
38  _next_button_events = new ButtonEventList;
39 
40  _mouse_x = 0;
41  _mouse_y = 0;
42  _win_width = 100;
43  _win_height = 100;
44  _mouse_on = false;
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: VirtualMouse::set_mouse_pos
49 // Access: Published
50 // Description: Sets the current mouse pixel location, where (0,0) is
51 // the upper left, and (width-1, height-1) is the lower
52 // right pixel of the virtual window.
53 ////////////////////////////////////////////////////////////////////
54 void VirtualMouse::
55 set_mouse_pos(int x, int y) {
56  _mouse_x = x;
57  _mouse_y = y;
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: VirtualMouse::set_window_size
62 // Access: Published
63 // Description: Sets the size of the "window" in which the mouse
64 // rolls. This changes the meaning of the values passed
65 // to set_mouse_pos().
66 ////////////////////////////////////////////////////////////////////
67 void VirtualMouse::
68 set_window_size(int width, int height) {
69  _win_width = width;
70  _win_height = height;
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: VirtualMouse::set_mouse_on
75 // Access: Published
76 // Description: Sets whether the mouse should appear to be within the
77 // window or not. If this is true, the mouse is within
78 // the window; if false, the mouse is not within the
79 // window (and set_mouse_pos() means nothing).
80 ////////////////////////////////////////////////////////////////////
81 void VirtualMouse::
82 set_mouse_on(bool flag) {
83  _mouse_on = flag;
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: VirtualMouse::press_button
88 // Access: Published
89 // Description: Simulates a mouse or keyboard button being depressed.
90 // This should be followed up by a call to
91 // release_button() sometime later (possibly
92 // immediately).
93 ////////////////////////////////////////////////////////////////////
94 void VirtualMouse::
96  _next_button_events->add_event(ButtonEvent(button, ButtonEvent::T_down));
97 }
98 
99 ////////////////////////////////////////////////////////////////////
100 // Function: VirtualMouse::release_button
101 // Access: Published
102 // Description: Simulates the button being released. This should
103 // follow a previous call to press_button().
104 ////////////////////////////////////////////////////////////////////
105 void VirtualMouse::
107  _next_button_events->add_event(ButtonEvent(button, ButtonEvent::T_up));
108 }
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function: VirtualMouse::do_transmit_data
112 // Access: Protected, Virtual
113 // Description: The virtual implementation of transmit_data(). This
114 // function receives an array of input parameters and
115 // should generate an array of output parameters. The
116 // input parameters may be accessed with the index
117 // numbers returned by the define_input() calls that
118 // were made earlier (presumably in the constructor);
119 // likewise, the output parameters should be set with
120 // the index numbers returned by the define_output()
121 // calls.
122 ////////////////////////////////////////////////////////////////////
123 void VirtualMouse::
124 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &,
125  DataNodeTransmit &output) {
126  // Swap in the button events, and clear them for next time.
127  PT(ButtonEventList) events = _button_events;
128  _button_events = _next_button_events;
129  _next_button_events = events;
130  _next_button_events->clear();
131  output.set_data(_button_events_output, EventParameter(_button_events));
132 
133  _pixel_size->set_value(LPoint2(_win_width, _win_height));
134  output.set_data(_pixel_size_output, EventParameter(_pixel_size));
135 
136  if (_mouse_on) {
137  // The mouse is within the window.
138  _pixel_xy->set_value(LPoint2(_mouse_x, _mouse_y));
139  output.set_data(_pixel_xy_output, EventParameter(_pixel_xy));
140 
141  // Normalize pixel motion to range [-1,1].
142  PN_stdfloat xf = (2.0f * (PN_stdfloat)_mouse_x) / (PN_stdfloat)_win_width - 1.0f;
143  PN_stdfloat yf = 1.0f - (2.0f * (PN_stdfloat)_mouse_y) / (PN_stdfloat)_win_height;
144  _xy->set_value(LPoint2(xf, yf));
145  output.set_data(_xy_output, EventParameter(_xy));
146  }
147 }
The fundamental type of node for the data graph.
Definition: dataNode.h:64
void press_button(ButtonHandle button)
Simulates a mouse or keyboard button being depressed.
An optional parameter associated with an event.
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 ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:28
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
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, height-1) is the lower right pixel of the virtual window.
This is a two-component point in space.
Definition: lpoint2.h:92
void set_mouse_on(bool flag)
Sets whether the mouse should appear to be within the window or not.
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...