From 3be64a40e77485a252d75c1bde19f4b46d62e260 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 23 Apr 2023 18:46:03 -0700 Subject: [PATCH] Working on parser --- src/dawn/display/Color.hpp | 27 +++++++++++ src/dawntools/vnscenetool/CMakeLists.txt | 1 + .../vnscenetool/VNSceneEventsParser.cpp | 7 +++ .../vnscenetool/VNSceneEventsParser.hpp | 5 +- src/dawntools/vnscenetool/VNSceneGen.cpp | 32 +++++++++++-- .../vnscenetool/VNSceneItemParser.cpp | 47 +++++++++++++++++++ .../vnscenetool/VNSceneItemParser.hpp | 27 +++++++++++ src/dawntools/vnscenetool/VNSceneParser.cpp | 13 ++++- src/dawntools/vnscenetool/VNSceneParser.hpp | 2 + .../vnscenetool/events/CMakeLists.txt | 1 + .../vnscenetool/events/VNSetEventParser.cpp | 45 ++++++++++++++++++ .../vnscenetool/events/VNSetEventParser.hpp | 30 ++++++++++++ 12 files changed, 232 insertions(+), 5 deletions(-) create mode 100644 src/dawntools/vnscenetool/VNSceneItemParser.cpp create mode 100644 src/dawntools/vnscenetool/VNSceneItemParser.hpp create mode 100644 src/dawntools/vnscenetool/events/VNSetEventParser.cpp create mode 100644 src/dawntools/vnscenetool/events/VNSetEventParser.hpp diff --git a/src/dawn/display/Color.hpp b/src/dawn/display/Color.hpp index 7daedd0d..aee9147f 100644 --- a/src/dawn/display/Color.hpp +++ b/src/dawn/display/Color.hpp @@ -18,6 +18,33 @@ namespace Dawn { (float_t)a / 100.0f }; } + + struct Color operator * (const float_t &x) { + return { + (uint8_t)(r * x), + (uint8_t)(g * x), + (uint8_t)(b * x), + (uint8_t)(a * x) + }; + } + + struct Color operator - (const struct Color &color) { + return { + (uint8_t)(r - color.r), + (uint8_t)(g - color.g), + (uint8_t)(b - color.b), + (uint8_t)(a - color.a) + }; + } + + struct Color operator + (const struct Color &color) { + return { + (uint8_t)(r + color.r), + (uint8_t)(g + color.g), + (uint8_t)(b + color.b), + (uint8_t)(a + color.a) + }; + } }; #define COLOR_WHITE { 255, 255, 255, 100 } diff --git a/src/dawntools/vnscenetool/CMakeLists.txt b/src/dawntools/vnscenetool/CMakeLists.txt index ff3749d2..865f7454 100644 --- a/src/dawntools/vnscenetool/CMakeLists.txt +++ b/src/dawntools/vnscenetool/CMakeLists.txt @@ -19,6 +19,7 @@ target_sources(vnscenetool VNSceneParser.cpp VNSceneEventsParser.cpp VNSceneGen.cpp + VNSceneItemParser.cpp ) # Includes diff --git a/src/dawntools/vnscenetool/VNSceneEventsParser.cpp b/src/dawntools/vnscenetool/VNSceneEventsParser.cpp index 154860b4..fe8223e7 100644 --- a/src/dawntools/vnscenetool/VNSceneEventsParser.cpp +++ b/src/dawntools/vnscenetool/VNSceneEventsParser.cpp @@ -41,6 +41,13 @@ int32_t VNSceneEventsParser::onParse( ret = (VNPositionEventParser()).parse(child, &event.position, error); if(ret != 0) return ret; + } else if(child->node == "set") { + VNSetEvent parser; + event.type = VN_SCENE_EVENT_TYPE_SET; + ret = (VNSetEventParser()).parse(child, &event.set, error); + if(ret != 0) return ret; + + } else { *error = "Unknown child node '" + child->node + "'"; return -1; diff --git a/src/dawntools/vnscenetool/VNSceneEventsParser.hpp b/src/dawntools/vnscenetool/VNSceneEventsParser.hpp index a004c298..33849d9d 100644 --- a/src/dawntools/vnscenetool/VNSceneEventsParser.hpp +++ b/src/dawntools/vnscenetool/VNSceneEventsParser.hpp @@ -6,11 +6,13 @@ #pragma once #include "events/VNTextEventParser.hpp" #include "events/VNPositionEventParser.hpp" +#include "events/VNSetEventParser.hpp" namespace Dawn { enum VNSceneEventType { VN_SCENE_EVENT_TYPE_TEXT, - VN_SCENE_EVENT_TYPE_POSITION + VN_SCENE_EVENT_TYPE_POSITION, + VN_SCENE_EVENT_TYPE_SET }; struct VNSceneEvent { @@ -18,6 +20,7 @@ namespace Dawn { struct VNTextEvent text; struct VNPositionEvent position; + struct VNSetEvent set; }; struct VNSceneEventList { diff --git a/src/dawntools/vnscenetool/VNSceneGen.cpp b/src/dawntools/vnscenetool/VNSceneGen.cpp index 183bf5f6..7b439e7f 100644 --- a/src/dawntools/vnscenetool/VNSceneGen.cpp +++ b/src/dawntools/vnscenetool/VNSceneGen.cpp @@ -41,10 +41,21 @@ void VNSceneGen::generate( line(&methodStage.body, "auto camera = Camera::create(this);", ""); line(&methodStage.body, "camera->fov = 0.436332f;", ""); line(&methodStage.body, "camera->transform->lookAt(glm::vec3(10, 10, 10), glm::vec3(0, 0, 0));", ""); - line(&methodStage.body, "", ""); - classInfo.includes.push_back("prefabs/SimpleSpinningCubePrefab.hpp"); - line(&methodStage.body, "auto cube = SimpleSpinningCubePrefab::create(this);", ""); + // Items + int32_t itemRefIndex = 0; + auto itItems = scene->items.begin(); + while(itItems != scene->items.end()) { + struct VNSceneItem item = *itItems; + if(item.ref.empty()) item.ref = "item" + std::to_string(itemRefIndex++); + classInfo.includes.push_back(item.prefab + ".hpp"); + + line(&methodStage.body, "", ""); + line(&methodStage.body, "auto " + item.ref + " = " + item.className + "::create(this);", ""); + + ++itItems; + } + line(&methodStage.body, "", ""); // Events @@ -60,27 +71,42 @@ void VNSceneGen::generate( std::string eventName = "event" + std::to_string(eventIndex); std::string initType = ""; std::string initArgs = ""; + std::string toInclude = ""; std::string prev = "previous"; std::vector afterLines; switch(itEvents->type) { case VN_SCENE_EVENT_TYPE_TEXT: initType = "VNTextEvent"; + toInclude = "games/vn/events/VNTextEvent.hpp"; line(&afterLines, eventName + "->" + "text = \"" + itEvents->text.texts.begin()->text + "\";", ""); break; case VN_SCENE_EVENT_TYPE_POSITION: initType = "VNPositionEvent"; + toInclude = "games/vn/events/VNPositionEvent.hpp"; line(&afterLines, eventName + "->item = " + itEvents->position.item + ";", ""); if(itEvents->position.x != "") line(&afterLines, eventName + "->" + "to.x = " + itEvents->position.x + ";", ""); if(itEvents->position.y != "") line(&afterLines, eventName + "->" + "to.y = " + itEvents->position.y + ";", ""); if(itEvents->position.z != "") line(&afterLines, eventName + "->" + "to.z = " + itEvents->position.z + ";", ""); break; + case VN_SCENE_EVENT_TYPE_SET: + initType = "VNSetEvent<" + itEvents->set.type + ">"; + toInclude = "games/vn/events/VNSetEvent.hpp"; + line(&afterLines, eventName + "->modifies = &" + itEvents->set.property + ";", ""); + line(&afterLines, eventName + "->to = " + itEvents->set.to + ";", ""); + if(itEvents->set.from != "") line(&afterLines, eventName + "->from = " + itEvents->set.from + ";", ""); + if(itEvents->set.duration != "") line(&afterLines, eventName + "->duration = " + itEvents->set.duration + ";", ""); + break; + + default: + std::cout << "Unknown event type: " << itEvents->type << std::endl; assertUnreachable(); } + if(!toInclude.empty()) classInfo.includes.push_back(toInclude); line(&methodStage.body, "", ""); line( &methodStage.body, diff --git a/src/dawntools/vnscenetool/VNSceneItemParser.cpp b/src/dawntools/vnscenetool/VNSceneItemParser.cpp new file mode 100644 index 00000000..e903e57a --- /dev/null +++ b/src/dawntools/vnscenetool/VNSceneItemParser.cpp @@ -0,0 +1,47 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "VNSceneItemParser.hpp" + +using namespace Dawn; + +std::vector VNSceneItemParser::getRequiredAttributes() { + return { "prefab" }; +} + +std::map VNSceneItemParser::getOptionalAttributes() { + return { + { "ref", "" } + }; +} + +int32_t VNSceneItemParser::onParse( + Xml *node, + std::map values, + struct VNSceneItem *out, + std::string *error +) { + out->ref = values["ref"]; + out->prefab = values["prefab"]; + + // Split prefab by / and use the last part as the class name + std::string clazz = out->prefab; + size_t pos = clazz.find_last_of('/'); + if(pos != std::string::npos) clazz = clazz.substr(pos+1); + out->className = clazz; + + // Make sure prefab and className is not empty + if(out->prefab.empty()) { + *error = "Prefab cannot be empty."; + return -1; + } + + if(out->className.empty()) { + *error = "Class name cannot be empty."; + return -1; + } + + return 0; +} \ No newline at end of file diff --git a/src/dawntools/vnscenetool/VNSceneItemParser.hpp b/src/dawntools/vnscenetool/VNSceneItemParser.hpp new file mode 100644 index 00000000..02515e9b --- /dev/null +++ b/src/dawntools/vnscenetool/VNSceneItemParser.hpp @@ -0,0 +1,27 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "util/XmlParser.hpp" + +namespace Dawn { + struct VNSceneItem { + std::string ref; + std::string prefab; + std::string className; + }; + + class VNSceneItemParser : public XmlParser { + protected: + std::vector getRequiredAttributes() override; + std::map getOptionalAttributes() override; + int32_t onParse( + Xml *node, + std::map values, + struct VNSceneItem *out, + std::string *error + ) override; + }; +} \ No newline at end of file diff --git a/src/dawntools/vnscenetool/VNSceneParser.cpp b/src/dawntools/vnscenetool/VNSceneParser.cpp index dd77240d..736ec8c5 100644 --- a/src/dawntools/vnscenetool/VNSceneParser.cpp +++ b/src/dawntools/vnscenetool/VNSceneParser.cpp @@ -29,9 +29,20 @@ int32_t VNSceneParser::onParse( Xml *child = *itChildren; // Parse event(s) - if(child->node == "events") { + if(child->node == "item") { + struct VNSceneItem item; + ret = (VNSceneItemParser()).parse(child, &item, error); + if(ret != 0) return ret; + out->items.push_back(item); + + } else if(child->node == "events") { ret = (VNSceneEventsParser()).parse(child, &out->events, error); if(ret != 0) return ret; + + } else { + // Unknown node + *error = "Unknown node '" + child->node + "' in "; + return -1; } itChildren++; } diff --git a/src/dawntools/vnscenetool/VNSceneParser.hpp b/src/dawntools/vnscenetool/VNSceneParser.hpp index 49bd3c5c..8a391919 100644 --- a/src/dawntools/vnscenetool/VNSceneParser.hpp +++ b/src/dawntools/vnscenetool/VNSceneParser.hpp @@ -4,10 +4,12 @@ // https://opensource.org/licenses/MIT #pragma once +#include "VNSceneItemParser.hpp" #include "VNSceneEventsParser.hpp" namespace Dawn { struct VNScene { + std::vector items; struct VNSceneEventList events; }; diff --git a/src/dawntools/vnscenetool/events/CMakeLists.txt b/src/dawntools/vnscenetool/events/CMakeLists.txt index aa7a159b..31ac61e8 100644 --- a/src/dawntools/vnscenetool/events/CMakeLists.txt +++ b/src/dawntools/vnscenetool/events/CMakeLists.txt @@ -8,4 +8,5 @@ target_sources(vnscenetool PRIVATE VNPositionEventParser.cpp VNTextEventParser.cpp + VNSetEventParser.cpp ) \ No newline at end of file diff --git a/src/dawntools/vnscenetool/events/VNSetEventParser.cpp b/src/dawntools/vnscenetool/events/VNSetEventParser.cpp new file mode 100644 index 00000000..878dcd7b --- /dev/null +++ b/src/dawntools/vnscenetool/events/VNSetEventParser.cpp @@ -0,0 +1,45 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "VNSetEventParser.hpp" + +using namespace Dawn; + +std::vector VNSetEventParser::getRequiredAttributes() { + return { "property", "type" }; +} + +std::map VNSetEventParser::getOptionalAttributes() { + return { + { "to", "" }, + { "value", "" }, + { "from", "" }, + { "duration", "" }, + { "curve", "" } + }; +} + +int32_t VNSetEventParser::onParse( + Xml *node, + std::map values, + struct VNSetEvent *out, + std::string *error +) { + if(values["to"] != "") { + out->to = values["to"]; + } else if(values["value"] != "") { + out->to = values["value"]; + } else { + *error = "Either 'to' or 'value' must be specified"; + return -1; + } + + out->type = values["type"]; + out->property = values["property"]; + out->from = values["from"]; + out->duration = values["duration"]; + out->curve = values["curve"]; + return 0; +} \ No newline at end of file diff --git a/src/dawntools/vnscenetool/events/VNSetEventParser.hpp b/src/dawntools/vnscenetool/events/VNSetEventParser.hpp new file mode 100644 index 00000000..d085fa54 --- /dev/null +++ b/src/dawntools/vnscenetool/events/VNSetEventParser.hpp @@ -0,0 +1,30 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "util/XmlParser.hpp" + +namespace Dawn { + struct VNSetEvent { + std::string property = ""; + std::string to = ""; + std::string from = ""; + std::string duration = ""; + std::string curve = ""; + std::string type = ""; + }; + + class VNSetEventParser : public XmlParser { + protected: + std::vector getRequiredAttributes() override; + std::map getOptionalAttributes() override; + int32_t onParse( + Xml *node, + std::map values, + struct VNSetEvent *out, + std::string *error + ) override; + }; +} \ No newline at end of file