interaction_tool.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <OgreCamera.h>
00031 #include <OgrePlane.h>
00032 #include <OgreRay.h>
00033 #include <OgreSceneNode.h>
00034 #include <OgreViewport.h>
00035 
00036 #include "rviz/render_panel.h"
00037 #include "rviz/selection/selection_handler.h"
00038 #include "rviz/selection/selection_manager.h"
00039 #include "rviz/view_controller.h"
00040 #include "rviz/viewport_mouse_event.h"
00041 #include "rviz/viewport_mouse_event.h"
00042 #include "rviz/visualization_manager.h"
00043 #include "rviz/load_resource.h"
00044 #include "rviz/properties/bool_property.h"
00045 
00046 #include "rviz/default_plugin/tools/interaction_tool.h"
00047 
00048 namespace rviz
00049 {
00050 
00051 InteractionTool::InteractionTool()
00052 {
00053   shortcut_key_ = 'i';
00054   hide_inactive_property_ = new BoolProperty("Hide Inactive Objects", true,
00055                                              "While holding down a mouse button, hide all other Interactive Objects.",
00056                                              getPropertyContainer(), SLOT( hideInactivePropertyChanged() ), this );
00057 }
00058 
00059 InteractionTool::~InteractionTool()
00060 {
00061 }
00062 
00063 void InteractionTool::onInitialize()
00064 {
00065   move_tool_.initialize( context_ );
00066   last_selection_frame_count_ = context_->getFrameCount();
00067   deactivate();
00068 }
00069 
00070 void InteractionTool::activate()
00071 {
00072   context_->getSelectionManager()->enableInteraction(true);
00073   context_->getSelectionManager()->setTextureSize(1);
00074 }
00075 
00076 void InteractionTool::deactivate()
00077 {
00078   context_->getSelectionManager()->enableInteraction(false);
00079 }
00080 
00081 void InteractionTool::updateFocus( const ViewportMouseEvent& event )
00082 {
00083   M_Picked results;
00084   // Pick exactly 1 pixel
00085   context_->getSelectionManager()->pick( event.viewport,
00086                                          event.x, event.y,
00087                                          event.x + 1, event.y + 1,
00088                                          results, true );
00089 
00090   last_selection_frame_count_ = context_->getFrameCount();
00091 
00092   InteractiveObjectPtr new_focused_object;
00093 
00094   // look for a valid handle in the result.
00095   M_Picked::iterator result_it = results.begin();
00096   if( result_it != results.end() )
00097   {
00098     Picked pick = result_it->second;
00099     SelectionHandler* handler = context_->getSelectionManager()->getHandler( pick.handle );
00100     if ( pick.pixel_count > 0 && handler )
00101     {
00102       InteractiveObjectPtr object = handler->getInteractiveObject().lock();
00103       if( object && object->isInteractive() )
00104       {
00105         new_focused_object = object;
00106       }
00107     }
00108   }
00109 
00110   // If the mouse has gone from one object to another, defocus the old
00111   // and focus the new.
00112   InteractiveObjectPtr new_obj = new_focused_object;
00113   InteractiveObjectPtr old_obj = focused_object_.lock();
00114   if( new_obj != old_obj )
00115   {
00116     // Only copy the event contents here, once we know we need to use
00117     // a modified version of it.
00118     ViewportMouseEvent event_copy = event;
00119     if( old_obj )
00120     {
00121       event_copy.type = QEvent::FocusOut;
00122       old_obj->handleMouseEvent( event_copy );
00123     }
00124 
00125     if( new_obj )
00126     {
00127       event_copy.type = QEvent::FocusIn;
00128       new_obj->handleMouseEvent( event_copy );
00129     }
00130   }
00131 
00132   focused_object_ = new_focused_object;
00133 }
00134 
00135 int InteractionTool::processMouseEvent( ViewportMouseEvent& event )
00136 {
00137   int flags = 0;
00138 
00139   if ( event.panel->contextMenuVisible() )
00140   {
00141     return flags;
00142   }
00143 
00144   // make sure we let the vis. manager render at least one frame between selection updates
00145   bool need_selection_update = context_->getFrameCount() > last_selection_frame_count_;
00146 
00147   // We are dragging if a button was down and is still down
00148   Qt::MouseButtons buttons = event.buttons_down & ( Qt::LeftButton | Qt::RightButton | Qt::MidButton );
00149   if ( event.type == QEvent::MouseButtonPress )
00150     buttons &= ~event.acting_button;
00151   bool dragging = buttons != 0;
00152 
00153   // unless we're dragging, check if there's a new object under the mouse
00154   if( need_selection_update &&
00155       !dragging &&
00156       event.type != QEvent::MouseButtonRelease )
00157   {
00158     updateFocus( event );
00159     flags = Render;
00160   }
00161 
00162   {
00163     InteractiveObjectPtr focused_object = focused_object_.lock();
00164     if( focused_object )
00165     {
00166       focused_object->handleMouseEvent( event );
00167       setCursor( focused_object->getCursor() );
00168       // this will disable everything but the current interactive object
00169       if ( hide_inactive_property_->getBool() )
00170       {
00171         context_->getSelectionManager()->enableInteraction(!dragging);
00172       }
00173     }
00174     else if( event.panel->getViewController() )
00175     {
00176       move_tool_.processMouseEvent( event );
00177       setCursor( move_tool_.getCursor() );
00178       if ( hide_inactive_property_->getBool() )
00179       {
00180         context_->getSelectionManager()->enableInteraction(true);
00181       }
00182     }
00183   }
00184 
00185   if( event.type == QEvent::MouseButtonRelease )
00186   {
00187     updateFocus( event );
00188   }
00189 
00190   return flags;
00191 }
00192 
00193 int InteractionTool::processKeyEvent( QKeyEvent* event, RenderPanel* panel )
00194 {
00195   return move_tool_.processKeyEvent( event, panel );
00196 }
00197 
00198 } // end namespace rviz
00199 
00200 #include <pluginlib/class_list_macros.h>
00201 PLUGINLIB_EXPORT_CLASS( rviz::InteractionTool, rviz::Tool )


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:27