yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
labeling.cc
Go to the documentation of this file.
1#include "app/core/labeling.h"
2
3#include <cstdint>
4#include <fstream>
5#include <sstream>
6#include <string>
7#include <unordered_map>
8#include <vector>
9
10#include "absl/strings/str_format.h"
11
12#include "app/core/constants.h"
13#include "app/gui/icons.h"
14#include "imgui/imgui.h"
15#include "imgui/misc/cpp/imgui_stdlib.h"
16
17namespace yaze {
18namespace app {
19namespace core {
20
21std::string UppercaseHexByte(uint8_t byte, bool leading) {
22 if (leading) {
23 std::string result = absl::StrFormat("0x%02X", byte);
24 return result;
25 }
26 std::string result = absl::StrFormat("%02X", byte);
27 return result;
28}
29std::string UppercaseHexWord(uint16_t word) {
30 std::string result = absl::StrFormat("0x%04X", word);
31 return result;
32}
33std::string UppercaseHexLong(uint32_t dword) {
34 std::string result = absl::StrFormat("0x%06X", dword);
35 return result;
36}
37std::string UppercaseHexLongLong(uint64_t qword) {
38 std::string result = absl::StrFormat("0x%08X", qword);
39 return result;
40}
41
42bool StringReplace(std::string& str, const std::string& from,
43 const std::string& to) {
44 size_t start = str.find(from);
45 if (start == std::string::npos) return false;
46
47 str.replace(start, from.length(), to);
48 return true;
49}
50
51bool ResourceLabelManager::LoadLabels(const std::string& filename) {
52 std::ifstream file(filename);
53 if (!file.is_open()) {
54 // Create the file if it does not exist
55 std::ofstream create_file(filename);
56 if (!create_file.is_open()) {
57 return false;
58 }
59 create_file.close();
60 file.open(filename);
61 if (!file.is_open()) {
62 return false;
63 }
64 }
65 filename_ = filename;
66
67 std::string line;
68 while (std::getline(file, line)) {
69 std::istringstream iss(line);
70 std::string type, key, value;
71 if (std::getline(iss, type, ',') && std::getline(iss, key, ',') &&
72 std::getline(iss, value)) {
73 labels_[type][key] = value;
74 }
75 }
76 labels_loaded_ = true;
77 return true;
78}
79
81 if (!labels_loaded_) {
82 return false;
83 }
84 std::ofstream file(filename_);
85 if (!file.is_open()) {
86 return false;
87 }
88 for (const auto& type_pair : labels_) {
89 for (const auto& label_pair : type_pair.second) {
90 file << type_pair.first << "," << label_pair.first << ","
91 << label_pair.second << std::endl;
92 }
93 }
94 file.close();
95 return true;
96}
97
99 if (!labels_loaded_) {
100 ImGui::Text("No labels loaded.");
101 return;
102 }
103
104 if (ImGui::Begin("Resource Labels", p_open)) {
105 for (const auto& type_pair : labels_) {
106 if (ImGui::TreeNode(type_pair.first.c_str())) {
107 for (const auto& label_pair : type_pair.second) {
108 std::string label_id = type_pair.first + "_" + label_pair.first;
109 ImGui::Text("%s: %s", label_pair.first.c_str(),
110 label_pair.second.c_str());
111 }
112 ImGui::TreePop();
113 }
114 }
115
116 if (ImGui::Button("Update Labels")) {
117 if (SaveLabels()) {
118 ImGui::Text("Labels updated successfully!");
119 } else {
120 ImGui::Text("Failed to update labels.");
121 }
122 }
123 }
124 ImGui::End();
125}
126
127void ResourceLabelManager::EditLabel(const std::string& type,
128 const std::string& key,
129 const std::string& newValue) {
130 labels_[type][key] = newValue;
131}
132
134 bool selected, const std::string& type, const std::string& key,
135 const std::string& defaultValue) {
136 std::string label = CreateOrGetLabel(type, key, defaultValue);
137 ImGui::Selectable(label.c_str(), selected,
138 ImGuiSelectableFlags_AllowDoubleClick);
139 std::string label_id = type + "_" + key;
140 if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
141 ImGui::OpenPopup(label_id.c_str());
142 }
143
144 if (ImGui::BeginPopupContextItem(label_id.c_str())) {
145 std::string* new_label = &labels_[type][key];
146 if (ImGui::InputText("##Label", new_label,
147 ImGuiInputTextFlags_EnterReturnsTrue)) {
148 labels_[type][key] = *new_label;
149 }
150 if (ImGui::Button(ICON_MD_CLOSE)) {
151 ImGui::CloseCurrentPopup();
152 }
153 ImGui::EndPopup();
154 }
155}
156
157std::string ResourceLabelManager::GetLabel(const std::string& type,
158 const std::string& key) {
159 return labels_[type][key];
160}
161
163 const std::string& type, const std::string& key,
164 const std::string& defaultValue) {
165 if (labels_.find(type) == labels_.end()) {
166 labels_[type] = std::unordered_map<std::string, std::string>();
167 }
168 if (labels_[type].find(key) == labels_[type].end()) {
169 labels_[type][key] = defaultValue;
170 }
171 return labels_[type][key];
172}
173
174} // namespace core
175} // namespace app
176} // namespace yaze
#define ICON_MD_CLOSE
Definition icons.h:416
std::string UppercaseHexLongLong(uint64_t qword)
Definition labeling.cc:37
bool StringReplace(std::string &str, const std::string &from, const std::string &to)
Definition labeling.cc:42
std::string UppercaseHexWord(uint16_t word)
Definition labeling.cc:29
std::string UppercaseHexLong(uint32_t dword)
Definition labeling.cc:33
std::string UppercaseHexByte(uint8_t byte, bool leading)
Definition labeling.cc:21
Definition common.cc:21
std::string GetLabel(const std::string &type, const std::string &key)
Definition labeling.cc:157
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > labels_
Definition labeling.h:53
bool LoadLabels(const std::string &filename)
Definition labeling.cc:51
std::string CreateOrGetLabel(const std::string &type, const std::string &key, const std::string &defaultValue)
Definition labeling.cc:162
void EditLabel(const std::string &type, const std::string &key, const std::string &newValue)
Definition labeling.cc:127
void SelectableLabelWithNameEdit(bool selected, const std::string &type, const std::string &key, const std::string &defaultValue)
Definition labeling.cc:133