yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
assembly_editor.cc
Go to the documentation of this file.
1#include "assembly_editor.h"
2
3#include <fstream>
4#include <string>
5#include <vector>
6
7#include "absl/strings/str_cat.h"
9#include "app/gui/icons.h"
11
12namespace yaze {
13namespace editor {
14
16
17namespace {
18
19std::vector<std::string> RemoveIgnoredFiles(
20 const std::vector<std::string>& files,
21 const std::vector<std::string>& ignored_files) {
22 std::vector<std::string> filtered_files;
23 for (const auto& file : files) {
24 // Remove subdirectory files
25 if (file.find('/') != std::string::npos) {
26 continue;
27 }
28 // Make sure the file has an extension
29 if (file.find('.') == std::string::npos) {
30 continue;
31 }
32 if (std::find(ignored_files.begin(), ignored_files.end(), file) ==
33 ignored_files.end()) {
34 filtered_files.push_back(file);
35 }
36 }
37 return filtered_files;
38}
39
40FolderItem LoadFolder(const std::string& folder) {
41 // Check if .gitignore exists in the folder
42 std::ifstream gitignore(folder + "/.gitignore");
43 std::vector<std::string> ignored_files;
44 if (gitignore.good()) {
45 std::string line;
46 while (std::getline(gitignore, line)) {
47 if (line[0] == '#') {
48 continue;
49 }
50 if (line[0] == '!') {
51 // Ignore the file
52 continue;
53 }
54 ignored_files.push_back(line);
55 }
56 }
57
58 FolderItem current_folder;
59 current_folder.name = folder;
60 auto root_files = FileDialogWrapper::GetFilesInFolder(current_folder.name);
61 current_folder.files = RemoveIgnoredFiles(root_files, ignored_files);
62
63 for (const auto& subfolder :
65 FolderItem folder_item;
66 folder_item.name = subfolder;
67 std::string full_folder = current_folder.name + "/" + subfolder;
68 auto folder_files = FileDialogWrapper::GetFilesInFolder(full_folder);
69 for (const auto& files : folder_files) {
70 // Remove subdirectory files
71 if (files.find('/') != std::string::npos) {
72 continue;
73 }
74 // Make sure the file has an extension
75 if (files.find('.') == std::string::npos) {
76 continue;
77 }
78 if (std::find(ignored_files.begin(), ignored_files.end(), files) !=
79 ignored_files.end()) {
80 continue;
81 }
82 folder_item.files.push_back(files);
83 }
84
85 for (const auto& subdir :
87 FolderItem subfolder_item;
88 subfolder_item.name = subdir;
89 subfolder_item.files = FileDialogWrapper::GetFilesInFolder(subdir);
90 folder_item.subfolders.push_back(subfolder_item);
91 }
92 current_folder.subfolders.push_back(folder_item);
93 }
94
95 return current_folder;
96}
97
98} // namespace
99
100void AssemblyEditor::OpenFolder(const std::string& folder_path) {
101 current_folder_ = LoadFolder(folder_path);
102}
103
104void AssemblyEditor::Update(bool& is_loaded) {
105 ImGui::Begin("Assembly Editor", &is_loaded);
106 if (ImGui::BeginMenuBar()) {
107 DrawFileMenu();
108 DrawEditMenu();
109 ImGui::EndMenuBar();
110 }
111
112 auto cpos = text_editor_.GetCursorPosition();
114 ImGui::Text("%6d/%-6d %6d lines | %s | %s | %s | %s", cpos.mLine + 1,
115 cpos.mColumn + 1, text_editor_.GetTotalLines(),
116 text_editor_.IsOverwrite() ? "Ovr" : "Ins",
117 text_editor_.CanUndo() ? "*" : " ",
118 text_editor_.GetLanguageDefinition().mName.c_str(),
119 current_file_.c_str());
120
121 text_editor_.Render("##asm_editor");
122 ImGui::End();
123}
124
126 ChangeActiveFile("assets/asm/template_song.asm");
127 auto cpos = text_editor_.GetCursorPosition();
129 ImGui::Text("%6d/%-6d %6d lines | %s | %s | %s | %s", cpos.mLine + 1,
130 cpos.mColumn + 1, text_editor_.GetTotalLines(),
131 text_editor_.IsOverwrite() ? "Ovr" : "Ins",
132 text_editor_.CanUndo() ? "*" : " ",
133 text_editor_.GetLanguageDefinition().mName.c_str(),
134 current_file_.c_str());
135
136 text_editor_.Render("##asm_editor", ImVec2(0, 0));
137}
138
140 ImGui::BeginTable("##table_view", 2,
141 ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg |
142 ImGuiTableFlags_Resizable);
143
144 // Table headers
145 ImGui::TableSetupColumn("Files", ImGuiTableColumnFlags_WidthFixed, 256.0f);
146 ImGui::TableSetupColumn("Editor", ImGuiTableColumnFlags_WidthStretch);
147
148 ImGui::TableHeadersRow();
149
150 // Table data
151 ImGui::TableNextRow();
152 ImGui::TableNextColumn();
153 if (current_folder_.name != "") {
155 } else {
156 if (ImGui::Button("Open Folder")) {
158 }
159 }
160
161 ImGui::TableNextColumn();
162
163 auto cpos = text_editor_.GetCursorPosition();
165 ImGui::Text("%6d/%-6d %6d lines | %s | %s | %s | %s", cpos.mLine + 1,
166 cpos.mColumn + 1, text_editor_.GetTotalLines(),
167 text_editor_.IsOverwrite() ? "Ovr" : "Ins",
168 text_editor_.CanUndo() ? "*" : " ",
169 text_editor_.GetLanguageDefinition().mName.c_str(),
170 current_file_.c_str());
171
172 text_editor_.Render("##asm_editor");
173
174 ImGui::EndTable();
175}
176
178 if (ImGui::BeginChild("##current_folder", ImVec2(0, 0), true,
179 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
180 if (ImGui::BeginTable("##file_table", 2,
181 ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg |
182 ImGuiTableFlags_Resizable |
183 ImGuiTableFlags_Sortable)) {
184 ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, 256.0f);
185 ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthStretch);
186
187 ImGui::TableHeadersRow();
188
189 for (const auto& file : current_folder_.files) {
190 ImGui::TableNextRow();
191 ImGui::TableNextColumn();
192 if (ImGui::Selectable(file.c_str())) {
193 ChangeActiveFile(absl::StrCat(current_folder_.name, "/", file));
194 }
195 ImGui::TableNextColumn();
196 ImGui::Text("File");
197 }
198
199 for (const auto& subfolder : current_folder_.subfolders) {
200 ImGui::TableNextRow();
201 ImGui::TableNextColumn();
202 if (ImGui::TreeNode(subfolder.name.c_str())) {
203 for (const auto& file : subfolder.files) {
204 ImGui::TableNextRow();
205 ImGui::TableNextColumn();
206 if (ImGui::Selectable(file.c_str())) {
207 ChangeActiveFile(absl::StrCat(current_folder_.name, "/",
208 subfolder.name, "/", file));
209 }
210 ImGui::TableNextColumn();
211 ImGui::Text("File");
212 }
213 ImGui::TreePop();
214 } else {
215 ImGui::TableNextColumn();
216 ImGui::Text("Folder");
217 }
218 }
219
220 ImGui::EndTable();
221 }
222
223 ImGui::EndChild();
224 }
225}
226
228 static int next_tab_id = 0;
229
230 if (ImGui::BeginTabBar("AssemblyFileTabBar", ImGuiTabBarFlags_None)) {
231 if (ImGui::TabItemButton(ICON_MD_ADD, ImGuiTabItemFlags_None)) {
232 if (std::find(active_files_.begin(), active_files_.end(),
234 // Room is already open
235 next_tab_id++;
236 }
237 active_files_.push_back(next_tab_id++); // Add new tab
238 }
239
240 // Submit our regular tabs
241 for (int n = 0; n < active_files_.Size;) {
242 bool open = true;
243
244 if (ImGui::BeginTabItem(files_[active_files_[n]].data(), &open,
245 ImGuiTabItemFlags_None)) {
246 auto cpos = text_editor_.GetCursorPosition();
247 {
248 std::ifstream t(current_file_);
249 if (t.good()) {
250 std::string str((std::istreambuf_iterator<char>(t)),
251 std::istreambuf_iterator<char>());
252 text_editor_.SetText(str);
253 } else {
254 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
255 "Error opening file: %s\n", current_file_.c_str());
256 }
257 }
258 ImGui::Text("%6d/%-6d %6d lines | %s | %s | %s | %s", cpos.mLine + 1,
259 cpos.mColumn + 1, text_editor_.GetTotalLines(),
260 text_editor_.IsOverwrite() ? "Ovr" : "Ins",
261 text_editor_.CanUndo() ? "*" : " ",
262 text_editor_.GetLanguageDefinition().mName.c_str(),
263 current_file_.c_str());
264
265 open_files_[active_files_[n]].Render("##asm_editor");
266 ImGui::EndTabItem();
267 }
268
269 if (!open)
270 active_files_.erase(active_files_.Data + n);
271 else
272 n++;
273 }
274
275 ImGui::EndTabBar();
276 }
277 ImGui::Separator();
278}
279
281 if (ImGui::BeginMenu("File")) {
282 if (ImGui::MenuItem("Open", "Ctrl+O")) {
284 ChangeActiveFile(filename);
285 }
286 if (ImGui::MenuItem("Save", "Ctrl+S")) {
287 // TODO: Implement this
288 }
289 ImGui::EndMenu();
290 }
291}
292
294 if (ImGui::BeginMenu("Edit")) {
295 if (ImGui::MenuItem("Undo", "Ctrl+Z")) {
296 text_editor_.Undo();
297 }
298 if (ImGui::MenuItem("Redo", "Ctrl+Y")) {
299 text_editor_.Redo();
300 }
301 ImGui::Separator();
302 if (ImGui::MenuItem("Cut", "Ctrl+X")) {
303 text_editor_.Cut();
304 }
305 if (ImGui::MenuItem("Copy", "Ctrl+C")) {
306 text_editor_.Copy();
307 }
308 if (ImGui::MenuItem("Paste", "Ctrl+V")) {
309 text_editor_.Paste();
310 }
311 ImGui::Separator();
312 if (ImGui::MenuItem("Find", "Ctrl+F")) {
313 // TODO: Implement this.
314 }
315 ImGui::EndMenu();
316 }
317}
318
320 if (!file_is_loaded_) {
321 std::ifstream t(current_file_);
322 if (t.good()) {
323 std::string str((std::istreambuf_iterator<char>(t)),
324 std::istreambuf_iterator<char>());
325 text_editor_.SetText(str);
326 } else {
327 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error opening file: %s\n",
328 current_file_.c_str());
329 }
330 file_is_loaded_ = true;
331 }
332}
333
334absl::Status AssemblyEditor::Cut() {
335 text_editor_.Cut();
336 return absl::OkStatus();
337}
338
339absl::Status AssemblyEditor::Copy() {
340 text_editor_.Copy();
341 return absl::OkStatus();
342}
343
344absl::Status AssemblyEditor::Paste() {
345 text_editor_.Paste();
346 return absl::OkStatus();
347}
348
349absl::Status AssemblyEditor::Undo() {
350 text_editor_.Undo();
351 return absl::OkStatus();
352}
353
354absl::Status AssemblyEditor::Redo() {
355 text_editor_.Redo();
356 return absl::OkStatus();
357}
358
359absl::Status AssemblyEditor::Update() { return absl::OkStatus(); }
360
361} // namespace editor
362} // namespace yaze
static std::string ShowOpenFileDialog()
ShowOpenFileDialog opens a file dialog and returns the selected filepath.
std::vector< TextEditor > open_files_
void ChangeActiveFile(const std::string_view &filename)
absl::Status Copy() override
void OpenFolder(const std::string &folder_path)
absl::Status Paste() override
absl::Status Update() override
std::vector< std::string > files_
absl::Status Redo() override
absl::Status Undo() override
absl::Status Cut() override
static std::vector< std::string > GetFilesInFolder(const std::string &folder_path)
static std::string ShowOpenFolderDialog()
ShowOpenFolderDialog opens a file dialog and returns the selected folder path.
static std::vector< std::string > GetSubdirectoriesInFolder(const std::string &folder_path)
#define ICON_MD_ADD
Definition icons.h:84
FolderItem LoadFolder(const std::string &folder)
std::vector< std::string > RemoveIgnoredFiles(const std::vector< std::string > &files, const std::vector< std::string > &ignored_files)
Editors are the view controllers for the application.
Main namespace for the application.
Definition controller.cc:18
std::vector< FolderItem > subfolders
std::vector< std::string > files