Esempio n. 1
0
void JSONSerializer::Traverse(const shared_ptr<Node>& root) {
  mNodes[root] = ++mNodeCount;

  /// Traverse slots
  for (auto slotPair : root->GetSerializableSlots()) {
    Slot* slot = slotPair.second;
    if (slot->mIsMultiSlot) {
      for (auto& node : slot->GetDirectMultiNodes()) {
        auto it = mNodes.find(node);
        if (it == mNodes.end()) {
          Traverse(node);
        }
      }
    }
    else {
      shared_ptr<Node> node = slot->GetDirectNode();
      if (node == nullptr || slot->IsDefaulted()) continue;
      auto it = mNodes.find(node);
      if (it == mNodes.end()) {
        Traverse(node);
      }
    }
  }

  mNodesList.push_back(root);
}
Esempio n. 2
0
void JSONSerializer::SerializeGeneralNode(
  rapidjson::Value& nodeValue, const shared_ptr<Node>& node)
{
  /// Save slots
  if (node->GetSerializableSlots().size() > 0) {
    rapidjson::Value slotsObject(rapidjson::kObjectType);
    for (const auto& slotPair : node->GetSerializableSlots()) {
      Slot* slot = slotPair.second;
      ASSERT(slot->GetName().get() != nullptr && !slot->GetName()->empty());
      rapidjson::Value slotObject(rapidjson::kObjectType);

      /// Save ghost flag
      if (slot->IsGhost()) {
        slotObject.AddMember("ghost", true, *mAllocator);
      }

      if (slot->mIsMultiSlot) {
        /// Save connections
        rapidjson::Value connections(rapidjson::kArrayType);
        for (const auto& connectedNode : slot->GetDirectMultiNodes()) {
          int connectedID = mNodes.at(connectedNode);
          connections.PushBack(connectedID, *mAllocator);
        }
        slotObject.AddMember("connect", connections, *mAllocator);
      }
      else {
        /// Save connection
        const auto& connectedNode = slot->GetDirectNode();
        if (connectedNode != nullptr && !slot->IsDefaulted()) {
          int connectedID = mNodes.at(connectedNode);
          slotObject.AddMember("connect", connectedID, *mAllocator);
        }

        /// Save default values
        FloatSlot* floatSlot;
        Vec2Slot* vec2Slot;
        Vec3Slot* vec3Slot;
        Vec4Slot* vec4Slot;
        StringSlot* stringSlot;

        if ((floatSlot = dynamic_cast<FloatSlot*>(slot)) != nullptr) {
          slotObject.AddMember("default", floatSlot->GetDefaultValue(), *mAllocator);
        }
        else if ((vec2Slot = dynamic_cast<Vec2Slot*>(slot)) != nullptr) {
          slotObject.AddMember("default", SerializeVec2(vec2Slot->GetDefaultValue()), 
            *mAllocator);
        }
        else if ((vec3Slot = dynamic_cast<Vec3Slot*>(slot)) != nullptr) {
          slotObject.AddMember("default", SerializeVec3(vec3Slot->GetDefaultValue()),
            *mAllocator);
        }
        else if ((vec4Slot = dynamic_cast<Vec4Slot*>(slot)) != nullptr) {
          slotObject.AddMember("default", SerializeVec4(vec4Slot->GetDefaultValue()),
            *mAllocator);
        }
        else if ((stringSlot = dynamic_cast<StringSlot*>(slot)) != nullptr) {
          slotObject.AddMember("default", stringSlot->GetDefaultValue(), *mAllocator);
        }
      }
      slotsObject.AddMember(rapidjson::Value(*slot->GetName(), *mAllocator),
        slotObject, *mAllocator);
    }
    nodeValue.AddMember("slots", slotsObject, *mAllocator);
  }
}