yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
memory_editor.cc
Go to the documentation of this file.
2#include "util/i18n/tr.h"
3
4#include "absl/strings/str_format.h"
10#include "imgui/imgui.h"
11
12namespace yaze {
13namespace editor {
14
15absl::Status MemoryEditor::Update() {
16 if (!active_) {
17 return absl::OkStatus();
18 }
19
20 ImGui::Begin("Hex Editor", &active_);
22 if (ImGui::Button(tr("Compare Rom"))) {
24 }
25
26 static uint64_t convert_address = 0;
27 gui::InputHex("SNES to PC", (int*)&convert_address, 6, 200.f);
28 SameLine();
29 Text("%x", SnesToPc(convert_address));
30
31 BEGIN_TABLE("Memory Comparison", 2, ImGuiTableFlags_Resizable);
32 SETUP_COLUMN("Source")
33 SETUP_COLUMN("Dest")
34
36 Text("%s", rom()->filename().data());
37 memory_widget_.DrawContents(rom()->mutable_data(), rom()->size());
38
42 ImGui::BeginGroup();
43 ImGui::BeginChild("Comparison ROM");
44 Text("%s", comparison_rom_.filename().data());
47 ImGui::EndChild();
48 ImGui::EndGroup();
49 }
50 END_TABLE()
51
52 ImGui::End();
53
54 return absl::OkStatus();
55}
56
57bool MemoryEditor::LoadComparisonRom(const std::string& path) {
58 // A cancelled file dialog returns an empty path; keep the current comparison.
59 if (path.empty()) {
60 return false;
61 }
62 const absl::Status status = comparison_rom_.LoadFromFile(path);
63 if (!status.ok()) {
64 PRINT_IF_ERROR(status);
65 return false;
66 }
67 show_compare_rom_ = true;
68 return true;
69}
70
72 // Gentle compaction only; preserve theme ItemSpacing.x for SameLine() gaps.
73 {
74 const ImGuiStyle& style = ImGui::GetStyle();
75 gui::StyleVarGuard toolbar_vars(
76 {{ImGuiStyleVar_FramePadding,
77 ImVec2(std::max(4.0f, style.FramePadding.x),
78 std::max(3.0f, style.FramePadding.y - 1.0f))},
79 {ImGuiStyleVar_ItemSpacing,
80 ImVec2(std::max(4.0f, style.ItemSpacing.x),
81 std::max(3.0f, style.ItemSpacing.y - 1.0f))}});
82
83 if (ImGui::Button(ICON_MD_LOCATION_SEARCHING " Jump")) {
84 ImGui::OpenPopup("JumpToAddress");
85 }
86 if (ImGui::IsItemHovered()) {
87 ImGui::SetTooltip(tr("Jump to specific address"));
88 }
89
90 ImGui::SameLine();
91 ImGui::BeginDisabled();
92 ImGui::Button(ICON_MD_SEARCH " Search");
93 ImGui::EndDisabled();
94 if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) {
95 ImGui::SetTooltip(
96 tr("Hex pattern search is not available yet. Use Jump to go to an "
97 "address."));
98 }
99
100 ImGui::SameLine();
101 if (ImGui::Button(ICON_MD_BOOKMARK " Bookmarks")) {
102 ImGui::OpenPopup("Bookmarks");
103 }
104 if (ImGui::IsItemHovered()) {
105 ImGui::SetTooltip(tr("Manage address bookmarks"));
106 }
107
108 ImGui::SameLine();
109 ImGui::Text(ICON_MD_MORE_VERT);
110 ImGui::SameLine();
111
112 // Show current address
113 if (current_address_ != 0) {
114 ImGui::TextColored(gui::GetInfoColor(), ICON_MD_LOCATION_ON " 0x%06X",
116 }
117 }
118
119 ImGui::Separator();
120
123}
124
126 if (ImGui::BeginPopupModal("JumpToAddress", nullptr,
127 ImGuiWindowFlags_AlwaysAutoResize)) {
128 ImGui::TextColored(gui::GetInfoColor(),
129 ICON_MD_LOCATION_SEARCHING " Jump to Address");
130 ImGui::Separator();
131 ImGui::Spacing();
132
133 ImGui::SetNextItemWidth(200);
134 if (ImGui::InputText("##jump_addr", jump_address_,
135 IM_ARRAYSIZE(jump_address_),
136 ImGuiInputTextFlags_CharsHexadecimal |
137 ImGuiInputTextFlags_EnterReturnsTrue)) {
138 // Parse and jump on Enter key
139 unsigned int addr;
140 if (sscanf(jump_address_, "%X", &addr) == 1) {
141 current_address_ = addr;
142 memory_widget_.GotoAddrAndHighlight(addr, addr + 1);
143 ImGui::CloseCurrentPopup();
144 }
145 }
146 ImGui::TextDisabled(tr("Format: 0x1C800 or 1C800"));
147
148 ImGui::Spacing();
149 ImGui::Separator();
150 ImGui::Spacing();
151
152 if (ImGui::Button(ICON_MD_CHECK " Go", ImVec2(120, 0))) {
153 unsigned int addr;
154 if (sscanf(jump_address_, "%X", &addr) == 1) {
155 current_address_ = addr;
156 memory_widget_.GotoAddrAndHighlight(addr, addr + 1);
157 }
158 ImGui::CloseCurrentPopup();
159 }
160 ImGui::SameLine();
161 if (ImGui::Button(ICON_MD_CANCEL " Cancel", ImVec2(120, 0))) {
162 ImGui::CloseCurrentPopup();
163 }
164 ImGui::EndPopup();
165 }
166}
167
169 if (ImGui::BeginPopupModal("Bookmarks", nullptr,
170 ImGuiWindowFlags_AlwaysAutoResize)) {
171 ImGui::TextColored(gui::GetWarningColor(),
172 ICON_MD_BOOKMARK " Memory Bookmarks");
173 ImGui::Separator();
174 ImGui::Spacing();
175
176 if (bookmarks_.empty()) {
177 ImGui::TextDisabled(ICON_MD_INFO " No bookmarks yet");
178 ImGui::Spacing();
179 ImGui::Separator();
180 ImGui::Spacing();
181
182 if (ImGui::Button(ICON_MD_ADD " Add Current Address", ImVec2(250, 0))) {
183 Bookmark new_bookmark;
184 new_bookmark.address = current_address_;
185 new_bookmark.name =
186 absl::StrFormat("Bookmark %zu", bookmarks_.size() + 1);
187 new_bookmark.description = "User-defined bookmark";
188 bookmarks_.push_back(new_bookmark);
189 }
190 } else {
191 // Bookmarks table
192 ImGui::BeginChild("##bookmarks_list", ImVec2(0, 300), true);
193 if (ImGui::BeginTable("##bookmarks_table", 3,
194 ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg |
195 ImGuiTableFlags_Resizable)) {
196 ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, 150);
197 ImGui::TableSetupColumn("Address", ImGuiTableColumnFlags_WidthFixed,
198 100);
199 ImGui::TableSetupColumn("Description",
200 ImGuiTableColumnFlags_WidthStretch);
201 ImGui::TableHeadersRow();
202
203 for (size_t i = 0; i < bookmarks_.size(); ++i) {
204 const auto& bm = bookmarks_[i];
205 ImGui::PushID(static_cast<int>(i));
206
207 ImGui::TableNextRow();
208 ImGui::TableNextColumn();
209 if (ImGui::Selectable(bm.name.c_str(), false,
210 ImGuiSelectableFlags_SpanAllColumns)) {
211 current_address_ = bm.address;
212 memory_widget_.GotoAddrAndHighlight(bm.address, bm.address + 1);
213 ImGui::CloseCurrentPopup();
214 }
215
216 ImGui::TableNextColumn();
217 ImGui::TextColored(gui::GetInfoColor(), tr("0x%06X"), bm.address);
218
219 ImGui::TableNextColumn();
220 ImGui::TextDisabled("%s", bm.description.c_str());
221
222 ImGui::PopID();
223 }
224 ImGui::EndTable();
225 }
226 ImGui::EndChild();
227
228 ImGui::Spacing();
229 if (ImGui::Button(ICON_MD_ADD " Add Bookmark", ImVec2(150, 0))) {
230 Bookmark new_bookmark;
231 new_bookmark.address = current_address_;
232 new_bookmark.name =
233 absl::StrFormat("Bookmark %zu", bookmarks_.size() + 1);
234 new_bookmark.description = "User-defined bookmark";
235 bookmarks_.push_back(new_bookmark);
236 }
237 ImGui::SameLine();
238 if (ImGui::Button(ICON_MD_CLEAR_ALL " Clear All", ImVec2(150, 0))) {
239 bookmarks_.clear();
240 }
241 }
242
243 ImGui::Spacing();
244 ImGui::Separator();
245 ImGui::Spacing();
246
247 if (ImGui::Button(ICON_MD_CLOSE " Close", ImVec2(250, 0))) {
248 ImGui::CloseCurrentPopup();
249 }
250
251 ImGui::EndPopup();
252 }
253}
254
255} // namespace editor
256} // namespace yaze
absl::Status LoadFromFile(const std::string &filename, const LoadOptions &options=LoadOptions::Defaults())
Definition rom.cc:632
auto filename() const
Definition rom.h:219
auto mutable_data()
Definition rom.h:214
auto size() const
Definition rom.h:212
std::vector< Bookmark > bookmarks_
gui::MemoryEditorWidget memory_widget_
absl::Status Update() override
bool LoadComparisonRom(const std::string &path)
gui::MemoryEditorWidget comparison_widget_
RAII guard for ImGui style vars.
Definition style_guard.h:71
static std::string ShowOpenFileDialog()
ShowOpenFileDialog opens a file dialog and returns the selected filepath. Uses global feature flag to...
#define ICON_MD_LOCATION_ON
Definition icons.h:1137
#define ICON_MD_LOCATION_SEARCHING
Definition icons.h:1139
#define ICON_MD_INFO
Definition icons.h:993
#define ICON_MD_CANCEL
Definition icons.h:364
#define ICON_MD_MORE_VERT
Definition icons.h:1243
#define ICON_MD_SEARCH
Definition icons.h:1673
#define ICON_MD_CHECK
Definition icons.h:397
#define ICON_MD_CLEAR_ALL
Definition icons.h:417
#define ICON_MD_ADD
Definition icons.h:86
#define ICON_MD_BOOKMARK
Definition icons.h:285
#define ICON_MD_CLOSE
Definition icons.h:418
#define SETUP_COLUMN(l)
Definition macro.h:12
#define END_TABLE()
Definition macro.h:20
#define BEGIN_TABLE(l, n, f)
Definition macro.h:11
#define PRINT_IF_ERROR(expression)
Definition macro.h:28
#define NEXT_COLUMN()
Definition macro.h:18
bool InputHex(const char *label, uint64_t *data)
Definition input.cc:438
ImVec4 GetWarningColor()
Definition ui_helpers.cc:54
ImVec4 GetInfoColor()
Definition ui_helpers.cc:64
uint32_t SnesToPc(uint32_t addr) noexcept
Definition snes.h:8
void SetComparisonData(const void *data)
void GotoAddrAndHighlight(size_t addr_min, size_t addr_max)
void DrawContents(void *mem_data_void, size_t mem_size, size_t base_display_addr=0x0000)