mirror of
https://github.com/arabine/open-story-teller.git
synced 2025-12-06 17:09:06 +01:00
Remove story in library + save/load variables
This commit is contained in:
parent
47552d4719
commit
6ec1f39db7
7 changed files with 116 additions and 8 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<Variable>(obj["label"].get<std::string>());
|
||||
v->SetUuid(obj["uuid"].get<std::string>());
|
||||
v->SetValueType(Variable::StringToValueType(obj["type"].get<std::string>()));
|
||||
v->SetScalePower(obj["scale"].get<int>());
|
||||
v->SetConstant(obj["constant"].get<bool>());
|
||||
v->SetVariableName(obj["name"].get<std::string>());
|
||||
|
||||
if (v->IsFloat())
|
||||
{
|
||||
v->SetFloatValue(std::stof(obj["value"].get<std::string>()));
|
||||
}
|
||||
else if (v->IsInteger())
|
||||
{
|
||||
v->SetIntegerValue(std::stoi(obj["value"].get<std::string>()));
|
||||
}
|
||||
else if (v->IsString())
|
||||
{
|
||||
v->SetTextValue(obj["value"].get<std::string>());
|
||||
}
|
||||
else if (v->IsBool())
|
||||
{
|
||||
v->SetBoolValue(obj["value"].get<std::string>() == "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();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -104,6 +104,21 @@ std::shared_ptr<StoryProject> 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";
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -15,8 +15,6 @@ VariablesWindow::VariablesWindow(IStoryManager &proj)
|
|||
void VariablesWindow::Initialize()
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue