Panda3D

mouseAndKeyboard.cxx

00001 // Filename: mouseAndKeyboard.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 "mouseAndKeyboard.h"
00016 #include "mouseData.h"
00017 #include "buttonHandle.h"
00018 #include "buttonEvent.h"
00019 #include "dataNodeTransmit.h"
00020 #include "graphicsWindow.h"
00021 
00022 TypeHandle MouseAndKeyboard::_type_handle;
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //     Function: MouseAndKeyboard::Constructor
00026 //       Access: Published
00027 //  Description:
00028 ////////////////////////////////////////////////////////////////////
00029 MouseAndKeyboard::
00030 MouseAndKeyboard(GraphicsWindow *window, int device, const string &name) :
00031   DataNode(name),
00032   _window(window),
00033   _device(device)
00034 {
00035   _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type());
00036   _pixel_size_output = define_output("pixel_size", EventStoreVec2::get_class_type());
00037   _xy_output = define_output("xy", EventStoreVec2::get_class_type());
00038   _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
00039   _pointer_events_output = define_output("pointer_events", PointerEventList::get_class_type());
00040 
00041   _pixel_xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
00042   _pixel_size = new EventStoreVec2(LPoint2(0.0f, 0.0f));
00043   _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
00044   _button_events = new ButtonEventList;
00045 }
00046 
00047 ////////////////////////////////////////////////////////////////////
00048 //     Function: MouseAndKeyboard::set_source
00049 //       Access: Published
00050 //  Description: Redirects the class to get the data from the mouse
00051 //               and keyboard associated with a different window
00052 //               and/or device number.
00053 ////////////////////////////////////////////////////////////////////
00054 void MouseAndKeyboard::
00055 set_source(GraphicsWindow *window, int device) {
00056   _window = window;
00057   _device = device;
00058 }
00059 
00060 ////////////////////////////////////////////////////////////////////
00061 //     Function: MouseAndKeyboard::get_source_window
00062 //       Access: Published
00063 //  Description: Returns the associated source window.
00064 ////////////////////////////////////////////////////////////////////
00065 PT(GraphicsWindow) MouseAndKeyboard::
00066 get_source_window() const {
00067   return _window;
00068 }
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //     Function: MouseAndKeyboard::get_source_device
00072 //       Access: Published
00073 //  Description: Returns the associated source device.
00074 ////////////////////////////////////////////////////////////////////
00075 int MouseAndKeyboard::
00076 get_source_device() const {
00077   return _device;
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function: MouseAndKeyboard::do_transmit_data
00082 //       Access: Protected, Virtual
00083 //  Description: The virtual implementation of transmit_data().  This
00084 //               function receives an array of input parameters and
00085 //               should generate an array of output parameters.  The
00086 //               input parameters may be accessed with the index
00087 //               numbers returned by the define_input() calls that
00088 //               were made earlier (presumably in the constructor);
00089 //               likewise, the output parameters should be set with
00090 //               the index numbers returned by the define_output()
00091 //               calls.
00092 ////////////////////////////////////////////////////////////////////
00093 void MouseAndKeyboard::
00094 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &,
00095                  DataNodeTransmit &output) {
00096   if (_window->has_button_event(_device)) {
00097     // Fill up the button events.
00098     _button_events->clear();
00099     while (_window->has_button_event(_device)) {
00100       ButtonEvent be = _window->get_button_event(_device);
00101       _button_events->add_event(be);
00102     }
00103     output.set_data(_button_events_output, EventParameter(_button_events));
00104   }
00105   if (_window->has_pointer_event(_device)) {
00106     PT(PointerEventList) pel = _window->get_pointer_events(_device);
00107     output.set_data(_pointer_events_output, EventParameter(pel));
00108   }
00109   
00110   // Get the window size.
00111   WindowProperties properties = _window->get_properties();
00112   if (properties.has_size()) {
00113     int w = properties.get_x_size();
00114     int h = properties.get_y_size();
00115 
00116     _pixel_size->set_value(LPoint2(w, h));
00117     output.set_data(_pixel_size_output, EventParameter(_pixel_size));
00118 
00119     if (_window->has_pointer(_device)) {
00120       const MouseData &mdata = _window->get_pointer(_device);
00121 
00122       if (mdata._in_window) {
00123         // Get mouse motion in pixels.
00124         _pixel_xy->set_value(LPoint2(mdata._xpos, mdata._ypos));
00125         output.set_data(_pixel_xy_output, EventParameter(_pixel_xy));
00126         
00127         // Normalize pixel motion to range [-1,1].
00128         PN_stdfloat xf = (PN_stdfloat)(2 * mdata._xpos) / (PN_stdfloat)w - 1.0f;
00129         PN_stdfloat yf = 1.0f - (PN_stdfloat)(2 * mdata._ypos) / (PN_stdfloat)h;
00130         
00131         _xy->set_value(LPoint2(xf, yf));
00132         output.set_data(_xy_output, EventParameter(_xy));
00133       }
00134     }
00135   }
00136 }
 All Classes Functions Variables Enumerations