Dawn/src/dawntools/util/generator/SceneItemGenerator.cpp

213 lines
5.7 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SceneItemGenerator.hpp"
using namespace Dawn;
void SceneItemGenerator::generateDependency(
int32_t &assetNumber,
int32_t &componentNumber,
int32_t &childNumber,
std::map<std::string, std::string> &assetMap,
std::vector<std::string> &includes,
std::vector<std::string> *publicProperties,
std::vector<std::string> *initBody,
std::vector<std::string> *assetBody,
std::string name,
std::string sceneRef,
std::vector<struct SceneItemComponent> &components,
std::vector<struct SceneItem> &children,
std::vector<struct SceneCode> &code,
struct SceneItemDependency dep
) {
switch(dep.type) {
case SCENE_ITEM_DEPENDENCY_TYPE_CODE: {
auto i = &code[dep.position];
SceneCodeGenerator::generate(
publicProperties,
initBody,
&includes,
i,
""
);
line(initBody, "", "");
break;
}
case SCENE_ITEM_DEPENDENCY_TYPE_COMPONENT: {
auto i = &components[dep.position];
SceneItemComponentGenerator::generate(
assetMap,
componentNumber,
includes,
name,
publicProperties,
initBody,
i,
""
);
line(initBody, "", "");
break;
}
case SCENE_ITEM_DEPENDENCY_TYPE_ITEM: {
auto i = &children[dep.position];
SceneItemGenerator::generate(
assetNumber,
componentNumber,
childNumber,
assetMap,
includes,
publicProperties,
initBody,
assetBody,
name,
sceneRef,
i,
""
);
line(initBody, "", "");
break;
}
default:
assertUnreachable("SceneItemGenerator::generateDependency: Unknown dependency type");
}
}
void SceneItemGenerator::generate(
int32_t &assetNumber,
int32_t &componentNumber,
int32_t &childNumber,
std::map<std::string, std::string> &assetMap,
std::vector<std::string> &includes,
std::vector<std::string> *publicProperties,
std::vector<std::string> *initBody,
std::vector<std::string> *assetBody,
std::string parentRef,
std::string sceneRef,
struct SceneItem *item,
std::string tabs
) {
assertNotNull(publicProperties, "SceneItemGenerator::generate: publicProperties cannot be null");
assertNotNull(initBody, "SceneItemGenerator::generate: initBody cannot be null");
assertNotNull(assetBody, "SceneItemGenerator::generate: assetBody cannot be null");
assertNotNull(item, "SceneItemGenerator::generate: item cannot be null");
// Determine interface
std::string name = "itm" + std::to_string(childNumber++);
std::string itemType = "SceneItem";
if(item->ref == "this") {
name = item->ref;
} else {
// Determine name
if(!item->ref.empty()) name = item->ref;
// Initialize, either prefab or created.
if(!item->prefab.empty()) {
// Determine prefab name, type and include
std::string prefabName = item->prefab;
if(prefabName.find("/") != std::string::npos) {
prefabName = prefabName.substr(prefabName.find_last_of("/") + 1);
}
itemType = prefabName;
// Create prefab
includes.push_back(item->prefab + ".hpp");
line(initBody, (item->ref.empty() ? "auto " : "") + name + " = " + prefabName + "::create(" + sceneRef + ");", "");
line(assetBody, "vectorAppend(&assets, " + prefabName + "::getRequiredAssets(man));", "");
} else {
// Not a prefab, init empty child.
line(initBody, (item->ref.empty() ? "auto " : "") + name + " = " + sceneRef + "->createSceneItem();", "");
}
// Property/Ref defintion
if(!item->ref.empty() && item->ref != "this") {
line(publicProperties, itemType + " *" + name + " = nullptr;", "");
}
}
// Process extra properties
if(item->position.size() > 0) {
line(initBody, name + "->transform.setLocalPosition(" + item->position + ");", "");
}
if(item->alignment.size() > 0) {
line(initBody, name + "->uiItem->alignment = " + item->alignment + ";", "");
}
if(item->alignX.size() > 0) {
line(initBody, name + "->uiItem->alignX = " + item->alignX + ";", "");
}
if(item->alignY.size() > 0) {
line(initBody, name + "->uiItem->alignY = " + item->alignY + ";", "");
}
if(item->menuX.size() > 0) {
line(initBody, name + "->menuItem->menuX = " + item->menuX + ";", "");
}
if(item->menuY.size() > 0) {
line(initBody, name + "->menuItem->menuY = " + item->menuY + ";", "");
}
if(item->scale.size() > 0) {
line(initBody, name + "->transform.setLocalScale(" + item->scale + ");", "");
}
if(item->lookAtPosition.size() > 0) {
line(initBody, name + "->transform.lookAt(" + item->lookAtPosition + ", " + item->lookAtTarget + ");", "");
}
if(!item->label.empty()) {
line(initBody, name + "->label->text = " + item->label + ";", "");
}
// Add assets
auto itAssets = item->assets.begin();
while(itAssets != item->assets.end()) {
SceneAssetGenerator::generate(
assetMap,
assetNumber,
publicProperties,
initBody,
assetBody,
&(*itAssets),
""
);
++itAssets;
}
// Add the dependencies
auto itDeps = item->dependencies.begin();
while(itDeps != item->dependencies.end()) {
auto dep = *itDeps;
SceneItemGenerator::generateDependency(
assetNumber,
componentNumber,
childNumber,
assetMap,
includes,
publicProperties,
initBody,
assetBody,
name,
sceneRef,
item->components,
item->children,
item->code,
dep
);
++itDeps;
}
// Set parent
if(!parentRef.empty()) {
line(initBody, name + "->transform.setParent(&"+parentRef+"->transform);", "");
}
}