diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fe9519ee..52924a5a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,7 @@ set( YAZE_APP_CORE_SRC app/core/common.cc app/core/controller.cc + app/core/pipeline.cc ) set( diff --git a/src/app/core/pipeline.cc b/src/app/core/pipeline.cc new file mode 100644 index 00000000..b236e512 --- /dev/null +++ b/src/app/core/pipeline.cc @@ -0,0 +1,36 @@ +#include "pipeline.h" + +#include +#include +#include +#include + +#include +#include + +#include "absl/strings/string_view.h" + +namespace yaze { +namespace app { +namespace core { + +void FileDialogPipeline(absl::string_view display_key, + absl::string_view file_extensions, + std::optional button_text, + std::function callback) { + if (button_text.has_value() && ImGui::Button(button_text->data())) { + ImGuiFileDialog::Instance()->OpenDialog(display_key.data(), "Choose File", + file_extensions.data(), "."); + } + + if (ImGuiFileDialog::Instance()->Display(display_key.data())) { + if (ImGuiFileDialog::Instance()->IsOk()) { + callback(); + } + ImGuiFileDialog::Instance()->Close(); + } +} + +} // namespace core +} // namespace app +} // namespace yaze \ No newline at end of file diff --git a/src/app/core/pipeline.h b/src/app/core/pipeline.h new file mode 100644 index 00000000..e6c2b687 --- /dev/null +++ b/src/app/core/pipeline.h @@ -0,0 +1,27 @@ +#ifndef YAZE_APP_CORE_PIPELINE_H +#define YAZE_APP_CORE_PIPELINE_H + +#include +#include +#include +#include + +#include +#include + +#include "absl/strings/string_view.h" + +namespace yaze { +namespace app { +namespace core { + +void FileDialogPipeline(absl::string_view display_key, + absl::string_view file_extensions, + std::optional button_text, + std::function callback); + +} // namespace core +} // namespace app +} // namespace yaze + +#endif \ No newline at end of file diff --git a/src/app/editor/graphics_editor.cc b/src/app/editor/graphics_editor.cc index a1f9b541..efd26847 100644 --- a/src/app/editor/graphics_editor.cc +++ b/src/app/editor/graphics_editor.cc @@ -7,6 +7,7 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" +#include "app/core/pipeline.h" #include "app/editor/palette_editor.h" #include "app/gfx/bitmap.h" #include "app/gfx/compression.h" @@ -80,32 +81,21 @@ absl::Status GraphicsEditor::DrawCgxImport() { ImGui::InputText("##CGXFile", cgx_file_name_, sizeof(cgx_file_name_)); ImGui::SameLine(); - // Open the file dialog when the user clicks the "Browse" button - if (ImGui::Button("Open CGX")) { - ImGuiFileDialog::Instance()->OpenDialog("ImportCgxKey", "Choose File", - ".CGX,.cgx\0", "."); - } - - if (ImGuiFileDialog::Instance()->Display("ImportCgxKey")) { - if (ImGuiFileDialog::Instance()->IsOk()) { - strncpy(cgx_file_path_, - ImGuiFileDialog::Instance()->GetFilePathName().c_str(), - sizeof(cgx_file_path_)); - strncpy(cgx_file_name_, - ImGuiFileDialog::Instance()->GetCurrentFileName().c_str(), - sizeof(cgx_file_name_)); - RETURN_IF_ERROR(temp_rom_.LoadFromFile(cgx_file_path_, /*z3_load=*/false)) - cgx_viewer_.LoadCgx(temp_rom_); - auto all_tiles_data = cgx_viewer_.GetCgxData(); - cgx_bitmap_.Create(core::kTilesheetWidth, 8192, core::kTilesheetDepth, - all_tiles_data.data(), all_tiles_data.size()); - rom_.RenderBitmap(&cgx_bitmap_); - is_open_ = true; - cgx_loaded_ = true; - } - ImGuiFileDialog::Instance()->Close(); - } - + core::FileDialogPipeline("ImportCgxKey", ".CGX,.cgx\0", "Open CGX", [&]() { + strncpy(cgx_file_path_, + ImGuiFileDialog::Instance()->GetFilePathName().c_str(), + sizeof(cgx_file_path_)); + strncpy(cgx_file_name_, + ImGuiFileDialog::Instance()->GetCurrentFileName().c_str(), + sizeof(cgx_file_name_)); + RETURN_IF_ERROR(temp_rom_.LoadFromFile(cgx_file_path_, /*z3_load=*/false)) + cgx_viewer_.LoadCgx(temp_rom_); + auto all_tiles_data = cgx_viewer_.GetCgxData(); + cgx_bitmap_.Create(core::kTilesheetWidth, 8192, core::kTilesheetDepth, + all_tiles_data.data(), all_tiles_data.size()); + is_open_ = true; + cgx_loaded_ = true; + }); return absl::OkStatus(); } diff --git a/src/app/editor/master_editor.cc b/src/app/editor/master_editor.cc index 9d9f6332..a50f1bc2 100644 --- a/src/app/editor/master_editor.cc +++ b/src/app/editor/master_editor.cc @@ -8,6 +8,7 @@ #include "absl/status/status.h" #include "app/core/constants.h" +#include "app/core/pipeline.h" #include "app/editor/assembly_editor.h" #include "app/editor/dungeon_editor.h" #include "app/editor/music_editor.h" @@ -84,18 +85,17 @@ void MasterEditor::UpdateScreen() { } void MasterEditor::DrawFileDialog() { - if (ImGuiFileDialog::Instance()->Display("ChooseFileDlgKey")) { - if (ImGuiFileDialog::Instance()->IsOk()) { - std::string filePathName = ImGuiFileDialog::Instance()->GetFilePathName(); - status_ = rom_.LoadFromFile(filePathName); - overworld_editor_.SetupROM(rom_); - graphics_editor_.SetupROM(rom_); - screen_editor_.SetupROM(rom_); - palette_editor_.SetupROM(rom_); - music_editor_.SetupROM(rom_); - } - ImGuiFileDialog::Instance()->Close(); - } + core::FileDialogPipeline("ChooseFileDlgKey", ".sfc,.smc", std::nullopt, + [&]() { + std::string filePathName = + ImGuiFileDialog::Instance()->GetFilePathName(); + status_ = rom_.LoadFromFile(filePathName); + overworld_editor_.SetupROM(rom_); + graphics_editor_.SetupROM(rom_); + screen_editor_.SetupROM(rom_); + palette_editor_.SetupROM(rom_); + music_editor_.SetupROM(rom_); + }); } void MasterEditor::DrawStatusPopup() {