-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEntityManager.cpp
748 lines (615 loc) · 21.5 KB
/
EntityManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#include "EntityManager.h"
#include "EntityReference.h"
#include <Urho3D/Core/Context.h>
#include <Urho3D/IO/Base64Archive.h>
#include <Urho3D/IO/MemoryBuffer.h>
#include <Urho3D/Scene/Scene.h>
#include <Urho3D/Scene/SceneEvents.h>
#include <Urho3D/SystemUI/Widgets.h>
#include <IconFontCppHeaders/IconsFontAwesome6.h>
#include <SDL_clipboard.h>
namespace Urho3D
{
namespace
{
struct MaterializationStatus
{
bool materialized_{};
void SerializeInBlock(Archive& archive, unsigned version)
{
SerializeValue(archive, "materialized", materialized_);
//
}
};
const ea::string defaultContainerName = "Entities";
void FlattenEntityHierarchy(EntityReference* entityReference)
{
Node* node = entityReference->GetNode();
Node* parentNode = node->GetParent();
// TODO: Ignore indirect children
static thread_local ea::vector<EntityReference*> childrenReferences;
node->FindComponents<EntityReference>(childrenReferences);
for (EntityReference* childReference : childrenReferences)
childReference->GetNode()->SetParent(parentNode);
}
} // namespace
EntityComponentFactory::EntityComponentFactory(const ea::string& name)
: name_(name)
{
}
EntityManager::EntityManager(Context* context)
: TrackedComponentRegistryBase(context, EntityReference::GetTypeStatic())
, entitiesContainerName_(defaultContainerName)
{
}
void EntityManager::RegisterObject(Context* context)
{
URHO3D_ATTRIBUTE("Entities Container Node", ea::string, entitiesContainerName_, defaultContainerName, AM_DEFAULT);
URHO3D_ACCESSOR_ATTRIBUTE("Data", GetDataAttr, SetDataAttr, ByteVector, Variant::emptyBuffer, AM_TEMPORARY | AM_NOEDIT);
// Artificial attribute that is used to attach custom inspector UI.
URHO3D_ACCESSOR_ATTRIBUTE("Placeholder", GetPlaceholderAttr, SetPlaceholderAttr, bool, false, AM_EDIT)
.SetScopeHint(AttributeScopeHint::Serializable);
}
void EntityManager::ApplyAttributes()
{
entitiesContainer_ = GetScene()->GetChild(entitiesContainerName_);
if (!entitiesContainer_)
entitiesContainer_ = GetScene()->CreateChild(entitiesContainerName_);
Synchronize();
}
void EntityManager::SerializeAuxiliaryData(Archive& archive)
{
SerializeRegistry(archive);
if (archive.IsInput())
registryDirty_ = true;
}
ea::string EntityManager::GetEntityLabel(entt::entity entity) const
{
return Format("{}", entity);
}
bool EntityManager::RenderManagerInspector()
{
bool changed = false;
ui::Indent();
{
ColorScopeGuard colorScopeGuard{ImGuiCol_Text, Color::YELLOW};
ui::Text("Materialized Entities:");
}
if (ui::BeginListBox("##Entities"))
{
for (const auto& [entity] : registry_.storage<entt::entity>().each())
{
const IdScopeGuard guard{entt::to_integral(entity)};
const ea::string label = GetEntityLabel(entity);
bool isMaterialized = IsEntityMaterialized(entity);
if (ui::Checkbox(label.c_str(), &isMaterialized))
{
ui_.pendingMaterializations_.emplace_back(entity, isMaterialized);
changed = true;
}
};
ui::EndListBox();
}
if (ui::Button(ICON_FA_SQUARE_PLUS " Add Entity"))
{
const entt::entity entity = registry_.create();
MaterializeEntity(entity);
}
ui::Unindent();
return changed;
}
void EntityManager::SetPlaceholderAttr(bool placeholder)
{
CommitActions();
}
void EntityManager::EnsureComponentTypesSorted()
{
if (!componentTypesSorted_)
{
ea::sort(componentFactories_.begin(), componentFactories_.end(),
[](const auto& lhs, const auto& rhs) { return lhs->GetName() < rhs->GetName(); });
componentTypesSorted_ = true;
}
}
bool EntityManager::RenderEntityInspector(entt::entity entity)
{
EnsureComponentTypesSorted();
bool changed = false;
ui::Indent();
RenderEntityHeader(entity);
if (RenderExistingComponents(entity))
changed = true;
if (EntityComponentFactory* factory = RenderCreateComponent(entity))
{
ui_.pendingCreateComponents_.emplace_back(entity, factory);
changed = true;
}
ui::Unindent();
return changed;
}
void EntityManager::RenderEntityHeader(entt::entity entity)
{
ColorScopeGuard colorScopeGuard{ImGuiCol_Text, Color::YELLOW};
ui::Text("Entity %s", Format("{}", entity).c_str());
ui::SameLine();
if (ui::Button(ICON_FA_COPY "##CopyEntityID"))
SDL_SetClipboardText(Format("{}", static_cast<unsigned>(entity)).c_str());
if (ui::IsItemHovered())
ui::SetTooltip("Copy entity ID to clipboard");
}
EntityComponentFactory* EntityManager::RenderCreateComponent(entt::entity entity)
{
ui::BeginDisabled(componentFactories_.empty());
if (ui::Button(ICON_FA_SQUARE_PLUS " Add EnTT Component"))
ui::OpenPopup("##AddEnTTComponent");
ui::EndDisabled();
EntityComponentFactory* result = nullptr;
if (ui::BeginPopup("##AddEnTTComponent"))
{
for (const auto& factory : componentFactories_)
{
const bool alreadyExists = factory->HasComponent(registry_, entity);
ui::BeginDisabled(alreadyExists);
if (ui::MenuItem(factory->GetName().c_str()))
result = factory.get();
ui::EndDisabled();
if (result)
{
ui::CloseCurrentPopup();
break;
}
}
ui::EndPopup();
}
return result;
}
bool EntityManager::RenderExistingComponents(entt::entity entity)
{
bool changed = false;
for (const auto& factory : componentFactories_)
{
const IdScopeGuard guard{factory->GetName().c_str()};
if (!factory->HasComponent(registry_, entity))
continue;
if (ui::Button(ICON_FA_TRASH_CAN "##RemoveComponent"))
{
ui_.pendingDestroyComponents_.emplace_back(entity, factory.get());
changed = true;
}
if (ui::IsItemHovered())
ui::SetTooltip("Remove this component from entity");
ui::SameLine();
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_DefaultOpen;
if (factory->IsEmpty())
flags |= ImGuiTreeNodeFlags_Bullet;
if (ui::CollapsingHeader(factory->GetName().c_str(), flags))
{
ui::Indent();
if (factory->RenderUI(registry_, entity))
{
ui_.pendingEditComponents_.push_back(factory.get());
changed = true;
}
ui::Unindent();
}
}
return changed;
}
void EntityManager::AddComponentType(ea::unique_ptr<EntityComponentFactory> factory)
{
componentFactories_.push_back(ea::move(factory));
componentTypesSorted_ = false;
}
EntityComponentFactory* EntityManager::FindComponentType(ea::string_view name) const
{
for (const auto& factory : componentFactories_)
{
if (factory->GetName() == name)
return factory.get();
}
return nullptr;
}
void EntityManager::CommitActions()
{
if (!ui_.pendingMaterializations_.empty())
{
for (const auto& [entity, isMaterialized] : ui_.pendingMaterializations_)
{
if (isMaterialized)
MaterializeEntity(entity);
else
DematerializeEntity(entity);
}
ui_.pendingMaterializations_.clear();
}
if (!ui_.pendingCreateComponents_.empty())
{
for (const auto& [entity, factory] : ui_.pendingCreateComponents_)
{
if (!registry_.valid(entity) || factory->HasComponent(registry_, entity))
{
URHO3D_LOGERROR("Cannot add component '{}' to entity {}", factory->GetName(), entity);
continue;
}
factory->CreateComponent(registry_, entity);
}
ui_.pendingCreateComponents_.clear();
}
if (!ui_.pendingDestroyComponents_.empty())
{
for (const auto& [entity, factory] : ui_.pendingDestroyComponents_)
{
if (!registry_.valid(entity) || !factory->HasComponent(registry_, entity))
{
URHO3D_LOGERROR("Cannot remove component '{}' from entity {}", factory->GetName(), entity);
continue;
}
factory->DestroyComponent(registry_, entity);
}
ui_.pendingDestroyComponents_.clear();
}
if (!ui_.pendingEditComponents_.empty())
{
for (const auto& factory : ui_.pendingEditComponents_)
factory->CommitActions(registry_);
ui_.pendingEditComponents_.clear();
}
}
void EntityManager::OnComponentAdded(TrackedComponentBase* baseComponent)
{
const auto entityReference = static_cast<EntityReference*>(baseComponent);
entityReference->GetNode()->AddListener(entityReference);
if (suppressComponentEvents_)
return;
pendingEntitiesAdded_.emplace(WeakPtr<EntityReference>(entityReference));
}
void EntityManager::OnComponentRemoved(TrackedComponentBase* baseComponent)
{
if (suppressComponentEvents_)
return;
const auto entityReference = static_cast<EntityReference*>(baseComponent);
pendingEntitiesAdded_.erase(WeakPtr<EntityReference>(entityReference));
const entt::entity entity = entityReference->Entity();
if (entity != entt::null)
{
URHO3D_ASSERT(registry_.valid(entity));
registry_.destroy(entity);
}
}
void EntityManager::OnAddedToScene(Scene* scene)
{
SubscribeToEvent(scene, E_SCENEFORCEDPOSTUPDATE, &EntityManager::ForcedPostUpdate);
}
void EntityManager::OnRemovedFromScene()
{
UnsubscribeFromEvent(E_SCENEFORCEDPOSTUPDATE);
}
void EntityManager::Synchronize()
{
if (synchronizationInProgress_)
return;
synchronizationInProgress_ = true;
for (EntityReference* entityReference : pendingEntitiesAdded_)
{
// If registry has spawned this entity, everything is already configured.
const entt::entity entityHint = entityReference->Entity();
if (EntityToReference(entityHint) == entityReference)
continue;
if (registry_.valid(entityHint) && EntityToReference(entityHint) == nullptr)
{
// If entity is known to the registry and is not yet connected, connect to it.
}
else
{
// New entity was added from the UI, create new entity.
entityReference->SetEntityInternal(registry_.create(entityHint));
}
const entt::entity entity = entityReference->Entity();
registry_.emplace<EntityMaterialized>(entity, WeakPtr<EntityReference>{entityReference});
}
pendingEntitiesAdded_.clear();
for (const auto& [entityReference, data] : pendingEntityDecodes_)
{
if (entityReference && entityReference->Entity() != entt::null)
DecodeEntity(entityReference->Entity(), data);
}
pendingEntityDecodes_.clear();
if (registryDirty_)
{
registryDirty_ = false;
EnsureEntitiesMaterialized();
}
synchronizationInProgress_ = false;
}
bool EntityManager::IsEntityMaterialized(entt::entity entity) const
{
URHO3D_ASSERT(registry_.valid(entity));
return registry_.any_of<EntityMaterialized>(entity);
}
bool EntityManager::IsEntityValid(entt::entity entity) const
{
return entity != entt::null && registry_.valid(entity);
}
EntityReference* EntityManager::EntityToReference(entt::entity entity) const
{
const auto data = entity != entt::null ? registry_.try_get<EntityMaterialized>(entity) : nullptr;
return data ? data->entityReference_.Get() : nullptr;
}
Node* EntityManager::EntityToNode(entt::entity entity) const
{
const EntityReference* reference = EntityToReference(entity);
return reference ? reference->GetNode() : nullptr;
}
entt::entity EntityManager::NodeToEntity(Node* node) const
{
if (node)
{
if (auto entityReference = node->GetComponent<EntityReference>())
return entityReference->Entity();
}
return entt::null;
}
Variant EntityManager::EntityToVariant(entt::entity entity)
{
return Variant{static_cast<unsigned>(entity)};
}
entt::entity EntityManager::VariantToEntity(const Variant& variant)
{
return static_cast<entt::entity>(variant.GetUInt());
}
void EntityManager::EnsureEntitiesMaterialized()
{
for (const auto& [entity] : registry_.storage<entt::entity>().each())
{
const auto* status = registry_.try_get<MaterializationStatus>(entity);
const auto* data = registry_.try_get<EntityMaterialized>(entity);
if (!data && (!status || status->materialized_))
MaterializeEntity(entity);
else if (data && status && !status->materialized_)
DematerializeEntity(entity);
};
}
EntityReference* EntityManager::MaterializeEntity(entt::entity entity)
{
if (EntityReference* existingEntityReference = EntityToReference(entity))
{
URHO3D_LOGWARNING("Entity {} is already materialized", entity);
return existingEntityReference;
}
URHO3D_LOGTRACE("Entity {} is materializing", entity);
Node* entityNode = entitiesContainer_->CreateChild("Entity");
auto entityReference = MakeShared<EntityReference>(context_);
entityReference->SetEntityInternal(entity);
registry_.emplace_or_replace<EntityMaterialized>(entity, WeakPtr<EntityReference>{entityReference});
registry_.emplace_or_replace<MaterializationStatus>(entity, MaterializationStatus{true});
suppressComponentEvents_ = true;
entityNode->AddComponent(entityReference, 0);
suppressComponentEvents_ = false;
OnEntityMaterialized(this, registry_, entity, entityReference);
URHO3D_ASSERT(IsEntityMaterialized(entity));
return entityReference;
}
void EntityManager::DematerializeEntity(entt::entity entity)
{
if (!IsEntityMaterialized(entity))
{
URHO3D_LOGWARNING("Entity {} is already dematerialized", entity);
return;
}
URHO3D_LOGTRACE("Entity {} is dematerializing", entity);
EntityReference* entityReference = registry_.get<EntityMaterialized>(entity).entityReference_;
URHO3D_ASSERT(entityReference);
OnEntityDematerialized(this, registry_, entity, entityReference);
FlattenEntityHierarchy(entityReference);
entityReference->SetEntityInternal(entt::null);
suppressComponentEvents_ = true;
entityReference->GetNode()->Remove();
suppressComponentEvents_ = false;
registry_.remove<EntityMaterialized>(entity);
registry_.emplace_or_replace<MaterializationStatus>(entity, MaterializationStatus{false});
}
ea::vector<entt::entity> EntityManager::GetEntities() const
{
ea::vector<entt::entity> result;
if (const auto* storage = registry_.storage<entt::entity>())
{
for (const auto& [entity] : storage->each())
result.push_back(entity);
}
return result;
}
ByteVector EntityManager::EncodeEntity(entt::registry& registry, entt::entity entity)
{
if (!registry.valid(entity))
{
URHO3D_LOGERROR("Cannot encode entity {}", entity);
return {};
}
VectorBuffer buffer;
BinaryOutputArchive archive{context_, buffer};
SerializeStandaloneEntity(archive, registry, entity);
return buffer.GetBuffer();
}
void EntityManager::DecodeEntity(entt::registry& registry, entt::entity entity, const ByteVector& data)
{
if (!registry.valid(entity))
{
URHO3D_LOGERROR("Cannot decode entity {}", entity);
return;
}
MemoryBuffer buffer{data};
BinaryInputArchive archive{context_, buffer};
SerializeStandaloneEntity(archive, registry, entity);
}
ByteVector EntityManager::EncodeEntity(entt::entity entity)
{
return EncodeEntity(registry_, entity);
}
void EntityManager::DecodeEntity(entt::entity entity, const ByteVector& data)
{
DecodeEntity(registry_, entity, data);
}
void EntityManager::QueueDecodeEntity(EntityReference* entityReference, const ByteVector& data)
{
pendingEntityDecodes_.emplace_back(WeakPtr<EntityReference>{entityReference}, data);
}
void EntityManager::SetDataAttr(const ByteVector& data)
{
MemoryBuffer buffer{data};
BinaryInputArchive archive(context_, buffer);
SerializeRegistry(archive);
registryDirty_ = true;
}
ByteVector EntityManager::GetDataAttr() const
{
VectorBuffer buffer;
BinaryOutputArchive archive(context_, buffer);
const_cast<EntityManager*>(this)->SerializeRegistry(archive);
return buffer.GetBuffer();
}
void EntityManager::SerializeRegistry(Archive& archive)
{
ea::vector<EntityMaterialized> entityReferences;
if (archive.IsInput())
{
for (const auto& [_, data] : registry_.storage<EntityMaterialized>().each())
entityReferences.push_back(data);
registry_.clear();
}
ConsumeArchiveException(
[&]
{
const auto block = archive.OpenUnorderedBlock("registry");
SerializeEntities(archive);
SerializeComponents<MaterializationStatus>(archive, "materializationStatus", registry_, 0);
SerializeUserComponents(archive);
});
if (archive.IsInput())
{
for (const auto& data : entityReferences)
{
const entt::entity entity = data.entityReference_->Entity();
if (registry_.valid(entity))
registry_.emplace<EntityMaterialized>(entity, data);
}
}
}
void EntityManager::SerializeEntities(Archive& archive)
{
const auto numEntities = static_cast<unsigned>(registry_.storage<entt::entity>().in_use());
const auto block = archive.OpenArrayBlock("entities", numEntities);
if (archive.IsInput())
{
for (unsigned i = 0; i < block.GetSizeHint(); ++i)
{
unsigned entityData = 0;
archive.Serialize("entity", entityData);
(void)registry_.create(static_cast<entt::entity>(entityData));
}
}
else
{
static thread_local ea::vector<entt::entity> entitiesBuffer;
auto& entities = entitiesBuffer;
entities.clear();
for (const auto& [entity] : registry_.storage<entt::entity>().each())
entities.push_back(entity);
ea::sort(entities.begin(), entities.end(), EntityIndexComparator{});
for (const entt::entity entity : entities)
{
auto entityData = static_cast<unsigned>(entity);
archive.Serialize("entity", entityData);
};
}
}
void EntityManager::SerializeUserComponents(Archive& archive)
{
EnsureComponentTypesSorted();
const auto storagesBlock = archive.OpenArrayBlock("storages", componentFactories_.size());
if (archive.IsInput())
{
for (unsigned i = 0; i < storagesBlock.GetSizeHint(); ++i)
{
const auto storageBlock = archive.OpenSafeUnorderedBlock("storage");
ea::string typeName;
SerializeValue(archive, "type", typeName);
unsigned version{};
SerializeValue(archive, "version", version);
if (const auto factory = FindComponentType(typeName))
factory->SerializeComponents(archive, registry_, version);
}
}
else
{
for (const auto& factory : componentFactories_)
{
const auto storageBlock = archive.OpenSafeUnorderedBlock("storage");
ea::string typeName = factory->GetName();
SerializeValue(archive, "type", typeName);
unsigned version = factory->GetVersion();
SerializeValue(archive, "version", version);
factory->SerializeComponents(archive, registry_, version);
}
}
}
void EntityManager::SerializeStandaloneEntity(Archive& archive, entt::registry& registry, entt::entity entity)
{
EnsureComponentTypesSorted();
const auto storagesBlock = archive.OpenArrayBlock("components", componentFactories_.size());
if (archive.IsInput())
{
for (unsigned i = 0; i < storagesBlock.GetSizeHint(); ++i)
{
const auto storageBlock = archive.OpenSafeUnorderedBlock("component");
ea::string typeName;
SerializeValue(archive, "_type", typeName);
bool shouldExist{};
SerializeValue(archive, "_exists", shouldExist);
unsigned version{};
SerializeValue(archive, "_version", version);
if (const auto factory = FindComponentType(typeName))
{
const bool exists = factory->HasComponent(registry, entity);
if (shouldExist)
{
if (!exists)
factory->CreateComponent(registry, entity);
factory->SerializeComponent(archive, registry, entity, version);
}
else if (exists)
{
factory->DestroyComponent(registry, entity);
}
}
}
}
else
{
for (const auto& factory : componentFactories_)
{
const auto storageBlock = archive.OpenSafeUnorderedBlock("component");
ea::string typeName = factory->GetName();
SerializeValue(archive, "_type", typeName);
bool exists = factory->HasComponent(registry, entity);
SerializeValue(archive, "_exists", exists);
unsigned version = factory->GetVersion();
SerializeValue(archive, "_version", version);
if (exists)
factory->SerializeComponent(archive, registry, entity, version);
}
}
}
unsigned EntityManager::GetEntityVersion(entt::entity entity)
{
return entt::to_version(entity);
}
unsigned EntityManager::GetEntityIndex(entt::entity entity)
{
return static_cast<unsigned>(entt::to_entity(entity));
}
void EntityManager::ForcedPostUpdate()
{
Synchronize();
OnPostUpdateSynchronized(this, registry_);
}
} // namespace Urho3D