From a5b94ab1734444df8817bc98351d7798a2a1ae8e Mon Sep 17 00:00:00 2001 From: scawful Date: Wed, 22 Jan 2025 13:32:45 -0500 Subject: [PATCH] Refactor folder item management and move NotifyValue to a new utility header --- src/app/core/common.cc | 10 ----- src/app/core/common.h | 57 ------------------------- src/app/editor/code/assembly_editor.cc | 8 ++-- src/app/editor/code/assembly_editor.h | 8 +++- src/app/editor/graphics/tile16_editor.h | 15 ++++--- src/util/notify.h | 55 ++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 78 deletions(-) create mode 100644 src/util/notify.h diff --git a/src/app/core/common.cc b/src/app/core/common.cc index a2001dda..eb4d34bf 100644 --- a/src/app/core/common.cc +++ b/src/app/core/common.cc @@ -56,16 +56,6 @@ uint32_t ldle3(uint8_t const *const p_arr) { return ldle(p_arr, 3); } } // namespace - -bool StringReplace(std::string &str, const std::string &from, - const std::string &to) { - size_t start = str.find(from); - if (start == std::string::npos) return false; - - str.replace(start, from.length(), to); - return true; -} - uint32_t Get24LocalFromPC(uint8_t *data, int addr, bool pc) { uint32_t ret = (PcToSnes(addr) & 0xFF0000) | (data[addr + 1] << 8) | data[addr]; diff --git a/src/app/core/common.h b/src/app/core/common.h index ed1c3d7b..6ef687ea 100644 --- a/src/app/core/common.h +++ b/src/app/core/common.h @@ -18,9 +18,6 @@ namespace yaze { */ namespace core { -bool StringReplace(std::string &str, const std::string &from, - const std::string &to); - /** * @class ExperimentFlags * @brief A class to manage experimental feature flags. @@ -115,52 +112,6 @@ class ExperimentFlags { } }; -/** - * @class NotifyValue - * @brief A class to manage a value that can be modified and notify when it - * changes. - */ -template -class NotifyValue { - public: - NotifyValue() : value_(), modified_(false), temp_value_() {} - NotifyValue(const T &value) - : value_(value), modified_(false), temp_value_() {} - - void set(const T &value) { - value_ = value; - modified_ = true; - } - - const T &get() { - modified_ = false; - return value_; - } - - T &mutable_get() { - modified_ = false; - temp_value_ = value_; - return temp_value_; - } - - void apply_changes() { - if (temp_value_ != value_) { - value_ = temp_value_; - modified_ = true; - } - } - - void operator=(const T &value) { set(value); } - operator T() { return get(); } - - bool modified() const { return modified_; } - - private: - T value_; - bool modified_; - T temp_value_; -}; - static const std::string kLogFileOut = "yaze_log.txt"; template @@ -234,14 +185,6 @@ uint16_t ldle16b_i(uint8_t const *const p_arr, size_t const p_index); // Load little endian halfword (16-bit) dereferenced from uint16_t ldle16b(uint8_t const *const p_arr); -struct FolderItem { - std::string name; - std::vector subfolders; - std::vector files; -}; - -typedef struct FolderItem FolderItem; - } // namespace core } // namespace yaze diff --git a/src/app/editor/code/assembly_editor.cc b/src/app/editor/code/assembly_editor.cc index 03f56fe0..89deaa9c 100644 --- a/src/app/editor/code/assembly_editor.cc +++ b/src/app/editor/code/assembly_editor.cc @@ -33,7 +33,7 @@ std::vector RemoveIgnoredFiles( return filtered_files; } -core::FolderItem LoadFolder(const std::string& folder) { +FolderItem LoadFolder(const std::string& folder) { // Check if .gitignore exists in the folder std::ifstream gitignore(folder + "/.gitignore"); std::vector ignored_files; @@ -51,14 +51,14 @@ core::FolderItem LoadFolder(const std::string& folder) { } } - core::FolderItem current_folder; + FolderItem current_folder; current_folder.name = folder; auto root_files = FileDialogWrapper::GetFilesInFolder(current_folder.name); current_folder.files = RemoveIgnoredFiles(root_files, ignored_files); for (const auto& subfolder : FileDialogWrapper::GetSubdirectoriesInFolder(current_folder.name)) { - core::FolderItem folder_item; + FolderItem folder_item; folder_item.name = subfolder; std::string full_folder = current_folder.name + "/" + subfolder; auto folder_files = FileDialogWrapper::GetFilesInFolder(full_folder); @@ -80,7 +80,7 @@ core::FolderItem LoadFolder(const std::string& folder) { for (const auto& subdir : FileDialogWrapper::GetSubdirectoriesInFolder(full_folder)) { - core::FolderItem subfolder_item; + FolderItem subfolder_item; subfolder_item.name = subdir; subfolder_item.files = FileDialogWrapper::GetFilesInFolder(subdir); folder_item.subfolders.push_back(subfolder_item); diff --git a/src/app/editor/code/assembly_editor.h b/src/app/editor/code/assembly_editor.h index 0a1cc5af..f9cac4db 100644 --- a/src/app/editor/code/assembly_editor.h +++ b/src/app/editor/code/assembly_editor.h @@ -11,6 +11,12 @@ namespace yaze { namespace editor { +struct FolderItem { + std::string name; + std::vector subfolders; + std::vector files; +}; + /** * @class AssemblyEditor * @brief Text editor for modifying assembly code. @@ -63,7 +69,7 @@ class AssemblyEditor : public Editor { int current_file_id_ = 0; std::string current_file_; - core::FolderItem current_folder_; + FolderItem current_folder_; TextEditor text_editor_; }; diff --git a/src/app/editor/graphics/tile16_editor.h b/src/app/editor/graphics/tile16_editor.h index 1c737058..f7d37d84 100644 --- a/src/app/editor/graphics/tile16_editor.h +++ b/src/app/editor/graphics/tile16_editor.h @@ -14,6 +14,7 @@ #include "app/rom.h" #include "app/zelda3/overworld/overworld.h" #include "imgui/imgui.h" +#include "util/notify.h" namespace yaze { namespace editor { @@ -23,7 +24,9 @@ namespace editor { */ class Tile16Editor : public gfx::GfxContext, public SharedRom { public: - Tile16Editor(std::array& tile16_individual) : tile16_individual_(tile16_individual) {} + Tile16Editor( + std::array &tile16_individual) + : tile16_individual_(tile16_individual) {} absl::Status InitBlockset(const gfx::Bitmap &tile16_blockset_bmp, const gfx::Bitmap ¤t_gfx_bmp, std::array &all_tiles_types); @@ -60,8 +63,8 @@ class Tile16Editor : public gfx::GfxContext, public SharedRom { int current_tile8_ = 0; uint8_t current_palette_ = 0; - core::NotifyValue notify_tile16; - core::NotifyValue notify_palette; + util::NotifyValue notify_tile16; + util::NotifyValue notify_palette; std::array all_tiles_types_; @@ -85,7 +88,7 @@ class Tile16Editor : public gfx::GfxContext, public SharedRom { gui::Canvas transfer_canvas_; gfx::Bitmap transfer_blockset_bmp_; - std::array& tile16_individual_; + std::array &tile16_individual_; std::vector current_gfx_individual_; PaletteEditor palette_editor_; @@ -94,8 +97,8 @@ class Tile16Editor : public gfx::GfxContext, public SharedRom { absl::Status status_; Rom transfer_rom_; - zelda3::Overworld transfer_overworld_{ transfer_rom_ }; - std::array transfer_gfx_; + zelda3::Overworld transfer_overworld_{transfer_rom_}; + std::array transfer_gfx_; absl::Status transfer_status_; }; diff --git a/src/util/notify.h b/src/util/notify.h new file mode 100644 index 00000000..0872fc29 --- /dev/null +++ b/src/util/notify.h @@ -0,0 +1,55 @@ +#ifndef YAZE_UTIL_NOTIFY_H +#define YAZE_UTIL_NOTIFY_H + +namespace yaze { +namespace util { + +/** + * @class NotifyValue + * @brief A class to manage a value that can be modified and notify when it + * changes. + */ +template +class NotifyValue { + public: + NotifyValue() : value_(), modified_(false), temp_value_() {} + NotifyValue(const T &value) + : value_(value), modified_(false), temp_value_() {} + + void set(const T &value) { + value_ = value; + modified_ = true; + } + + const T &get() { + modified_ = false; + return value_; + } + + T &mutable_get() { + modified_ = false; + temp_value_ = value_; + return temp_value_; + } + + void apply_changes() { + if (temp_value_ != value_) { + value_ = temp_value_; + modified_ = true; + } + } + + void operator=(const T &value) { set(value); } + operator T() { return get(); } + + bool modified() const { return modified_; } + + private: + T value_; + bool modified_; + T temp_value_; +}; +} // namespace util +} // namespace yaze + +#endif \ No newline at end of file