yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
project.cc
Go to the documentation of this file.
1#include "project.h"
2
3#include <fstream>
4#include <string>
5
7#include "app/gui/icons.h"
8#include "imgui/imgui.h"
9#include "imgui/misc/cpp/imgui_stdlib.h"
10
11namespace yaze {
12namespace app {
13
14absl::Status Project::Open(const std::string& project_path) {
15 filepath = project_path;
16 name = project_path.substr(project_path.find_last_of("/") + 1);
17
18 std::ifstream in(project_path);
19
20 if (!in.good()) {
21 return absl::InternalError("Could not open project file.");
22 }
23
24 std::string line;
25 std::getline(in, name);
26 std::getline(in, filepath);
27 std::getline(in, rom_filename_);
28 std::getline(in, code_folder_);
29 std::getline(in, labels_filename_);
30 std::getline(in, keybindings_file);
31
32 while (std::getline(in, line)) {
33 if (line == kEndOfProjectFile) {
34 break;
35 }
36 }
37
38 in.close();
39
40 return absl::OkStatus();
41}
42
43absl::Status Project::Save() {
45
46 std::ofstream out(filepath + "/" + name + ".yaze");
47 if (!out.good()) {
48 return absl::InternalError("Could not open project file.");
49 }
50
51 out << name << std::endl;
52 out << filepath << std::endl;
53 out << rom_filename_ << std::endl;
54 out << code_folder_ << std::endl;
55 out << labels_filename_ << std::endl;
56 out << keybindings_file << std::endl;
57
58 out << kEndOfProjectFile << std::endl;
59
60 out.close();
61
62 return absl::OkStatus();
63}
64
65bool ResourceLabelManager::LoadLabels(const std::string& filename) {
66 std::ifstream file(filename);
67 if (!file.is_open()) {
68 // Create the file if it does not exist
69 std::ofstream create_file(filename);
70 if (!create_file.is_open()) {
71 return false;
72 }
73 create_file.close();
74 file.open(filename);
75 if (!file.is_open()) {
76 return false;
77 }
78 }
79 filename_ = filename;
80
81 std::string line;
82 while (std::getline(file, line)) {
83 std::istringstream iss(line);
84 std::string type, key, value;
85 if (std::getline(iss, type, ',') && std::getline(iss, key, ',') &&
86 std::getline(iss, value)) {
87 labels_[type][key] = value;
88 }
89 }
90 labels_loaded_ = true;
91 return true;
92}
93
95 if (!labels_loaded_) {
96 return false;
97 }
98 std::ofstream file(filename_);
99 if (!file.is_open()) {
100 return false;
101 }
102 for (const auto& type_pair : labels_) {
103 for (const auto& label_pair : type_pair.second) {
104 file << type_pair.first << "," << label_pair.first << ","
105 << label_pair.second << std::endl;
106 }
107 }
108 file.close();
109 return true;
110}
111
113 if (!labels_loaded_) {
114 ImGui::Text("No labels loaded.");
115 return;
116 }
117
118 if (ImGui::Begin("Resource Labels", p_open)) {
119 for (const auto& type_pair : labels_) {
120 if (ImGui::TreeNode(type_pair.first.c_str())) {
121 for (const auto& label_pair : type_pair.second) {
122 std::string label_id = type_pair.first + "_" + label_pair.first;
123 ImGui::Text("%s: %s", label_pair.first.c_str(),
124 label_pair.second.c_str());
125 }
126 ImGui::TreePop();
127 }
128 }
129
130 if (ImGui::Button("Update Labels")) {
131 if (SaveLabels()) {
132 ImGui::Text("Labels updated successfully!");
133 } else {
134 ImGui::Text("Failed to update labels.");
135 }
136 }
137 }
138 ImGui::End();
139}
140
141void ResourceLabelManager::EditLabel(const std::string& type,
142 const std::string& key,
143 const std::string& newValue) {
144 labels_[type][key] = newValue;
145}
146
148 bool selected, const std::string& type, const std::string& key,
149 const std::string& defaultValue) {
150 std::string label = CreateOrGetLabel(type, key, defaultValue);
151 ImGui::Selectable(label.c_str(), selected,
152 ImGuiSelectableFlags_AllowDoubleClick);
153 std::string label_id = type + "_" + key;
154 if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
155 ImGui::OpenPopup(label_id.c_str());
156 }
157
158 if (ImGui::BeginPopupContextItem(label_id.c_str())) {
159 std::string* new_label = &labels_[type][key];
160 if (ImGui::InputText("##Label", new_label,
161 ImGuiInputTextFlags_EnterReturnsTrue)) {
162 labels_[type][key] = *new_label;
163 }
164 if (ImGui::Button(ICON_MD_CLOSE)) {
165 ImGui::CloseCurrentPopup();
166 }
167 ImGui::EndPopup();
168 }
169}
170
171std::string ResourceLabelManager::GetLabel(const std::string& type,
172 const std::string& key) {
173 return labels_[type][key];
174}
175
177 const std::string& type, const std::string& key,
178 const std::string& defaultValue) {
179 if (labels_.find(type) == labels_.end()) {
180 labels_[type] = std::unordered_map<std::string, std::string>();
181 }
182 if (labels_[type].find(key) == labels_[type].end()) {
183 labels_[type][key] = defaultValue;
184 }
185 return labels_[type][key];
186}
187
188} // namespace app
189} // namespace yaze
#define RETURN_IF_ERROR(expression)
Definition constants.h:62
#define ICON_MD_CLOSE
Definition icons.h:416
constexpr char kEndOfProjectFile[]
Definition project.h:17
Definition common.cc:22
std::string name
Definition project.h:48
std::string filepath
Definition project.h:50
std::string code_folder_
Definition project.h:52
absl::Status Open(const std::string &project_path)
Definition project.cc:14
std::string labels_filename_
Definition project.h:53
std::string keybindings_file
Definition project.h:54
absl::Status CheckForEmptyFields()
Definition project.h:34
absl::Status Save()
Definition project.cc:43
std::string rom_filename_
Definition project.h:51
std::string CreateOrGetLabel(const std::string &type, const std::string &key, const std::string &defaultValue)
Definition project.cc:176
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > labels_
Definition project.h:82
void DisplayLabels(bool *p_open)
Definition project.cc:112
void SelectableLabelWithNameEdit(bool selected, const std::string &type, const std::string &key, const std::string &defaultValue)
Definition project.cc:147
std::string GetLabel(const std::string &type, const std::string &key)
Definition project.cc:171
void EditLabel(const std::string &type, const std::string &key, const std::string &newValue)
Definition project.cc:141
bool LoadLabels(const std::string &filename)
Definition project.cc:65