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