Panda3D

mouseSubregion.cxx

00001 // Filename: mouseSubregion.cxx
00002 // Created by:  drose (13May05)
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 "mouseSubregion.h"
00016 #include "dataNodeTransmit.h"
00017 
00018 TypeHandle MouseSubregion::_type_handle;
00019 
00020 ////////////////////////////////////////////////////////////////////
00021 //     Function: MouseSubregion::Constructor
00022 //       Access: Public
00023 //  Description:
00024 ////////////////////////////////////////////////////////////////////
00025 MouseSubregion::
00026 MouseSubregion(const string &name) :
00027   MouseInterfaceNode(name)
00028 {
00029   _pixel_xy_input = define_input("pixel_xy", EventStoreVec2::get_class_type());
00030   _pixel_size_input = define_input("pixel_size", EventStoreVec2::get_class_type());
00031   _xy_input = define_input("xy", EventStoreVec2::get_class_type());
00032   _button_events_input = define_input("button_events", ButtonEventList::get_class_type());
00033 
00034   _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type());
00035   _pixel_size_output = define_output("pixel_size", EventStoreVec2::get_class_type());
00036   _xy_output = define_output("xy", EventStoreVec2::get_class_type());
00037   _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
00038 
00039   _pixel_xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
00040   _pixel_size = new EventStoreVec2(LPoint2(0.0f, 0.0f));
00041   _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
00042   _button_events = new ButtonEventList;
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: MouseSubregion::Destructor
00047 //       Access: Published
00048 //  Description:
00049 ////////////////////////////////////////////////////////////////////
00050 MouseSubregion::
00051 ~MouseSubregion() {
00052 }
00053 
00054 ////////////////////////////////////////////////////////////////////
00055 //     Function: MouseSubregion::do_transmit_data
00056 //       Access: Protected, Virtual
00057 //  Description: The virtual implementation of transmit_data().  This
00058 //               function receives an array of input parameters and
00059 //               should generate an array of output parameters.  The
00060 //               input parameters may be accessed with the index
00061 //               numbers returned by the define_input() calls that
00062 //               were made earlier (presumably in the constructor);
00063 //               likewise, the output parameters should be set with
00064 //               the index numbers returned by the define_output()
00065 //               calls.
00066 ////////////////////////////////////////////////////////////////////
00067 void MouseSubregion::
00068 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &input,
00069                  DataNodeTransmit &output) {
00070   bool has_mouse = false;
00071 
00072   if (input.has_data(_xy_input)) {
00073     const EventStoreVec2 *xy;
00074     DCAST_INTO_V(xy, input.get_data(_xy_input).get_ptr());
00075     const LVecBase2 &p = xy->get_value();
00076 
00077     // Scale the old value into the new range.
00078     LVecBase2 n((p[0] - _minx) * _scalex - 1.0f, (p[1] - _miny) * _scaley - 1.0f);
00079 
00080     // If the mouse is indeed within the display region, pass it down.
00081     if (n[0] >= -1.0f && n[0] <= 1.0f &&
00082         n[1] >= -1.0f && n[1] <= 1.0f) {
00083       _xy->set_value(n);
00084       output.set_data(_xy_output, EventParameter(_xy));
00085 
00086       // Also compute the pixel coordinates, based on the supplied
00087       // pixel_size.
00088       if (input.has_data(_pixel_size_input)) {
00089         const EventStoreVec2 *pixel_size;
00090         DCAST_INTO_V(pixel_size, input.get_data(_pixel_size_input).get_ptr());
00091         const LVecBase2 &s = pixel_size->get_value();
00092 
00093         PN_stdfloat xf = (1.0f + n[0]) * 0.5f * s[0];
00094         PN_stdfloat yf = (1.0f - n[1]) * 0.5f * s[1];
00095         
00096         _pixel_xy->set_value(LPoint2(xf, yf));
00097         output.set_data(_pixel_xy_output, EventParameter(_pixel_xy));
00098       }
00099 
00100       has_mouse = true;
00101     }
00102   }
00103 
00104   if (has_mouse) {
00105     // If we have the mouse, send all of the mouse buttons.
00106     output.set_data(_button_events_output, input.get_data(_button_events_input));
00107   } else {
00108     // Otherwise, send only the button-up events.
00109     _button_events->clear();
00110 
00111     if (input.has_data(_button_events_input)) {
00112       const ButtonEventList *button_events;
00113       DCAST_INTO_V(button_events, input.get_data(_button_events_input).get_ptr());
00114       int num_events = button_events->get_num_events();
00115       for (int i = 0; i < num_events; i++) {
00116         const ButtonEvent &be = button_events->get_event(i);
00117         if (be._type == ButtonEvent::T_up) {
00118           // Don't suppress this button event; pass it through.
00119           _button_events->add_event(be);
00120         }
00121       }
00122     }
00123 
00124     if (_button_events->get_num_events() != 0) {
00125       output.set_data(_button_events_output, EventParameter(_button_events));
00126     }
00127   }
00128 
00129 
00130   // Now scale the window size.
00131   if (input.has_data(_pixel_size_input)) {
00132     const EventStoreVec2 *pixel_size;
00133     DCAST_INTO_V(pixel_size, input.get_data(_pixel_size_input).get_ptr());
00134     const LVecBase2 &s = pixel_size->get_value();
00135 
00136     LVecBase2 n(s[0] * (_r - _l), s[1] * (_t - _b));
00137     _pixel_size->set_value(n);
00138     output.set_data(_pixel_size_output, EventParameter(_pixel_size));
00139   }
00140 }
 All Classes Functions Variables Enumerations