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