diff options
| author | ilotterytea <iltsu@alright.party> | 2025-02-03 00:05:27 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-02-03 00:05:47 +0500 |
| commit | 9362a01f6f8b6339944f5ddf8d2f597ca672f56e (patch) | |
| tree | b81cd4e947819560261ea60bb173b7b0f0653529 | |
| parent | cac7fa97ec7b2db3a3153f9a82a31c59e5c333ae (diff) | |
feat: import package
| -rw-r--r-- | src/editor.cpp | 18 | ||||
| -rw-r--r-- | src/package.cpp | 114 | ||||
| -rw-r--r-- | src/package.hpp | 1 |
3 files changed, 132 insertions, 1 deletions
diff --git a/src/editor.cpp b/src/editor.cpp index 5417a59..6d7e0d6 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -302,10 +302,26 @@ namespace silly::editor { if (ImGui::BeginMainMenuBar()) { if (ImGui::BeginMenu("File")) { - if (ImGui::MenuItem("New package")) { + if (ImGui::MenuItem("New")) { createNewPackage(window); } + if (ImGui::MenuItem("Open...")) { + NFD::UniquePath outPath; + + nfdfilteritem_t filterItem[1] = {{"sillypackage text format", "txt"}}; + + nfdresult_t result = NFD::OpenDialog(outPath, filterItem, 1); + if (result == NFD_OKAY) { + this->newFloorState = std::nullopt; + this->newLevelState = std::nullopt; + this->newPackageState = std::nullopt; + this->newTileState = std::nullopt; + std::string path = outPath.get(); + this->package.load(PACKAGE_TXT, path); + } + } + ImGui::Separator(); if (!pkg.get_levels().empty() && diff --git a/src/package.cpp b/src/package.cpp index d07fa9f..9b35c63 100644 --- a/src/package.cpp +++ b/src/package.cpp @@ -84,4 +84,118 @@ namespace silly::editor { std::filesystem::copy_file(old_path, new_path); } } + + void LevelPackage::load(LevelPackageFormat format, std::string &file_path) { + if (format != PACKAGE_TXT) { + return; + } + + std::string folder_path; +#ifdef WIN32 + char delim = '\\'; +#else + char delim = '/'; +#endif + + { + auto parts = utils::split_text(file_path, delim); + auto name = parts[parts.size() - 1]; + folder_path = file_path.substr(0, file_path.size() - name.size() - 1); + } + + this->clear(); + + std::ifstream ifs(file_path); + + int sector = 0; + std::string line; + + while (std::getline(ifs, line)) { + // skip comments + if (line[0] == '#' || line.empty()) continue; + + // changing sector + if (line == "[package]") { + sector = 0; + continue; + } else if (line == "[tileset]") { + sector = 1; + continue; + } else if (line == "[level]") { + sector = 2; + continue; + } else if (line == "[[floor]]") { + sector = 3; + continue; + } else if (line == "[[[tiles]]]") { + sector = 4; + continue; + } + + auto parts = utils::split_text(line, ';'); + + switch (sector) { + // package + case 0: { + std::string name = parts.at(0); + // TODO: do something with version + int version = std::stoi(parts.at(1)); + this->set_name(name); + break; + } + // tileset + case 1: { + int id = std::stoi(parts.at(0)); + int type = std::stoi(parts.at(1)); + std::string file_name = parts.at(2); + this->tileset.add_entry(id, folder_path + delim + file_name, + (TilesetTileType)type); + break; + } + // level + case 2: { + std::string name = parts.at(0); + TileLevel level(name); + this->add_level(level); + this->move_to_level_index(this->levels.size() - 1); + break; + } + // floor + case 3: { + int width = std::stoi(parts.at(0)); + int height = std::stoi(parts.at(1)); + + TileFloor floor(width, height); + this->get_current_level().add_floor(floor); + break; + } + // tiles + case 4: { + int tile_id = std::stoi(parts.at(0)); + int x = std::stoi(parts.at(1)); + int y = std::stoi(parts.at(2)); + float rotation = std::stof(parts.at(3)); + + auto tile = + std::find_if(this->tileset.get_entries().begin(), + this->tileset.get_entries().end(), + [&tile_id](const std::shared_ptr<TilesetTile> &t) { + return t->id == tile_id; + }); + + if (tile == this->tileset.get_entries().end()) { + break; + } + + this->get_current_level().get_current_floor().place_tile( + *tile, {x, y}, rotation); + break; + } + default: + break; + } + } + + ifs.close(); + } }
\ No newline at end of file diff --git a/src/package.hpp b/src/package.hpp index 9baa683..4cce1f8 100644 --- a/src/package.hpp +++ b/src/package.hpp @@ -29,6 +29,7 @@ namespace silly::editor { std::string export_to_string() const; void save(LevelPackageFormat format, std::string &file_path) const; + void load(LevelPackageFormat format, std::string &file_path); void clear(); |
