00001 // Filename: virtualMouse.cxx 00002 // Created by: drose (12Mar02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "virtualMouse.h" 00020 00021 TypeHandle VirtualMouse::_type_handle; 00022 00023 //////////////////////////////////////////////////////////////////// 00024 // Function: VirtualMouse::Constructor 00025 // Access: Published 00026 // Description: 00027 //////////////////////////////////////////////////////////////////// 00028 VirtualMouse:: 00029 VirtualMouse(const string &name) : 00030 DataNode(name) 00031 { 00032 _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type()); 00033 _xy_output = define_output("xy", EventStoreVec2::get_class_type()); 00034 _button_events_output = define_output("button_events", ButtonEventList::get_class_type()); 00035 00036 _pixel_xy = new EventStoreVec2(LPoint2f(0.0f, 0.0f)); 00037 _xy = new EventStoreVec2(LPoint2f(0.0f, 0.0f)); 00038 _button_events = new ButtonEventList; 00039 _next_button_events = new ButtonEventList; 00040 00041 _mouse_x = 0; 00042 _mouse_y = 0; 00043 _win_width = 100; 00044 _win_height = 100; 00045 _mouse_on = false; 00046 } 00047 00048 //////////////////////////////////////////////////////////////////// 00049 // Function: VirtualMouse::set_mouse_pos 00050 // Access: Published 00051 // Description: Sets the current mouse pixel location, where (0,0) is 00052 // the upper left, and (width-1, height-1) is the lower 00053 // right pixel of the virtual window. 00054 //////////////////////////////////////////////////////////////////// 00055 void VirtualMouse:: 00056 set_mouse_pos(int x, int y) { 00057 _mouse_x = x; 00058 _mouse_y = y; 00059 } 00060 00061 //////////////////////////////////////////////////////////////////// 00062 // Function: VirtualMouse::set_window_size 00063 // Access: Published 00064 // Description: Sets the size of the "window" in which the mouse 00065 // rolls. This changes the meaning of the values passed 00066 // to set_mouse_pos(). 00067 //////////////////////////////////////////////////////////////////// 00068 void VirtualMouse:: 00069 set_window_size(int width, int height) { 00070 _win_width = width; 00071 _win_height = height; 00072 } 00073 00074 //////////////////////////////////////////////////////////////////// 00075 // Function: VirtualMouse::set_mouse_on 00076 // Access: Published 00077 // Description: Sets whether the mouse should appear to be within the 00078 // window or not. If this is true, the mouse is within 00079 // the window; if false, the mouse is not within the 00080 // window (and set_mouse_pos() means nothing). 00081 //////////////////////////////////////////////////////////////////// 00082 void VirtualMouse:: 00083 set_mouse_on(bool flag) { 00084 _mouse_on = flag; 00085 } 00086 00087 //////////////////////////////////////////////////////////////////// 00088 // Function: VirtualMouse::press_button 00089 // Access: Published 00090 // Description: Simulates a mouse or keyboard button being depressed. 00091 // This should be followed up by a call to 00092 // release_button() sometime later (possibly 00093 // immediately). 00094 //////////////////////////////////////////////////////////////////// 00095 void VirtualMouse:: 00096 press_button(ButtonHandle button) { 00097 _next_button_events->add_event(ButtonEvent(button, ButtonEvent::T_down)); 00098 } 00099 00100 //////////////////////////////////////////////////////////////////// 00101 // Function: VirtualMouse::release_button 00102 // Access: Published 00103 // Description: Simulates the button being released. This should 00104 // follow a previous call to press_button(). 00105 //////////////////////////////////////////////////////////////////// 00106 void VirtualMouse:: 00107 release_button(ButtonHandle button) { 00108 _next_button_events->add_event(ButtonEvent(button, ButtonEvent::T_up)); 00109 } 00110 00111 //////////////////////////////////////////////////////////////////// 00112 // Function: VirtualMouse::do_transmit_data 00113 // Access: Protected, Virtual 00114 // Description: The virtual implementation of transmit_data(). This 00115 // function receives an array of input parameters and 00116 // should generate an array of output parameters. The 00117 // input parameters may be accessed with the index 00118 // numbers returned by the define_input() calls that 00119 // were made earlier (presumably in the constructor); 00120 // likewise, the output parameters should be set with 00121 // the index numbers returned by the define_output() 00122 // calls. 00123 //////////////////////////////////////////////////////////////////// 00124 void VirtualMouse:: 00125 do_transmit_data(const DataNodeTransmit &, DataNodeTransmit &output) { 00126 // Swap in the button events, and clear them for next time. 00127 PT(ButtonEventList) events = _button_events; 00128 _button_events = _next_button_events; 00129 _next_button_events = events; 00130 _next_button_events->clear(); 00131 output.set_data(_button_events_output, EventParameter(_button_events)); 00132 00133 if (_mouse_on) { 00134 // The mouse is within the window. 00135 _pixel_xy->set_value(LPoint2f(_mouse_x, _mouse_y)); 00136 output.set_data(_pixel_xy_output, EventParameter(_pixel_xy)); 00137 00138 // Normalize pixel motion to range [-1,1]. 00139 float xf = (2.0f * (float)_mouse_x) / (float)_win_width - 1.0f; 00140 float yf = 1.0f - (2.0f * (float)_mouse_y) / (float)_win_height; 00141 _xy->set_value(LPoint2f(xf, yf)); 00142 output.set_data(_xy_output, EventParameter(_xy)); 00143 } 00144 }