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 <OGRE/OgreCamera.h> 00031 #include <OGRE/OgrePlane.h> 00032 #include <OGRE/OgreRay.h> 00033 #include <OGRE/OgreSceneNode.h> 00034 #include <OGRE/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 00044 #include "rviz/default_plugin/tools/interaction_tool.h" 00045 00046 namespace rviz 00047 { 00048 00049 InteractionTool::InteractionTool() 00050 : MoveTool() 00051 { 00052 name_ = "Interact"; 00053 shortcut_key_ = 'i'; 00054 } 00055 00056 InteractionTool::~InteractionTool() 00057 { 00058 } 00059 00060 void InteractionTool::onInitialize() 00061 { 00062 last_selection_frame_count_ = manager_->getFrameCount(); 00063 deactivate(); 00064 } 00065 00066 void InteractionTool::activate() 00067 { 00068 manager_->getSelectionManager()->enableInteraction(true); 00069 manager_->getSelectionManager()->setTextureSize(2); 00070 } 00071 00072 void InteractionTool::deactivate() 00073 { 00074 manager_->getSelectionManager()->enableInteraction(false); 00075 } 00076 00077 void InteractionTool::updateFocus( const ViewportMouseEvent& event ) 00078 { 00079 M_Picked results; 00080 // Pick exactly 1 pixel 00081 manager_->getSelectionManager()->pick( event.viewport, 00082 event.x, event.y, 00083 event.x + 1, event.y + 1, 00084 results, true ); 00085 00086 last_selection_frame_count_ = manager_->getFrameCount(); 00087 00088 InteractiveObjectPtr new_focused_object; 00089 00090 // look for a valid handle in the result. 00091 M_Picked::iterator result_it = results.begin(); 00092 if( result_it != results.end() ) 00093 { 00094 Picked pick = result_it->second; 00095 SelectionHandlerPtr handler = manager_->getSelectionManager()->getHandler( pick.handle ); 00096 if ( pick.pixel_count > 0 && handler.get() ) 00097 { 00098 InteractiveObjectPtr object = handler->getInteractiveObject().lock(); 00099 if( object && object->isInteractive() ) 00100 { 00101 new_focused_object = object; 00102 } 00103 } 00104 } 00105 00106 // If the mouse has gone from one object to another, defocus the old 00107 // and focus the new. 00108 InteractiveObjectPtr new_obj = new_focused_object; 00109 InteractiveObjectPtr old_obj = focused_object_.lock(); 00110 if( new_obj != old_obj ) 00111 { 00112 // Only copy the event contents here, once we know we need to use 00113 // a modified version of it. 00114 ViewportMouseEvent event_copy = event; 00115 if( old_obj ) 00116 { 00117 event_copy.type = QEvent::FocusOut; 00118 old_obj->handleMouseEvent( event_copy ); 00119 } 00120 00121 if( new_obj ) 00122 { 00123 event_copy.type = QEvent::FocusIn; 00124 new_obj->handleMouseEvent( event_copy ); 00125 } 00126 } 00127 00128 focused_object_ = new_focused_object; 00129 } 00130 00131 int InteractionTool::processMouseEvent( ViewportMouseEvent& event ) 00132 { 00133 int flags = 0; 00134 00135 // make sure we let the vis. manager render at least one frame between selection updates 00136 bool need_selection_update = manager_->getFrameCount() > last_selection_frame_count_; 00137 bool dragging = (event.type == QEvent::MouseMove && event.buttons_down != Qt::NoButton); 00138 00139 // unless we're dragging, check if there's a new object under the mouse 00140 if( need_selection_update && 00141 !dragging && 00142 event.type != QEvent::MouseButtonRelease ) 00143 { 00144 updateFocus( event ); 00145 flags = Render; 00146 } 00147 00148 { 00149 InteractiveObjectPtr focused_object = focused_object_.lock(); 00150 if( focused_object ) 00151 { 00152 focused_object->handleMouseEvent( event ); 00153 } 00154 else if( event.panel->getViewController() ) 00155 { 00156 event.panel->getViewController()->handleMouseEvent( event ); 00157 } 00158 } 00159 00160 if( event.type == QEvent::MouseButtonRelease ) 00161 { 00162 updateFocus( event ); 00163 } 00164 00165 return flags; 00166 } 00167 00168 } // end namespace rviz 00169 00170 #include <pluginlib/class_list_macros.h> 00171 PLUGINLIB_DECLARE_CLASS( rviz, Interact, rviz::InteractionTool, rviz::Tool )