$search
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 "interaction_tool.h" 00031 00032 #include "rviz/visualization_manager.h" 00033 #include "rviz/properties/property_manager.h" 00034 #include "rviz/properties/property.h" 00035 #include "rviz/viewport_mouse_event.h" 00036 #include "rviz/render_panel.h" 00037 #include "rviz/view_controller.h" 00038 #include "rviz/viewport_mouse_event.h" 00039 #include "rviz/selection/selection_handler.h" 00040 #include "rviz/selection/selection_manager.h" 00041 00042 #include <OGRE/OgreRay.h> 00043 #include <OGRE/OgrePlane.h> 00044 #include <OGRE/OgreCamera.h> 00045 #include <OGRE/OgreSceneNode.h> 00046 #include <OGRE/OgreViewport.h> 00047 00048 namespace rviz 00049 { 00050 00051 // helper class, which acts as SelectionHandler and forwards all mouse events to the view controller 00052 class ViewControllerHandler: public SelectionHandler 00053 { 00054 virtual bool isInteractive() { return true; } 00055 virtual void handleMouseEvent(const Picked& obj, ViewportMouseEvent& event) 00056 { 00057 if ( event.panel->getViewController() ) 00058 { 00059 event.panel->getViewController()->handleMouseEvent( event ); 00060 } 00061 } 00062 }; 00063 00064 00065 InteractionTool::InteractionTool( const std::string& name, char shortcut_key, 00066 VisualizationManager* manager ) : Tool( name, shortcut_key, manager ) 00067 , focused_object_(0) 00068 , view_controller_handler_( new ViewControllerHandler() ) 00069 , last_selection_frame_count_(manager->getFrameCount()) 00070 { 00071 deactivate(); 00072 00073 view_controller_handle_ = manager->getSelectionManager()->createHandle(); 00074 manager->getSelectionManager()->addObject(view_controller_handle_, view_controller_handler_ ); 00075 } 00076 00077 InteractionTool::~InteractionTool() 00078 { 00079 manager_->getSelectionManager()->removeObject( view_controller_handle_ ); 00080 } 00081 00082 void InteractionTool::activate() 00083 { 00084 manager_->getSelectionManager()->enableInteraction(true); 00085 manager_->getSelectionManager()->setTextureSize(2); 00086 } 00087 00088 void InteractionTool::deactivate() 00089 { 00090 manager_->getSelectionManager()->enableInteraction(false); 00091 } 00092 00093 void InteractionTool::update(float wall_dt, float ros_dt) 00094 { 00095 } 00096 00097 void InteractionTool::updateSelection( SelectionHandlerPtr &focused_handler, ViewportMouseEvent event ) 00098 { 00099 M_Picked results; 00100 // Pick exactly 1 pixel 00101 manager_->getSelectionManager()->pick( event.viewport, 00102 event.x, event.y, 00103 event.x + 1, event.y + 1, 00104 results, true ); 00105 00106 last_selection_frame_count_ = manager_->getFrameCount(); 00107 00108 SelectionHandlerPtr new_focused_handler; 00109 Picked new_focused_object; 00110 new_focused_object.pixel_count = 0; 00111 00112 // look for a valid handle in the result. 00113 M_Picked::iterator result_it = results.begin(); 00114 if( result_it != results.end() ) 00115 { 00116 Picked object = result_it->second; 00117 SelectionHandlerPtr handler = manager_->getSelectionManager()->getHandler( object.handle ); 00118 if ( object.pixel_count > 0 && handler.get() && handler->isInteractive() ) 00119 { 00120 new_focused_object = object; 00121 new_focused_handler = handler; 00122 } 00123 } 00124 00125 // switch to view controller handler if nothing else is there 00126 if ( !new_focused_handler.get() ) 00127 { 00128 new_focused_handler = view_controller_handler_; 00129 new_focused_object.handle = view_controller_handle_; 00130 } 00131 00132 // if the mouse has gone from one object to another, 00133 // pass on focus 00134 if ( new_focused_handler.get() != focused_handler.get() ) 00135 { 00136 if ( focused_handler.get() ) 00137 { 00138 event.type = QEvent::FocusOut; 00139 focused_handler->handleMouseEvent( focused_object_, event ); 00140 } 00141 00142 ROS_DEBUG( "Switch focus to %d", new_focused_object.handle ); 00143 event.type = QEvent::FocusIn; 00144 new_focused_handler->handleMouseEvent( focused_object_, event ); 00145 } 00146 00147 focused_handler = new_focused_handler; 00148 focused_object_ = new_focused_object; 00149 } 00150 00151 int InteractionTool::processMouseEvent( ViewportMouseEvent& event ) 00152 { 00153 int flags = 0; 00154 // get the handler which was active last time 00155 SelectionHandlerPtr focused_handler; 00156 focused_handler = manager_->getSelectionManager()->getHandler( focused_object_.handle ); 00157 00158 // make sure we let the vis. manager render at least one frame between selection updates 00159 bool need_selection_update = manager_->getFrameCount() > last_selection_frame_count_; 00160 00161 // unless we're dragging, check if there's a new object under the mouse 00162 if( need_selection_update && event.type != QEvent::MouseMove && event.type != QEvent::MouseButtonRelease ) 00163 { 00164 updateSelection( focused_handler, event ); 00165 flags = Render; 00166 } 00167 00168 if( focused_handler.get() ) 00169 { 00170 focused_handler->handleMouseEvent( focused_object_, event ); 00171 } 00172 00173 if( event.type == QEvent::MouseButtonRelease ) 00174 { 00175 updateSelection( focused_handler, event ); 00176 } 00177 00178 return flags; 00179 } 00180 00181 void InteractionTool::enumerateProperties(PropertyManager* property_manager, const CategoryPropertyWPtr& parent) 00182 { 00183 } 00184 00185 } 00186