Prefab Generator Refactor

This commit is contained in:
2023-05-16 08:56:05 -07:00
parent e6c966f3dd
commit 39fffec483
32 changed files with 707 additions and 268 deletions

View File

@ -0,0 +1,17 @@
# Copyright (c) 2023 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
set(D ${CMAKE_CURRENT_LIST_DIR})
set(
DAWN_TOOL_SOURCES
${DAWN_TOOL_SOURCES}
${D}/SceneItemGenerator.cpp
${D}/SceneAssetGenerator.cpp
${D}/SceneItemComponentGenerator.cpp
CACHE INTERNAL
${DAWN_CACHE_TARGET}
)

View File

@ -0,0 +1,38 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SceneAssetGenerator.hpp"
using namespace Dawn;
void SceneAssetGenerator::generate(
std::map<std::string, std::string> &assetMap,
int32_t &assetNumber,
std::vector<std::string> *initBody,
std::vector<std::string> *assetsBody,
struct SceneAsset *asset,
std::string tabs
) {
std::string assetType = "";
asset->usageName = "asset" + std::to_string(assetNumber++);
switch(asset->type) {
case SCENE_ASSET_TYPE_TEXTURE:
assetType = "TextureAsset";
assetMap[asset->fileName] = "&" + asset->usageName + "->texture";
break;
case SCENE_ASSET_TYPE_TRUETYPE_FONT:
assetType = "TrueTypeAsset";
assetMap[asset->fileName] = "&" + asset->usageName + "->font";
break;
default:
assertUnreachable();
}
line(initBody, "auto " + asset->usageName + " = man->get<" + assetType + ">(\"" + asset->fileName + "\");", "");
line(assetsBody, "assets.push_back(man->get<" + assetType + ">(\"" + asset->fileName + "\"));", "");
}

View File

@ -0,0 +1,22 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "util/parser/SceneAssetParser.hpp"
#include "util/CodeGen.hpp"
namespace Dawn {
class SceneAssetGenerator : public CodeGen {
public:
static void generate(
std::map<std::string, std::string> &assetMap,
int32_t &assetNumber,
std::vector<std::string> *initBody,
std::vector<std::string> *assetsBody,
struct SceneAsset *asset,
std::string tabs
);
};
}

View File

@ -0,0 +1,43 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SceneItemComponentGenerator.hpp"
using namespace Dawn;
void SceneItemComponentGenerator::generate(
std::map<std::string, std::string> &assetMap,
int32_t &componentNumber,
std::vector<std::string> &includes,
std::string itemRef,
std::vector<std::string> *publicProperties,
std::vector<std::string> *initBody,
struct SceneItemComponent *component,
std::string tabs
) {
auto componentName = "cmp" + std::to_string(componentNumber++);
bool_t componentInit = true;
if(component->ref.size() > 0) {
componentInit = false;
componentName = component->ref;
line(publicProperties, component->type + " *" + component->ref + " = nullptr;", "");
}
// Initialize
line(initBody, (componentInit ? "auto " : "") + componentName + " = " + itemRef + "->addComponent<" + component->type + ">();", "");
// Now set each property
auto itValues = component->values.begin();
while(itValues != component->values.end()) {
auto value = itValues->second;
if(assetMap.find(value) != assetMap.end()) {
value = assetMap[value];
}
line(initBody, componentName + "->" + itValues->first + " = " + value + ";", "");
++itValues;
}
includes.push_back(component->include);
}

View File

@ -0,0 +1,24 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "util/parser/SceneItemComponentParser.hpp"
#include "util/CodeGen.hpp"
namespace Dawn {
class SceneItemComponentGenerator : public CodeGen {
public:
static void generate(
std::map<std::string, std::string> &assetMap,
int32_t &componentNumber,
std::vector<std::string> &includes,
std::string itemRef,
std::vector<std::string> *publicProperties,
std::vector<std::string> *initBody,
struct SceneItemComponent *component,
std::string tabs
);
};
}

View File

@ -0,0 +1,101 @@
// 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::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,
struct SceneItem *item,
std::string tabs
) {
auto name = "itm" + std::to_string(childNumber++);
if(item->ref == "this") {
name = item->ref;
} else {
bool_t init = true;
if(item->ref.size() > 0) {
init = false;
name = item->ref;
if(item->ref != "this") {
line(publicProperties, "SceneItemComponent *" + name + " = nullptr;", "");
}
}
line(initBody, (init ? "auto " : "") + name + " = scene->createSceneItem();", "");
}
// Process extra properties
if(item->position.size() > 0) {
line(initBody, name + "->transform.setLocalPosition(" + item->position + ");", "");
}
if(item->scale.size() > 0) {
line(initBody, name + "->transform.setLocalScale(" + item->scale + ");", "");
}
// Add assets
auto itAssets = item->assets.begin();
while(itAssets != item->assets.end()) {
SceneAssetGenerator::generate(
assetMap,
assetNumber,
initBody,
assetBody,
&(*itAssets),
""
);
++itAssets;
}
// Add components for children
auto itComponents = item->components.begin();
while(itComponents != item->components.end()) {
SceneItemComponentGenerator::generate(
assetMap,
componentNumber,
includes,
name,
publicProperties,
initBody,
&(*itComponents),
""
);
++itComponents;
}
// Process sub children
auto itChildren = item->children.begin();
while(itChildren != item->children.end()) {
SceneItemGenerator::generate(
assetNumber,
componentNumber,
childNumber,
assetMap,
includes,
publicProperties,
initBody,
assetBody,
name,
&(*itChildren),
""
);
++itChildren;
}
// Set parent
if(!parentRef.empty()) {
line(initBody, name + "->transform.setParent(&"+parentRef+"->transform);", "");
}
}

View File

@ -0,0 +1,28 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "SceneItemComponentGenerator.hpp"
#include "SceneAssetGenerator.hpp"
#include "util/parser/SceneItemParser.hpp"
namespace Dawn {
class SceneItemGenerator : public CodeGen {
public:
static void 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,
struct SceneItem *item,
std::string tabs
);
};
}