diff --git a/core/story-manager/lib/variable.h b/core/story-manager/lib/variable.h index fe46367..425dcdd 100644 --- a/core/story-manager/lib/variable.h +++ b/core/story-manager/lib/variable.h @@ -208,6 +208,24 @@ public: return result; } + static std::string ValueTypeToString(ValueType type) { + switch (type) { + case ValueType::INTEGER: return "Integer"; + case ValueType::FLOAT: return "Float"; + case ValueType::BOOL: return "Bool"; + case ValueType::STRING: return "String"; + default: return "Unknown"; + } + } + + static ValueType StringToValueType(const std::string& type) { + if (type == "Integer") return ValueType::INTEGER; + if (type == "Float") return ValueType::FLOAT; + if (type == "Bool") return ValueType::BOOL; + if (type == "String") return ValueType::STRING; + throw std::runtime_error("[variable.h] StringToValueType(): Invalid value type string"); + } + private: std::string m_variableName; // nom humain ValueType m_valueType; diff --git a/core/story-manager/src/story_project.cpp b/core/story-manager/src/story_project.cpp index 2f8a706..fc4e9a7 100644 --- a/core/story-manager/src/story_project.cpp +++ b/core/story-manager/src/story_project.cpp @@ -501,6 +501,40 @@ bool StoryProject::Load(ResourceManager &manager) ModelFromJson(j); m_initialized = true; } + + if (j.contains("variables")) + { + nlohmann::json variablesData = j["variables"]; + + for (const auto &obj : variablesData) + { + auto v = std::make_shared(obj["label"].get()); + v->SetUuid(obj["uuid"].get()); + v->SetValueType(Variable::StringToValueType(obj["type"].get())); + v->SetScalePower(obj["scale"].get()); + v->SetConstant(obj["constant"].get()); + v->SetVariableName(obj["name"].get()); + + if (v->IsFloat()) + { + v->SetFloatValue(std::stof(obj["value"].get())); + } + else if (v->IsInteger()) + { + v->SetIntegerValue(std::stoi(obj["value"].get())); + } + else if (v->IsString()) + { + v->SetTextValue(obj["value"].get()); + } + else if (v->IsBool()) + { + v->SetBoolValue(obj["value"].get() == "true"); + } + + m_variables.push_back(v); + } + } } } } @@ -542,6 +576,40 @@ void StoryProject::Save(ResourceManager &manager) ModelToJson(model); j["pages"] = model; + nlohmann::json variablesData; + for (const auto &v : m_variables) + { + std::string value; + + if (v->IsFloat()) + { + value = std::to_string(v->GetFloatValue()); + } + else if (v->IsInteger()) + { + value = std::to_string(v->GetIntegerValue()); + } + else if (v->IsString()) + { + value = v->GetStringValue(); + } + else if (v->IsBool()) + { + value = v->GetBoolValue() ? "true" : "false"; + } + + nlohmann::json obj = {{"name", v->GetVariableName()}, + {"label", v->GetLabel()}, + {"uuid", v->GetUuid()}, + {"value", value}, + {"scale", v->GetScalePower()}, + {"constant", v->IsConstant()}, + {"type", Variable::ValueTypeToString(v->GetValueType())}}; + + variablesData.push_back(obj); + } + j["variables"] = variablesData; + std::ofstream o(m_project_file_path); o << std::setw(4) << j << std::endl; } @@ -553,6 +621,7 @@ void StoryProject::Clear() m_working_dir = ""; m_project_file_path = ""; m_initialized = false; + m_variables.clear(); } diff --git a/shared/library_manager.cpp b/shared/library_manager.cpp index ae0493a..e5c3719 100644 --- a/shared/library_manager.cpp +++ b/shared/library_manager.cpp @@ -104,6 +104,21 @@ std::shared_ptr LibraryManager::GetStory(const std::string &uuid) return current; } +void LibraryManager::RemoveStory(const std::string &uuid) +{ + for (const auto &s : m_projectsList) + { + if (s->GetUuid() == uuid) + { + // Just rename the project file, keep the whole directory + auto oldname = s->GetProjectFilePath(); + std::filesystem::rename(oldname, oldname + "__removed"); + break; + } + } + Scan(); +} + std::string LibraryManager::IndexFileName() const { auto p = std::filesystem::path(m_library_path) / "index.ost"; diff --git a/shared/library_manager.h b/shared/library_manager.h index b6e31ba..e9cf2ac 100644 --- a/shared/library_manager.h +++ b/shared/library_manager.h @@ -45,6 +45,7 @@ public: std::string GetStoreUrl() const { return m_storeUrl; } void AddStory(IStoryDb::Info &info, int origin); + void RemoveStory(const std::string &uuid); void ParseCommunityStore(const std::string &jsonFileName); void ParseCommercialStore(const std::string &jsonFileName); diff --git a/story-editor/src/windows/library_window.cpp b/story-editor/src/windows/library_window.cpp index b563721..90fabe6 100644 --- a/story-editor/src/windows/library_window.cpp +++ b/story-editor/src/windows/library_window.cpp @@ -481,6 +481,8 @@ void LibraryWindow::Draw() ); } + std::string rmUuid; + if (ImGui::BeginTable("library_table", 3, tableFlags)) { ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed); @@ -539,11 +541,16 @@ void LibraryWindow::Draw() ImGui::SameLine(); if (ImGui::SmallButton("Remove")) { - + rmUuid = p->GetUuid(); } ImGui::PopID(); } ImGui::EndTable(); + + if (!rmUuid.empty()) + { + m_libraryManager.RemoveStory(rmUuid); + } } ImGui::EndTabItem(); } diff --git a/story-editor/src/windows/main_window.cpp b/story-editor/src/windows/main_window.cpp index 0791bea..2384d60 100644 --- a/story-editor/src/windows/main_window.cpp +++ b/story-editor/src/windows/main_window.cpp @@ -924,11 +924,11 @@ void MainWindow::RefreshProjectInformation() void MainWindow::CloseProject() { - // if (m_story) - // { - // m_story->Clear(); - // m_story.reset(); - // } + if (m_story) + { + m_story->Clear(); + m_story.reset(); + } m_resources.Clear(); diff --git a/story-editor/src/windows/variables_window.cpp b/story-editor/src/windows/variables_window.cpp index b67cd2c..eb0bc0b 100644 --- a/story-editor/src/windows/variables_window.cpp +++ b/story-editor/src/windows/variables_window.cpp @@ -15,8 +15,6 @@ VariablesWindow::VariablesWindow(IStoryManager &proj) void VariablesWindow::Initialize() { - - }