示例#1
0
void Node::removeSFNodes() 
{
	SceneGraph *sg = getSceneGraph();
	if (sg) {
		for (ScriptNode *script = sg->findScriptNode(); script; script=script->nextTraversal()) {
			for (int n=0; n<script->getNFields(); n++) {
				Field *field = script->getField(n);
				if (field->getType() == fieldTypeSFNode) {
					SFNode *sfnode = (SFNode *)field;
					if (sfnode->getValue() == this)
						sfnode->setValue((Node *)NULL);
				}
			}
		}
	}
}
void H3DViewerPopupMenus::OnTreeViewAddChildNode( wxCommandEvent& event ) {
  wxTreeItemId id = treeview_dialog->TreeViewTree->GetSelection();
  
  H3DViewerTreeViewDialog::TreeIdMap::iterator ni = treeview_dialog->node_map.find( id.m_pItem );
  if( ni == treeview_dialog->node_map.end() ) {
    wxMessageBox( wxT("Selected tree item is not a node"),
                  wxT("Error"),
                  wxOK | wxICON_EXCLAMATION);
  }

  vector< wxString > node_fields;

  Node *selected_node = (*ni).second.get();
  H3DNodeDatabase *db = H3DNodeDatabase::lookupNodeInstance( selected_node );
  for( H3DNodeDatabase::FieldDBConstIterator i = db->fieldDBBegin();
       db->fieldDBEnd() != i; ++i ) {
    Field *f = i.getField( selected_node ); 
    if( SFNode *sfnode = dynamic_cast< SFNode * >( f ) ) {
      if( sfnode->getAccessType() == Field::INPUT_ONLY ||
          sfnode->getAccessType() == Field::INPUT_OUTPUT ) {
        node_fields.push_back( wxString(sfnode->getName().c_str(),wxConvUTF8) );
      } 
    } else if( MFNode *mfnode = dynamic_cast< MFNode * >( f ) ) {
      if( mfnode->getAccessType() == Field::INPUT_ONLY ||
          mfnode->getAccessType() == Field::INPUT_OUTPUT ) {
        node_fields.push_back( wxString(mfnode->getName().c_str(), wxConvUTF8) );
      }
    }
  }
  
  if( node_fields.empty() ) {
    wxMessageBox( wxT("Selected node does not have a SFNode or MFNode field."),
                  wxT("Error"),
                  wxOK | wxICON_EXCLAMATION);
    return;
  }

  string field_to_change;

  if( node_fields.size() > 1 ) {
    wxString *choices = new wxString[ node_fields.size() ];
    for( unsigned int i = 0; i < node_fields.size(); ++i ) {
      choices[i] = node_fields[i];
    }
    wxSingleChoiceDialog *choice_dialog = new wxSingleChoiceDialog ( this,
                                                                     wxT("Add/replace node in which field.."),
                                                                     wxT(""),
                                                                     node_fields.size(),
                                                                     choices );
    delete [] choices;

    if (choice_dialog->ShowModal() == wxID_OK) {
      field_to_change = std::string( node_fields[ choice_dialog->GetSelection() ].mb_str() );
    } else {
      return;
    }
  }

  wxTextEntryDialog *node_name_dialog = 
    new wxTextEntryDialog(this, 
                          wxT("Enter the name of the node type you want to use" ),
                          wxT("Add/replace node" ) );
  if (node_name_dialog->ShowModal() == wxID_OK) {
    Node *new_node = H3DNodeDatabase::createNode( std::string(node_name_dialog->GetValue().mb_str()) );
    if( !new_node ) {
      wxMessageBox( wxT("No such node type exists: " + node_name_dialog->GetValue()),
                    wxT("Error"),
                    wxOK | wxICON_EXCLAMATION);
    } else {
      Field *f = selected_node->getField( field_to_change );
      SFNode *sfnode = dynamic_cast< SFNode * >( f );
      MFNode *mfnode = dynamic_cast< MFNode * >( f );
       
      if( sfnode ) {
        AutoRef< Node > old_node( sfnode->getValue() );
        try { 
          sfnode->setValue( new_node );
        } catch (...) {
          sfnode->setValue( old_node.get() );
          wxMessageBox( wxT("Invalid node type for field"),
                        wxT("Error"),
                        wxOK | wxICON_EXCLAMATION);
        }
      } else if( mfnode ) {
        try { 
          mfnode->push_back( new_node );
        } catch (...) {
          wxMessageBox( wxT("Invalid node type for field"),
                        wxT("Error"),
                        wxOK | wxICON_EXCLAMATION);
        }
      }
    }
  } 
}