Working on parser
This commit is contained in:
		@@ -19,6 +19,7 @@ target_sources(vnscenetool
 | 
			
		||||
    VNSceneParser.cpp
 | 
			
		||||
    VNSceneEventsParser.cpp
 | 
			
		||||
    VNSceneGen.cpp
 | 
			
		||||
    VNSceneItemParser.cpp
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Includes
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -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<std::string> 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,
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										47
									
								
								src/dawntools/vnscenetool/VNSceneItemParser.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/dawntools/vnscenetool/VNSceneItemParser.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -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<std::string> VNSceneItemParser::getRequiredAttributes() {
 | 
			
		||||
  return { "prefab" };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::map<std::string, std::string> VNSceneItemParser::getOptionalAttributes() {
 | 
			
		||||
  return {
 | 
			
		||||
    { "ref", "" }
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int32_t VNSceneItemParser::onParse(
 | 
			
		||||
  Xml *node,
 | 
			
		||||
  std::map<std::string, std::string> 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;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								src/dawntools/vnscenetool/VNSceneItemParser.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/dawntools/vnscenetool/VNSceneItemParser.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -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<struct VNSceneItem> {
 | 
			
		||||
    protected:
 | 
			
		||||
      std::vector<std::string> getRequiredAttributes() override;
 | 
			
		||||
      std::map<std::string, std::string> getOptionalAttributes() override;
 | 
			
		||||
      int32_t onParse(
 | 
			
		||||
        Xml *node,
 | 
			
		||||
        std::map<std::string, std::string> values,
 | 
			
		||||
        struct VNSceneItem *out,
 | 
			
		||||
        std::string *error
 | 
			
		||||
      ) override;
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
@@ -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 <vnscene>";
 | 
			
		||||
      return -1;
 | 
			
		||||
    }
 | 
			
		||||
    itChildren++;
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -4,10 +4,12 @@
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "VNSceneItemParser.hpp"
 | 
			
		||||
#include "VNSceneEventsParser.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  struct VNScene {
 | 
			
		||||
    std::vector<struct VNSceneItem> items;
 | 
			
		||||
    struct VNSceneEventList events;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -8,4 +8,5 @@ target_sources(vnscenetool
 | 
			
		||||
  PRIVATE
 | 
			
		||||
    VNPositionEventParser.cpp
 | 
			
		||||
    VNTextEventParser.cpp
 | 
			
		||||
    VNSetEventParser.cpp
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										45
									
								
								src/dawntools/vnscenetool/events/VNSetEventParser.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/dawntools/vnscenetool/events/VNSetEventParser.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -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<std::string> VNSetEventParser::getRequiredAttributes() {
 | 
			
		||||
  return { "property", "type" };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::map<std::string, std::string> VNSetEventParser::getOptionalAttributes() {
 | 
			
		||||
  return {
 | 
			
		||||
    { "to", "" },
 | 
			
		||||
    { "value", "" },
 | 
			
		||||
    { "from", "" },
 | 
			
		||||
    { "duration", "" },
 | 
			
		||||
    { "curve", "" }
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int32_t VNSetEventParser::onParse(
 | 
			
		||||
  Xml *node,
 | 
			
		||||
  std::map<std::string, std::string> 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;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								src/dawntools/vnscenetool/events/VNSetEventParser.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/dawntools/vnscenetool/events/VNSetEventParser.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -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<struct VNSetEvent> {
 | 
			
		||||
    protected:
 | 
			
		||||
      std::vector<std::string> getRequiredAttributes() override;
 | 
			
		||||
      std::map<std::string, std::string> getOptionalAttributes() override;
 | 
			
		||||
      int32_t onParse(
 | 
			
		||||
        Xml *node,
 | 
			
		||||
        std::map<std::string, std::string> values,
 | 
			
		||||
        struct VNSetEvent *out,
 | 
			
		||||
        std::string *error
 | 
			
		||||
      ) override;
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user