$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 "render_panel.h" 00031 #include "visualization_manager.h" 00032 #include "display.h" 00033 #include "display_wrapper.h" 00034 #include "tools/tool.h" 00035 #include "viewport_mouse_event.h" 00036 #include "view_controller.h" 00037 00038 #include <boost/bind.hpp> 00039 00040 #include <OGRE/OgreRoot.h> 00041 #include <OGRE/OgreViewport.h> 00042 00043 namespace rviz 00044 { 00045 00046 RenderPanel::RenderPanel( wxWindow* parent, bool create_render_window, Display* display ) 00047 : wxOgreRenderWindow( Ogre::Root::getSingletonPtr(), parent, wxID_ANY, wxDefaultPosition, wxSize(800, 600), wxSUNKEN_BORDER, wxDefaultValidator, create_render_window ) 00048 , mouse_x_( 0 ) 00049 , mouse_y_( 0 ) 00050 , manager_(0) 00051 , scene_manager_(0) 00052 , camera_(0) 00053 , view_controller_(0) 00054 , display_(display) 00055 { 00056 SetFocus(); 00057 Connect( wxEVT_CHAR, wxKeyEventHandler( RenderPanel::onChar ), NULL, this ); 00058 00059 Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00060 Connect( wxEVT_MIDDLE_DOWN, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00061 Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00062 Connect( wxEVT_MOTION, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00063 Connect( wxEVT_LEFT_UP, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00064 Connect( wxEVT_MIDDLE_UP, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00065 Connect( wxEVT_RIGHT_UP, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00066 Connect( wxEVT_MOUSEWHEEL, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00067 Connect( wxEVT_LEFT_DCLICK, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00068 00069 Connect( wxEVT_CONTEXT_MENU, wxContextMenuEventHandler(RenderPanel::onContextMenu), NULL, this ); 00070 Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( RenderPanel::onClose ), NULL, this ); 00071 } 00072 00073 RenderPanel::~RenderPanel() 00074 { 00075 delete view_controller_; 00076 scene_manager_->destroyCamera(camera_); 00077 00078 Disconnect( wxEVT_CHAR, wxKeyEventHandler( RenderPanel::onChar ), NULL, this ); 00079 Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00080 Disconnect( wxEVT_MIDDLE_DOWN, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00081 Disconnect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00082 Disconnect( wxEVT_MOTION, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00083 Disconnect( wxEVT_LEFT_UP, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00084 Disconnect( wxEVT_MIDDLE_UP, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00085 Disconnect( wxEVT_RIGHT_UP, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00086 Disconnect( wxEVT_MOUSEWHEEL, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00087 Disconnect( wxEVT_LEFT_DCLICK, wxMouseEventHandler( RenderPanel::onRenderWindowMouseEvents ), NULL, this ); 00088 00089 Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler(RenderPanel::onClose), NULL, this ); 00090 } 00091 00092 void RenderPanel::initialize(Ogre::SceneManager* scene_manager, VisualizationManager* manager) 00093 { 00094 manager_ = manager; 00095 scene_manager_ = scene_manager; 00096 00097 std::stringstream ss; 00098 static int count = 0; 00099 ss << "RenderPanelCamera" << count++; 00100 camera_ = scene_manager_->createCamera(ss.str()); 00101 00102 wxOgreRenderWindow::setCamera(camera_); 00103 } 00104 00105 void RenderPanel::onClose( wxCloseEvent& event ) 00106 { 00107 if( display_ != NULL && manager_ != NULL ) 00108 { 00109 // Have to use the DisplayWrapper disable function so the checkbox 00110 // gets unchecked, since it owns the "enabled" property. 00111 DisplayWrapper* wrapper = manager_->getDisplayWrapper( display_ ); 00112 if( wrapper != NULL ) 00113 { 00114 wrapper->setEnabled( false ); 00115 } 00116 } 00117 } 00118 00119 void RenderPanel::onRenderWindowMouseEvents( wxMouseEvent& event ) 00120 { 00121 int last_x = mouse_x_; 00122 int last_y = mouse_y_; 00123 00124 mouse_x_ = event.GetX(); 00125 mouse_y_ = event.GetY(); 00126 00127 if (manager_) 00128 { 00129 SetFocus(); 00130 00131 ViewportMouseEvent vme(this, getViewport(), event, last_x, last_y); 00132 manager_->handleMouseEvent(vme); 00133 } 00134 } 00135 00136 void RenderPanel::onChar( wxKeyEvent& event ) 00137 { 00138 if (manager_) 00139 { 00140 manager_->handleChar( event ); 00141 } 00142 } 00143 00144 void RenderPanel::setViewController(ViewController* controller) 00145 { 00146 if (view_controller_) 00147 { 00148 view_controller_->deactivate(); 00149 } 00150 00151 delete view_controller_; 00152 view_controller_ = controller; 00153 00154 view_controller_->activate(camera_, manager_ ? manager_->getTargetFrame() : ""); 00155 } 00156 00157 void RenderPanel::createRenderWindow() 00158 { 00159 wxOgreRenderWindow::createRenderWindow(); 00160 wxOgreRenderWindow::setCamera(camera_); 00161 } 00162 00163 void RenderPanel::setContextMenu( boost::shared_ptr<wxMenu> menu ) 00164 { 00165 boost::mutex::scoped_lock lock(context_menu_mutex_); 00166 context_menu_ = menu; 00167 } 00168 00169 void RenderPanel::onContextMenu( wxContextMenuEvent& event ) 00170 { 00171 boost::shared_ptr<wxMenu> context_menu; 00172 { 00173 boost::mutex::scoped_lock lock(context_menu_mutex_); 00174 context_menu.swap(context_menu_); 00175 } 00176 00177 if ( context_menu ) 00178 { 00179 PopupMenu( context_menu.get(), event.GetPosition().x, event.GetPosition().y ); 00180 } 00181 } 00182 00183 /* START_WX-2.9_COMPAT_CODE 00184 This code is related to ticket: https://code.ros.org/trac/ros-pkg/ticket/5156 00185 */ 00186 #if wxMAJOR_VERSION == 2 and wxMINOR_VERSION == 9 // If wxWidgets 2.9.x 00187 void RenderPanel::addPendingEvent(const wxEvent& event) { 00188 this->AddPendingEvent(event); 00189 } 00190 #endif 00191 /* END_WX-2.9_COMPAT_CODE */ 00192 00193 } // namespace rviz