Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/device/mouseAndKeyboard.cxx

Go to the documentation of this file.
00001 // Filename: mouseAndKeyboard.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 "mouseAndKeyboard.h"
00020 #include "mouseData.h"
00021 #include "buttonHandle.h"
00022 #include "buttonEvent.h"
00023 #include "dataNodeTransmit.h"
00024 #include "graphicsWindow.h"
00025 
00026 TypeHandle MouseAndKeyboard::_type_handle;
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //     Function: MouseAndKeyboard::Constructor
00030 //       Access: Public
00031 //  Description:
00032 ////////////////////////////////////////////////////////////////////
00033 MouseAndKeyboard::
00034 MouseAndKeyboard(GraphicsWindow *window, int device, const string &name) :
00035   DataNode(name),
00036   _window(window),
00037   _device(device)
00038 {
00039   _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type());
00040   _xy_output = define_output("xy", EventStoreVec2::get_class_type());
00041   _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
00042 
00043   _pixel_xy = new EventStoreVec2(LPoint2f(0.0f, 0.0f));
00044   _xy = new EventStoreVec2(LPoint2f(0.0f, 0.0f));
00045   _button_events = new ButtonEventList;
00046 }
00047 
00048 ////////////////////////////////////////////////////////////////////
00049 //     Function: MouseAndKeyboard::set_source
00050 //       Access: Public
00051 //  Description: Redirects the class to get the data from the mouse
00052 //               and keyboard associated with a different window
00053 //               and/or device number.
00054 ////////////////////////////////////////////////////////////////////
00055 void MouseAndKeyboard::
00056 set_source(GraphicsWindow *window, int device) {
00057   _window = window;
00058   _device = device;
00059 }
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //     Function: MouseAndKeyboard::do_transmit_data
00063 //       Access: Protected, Virtual
00064 //  Description: The virtual implementation of transmit_data().  This
00065 //               function receives an array of input parameters and
00066 //               should generate an array of output parameters.  The
00067 //               input parameters may be accessed with the index
00068 //               numbers returned by the define_input() calls that
00069 //               were made earlier (presumably in the constructor);
00070 //               likewise, the output parameters should be set with
00071 //               the index numbers returned by the define_output()
00072 //               calls.
00073 ////////////////////////////////////////////////////////////////////
00074 void MouseAndKeyboard::
00075 do_transmit_data(const DataNodeTransmit &, DataNodeTransmit &output) {
00076   if (_window->has_button_event(_device)) {
00077     // Fill up the button events.
00078     _button_events->clear();
00079     while (_window->has_button_event(_device)) {
00080       ButtonEvent be = _window->get_button_event(_device);
00081       _button_events->add_event(be);
00082     }
00083     output.set_data(_button_events_output, EventParameter(_button_events));
00084   }
00085 
00086   if (_window->has_pointer(_device)) {
00087     const MouseData &mdata = _window->get_mouse_data(_device);
00088 
00089     if (mdata._in_window) {
00090       // Get mouse motion in pixels.
00091       _pixel_xy->set_value(LPoint2f(mdata._xpos, mdata._ypos));
00092       output.set_data(_pixel_xy_output, EventParameter(_pixel_xy));
00093 
00094       WindowProperties properties = _window->get_properties();
00095       int w = properties.get_x_size();
00096       int h = properties.get_y_size();
00097 
00098       // Normalize pixel motion to range [-1,1].
00099       float xf = (float)(2 * mdata._xpos) / (float)w - 1.0f;
00100       float yf = 1.0f - (float)(2 * mdata._ypos) / (float)h;
00101 
00102       _xy->set_value(LPoint2f(xf, yf));
00103       output.set_data(_xy_output, EventParameter(_xy));
00104     }
00105   }
00106 }

Generated on Fri May 2 00:36:15 2003 for Panda by doxygen1.3