Improved connecting

This commit is contained in:
Pascal Serrarens 2024-11-29 15:06:08 +01:00
parent 5ee5debf35
commit 6356769aa9
3 changed files with 23 additions and 14 deletions

View File

@ -14,20 +14,27 @@ void Sensor::SetParent(Thing *parent) {
}
}
void Sensor::ConnectTo(Thing *thing) {
this->name = thing->name;
this->id = thing->id;
void Sensor::ConnectTo(Thing *oldThing) {
this->name = oldThing->name;
this->id = oldThing->id;
Thing *thingParent = thing->GetParent();
thingParent->RemoveChild(thing);
thingParent->AddChild(this);
this->children = thing->GetChildren();
this->position = thing->position;
this->orientation = thing->orientation;
Thing *oldParent = oldThing->GetParent();
oldParent->RemoveChild(oldThing);
oldParent->AddChild(this);
for (int childIx = 0; childIx < oldThing->childCount; childIx++) {
Thing *child = oldThing->GetChild(childIx);
this->AddChild(child);
}
this->position = oldThing->position;
this->orientation = oldThing->orientation;
// delete (thing); // can we do this?
}
void Sensor::ConnectTo(Thing *rootThing, const char *thingName) {
Thing *thing = rootThing->FindChild(thingName);
this->ConnectTo(thing);
if (thing != nullptr)
this->ConnectTo(thing);
}

View File

@ -65,12 +65,13 @@ void Thing::AddChild(Thing *child) {
this->childCount++;
}
Thing *Passer::RoboidControl::Thing::RemoveChild(Thing *child) {
Thing **newChildren = new Thing *[this->childCount - 1];
Thing *Thing::RemoveChild(Thing *child) {
int newChildCount = this->childCount - 1;
Thing **newChildren = new Thing *[newChildCount];
unsigned char newChildIx = 0;
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
if (this->children[childIx] != child) {
if (newChildIx == this->childCount - 1) { // We did not find the child
if (newChildIx == newChildCount) { // We did not find the child
// stop copying and return nothing
delete[] newChildren;
return nullptr;
@ -83,7 +84,7 @@ Thing *Passer::RoboidControl::Thing::RemoveChild(Thing *child) {
delete[] this->children;
this->children = newChildren;
this->childCount--;
this->childCount = newChildCount;
return child;
}

View File

@ -117,6 +117,7 @@ protected:
static const unsigned int SensorType = 0x4000;
/// @brief Bitmap for Roboid type
static const unsigned int RoboidType = 0x2000;
static const unsigned char CustomType = 0x80;
/// @brief Basic Thing types
enum class Type {