void Object::UnserializeFrom(gd::Project & project, const SerializerElement & element) { //Name and type are already loaded. objectVariables.UnserializeFrom(element.GetChild("variables", 0, "Variables")); //Compatibility with GD <= 3.3 if (element.HasChild("Automatism")) { for (std::size_t i = 0; i < element.GetChildrenCount("Automatism"); ++i) { SerializerElement & behaviorElement = element.GetChild("Automatism", i); gd::String autoType = behaviorElement.GetStringAttribute("type", "", "Type") .FindAndReplace("Automatism", "Behavior"); gd::String autoName = behaviorElement.GetStringAttribute("name", "", "Name"); Behavior* behavior = project.CreateBehavior(autoType); if ( behavior != NULL ) { behavior->SetName(autoName); behavior->UnserializeFrom(behaviorElement); behaviors[behavior->GetName()] = behavior; } else std::cout << "WARNING: Unknown behavior " << autoType << std::endl; } } //End of compatibility code else { SerializerElement & behaviorsElement = element.GetChild("behaviors", 0, "automatisms"); behaviorsElement.ConsiderAsArrayOf("behavior", "automatism"); for (std::size_t i = 0; i < behaviorsElement.GetChildrenCount(); ++i) { SerializerElement & behaviorElement = behaviorsElement.GetChild(i); gd::String autoType = behaviorElement.GetStringAttribute("type") .FindAndReplace("Automatism", "Behavior"); //Compatibility with GD <= 4 gd::String autoName = behaviorElement.GetStringAttribute("name"); Behavior* behavior = project.CreateBehavior(autoType); if ( behavior != NULL ) { behavior->SetName(autoName); behavior->UnserializeFrom(behaviorElement); behaviors[behavior->GetName()] = behavior; } else std::cout << "WARNING: Unknown behavior " << autoType << std::endl; } } DoUnserializeFrom(project, element); }
gd::Behavior * Object::AddNewBehavior(gd::Project & project, const gd::String & type, const gd::String & name) { Behavior * behavior = project.GetCurrentPlatform().CreateBehavior(type); if ( behavior != NULL ) { behavior->SetName(name); behaviors[behavior->GetName()] = behavior; } return behavior; }
bool Object::RenameBehavior(const gd::String & name, const gd::String & newName) { if ( behaviors.find(name) == behaviors.end() || behaviors.find(newName) != behaviors.end() ) return false; Behavior * aut = behaviors.find(name)->second; behaviors.erase(name); behaviors[newName] = aut; aut->SetName(newName); return true; }