contrib/brl/bbas/bgui3d/bgui3d_fullviewer_tableau.cxx
Go to the documentation of this file.
00001 // This is brl/bbas/bgui3d/bgui3d_fullviewer_tableau.cxx
00002 #include "bgui3d_fullviewer_tableau.h"
00003 //:
00004 // \file
00005 
00006 #include "bgui3d_file_io.h"
00007 #include <vgui/vgui_dialog.h>
00008 #include <vgui/vgui_command.h>
00009 #include <vgui/vgui_menu.h>
00010 #include <vgui/vgui_popup_params.h>
00011 
00012 #include <Inventor/nodes/SoSeparator.h>
00013 #include <Inventor/SoDB.h>
00014 #include <Inventor/SbViewportRegion.h>
00015 #include <Inventor/nodes/SoSwitch.h>
00016 #include <Inventor/nodes/SoText2.h>
00017 #include <Inventor/nodes/SoNode.h>
00018 #include <Inventor/SoSceneManager.h>
00019 #include <Inventor/misc/SoChildList.h>
00020 
00021 #include <vcl_cassert.h>
00022 
00023 //: Constructor
00024 bgui3d_fullviewer_tableau::bgui3d_fullviewer_tableau(SoNode * scene_root)
00025  : bgui3d_viewer_tableau(scene_root)
00026 {
00027 }
00028 
00029 
00030 //: Destructor
00031 bgui3d_fullviewer_tableau::~bgui3d_fullviewer_tableau()
00032 {
00033 }
00034 
00035 
00036 bool bgui3d_fullviewer_tableau::handle(const vgui_event& e)
00037 {
00038   if ( e.type == vgui_KEY_PRESS && e.key == 'm')
00039   {
00040     if ( interaction_type_ == CAMERA )
00041       set_interaction_type( SCENEGRAPH );
00042     else
00043       set_interaction_type( CAMERA );
00044   }
00045   return bgui3d_tableau::handle(e);
00046 }
00047 
00048 
00049 //----------------------------------------------------------------------------
00050 //: A vgui command used to write a scene graph to disk
00051 class bgui3d_export_command : public vgui_command
00052 {
00053  public:
00054   bgui3d_export_command(bgui3d_fullviewer_tableau* tab) : bgui3d_fullviewer_tab(tab) {}
00055   void execute()
00056   {
00057     vgui_dialog export_dlg("Export Scene Graph");
00058     static vcl_string file_name = "";
00059     static vcl_string ext = "";
00060     export_dlg.file("File:", ext, file_name);
00061     static int type = 0;
00062     export_dlg.choice("Type:","IV","VRML","VRML 2.0", type);
00063     static bool full_scene = false;
00064     export_dlg.checkbox("Include camera and headlight", full_scene);
00065     if ( !export_dlg.ask())
00066       return;
00067 
00068     SoNode* root = NULL;
00069     if (full_scene)
00070       root = bgui3d_fullviewer_tab->scene_root();
00071     else
00072       root = bgui3d_fullviewer_tab->user_scene_root();
00073 
00074     switch (type)
00075     {
00076      case 0 : bgui3d_export_iv(root, file_name); break;
00077      case 1 : bgui3d_export_vrml(root, file_name); break;
00078      case 2 : bgui3d_export_vrml2(root, file_name); break;
00079      default: assert(!"This should not happen"); break;
00080     }
00081   }
00082 
00083   bgui3d_fullviewer_tableau *bgui3d_fullviewer_tab;
00084 };
00085 
00086 
00087 //----------------------------------------------------------------------------
00088 //: A vgui command used to import a scene graph from a file
00089 class bgui3d_import_command : public vgui_command
00090 {
00091  public:
00092   bgui3d_import_command(bgui3d_fullviewer_tableau* tab) : bgui3d_fullviewer_tab(tab) {}
00093   void execute()
00094   {
00095     vgui_dialog import_dlg("Import Scene Graph");
00096     static vcl_string file_name = "";
00097     static vcl_string ext = "iv";
00098     import_dlg.file("File:", ext, file_name);
00099     static bool overwrite = false;
00100     import_dlg.checkbox("Overwrite existing scene", overwrite);
00101     if ( !import_dlg.ask())
00102       return;
00103 
00104     // read the file
00105     SoNode* new_scene = bgui3d_import_file(file_name);
00106     new_scene->ref();
00107 
00108     if (overwrite)
00109     {
00110 #if 0
00111       bgui3d_fullviewer_tab->set_scene_root(new_scene);
00112 #endif // 0
00113       // get the old scene_root
00114       SoNode* old_scene = bgui3d_fullviewer_tab->user_scene_root();
00115       SoSeparator* scene_root = NULL;
00116 
00117       if (old_scene && SoSeparator::getClassTypeId() == old_scene->getTypeId()) {
00118         scene_root = (SoSeparator*)old_scene;
00119         scene_root->removeAllChildren();
00120         scene_root->addChild(new_scene);
00121       }
00122     }
00123     // add the new scene to the existing one
00124     else
00125     {
00126       // get the old scene_root
00127       SoNode* old_scene = bgui3d_fullviewer_tab->user_scene_root();
00128       SoSeparator* scene_root = NULL;
00129 
00130       // if the old scene_root was not a separator make it one
00131       if (old_scene && SoSeparator::getClassTypeId() == old_scene->getTypeId())
00132         scene_root = (SoSeparator*)old_scene;
00133       else {
00134         scene_root = new SoSeparator;
00135         scene_root->addChild(old_scene);
00136         bgui3d_fullviewer_tab->set_scene_root(scene_root);
00137       }
00138       // add the new scene
00139       scene_root->addChild(new_scene);
00140     }
00141     new_scene->unref();
00142   }
00143 
00144   bgui3d_fullviewer_tableau *bgui3d_fullviewer_tab;
00145 };
00146 
00147 
00148 //----------------------------------------------------------------------------
00149 //: A vgui command used to toggle animation
00150 class bgui3d_animate_command : public vgui_command
00151 {
00152  public:
00153   bgui3d_animate_command(bgui3d_fullviewer_tableau* tab) : bgui3d_fullviewer_tab(tab) {}
00154   void execute()
00155   {
00156     if ( bgui3d_fullviewer_tab->is_idle_enabled() )
00157       bgui3d_fullviewer_tab->disable_idle();
00158     else
00159       bgui3d_fullviewer_tab->enable_idle();
00160   }
00161 
00162   bgui3d_fullviewer_tableau *bgui3d_fullviewer_tab;
00163 };
00164 
00165 
00166 //----------------------------------------------------------------------------
00167 //: A vgui command used to toggle the headlight
00168 class bgui3d_headlight_command : public vgui_command
00169 {
00170  public:
00171   bgui3d_headlight_command(bgui3d_fullviewer_tableau* tab) : bgui3d_fullviewer_tab(tab) {}
00172   void execute()
00173   {
00174     bool headlight = bgui3d_fullviewer_tab->is_headlight();
00175     bgui3d_fullviewer_tab->set_headlight(!headlight);
00176   }
00177 
00178   bgui3d_fullviewer_tableau *bgui3d_fullviewer_tab;
00179 };
00180 
00181 
00182 //----------------------------------------------------------------------------
00183 //: A vgui command used to toggle camera type
00184 class bgui3d_camera_type_command : public vgui_command
00185 {
00186  public:
00187   bgui3d_camera_type_command(bgui3d_fullviewer_tableau* tab,
00188                              bgui3d_fullviewer_tableau::camera_type_enum type)
00189    : bgui3d_fullviewer_tab(tab), cam_type(type) {}
00190   void execute()
00191   {
00192     bgui3d_fullviewer_tab->set_camera_type(cam_type);
00193   }
00194 
00195   bgui3d_fullviewer_tableau *bgui3d_fullviewer_tab;
00196   bgui3d_fullviewer_tableau::camera_type_enum cam_type;
00197 };
00198 
00199 
00200 //----------------------------------------------------------------------------
00201 //: A vgui command used to toggle interaction type
00202 class bgui3d_interaction_type_command : public vgui_command
00203 {
00204  public:
00205   bgui3d_interaction_type_command(bgui3d_fullviewer_tableau* tab,
00206                                   bgui3d_tableau::interaction_type_enum type)
00207   : bgui3d_fullviewer_tab(tab), interaction_type(type) {}
00208 
00209   void execute()
00210   {
00211     bgui3d_fullviewer_tab->set_interaction_type(interaction_type);
00212   }
00213 
00214   bgui3d_fullviewer_tableau *bgui3d_fullviewer_tab;
00215   bgui3d_fullviewer_tableau::interaction_type_enum interaction_type;
00216 };
00217 
00218 
00219 //----------------------------------------------------------------------------
00220 //: A vgui command used to select a camera
00221 class bgui3d_select_camera_command : public vgui_command
00222 {
00223  public:
00224     bgui3d_select_camera_command(bgui3d_fullviewer_tableau* tab,int idx)
00225   : bgui3d_fullviewer_tab(tab), index(idx) {}
00226     void execute()
00227     {
00228       bgui3d_fullviewer_tab->select_camera(index);
00229     }
00230 
00231     bgui3d_fullviewer_tableau *bgui3d_fullviewer_tab;
00232     int index;
00233 };
00234 
00235 
00236 //----------------------------------------------------------------------------
00237 //: Builds a popup menu
00238 void bgui3d_fullviewer_tableau::get_popup(const vgui_popup_params& /*params*/, // unused - FIXME
00239                                           vgui_menu &menu)
00240 {
00241   vcl_string animation_item;
00242   if ( this->is_idle_enabled() )
00243     animation_item = "Disable Animation";
00244   else
00245     animation_item = "Enable Animation";
00246 
00247   vcl_string headlight_item;
00248   if ( this->is_headlight() )
00249     headlight_item = "Disable Headlight";
00250   else
00251     headlight_item = "Enable Headlight";
00252 
00253   vcl_string check_on = "[x]";
00254   vcl_string check_off = "[ ]";
00255 
00256   if ( camera_group_ ) {
00257     int active_cam_idx = camera_group_->whichChild.getValue();
00258     SoChildList* list = camera_group_->getChildren();
00259     vgui_menu camera_list_menu;
00260     if (!this->find_cameras(this->user_scene_root()).empty()) {
00261       vcl_string name = ((active_cam_idx<0)?check_on:check_off) +" Scene Camera";
00262       camera_list_menu.add(name,new bgui3d_select_camera_command(this,-1));
00263     }
00264     for (int i=0; i<list->getLength(); ++i) {
00265       vcl_string name((*list)[i]->getName().getString());
00266       name = ((i==active_cam_idx)?check_on:check_off) +" "+ name;
00267       camera_list_menu.add(name,new bgui3d_select_camera_command(this,i));
00268     }
00269     if (camera_list_menu.size() > 1)
00270       menu.add("Choose Camera",camera_list_menu);
00271   }
00272 
00273   vgui_menu camera_menu;
00274   camera_type_enum cam_type = this->camera_type();
00275 
00276   camera_menu.add(((cam_type==ORTHOGONAL)?check_on:check_off) + " Orthogonal",
00277                   new bgui3d_camera_type_command(this,ORTHOGONAL));
00278   camera_menu.add(((cam_type==PERSPECTIVE)?check_on:check_off) + " Perspective",
00279                   new bgui3d_camera_type_command(this,PERSPECTIVE));
00280 
00281   vgui_menu interaction_menu;
00282   interaction_type_enum interaction_type = this->interaction_type();
00283 
00284   interaction_menu.add(((interaction_type==bgui3d_tableau::CAMERA)?check_on:check_off) + " Camera",
00285                        new bgui3d_interaction_type_command(this,bgui3d_tableau::CAMERA));
00286   interaction_menu.add(((interaction_type==bgui3d_tableau::SCENEGRAPH)?check_on:check_off) + " SceneGraph",
00287                        new bgui3d_interaction_type_command(this,bgui3d_tableau::SCENEGRAPH));
00288 
00289   menu.add("Export Scene", new bgui3d_export_command(this));
00290   menu.add("Import Scene", new bgui3d_import_command(this));
00291   menu.add("Interaction", interaction_menu);
00292   menu.add("Camera", camera_menu);
00293   menu.add(animation_item, new bgui3d_animate_command(this));
00294   menu.add(headlight_item, new bgui3d_headlight_command(this));
00295 }