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); } }
QScriptValue toScriptValueComponentVector(QScriptEngine *engine, const Entity::ComponentVector &components) { QScriptValue obj = engine->newArray(); Entity::ComponentVector::const_iterator iter = components.begin(); quint32 i = 0; while(iter != components.end()) { ComponentPtr comp = (*iter); if (comp.get()) { obj.setProperty(i, engine->newQObject(comp.get())); i++; } ++iter; } return obj; }
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()); }
bool ComponentGroup::ContainsComponent(ComponentPtr component) const { for(uint i = 0; i < components_.size(); i++) { if(components_[i].expired()) continue; if(components_[i].lock().get() == component.get()) return true; } return false; }
void EC_Mesh::SetPlaceable(ComponentPtr placeable) { if (placeable && !dynamic_cast<EC_Placeable*>(placeable.get())) { LogError("Attempted to set placeable which is not " + EC_Placeable::TypeNameStatic()); return; } DetachEntity(); placeable_ = placeable; AttachEntity(); }
void EC_OgreCustomObject::SetPlaceable(ComponentPtr placeable) { if (!dynamic_cast<EC_Placeable*>(placeable.get())) { OgreRenderingModule::LogError("Attempted to set placeable which is not " + EC_Placeable::TypeNameStatic().toStdString()); return; } DetachEntity(); placeable_ = placeable; AttachEntity(); }
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); } }
void Entity::addComponent(ComponentPtr component) { std::type_index id = std::type_index(typeid(*component.get())); assert(componentMap.count(id) == 0); EntityPtr ptr = this->shared_from_this(); component->attachEntity = ptr; this->components.push_back(component); this->componentMap[id] = component; CacheComponents(); component->registerEvents(); if (this->state >= EntityState::Awaked) { CallEvent(component.get(), Events::Awake); if (this->node.get() != nullptr && this->node->hasParent()) { CallEvent(component.get(), Events::Enter); } } }
bool ComponentGroup::RemoveComponent(ComponentPtr comp) { if (!ContainsComponent(comp)) return false; std::vector<ComponentWeakPtr>::iterator iter = components_.begin(); for(; iter != components_.end(); ++iter) { ComponentPtr comp_ptr = iter->lock(); if (comp.get() == comp_ptr.get()) { editor_->RemoveComponent(comp); components_.erase(iter); return true; } } return false; }
/// @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; }
void Entity::SetDescription(const QString &desc) { ComponentPtr comp = GetOrCreateComponent(EC_Name::TypeNameStatic(), AttributeChange::Default, true); EC_Name * ecName = checked_static_cast<EC_Name*>(comp.get()); ecName->description.Set(desc, AttributeChange::Default); }
void Entity::SetName(const QString &name) { ComponentPtr comp = GetOrCreateComponent(EC_Name::TypeNameStatic(), AttributeChange::Default, true); EC_Name * ecName = checked_static_cast<EC_Name*>(comp.get()); ecName->name.Set(name, AttributeChange::Default); }
void EC_MediaPlayer::PrepareComponent() { // Don't do anything if rendering is not enabled if (!ViewEnabled() || GetFramework()->IsHeadless()) return; // Some security checks if (componentPrepared_) { LogWarning("EC_MediaPlayer: Preparations seem to be done already, you might not want to do this multiple times."); } if (!mediaPlayer_) { LogError("EC_MediaPlayer: Cannot start preparing, webview object is null. This should never happen!"); return; } // Get parent and connect to the component removed signal. Entity *parent = ParentEntity(); if (parent) connect(parent, SIGNAL(ComponentRemoved(IComponent*, AttributeChange::Type)), SLOT(ComponentRemoved(IComponent*, AttributeChange::Type)), Qt::UniqueConnection); else { LogError("EC_MediaPlayer: Could not get parent entity pointer!"); return; } // Get EC_Mesh component EC_Mesh *mesh = GetMeshComponent(); if (!mesh) { // Wait for EC_Mesh to be added. connect(parent, SIGNAL(ComponentAdded(IComponent*, AttributeChange::Type)), SLOT(ComponentAdded(IComponent*, AttributeChange::Type)), Qt::UniqueConnection); return; } else { // Inspect if this mesh is ready for rendering. EC_Mesh being present != being loaded into Ogre and ready for rendering. if (!mesh->GetEntity()) { connect(mesh, SIGNAL(MeshChanged()), SLOT(TargetMeshReady()), Qt::UniqueConnection); return; } else connect(mesh, SIGNAL(MaterialChanged(uint, const QString&)), SLOT(TargetMeshMaterialChanged(uint, const QString&)), Qt::UniqueConnection); } if (sceneCanvasName_.isEmpty()) sceneCanvasName_ = "VlcMediaPlayerCanvas-" + QUuid::createUuid().toString().replace("{", "").replace("}", ""); // Get or create local EC_WidgetCanvas component ComponentPtr iComponent = parent->GetOrCreateComponent(EC_WidgetCanvas::TypeNameStatic(), sceneCanvasName_, AttributeChange::LocalOnly, false); EC_WidgetCanvas *sceneCanvas = dynamic_cast<EC_WidgetCanvas*>(iComponent.get()); if (!sceneCanvas) { LogError("EC_MediaPlayer: Could not get or create EC_WidgetCanvas component!"); return; } sceneCanvas->SetTemporary(true); sceneCanvas->SetSelfIllumination(getilluminating()); // All the needed components are present, mark prepared as true. componentPrepared_ = true; // We are now prepared, check enabled state and restore possible materials now if (!getenabled()) sceneCanvas->RestoreOriginalMeshMaterials(); // Show downloading info icon or if not downloading, // ask for a image update from the player if (pendingMediaDownload_) OnFrameUpdate(downloadingLogo_); else mediaPlayer_->ForceUpdateImage(); }