From cc03146cfc8bf93e6680e53f14c46fcba7789744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=A9meryc?= Date: Mon, 10 Jul 2023 14:45:40 -0400 Subject: [PATCH] Use FileName as Class Name Wren Script --- Editor/src/Windows/FileSystemUI.cpp | 26 +++++++++++++++++++------ Nuake/src/Core/FileSystem.cpp | 7 +++++++ Nuake/src/Core/FileSystem.h | 1 + Nuake/src/Core/String.cpp | 30 +++++++++++++++++++++++++++++ Nuake/src/Core/String.h | 3 +++ 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/Editor/src/Windows/FileSystemUI.cpp b/Editor/src/Windows/FileSystemUI.cpp index ea6e43de..6c4f7649 100644 --- a/Editor/src/Windows/FileSystemUI.cpp +++ b/Editor/src/Windows/FileSystemUI.cpp @@ -9,13 +9,15 @@ #include "src/Rendering/Textures/TextureManager.h" #include "EditorInterface.h" -const std::string TEMPLATE_SCRIPT = R"(import "Nuake:Engine" for Engine +const std::string TEMPLATE_SCRIPT_FIRST = R"(import "Nuake:Engine" for Engine import "Nuake:ScriptableEntity" for ScriptableEntity import "Nuake:Input" for Input import "Nuake:Math" for Vector3, Math import "Nuake:Scene" for Scene -class MyEntityScript is ScriptableEntity { +class )"; + +const std::string TEMPLATE_SCRIPT_SECOND = R"( is ScriptableEntity { construct new() { } @@ -246,6 +248,7 @@ namespace Nuake if (ImGui::MenuItem("Wren Script")) { std::string path = FileDialog::SaveFile("*.wren"); + if (!String::EndsWith(path, ".wren")) { path += ".wren"; @@ -253,11 +256,22 @@ namespace Nuake if (!path.empty()) { - FileSystem::BeginWriteFile(path); - FileSystem::WriteLine(TEMPLATE_SCRIPT); - FileSystem::EndWriteFile(); + std::string fileName = String::ToUpper(FileSystem::GetFileNameFromPath(path)); + fileName = String::RemoveWhiteSpace(fileName); - RefreshFileBrowser(); + if(!String::IsDigit(fileName[0])) + { + + FileSystem::BeginWriteFile(path); + FileSystem::WriteLine(TEMPLATE_SCRIPT_FIRST + fileName + TEMPLATE_SCRIPT_SECOND); + FileSystem::EndWriteFile(); + + RefreshFileBrowser(); + } + else + { + Logger::Log("[FileSystem] Cannot create script files that starts with a number.", CRITICAL); + } } } diff --git a/Nuake/src/Core/FileSystem.cpp b/Nuake/src/Core/FileSystem.cpp index 1376a051..5c744d1d 100644 --- a/Nuake/src/Core/FileSystem.cpp +++ b/Nuake/src/Core/FileSystem.cpp @@ -210,4 +210,11 @@ namespace Nuake return nullptr; } + + std::string FileSystem::GetFileNameFromPath(const std::string& path) + { + const auto split = String::Split(path, '\\'); + return String::Split(split[split.size() - 1], '.')[0]; + } + } diff --git a/Nuake/src/Core/FileSystem.h b/Nuake/src/Core/FileSystem.h index f0f8ecf1..9febe52c 100644 --- a/Nuake/src/Core/FileSystem.h +++ b/Nuake/src/Core/FileSystem.h @@ -31,6 +31,7 @@ namespace Nuake static std::string AbsoluteToRelative(const std::string& path); static Ref GetFileTree(); static Ref GetFile(const std::string& path); + static std::string GetFileNameFromPath(const std::string& path); static void ScanDirectory(Ref directory); static void GetDirectories(); diff --git a/Nuake/src/Core/String.cpp b/Nuake/src/Core/String.cpp index b37ae72c..b71007de 100644 --- a/Nuake/src/Core/String.cpp +++ b/Nuake/src/Core/String.cpp @@ -19,6 +19,18 @@ namespace Nuake { return false; } + bool String::IsDigit(const char& character) + { + return character <= 57 && character >= 48; + } + + std::string String::RemoveWhiteSpace(const std::string& string) + { + std::string result = string; + result.erase(remove_if(result.begin(), result.end(), isspace), result.end()); + return result; + } + std::vector String::Split(const std::string& string, char delimiter) { std::vector result; @@ -37,4 +49,22 @@ namespace Nuake { { return std::stof(string); } + + std::string String::ToUpper(const std::string& string) + { + if(string.length() == 0) + { + return ""; + } + + auto split = Split(string, ' '); + std::string result; + for (auto& word : split) + { + word[0] = std::toupper(word[0]); + result += word; + } + + return result; + } } \ No newline at end of file diff --git a/Nuake/src/Core/String.h b/Nuake/src/Core/String.h index 643028ee..50e632b0 100644 --- a/Nuake/src/Core/String.h +++ b/Nuake/src/Core/String.h @@ -11,8 +11,11 @@ namespace Nuake { public: static bool BeginsWith(const std::string& string, const std::string& begin); static bool EndsWith(const std::string& string, const std::string& end); + static bool IsDigit(const char& character); + static std::string RemoveWhiteSpace(const std::string& string); static std::vector Split(const std::string& string, char delimiter); static float ToFloat(const std::string& string); + static std::string ToUpper(const std::string& string); }; }