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