Add ResourceLabelManager class

This commit is contained in:
scawful
2024-01-22 03:26:37 -05:00
parent a115b7ebe3
commit 5455b2a425
8 changed files with 164 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ set(
YAZE_APP_CORE_SRC
app/core/common.cc
app/core/controller.cc
app/core/labeling.cc
)
set(

97
src/app/core/labeling.cc Normal file
View File

@@ -0,0 +1,97 @@
#include "app/core/labeling.h"
#include <imgui/imgui.h>
#include <imgui/misc/cpp/imgui_stdlib.h>
#include <cstdint>
#include <fstream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "app/core/common.h"
#include "app/core/constants.h"
namespace yaze {
namespace app {
namespace core {
bool ResourceLabelManager::LoadLabels(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
return false;
}
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string type, key, value;
if (std::getline(iss, type, ',') && std::getline(iss, key, ',') &&
std::getline(iss, value)) {
labels_[type][key] = value;
}
}
return true;
}
void ResourceLabelManager::DisplayLabels() {
if (ImGui::Begin("Resource Labels")) {
for (const auto& type_pair : labels_) {
if (ImGui::TreeNode(type_pair.first.c_str())) {
for (const auto& label_pair : type_pair.second) {
std::string label_id = type_pair.first + "_" + label_pair.first;
ImGui::Text("%s: %s", label_pair.first.c_str(),
label_pair.second.c_str());
}
ImGui::TreePop();
}
}
}
ImGui::End();
}
void ResourceLabelManager::EditLabel(const std::string& type,
const std::string& key,
const std::string& newValue) {
labels_[type][key] = newValue;
}
void ResourceLabelManager::SelectableLabelWithNameEdit(
bool selected, const std::string& type, const std::string& key,
const std::string& defaultValue) {
std::string label = CreateOrGetLabel(type, key, defaultValue);
ImGui::Selectable(label.c_str(), selected,
ImGuiSelectableFlags_AllowDoubleClick);
std::string label_id = type + "_" + key;
if (ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
ImGui::OpenPopup(label_id.c_str());
}
if (ImGui::BeginPopupContextItem(label_id.c_str())) {
char* new_label = labels_[type][key].data();
if (ImGui::InputText("##Label", new_label, labels_[type][key].size() + 1,
ImGuiInputTextFlags_EnterReturnsTrue)) {
labels_[type][key] = new_label;
}
ImGui::EndPopup();
}
// ImGui::PopID();
}
std::string ResourceLabelManager::CreateOrGetLabel(
const std::string& type, const std::string& key,
const std::string& defaultValue) {
if (labels_.find(type) == labels_.end()) {
labels_[type] = std::unordered_map<std::string, std::string>();
}
if (labels_[type].find(key) == labels_[type].end()) {
labels_[type][key] = defaultValue;
}
return labels_[type][key];
}
} // namespace core
} // namespace app
} // namespace yaze

52
src/app/core/labeling.h Normal file
View File

@@ -0,0 +1,52 @@
#ifndef YAZE_APP_CORE_LABELING_H_
#define YAZE_APP_CORE_LABELING_H_
#include <cstdint>
#include <fstream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "app/core/common.h"
#include "app/core/constants.h"
namespace yaze {
namespace app {
namespace core {
// Default types
static constexpr absl::string_view kDefaultTypes[] = {
"Dungeon Names", "Dungeon Room Names", "Overworld Map Names"};
class ResourceLabelManager {
public:
ResourceLabelManager() = default;
bool LoadLabels(const std::string& filename);
void DisplayLabels();
void EditLabel(const std::string& type, const std::string& key,
const std::string& newValue);
void SelectableLabelWithNameEdit(bool selected, const std::string& type,
const std::string& key,
const std::string& defaultValue);
std::string CreateOrGetLabel(const std::string& type, const std::string& key,
const std::string& defaultValue);
private:
struct ResourceType {
std::string key_name;
std::string display_description;
};
std::unordered_map<std::string, std::unordered_map<std::string, std::string>>
labels_;
};
} // namespace core
} // namespace app
} // namespace yaze
#endif // YAZE_APP_CORE_LABELING_H_

View File

@@ -3,6 +3,7 @@
#include <imgui/imgui.h>
#include "app/core/common.h"
#include "app/core/labeling.h"
#include "app/gfx/snes_palette.h"
#include "app/gui/canvas.h"
#include "app/gui/icons.h"
@@ -279,8 +280,11 @@ void DungeonEditor::DrawRoomSelector() {
ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
int i = 0;
for (const auto each_room_name : zelda3::dungeon::kRoomNames) {
ImGui::Selectable(each_room_name.data(), current_room_id_ == i,
ImGuiSelectableFlags_AllowDoubleClick);
// ImGui::Selectable(each_room_name.data(), current_room_id_ == i,
// ImGuiSelectableFlags_AllowDoubleClick);
rom()->resource_label()->SelectableLabelWithNameEdit(
current_room_id_ == i, "Dungeon Room Names", each_room_name.data(),
zelda3::dungeon::kRoomNames[i].data());
if (ImGui::IsItemClicked()) {
active_rooms_.push_back(i);
}

View File

@@ -5,6 +5,7 @@
#include "app/core/common.h"
#include "app/core/editor.h"
#include "app/core/labeling.h"
#include "app/editor/modules/gfx_group_editor.h"
#include "app/editor/modules/palette_editor.h"
#include "app/gui/canvas.h"
@@ -109,7 +110,6 @@ class DungeonEditor : public Editor,
std::vector<int64_t> room_size_pointers_;
// Add member variables to track the selected set
uint16_t selected_blockset_ = 0xFFFF; // 0xFFFF indicates no selection
uint16_t selected_spriteset_ = 0xFFFF;
uint16_t selected_palette_ = 0xFFFF;

View File

@@ -31,7 +31,8 @@
#include "absl/strings/string_view.h" // for string_view
#include "app/core/common.h"
#include "app/core/constants.h" // for Bytes, uchar, armorPalettes
#include "app/gfx/bitmap.h" // for Bitmap, BitmapTable
#include "app/core/labeling.h"
#include "app/gfx/bitmap.h" // for Bitmap, BitmapTable
#include "app/gfx/compression.h"
#include "app/gfx/snes_palette.h" // for PaletteGroup, SNESColor
#include "app/gfx/snes_tile.h"
@@ -559,6 +560,8 @@ class ROM : public core::ExperimentFlags {
return false;
}
auto resource_label() { return &resource_label_manager_; }
private:
long size_ = 0;
bool is_loaded_ = false;
@@ -575,6 +578,7 @@ class ROM : public core::ExperimentFlags {
gfx::BitmapTable link_graphics_;
gfx::SNESPalette link_palette_;
PaletteGroupMap palette_groups_;
core::ResourceLabelManager resource_label_manager_;
std::stack<std::function<void()>> changes_;
std::shared_ptr<SDL_Renderer> renderer_;

View File

@@ -5,6 +5,7 @@ add_executable(
cli/command_handler.cc
app/rom.cc
app/core/common.cc
app/core/labeling.cc
app/gui/pipeline.cc
${YAZE_APP_EMU_SRC}
${YAZE_APP_GFX_SRC}