ComponentPtr ComponentManager::CloneComponent(const ComponentPtr &component) { ComponentFactoryMap::const_iterator iter = factories_.find(component->TypeName()); if (iter == factories_.end()) return ComponentPtr(); ComponentPtr newComponent = (*iter->second.get())(component); return newComponent; }
bool ComponentGroup::AddComponent(ComponentPtr comp) { PROFILE(ComponentGroup_AddComponent); //Check if the component have already added to component group or it's name or type are different for the component group. if(ContainsComponent(comp) || comp->Name() != name_ || comp->TypeName() != typeName_) return false; components_.push_back(ComponentWeakPtr(comp)); editor_->AddNewComponent(comp); return true; }
ComponentItem::ComponentItem(const ComponentPtr &comp, EntityItem *parent) : QTreeWidgetItem(parent), parentItem(parent), ptr(comp), typeId(comp->TypeId()), typeName(comp->TypeName()), name(comp->Name()) { SetText(comp.get()); }
void Entity::AddComponent(component_id_t id, const ComponentPtr &component, AttributeChange::Type change) { // Must exist and be free if (component && component->ParentEntity() == 0) { if (!id) { bool authority = true; if (scene_) authority = scene_->IsAuthority(); // Loop until we find a free ID for (;;) { if (authority) id = component->IsReplicated() ? idGenerator_.AllocateReplicated() : idGenerator_.AllocateLocal(); else id = component->IsReplicated() ? idGenerator_.AllocateUnacked() : idGenerator_.AllocateLocal(); if (components_.find(id) == components_.end()) break; } } else { component->SetReplicated(id < UniqueIdGenerator::FIRST_LOCAL_ID); // If component ID is specified manually, but it already exists, it is an error. Do not add the component in that case. if (components_.find(id) != components_.end()) { LogError("Can not add component: a component with id " + QString::number(id) + " already exists in entity " + ToString()); return; } // Whenever a manual replicated ID is assigned, reset the ID generator to the highest value to avoid unnecessary free ID probing in the future if (id < UniqueIdGenerator::FIRST_LOCAL_ID) idGenerator_.ResetReplicatedId(std::max(id, idGenerator_.id)); } QString componentTypeName = component->TypeName(); componentTypeName.replace(0, 3, ""); componentTypeName = componentTypeName.toLower(); // We already have 'name' property in Entity, so ignore "EC_Name" ("name") here. if (componentTypeName != "name" && !property(componentTypeName.toStdString().c_str()).isValid()) { QVariant var = QVariant::fromValue<QObject*>(component.get()); setProperty(componentTypeName.toStdString().c_str(), var); } component->SetNewId(id); component->SetParentEntity(this); components_[id] = component; if (change != AttributeChange::Disconnected) emit ComponentAdded(component.get(), change == AttributeChange::Default ? component->UpdateMode() : change); if (scene_) scene_->EmitComponentAdded(this, component.get(), change); } }
ComponentGroup::ComponentGroup(ComponentPtr component, ECComponentEditor *editor, bool isDynamic): editor_(editor), isDynamic_(isDynamic) { assert(component); // No point to add component to editor cause it's already added in ECBrowser's AddNewComponentToGroup mehtod. if(component) { components_.push_back(ComponentWeakPtr(component)); name_ = component->Name(); typeName_ = component->TypeName(); } }
void Entity::RemoveComponent(const ComponentPtr &component, AttributeChange::Type change) { if (component) { ComponentVector::iterator iter = std::find(components_.begin(), components_.end(), component); if (iter != components_.end()) { QString componentTypeName = component->TypeName(); componentTypeName.replace(0, 3, ""); componentTypeName = componentTypeName.toLower(); if(property(componentTypeName.toStdString().c_str()).isValid()) { QObject *obj = property(componentTypeName.toStdString().c_str()).value<QObject*>(); //Make sure that QObject is inherited by the IComponent. if (obj && dynamic_cast<IComponent*>(obj)) { //Make sure that name is matching incase there are many of same type of components in entity. if (dynamic_cast<IComponent*>(obj)->Name() == component->Name()) setProperty(componentTypeName.toStdString().c_str(), QVariant()); } } if (change != AttributeChange::Disconnected) emit ComponentRemoved((*iter).get(), change == AttributeChange::Default ? component->GetUpdateMode() : change); if (scene_) scene_->EmitComponentRemoved(this, (*iter).get(), change); (*iter)->SetParentEntity(0); components_.erase(iter); } else { LogWarning("Failed to remove component: " + component->TypeName() + " from entity: " + QString::number(GetId())); } } }
void Entity::RemoveComponent(const ComponentPtr &component, AttributeChange::Type change) { if (component) { for(ComponentMap::Iterator it = components_.Begin(); it != components_.End(); ++it) if (it->second_ == component) { RemoveComponent(it, change); return; } LogWarning("Entity::RemoveComponent: Failed to find " + component->TypeName() + " \"" + component->Name() + "\" from " + ToString() + "."); } }
void Entity::RemoveComponent(const ComponentPtr &component, AttributeChange::Type change) { if (component) { ComponentMap::iterator iter = components_.find(component->Id()); if (iter != components_.end()) { RemoveComponent(iter, change); } else { LogWarning("Failed to remove component: " + component->TypeName() + " from entity: " + QString::number(Id())); } } }
void Entity::AddComponent(const ComponentPtr &component, AttributeChange::Type change) { // Must exist and be free if (component && component->GetParentEntity() == 0) { QString componentTypeName = component->TypeName(); componentTypeName.replace(0, 3, ""); componentTypeName = componentTypeName.toLower(); if(!property(componentTypeName.toStdString().c_str()).isValid()) { QVariant var = QVariant::fromValue<QObject*>(component.get()); setProperty(componentTypeName.toStdString().c_str(), var); } component->SetParentEntity(this); components_.push_back(component); if (change != AttributeChange::Disconnected) emit ComponentAdded(component.get(), change == AttributeChange::Default ? component->GetUpdateMode() : change); if (scene_) scene_->EmitComponentAdded(this, component.get(), change); } }
/// @todo made some major changes to this function - ensure that everything is working right. bool ComponentGroup::IsSameComponent(ComponentPtr component) const { assert(component); if(!IsValid()) return false; PROFILE(ComponentGroup_IsSameComponent); if(component->TypeName() != typeName_ || component->Name() != name_) return false; // If component type is dynamic component we need to compere their attributes aswell. To ensure // that they are holding exactly the same attributes. if(isDynamic_) { EC_DynamicComponent *thisComponent = dynamic_cast<EC_DynamicComponent*>(components_[0].lock().get()); EC_DynamicComponent *compareComponent = dynamic_cast<EC_DynamicComponent*>(component.get()); if(!compareComponent || !thisComponent) return false; if(!thisComponent->ContainSameAttributes(*compareComponent)) return false; } return true; }