diff --git a/.gitignore b/.gitignore index 45833595..d7c3cd4c 100644 --- a/.gitignore +++ b/.gitignore @@ -85,4 +85,6 @@ assets/borrowed /vita /tools -._* \ No newline at end of file +._* + +*~ \ No newline at end of file diff --git a/assets/games/liminal/prefabs/Button.xml b/assets/games/liminal/prefabs/Button.xml deleted file mode 100644 index 65f2a6a4..00000000 --- a/assets/games/liminal/prefabs/Button.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - useEvent([&]{ - uiItem->color = COLOR_RED; - }, menuItem->eventHoveredOn); - - useEvent([&]{ - uiItem->color = COLOR_BLUE; - }, menuItem->eventHoveredOff); - - useEvent([&]{ - uiItem->color = COLOR_GREEN; - }, menuItem->eventSelected); - - \ No newline at end of file diff --git a/assets/games/liminal/prefabs/ButtonPrefab.xml b/assets/games/liminal/prefabs/ButtonPrefab.xml new file mode 100644 index 00000000..b87ad686 --- /dev/null +++ b/assets/games/liminal/prefabs/ButtonPrefab.xml @@ -0,0 +1,118 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Hello Button. + + + + + useEvent([&]{ + hoverDeocration->color = COLOR_WHITE; + }, menuItem->eventHoveredOn); + + useEvent([&]{ + hoverDeocration->color = COLOR_TRANSPARENT; + }, menuItem->eventHoveredOff); + + \ No newline at end of file diff --git a/assets/games/liminal/prefabs/CMakeLists.txt b/assets/games/liminal/prefabs/CMakeLists.txt index a316a27b..e224b9f1 100644 --- a/assets/games/liminal/prefabs/CMakeLists.txt +++ b/assets/games/liminal/prefabs/CMakeLists.txt @@ -4,7 +4,7 @@ # https://opensource.org/licenses/MIT tool_prefab(${CMAKE_CURRENT_LIST_DIR}/AvePrefab.xml) -tool_prefab(${CMAKE_CURRENT_LIST_DIR}/Button.xml) +tool_prefab(${CMAKE_CURRENT_LIST_DIR}/ButtonPrefab.xml) tool_prefab(${CMAKE_CURRENT_LIST_DIR}/EthPrefab.xml) tool_prefab(${CMAKE_CURRENT_LIST_DIR}/CraigPrefab.xml) tool_prefab(${CMAKE_CURRENT_LIST_DIR}/RoninPrefab.xml) diff --git a/assets/games/liminal/prefabs/VNTextbox.xml b/assets/games/liminal/prefabs/VNTextbox.xml index 20fa4f01..d8a0ec8f 100644 --- a/assets/games/liminal/prefabs/VNTextbox.xml +++ b/assets/games/liminal/prefabs/VNTextbox.xml @@ -26,11 +26,17 @@ + + + + + + - + \ No newline at end of file diff --git a/assets/games/liminal/textures/CMakeLists.txt b/assets/games/liminal/textures/CMakeLists.txt index 4e80536b..d79af33b 100644 --- a/assets/games/liminal/textures/CMakeLists.txt +++ b/assets/games/liminal/textures/CMakeLists.txt @@ -9,6 +9,7 @@ include("${CMAKE_CURRENT_LIST_DIR}/ave/CMakeLists.txt") include("${CMAKE_CURRENT_LIST_DIR}/craig/CMakeLists.txt") include("${CMAKE_CURRENT_LIST_DIR}/eth/CMakeLists.txt") include("${CMAKE_CURRENT_LIST_DIR}/ronin/CMakeLists.txt") +include("${CMAKE_CURRENT_LIST_DIR}/ui/CMakeLists.txt") tool_texture(texture_border FILE=${CMAKE_CURRENT_LIST_DIR}/texture_test.png diff --git a/assets/games/liminal/textures/ui/CMakeLists.txt b/assets/games/liminal/textures/ui/CMakeLists.txt new file mode 100644 index 00000000..9ed47a38 --- /dev/null +++ b/assets/games/liminal/textures/ui/CMakeLists.txt @@ -0,0 +1,34 @@ +# Copyright (c) 2023 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +set(BUTTON_TEXTURE_OPTIONS + FILTER_MIN=nearest + FILTER_MAG=nearest +) + +tool_texture(button_background + FILE="${CMAKE_CURRENT_LIST_DIR}/button_background.png" + ${BUTTON_TEXTURE_OPTIONS} +) + +tool_texture(button_hover_element + FILE="${CMAKE_CURRENT_LIST_DIR}/button_hover_element.png" + ${BUTTON_TEXTURE_OPTIONS} +) + +tool_texture(button_wings_down + FILE="${CMAKE_CURRENT_LIST_DIR}/button_wings_down.png" + ${BUTTON_TEXTURE_OPTIONS} +) + +tool_texture(button_wings_inactive + FILE="${CMAKE_CURRENT_LIST_DIR}/button_wings_inactive.png" + ${BUTTON_TEXTURE_OPTIONS} +) + +tool_texture(button_wings_open + FILE="${CMAKE_CURRENT_LIST_DIR}/button_wings_open.png" + ${BUTTON_TEXTURE_OPTIONS} +) \ No newline at end of file diff --git a/assets/games/liminal/textures/ui/button_background.png b/assets/games/liminal/textures/ui/button_background.png new file mode 100644 index 00000000..2df5099c Binary files /dev/null and b/assets/games/liminal/textures/ui/button_background.png differ diff --git a/assets/games/liminal/textures/ui/button_hover_element.png b/assets/games/liminal/textures/ui/button_hover_element.png new file mode 100644 index 00000000..10c2db8b Binary files /dev/null and b/assets/games/liminal/textures/ui/button_hover_element.png differ diff --git a/assets/games/liminal/textures/ui/button_wings_down.png b/assets/games/liminal/textures/ui/button_wings_down.png new file mode 100644 index 00000000..584a04e4 Binary files /dev/null and b/assets/games/liminal/textures/ui/button_wings_down.png differ diff --git a/assets/games/liminal/textures/ui/button_wings_inactive.png b/assets/games/liminal/textures/ui/button_wings_inactive.png new file mode 100644 index 00000000..659e1717 Binary files /dev/null and b/assets/games/liminal/textures/ui/button_wings_inactive.png differ diff --git a/assets/games/liminal/textures/ui/button_wings_open.png b/assets/games/liminal/textures/ui/button_wings_open.png new file mode 100644 index 00000000..e941841a Binary files /dev/null and b/assets/games/liminal/textures/ui/button_wings_open.png differ diff --git a/src/dawn/scene/components/ui/CMakeLists.txt b/src/dawn/scene/components/ui/CMakeLists.txt index 1c6c5c19..e1c924b2 100644 --- a/src/dawn/scene/components/ui/CMakeLists.txt +++ b/src/dawn/scene/components/ui/CMakeLists.txt @@ -12,6 +12,7 @@ target_sources(${DAWN_TARGET_NAME} UIImage.cpp UIEmpty.cpp UIBorder.cpp + UIMesh.cpp ) add_subdirectory(menu) diff --git a/src/dawn/scene/components/ui/UIComponent.cpp b/src/dawn/scene/components/ui/UIComponent.cpp index 2bbe0451..5c0e74be 100644 --- a/src/dawn/scene/components/ui/UIComponent.cpp +++ b/src/dawn/scene/components/ui/UIComponent.cpp @@ -41,27 +41,61 @@ void UIComponent::updateAlignment() { parentInnerWidth = dimensional->getContentWidth(); parentInnerHeight = dimensional->getContentHeight(); - UIComponent::calculateDimensions( - this->alignX, - this->alignUnitLeft, - this->alignUnitRight, - &translate.x, - &this->width, - parentInnerWidth, - this->getContentWidth(), - glm::vec2(align[0], align[2]) - ); - UIComponent::calculateDimensions( - this->alignY, - this->alignUnitTop, - this->alignUnitBottom, - &translate.y, - &this->height, - parentInnerHeight, - this->getContentHeight(), - glm::vec2(align[1], align[3]) + // Should we be doing width first, or height first? + bool_t heightFirst = ( + this->alignUnitLeft == UI_COMPONENT_ALIGN_UNIT_RATIO || + this->alignUnitRight == UI_COMPONENT_ALIGN_UNIT_RATIO ); + if(heightFirst) { + UIComponent::calculateDimensions( + this->alignY, + this->alignUnitTop, + this->alignUnitBottom, + &translate.y, + &this->height, + parentInnerHeight, + this->getContentHeight(), + this->width, + glm::vec2(align[1], align[3]) + ); + UIComponent::calculateDimensions( + this->alignX, + this->alignUnitLeft, + this->alignUnitRight, + &translate.x, + &this->width, + parentInnerWidth, + this->getContentWidth(), + this->height, + glm::vec2(align[0], align[2]) + ); + } else { + UIComponent::calculateDimensions( + this->alignY, + this->alignUnitTop, + this->alignUnitBottom, + &translate.y, + &this->height, + parentInnerHeight, + this->getContentHeight(), + this->width, + glm::vec2(align[1], align[3]) + ); + UIComponent::calculateDimensions( + this->alignX, + this->alignUnitLeft, + this->alignUnitRight, + &translate.x, + &this->width, + parentInnerWidth, + this->getContentWidth(), + this->height, + glm::vec2(align[0], align[2]) + ); + } + + translate.x += dimensional->getChildOffsetX(); translate.y += dimensional->getChildOffsetY(); @@ -73,10 +107,14 @@ void UIComponent::updateAlignment() { float_t UIComponent::calculateAlignmentValue( float_t alignmentValue, float_t parentSize, + float_t ratioSize, enum UIComponentAlignUnit unit ) { if(unit == UI_COMPONENT_ALIGN_UNIT_SCALE) return alignmentValue; - return (alignmentValue / 100.0f) * parentSize; + if(unit == UI_COMPONENT_ALIGN_UNIT_PERCENT) return (alignmentValue / 100.0f) * parentSize; + if(unit == UI_COMPONENT_ALIGN_UNIT_RATIO) return (alignmentValue / 100.0f) * ratioSize; + assertUnreachable("UIComponent::calculateAlignmentValue: Unknown alignment unit"); + return -1; } void UIComponent::calculateDimensions( @@ -87,6 +125,7 @@ void UIComponent::calculateDimensions( float_t *size, float_t outerSize, float_t innerSize, + float_t ratioSize, glm::vec2 alignment ) { assertNotNull(position, "UIComponent::calculateDimensions: Position cannot be null"); @@ -99,11 +138,13 @@ void UIComponent::calculateDimensions( *position = UIComponent::calculateAlignmentValue( alignment[0], outerSize, + ratioSize, unitStart ); *size = outerSize - (*position + UIComponent::calculateAlignmentValue( alignment[1], outerSize, + ratioSize, unitEnd )); break; @@ -115,28 +156,29 @@ void UIComponent::calculateDimensions( *position = UIComponent::calculateAlignmentValue( alignment[0], outerSize, + ratioSize, unitStart ); *size = UIComponent::calculateAlignmentValue( alignment[1], outerSize, + ratioSize, unitEnd ); break; } case UI_COMPONENT_ALIGN_MIDDLE: { - *size = mathMax( - innerSize, - UIComponent::calculateAlignmentValue( - alignment[1], - outerSize, - unitEnd - ) + *size = UIComponent::calculateAlignmentValue( + alignment[1], + outerSize, + ratioSize, + unitEnd ); - *position = (outerSize / 2.0f) - (*size / 2.0f) + UIComponent::calculateAlignmentValue( + *position = (outerSize / 2.0f) - ((*size) / 2.0f) + UIComponent::calculateAlignmentValue( alignment[0], outerSize, + ratioSize, unitStart ); break; @@ -146,11 +188,13 @@ void UIComponent::calculateDimensions( *size = UIComponent::calculateAlignmentValue( alignment[0], outerSize, + ratioSize, unitStart ); *position = outerSize - *size - UIComponent::calculateAlignmentValue( alignment[1], outerSize, + ratioSize, unitEnd ); break; diff --git a/src/dawn/scene/components/ui/UIComponent.hpp b/src/dawn/scene/components/ui/UIComponent.hpp index d2b67ad7..e43fe6aa 100644 --- a/src/dawn/scene/components/ui/UIComponent.hpp +++ b/src/dawn/scene/components/ui/UIComponent.hpp @@ -21,7 +21,8 @@ namespace Dawn { enum UIComponentAlignUnit { UI_COMPONENT_ALIGN_UNIT_SCALE, - UI_COMPONENT_ALIGN_UNIT_PERCENT + UI_COMPONENT_ALIGN_UNIT_PERCENT, + UI_COMPONENT_ALIGN_UNIT_RATIO }; class UIComponent : public SceneItemComponent, public UIComponentDimensional { @@ -53,12 +54,14 @@ namespace Dawn { * * @param alignmentValue Alignment value. * @param parentSize Parent size. + * @param ratioSize The dimension that the ratio is based on. * @param unit Alignment unit. * @return The calculated alignment value. */ static float_t calculateAlignmentValue( float_t alignmentValue, float_t parentSize, + float_t ratioSize, enum UIComponentAlignUnit unit ); @@ -72,6 +75,7 @@ namespace Dawn { * @param size Output size floating point. * @param outerSize Outer size (of the parent). * @param innerSize Inner size (of this element's content). + * @param ratioSize The size that the opposite dimension is. * @param alignment Alignment settings. */ static void calculateDimensions( @@ -82,6 +86,7 @@ namespace Dawn { float_t *size, float_t outerSize, float_t innerSize, + float_t ratioSize, glm::vec2 alignment ); diff --git a/src/dawn/scene/components/ui/UIImage.cpp b/src/dawn/scene/components/ui/UIImage.cpp index 5168b025..71b36049 100644 --- a/src/dawn/scene/components/ui/UIImage.cpp +++ b/src/dawn/scene/components/ui/UIImage.cpp @@ -10,7 +10,8 @@ using namespace Dawn; UIImage::UIImage(SceneItem *item) : texture(nullptr), - UIComponentRenderable(item) + UIComponentRenderable(item), + uvs(glm::vec4(0, 1, 1, 0)) { } @@ -42,7 +43,6 @@ std::vector UIImage::getUIRenderPasses() { item.w = this->transform->getWorldPosition().z; item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND; item.mesh = &mesh; - return { item }; } @@ -51,13 +51,22 @@ void UIImage::onStart() { useEvent([&]{ QuadMesh::bufferPositions(&mesh, - glm::vec2(0, 0), glm::vec2(width, height), 0 + glm::vec2(0, 0), + glm::vec2(width, height), 0 ); }, this->eventAlignmentUpdated); - QuadMesh::initQuadMesh(&mesh, - glm::vec2(0, 0), glm::vec2(0, 1), - glm::vec2(width, height), glm::vec2(1, 0), + useEffect([&]{ + QuadMesh::bufferCoordinates(&mesh, + glm::vec2(this->uvs._realValue[0], this->uvs._realValue[1]), + glm::vec2(this->uvs._realValue[2], this->uvs._realValue[3]), + 0 + ); + }, this->uvs); + + QuadMesh::initQuadMesh(&mesh, + glm::vec2(0, 0), glm::vec2(this->uvs._realValue[0], this->uvs._realValue[1]), + glm::vec2(width, height), glm::vec2(this->uvs._realValue[2], this->uvs._realValue[3]), 0.0f ); } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UIImage.hpp b/src/dawn/scene/components/ui/UIImage.hpp index 675f3aad..bcd0a766 100644 --- a/src/dawn/scene/components/ui/UIImage.hpp +++ b/src/dawn/scene/components/ui/UIImage.hpp @@ -17,6 +17,8 @@ namespace Dawn { struct Color color = COLOR_WHITE; // @optional StateProperty texture; + // @optional + StateProperty uvs; UIImage(SceneItem *item); diff --git a/src/dawn/scene/components/ui/UIMesh.cpp b/src/dawn/scene/components/ui/UIMesh.cpp new file mode 100644 index 00000000..5cf0423c --- /dev/null +++ b/src/dawn/scene/components/ui/UIMesh.cpp @@ -0,0 +1,56 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "UIMesh.hpp" +#include "game/DawnGame.hpp" + +using namespace Dawn; + +UIMesh::UIMesh(SceneItem *i) : + texture(nullptr), + UIComponentRenderable(i) +{ + +} + +float_t UIMesh::getContentWidth() { + if(this->texture != nullptr) return this->texture->getWidth(); + return this->width; +} + +float_t UIMesh::getContentHeight() { + if(this->texture != nullptr) return this->texture->getHeight(); + return this->height; +} + +std::vector UIMesh::getUIRenderPasses() { + struct ShaderPassItem item; + auto shader = getGame()->renderManager.uiShader; + item.shader = shader; + item.colorValues[shader->paramColor] = this->color; + item.parameterBuffers[shader->bufferUiCanvas] = &getCanvas()->shaderBuffer; + item.matrixValues[shader->paramModel] = this->transform->getWorldTransform(); + if(this->texture == nullptr) { + item.boolValues[shader->paramHasTexture] = false; + } else { + item.boolValues[shader->paramHasTexture] = true; + item.textureSlots[0] = this->texture; + item.textureValues[shader->paramTexture] = 0; + } + item.w = this->transform->getWorldPosition().z; + item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND; + item.mesh = &mesh; + return { item }; +} + +void UIMesh::onStart() { + useEffect([&]{ + + }, this->positions)(); + + useEffect([&]{ + + }, this->uvs)(); +} \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UIMesh.hpp b/src/dawn/scene/components/ui/UIMesh.hpp new file mode 100644 index 00000000..b9703ec2 --- /dev/null +++ b/src/dawn/scene/components/ui/UIMesh.hpp @@ -0,0 +1,31 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "UIComponentRenderable.hpp" +#include "display/mesh/QuadMesh.hpp" + +namespace Dawn { + class UIMesh : public UIComponentRenderable { + private: + Mesh mesh; + + public: + // @optional + StateProperty> positions; + // @optional + StateProperty> uvs; + // @optional + struct Color color = COLOR_WHITE; + // @optional + StateProperty texture; + + UIMesh(SceneItem *item); + float_t getContentWidth() override; + float_t getContentHeight() override; + std::vector getUIRenderPasses() override; + void onStart() override; + }; +} \ No newline at end of file diff --git a/src/dawnshared/display/Color.cpp b/src/dawnshared/display/Color.cpp index e9caa202..64304b0b 100644 --- a/src/dawnshared/display/Color.cpp +++ b/src/dawnshared/display/Color.cpp @@ -32,6 +32,9 @@ struct Color Color::fromString(std::string str) { } else if(stringIncludes(lower, "blue")) { return COLOR_BLUE; + + } else if(stringIncludes(lower, "transparent")) { + return COLOR_TRANSPARENT; } // Hex code? diff --git a/src/dawnshared/display/Color.hpp b/src/dawnshared/display/Color.hpp index 3eeca8fa..82ec6a59 100644 --- a/src/dawnshared/display/Color.hpp +++ b/src/dawnshared/display/Color.hpp @@ -86,5 +86,5 @@ namespace Dawn { #define COLOR_CORNFLOWER_BLUE COLOR_DEF(0.4f, 0.6f, 0.9f, 1.0f) #define COLOR_WHITE_TRANSPARENT COLOR_DEF(1.0f, 1.0f, 1.0f, 0.0f) #define COLOR_BLACK_TRANSPARENT COLOR_DEF(0.0f, 0.0f, 0.0f, 0.0f) - #define COLOR_TRANSPARENT COLOR_BLACK_TRANSPARENT + #define COLOR_TRANSPARENT COLOR_WHITE_TRANSPARENT } \ No newline at end of file diff --git a/src/dawnshared/util/parser/TypeParsers.hpp b/src/dawnshared/util/parser/TypeParsers.hpp index 2d020875..c518ef55 100644 --- a/src/dawnshared/util/parser/TypeParsers.hpp +++ b/src/dawnshared/util/parser/TypeParsers.hpp @@ -110,7 +110,7 @@ namespace Dawn { // Split string by comma into two strings that we pass into float auto split = stringSplit(v, ","); if(split.size() != 4) { - *error = "Invalid vec4 value: " + v; + *error = "Invalid vec4 value: " + v + " (incorrect split len " + std::to_string(split.size()) + ")"; return std::string(""); } return std::string( @@ -125,6 +125,28 @@ namespace Dawn { static inline std::string colorParser(std::string v, std::string *error) { return "Color::fromString(" + stringParser(v, error) + ")"; + }; + + static inline std::string uiComponentAlignParser(std::string v, std::string *error) { + v = stringToLowercase(v); + if(v.find("left") != std::string::npos) return "UI_COMPONENT_ALIGN_START"; + if(v.find("center") != std::string::npos) return "UI_COMPONENT_ALIGN_MIDDLE"; + if(v.find("right") != std::string::npos) return "UI_COMPONENT_ALIGN_END"; + if(v.find("top") != std::string::npos) return "UI_COMPONENT_ALIGN_START"; + if(v.find("bottom") != std::string::npos) return "UI_COMPONENT_ALIGN_END"; + if(v.find("stretch") != std::string::npos) return "UI_COMPONENT_ALIGN_STRETCH"; + if(v.find("start") != std::string::npos) return "UI_COMPONENT_ALIGN_START"; + if(v.find("middle") != std::string::npos) return "UI_COMPONENT_ALIGN_MIDDLE"; + if(v.find("end") != std::string::npos) return "UI_COMPONENT_ALIGN_END"; + *error = "Invalid UIComponentAlign value: " + v; + } + + static inline std::string uiComponentAlignUnitParser(std::string v, std::string *error) { + v = stringToLowercase(v); + if(v.find("scale") != std::string::npos) return "UI_COMPONENT_ALIGN_UNIT_SCALE"; + if(v.find("percent") != std::string::npos) return "UI_COMPONENT_ALIGN_UNIT_PERCENT"; + if(v.find("ratio") != std::string::npos) return "UI_COMPONENT_ALIGN_UNIT_RATIO"; + *error = "Invalid UIComponentAlignUnit value: " + v; } static inline std::function parserFromTypeName(std::string type) { @@ -149,7 +171,14 @@ namespace Dawn { } else if(type == "flag_t") { parser = rawParser; } else if(type.starts_with("enum")) { - parser = rawParser; + // Handle Enum Cases + if(type.ends_with("UIComponentAlign")) { + parser = uiComponentAlignParser; + } else if(type.ends_with("UIComponentAlignUnit")) { + parser = uiComponentAlignUnitParser; + } else { + parser = rawParser; + } } else if(type.find("*") == (type.size() - 1)) { type = type.substr(0, type.size() - 1); parser = rawParser; diff --git a/src/dawntools/util/parser/TypeParsers.hpp b/src/dawntools/util/parser/TypeParsers.hpp deleted file mode 100644 index 8bdab236..00000000 --- a/src/dawntools/util/parser/TypeParsers.hpp +++ /dev/null @@ -1,155 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "util/string.hpp" - -namespace Dawn { - static inline std::string rawParser(std::string v, std::string *error) { - return v; - }; - - static inline std::string stringParser(std::string v, std::string *error) { - return "\"" + v + "\""; - }; - - static inline std::string floatParser(std::string v, std::string *error) { - v = stringTrim(v); - - // Make sure number contains decimal - if(v.find(".") == std::string::npos) { - v += ".0"; - } - // Make sure number contains a number before the decimal - if(v.find(".") == 0) { - v = "0" + v; - } - // Make sure ends with f - if(v.find("f") == std::string::npos) { - v += "f"; - } - return v; - }; - - static inline std::string intParser(std::string v, std::string *error) { - v = stringTrim(v); - return v; - } - - static inline std::string boolParser(std::string v, std::string *error) { - v = stringTrim(v); - if(v == "true") return "true"; - if(v == "false") return "false"; - *error = "Invalid bool value: " + v; - return std::string(""); - } - - static inline std::string vec2Parser(std::string v, std::string *error) { - // Split string by comma into two strings that we pass into float - auto split = stringSplit(v, ","); - if(split.size() != 2) { - *error = "Invalid vec2 value: " + v; - return std::string(""); - } - return std::string( - "glm::vec2(" + - floatParser(split[0], error) + ", " + - floatParser(split[1], error) + - ")" - ); - }; - - - static inline std::string vec3Parser(std::string v, std::string *error) { - // Split string by comma into two strings that we pass into float - auto split = stringSplit(v, ","); - if(split.size() != 3) { - *error = "Invalid vec3 value: " + v; - return std::string(""); - } - return std::string( - "glm::vec3(" + - floatParser(split[0], error) + ", " + - floatParser(split[1], error) + ", " + - floatParser(split[2], error) + - ")" - ); - }; - - - static inline std::string vec6Parser(std::string v, std::string *error) { - // Split string by comma into two strings that we pass into float - auto split = stringSplit(v, ","); - if(split.size() != 6) { - *error = "Invalid vec6 value: " + v; - return std::string(""); - } - return std::string( - "glm::vec3(" + - floatParser(split[0], error) + ", " + - floatParser(split[1], error) + ", " + - floatParser(split[2], error) + ", " + - floatParser(split[3], error) + ", " + - floatParser(split[4], error) + ", " + - floatParser(split[5], error) + - ")" - ); - }; - - - static inline std::string vec4Parser(std::string v, std::string *error) { - // Split string by comma into two strings that we pass into float - auto split = stringSplit(v, ","); - if(split.size() != 4) { - *error = "Invalid vec4 value: " + v; - return std::string(""); - } - return std::string( - "glm::vec4(" + - floatParser(split[0], error) + ", " + - floatParser(split[1], error) + ", " + - floatParser(split[2], error) + ", " + - floatParser(split[3], error) + - ")" - ); - }; - - static inline std::string colorParser(std::string v, std::string *error) { - return rawParser(v, error); - } - - static inline std::function parserFromTypeName(std::string type) { - std::function parser = rawParser; - - if(type.find("string") != std::string::npos) { - parser = stringParser; - } else if(type.find("float") != std::string::npos) { - parser = floatParser; - } else if(type.find("Color") != std::string::npos) { - parser = colorParser; - } else if(type.find("vec2") != std::string::npos) { - parser = vec2Parser; - } else if(type.find("vec3") != std::string::npos) { - parser = vec3Parser; - } else if(type.find("vec4") != std::string::npos) { - parser = vec4Parser; - } else if(type == "int32_t" || type == "int") { - parser = intParser; - } else if(type == "bool_t") { - parser = boolParser; - } else if(type == "flag_t") { - parser = rawParser; - } else if(type.starts_with("enum")) { - parser = rawParser; - } else if(type.find("*") == (type.size() - 1)) { - type = type.substr(0, type.size() - 1); - parser = rawParser; - } else { - throw std::string("Invalid parser type"); - } - - return parser; - } -} \ No newline at end of file