yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
water_fill_panel.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_DUNGEON_PANELS_WATER_FILL_PANEL_H
2#define YAZE_APP_EDITOR_DUNGEON_PANELS_WATER_FILL_PANEL_H
3
4#include <algorithm>
5#include <array>
6#include <cstdint>
7#include <exception>
8#include <fstream>
9#include <string>
10#include <unordered_map>
11#include <vector>
12#include "util/i18n/tr.h"
13
14#include "absl/strings/str_format.h"
20#include "app/gui/core/icons.h"
21#include "core/features.h"
22#include "util/file_util.h"
25
26namespace yaze::editor {
27
29 public:
31 DungeonObjectInteraction* interaction)
32 : viewer_(viewer), interaction_(interaction) {}
33
34 std::string GetId() const override { return "dungeon.water_fill"; }
35 std::string GetDisplayName() const override { return "Water Fill"; }
36 std::string GetIcon() const override { return ICON_MD_WATER_DROP; }
37 std::string GetEditorCategory() const override { return "Dungeon"; }
38
39 void SetCanvasViewer(DungeonCanvasViewer* viewer) { viewer_ = viewer; }
41 interaction_ = interaction;
42 }
43
44 void Draw(bool* p_open) override {
45 (void)p_open;
46 const auto& theme = AgentUI::GetTheme();
47
48 if (!viewer_ || !viewer_->HasRooms() || !viewer_->rom() ||
49 !viewer_->rom()->is_loaded() || !viewer_->rooms()) {
50 ImGui::TextDisabled(ICON_MD_INFO " No dungeon rooms loaded.");
51 return;
52 }
53
54 auto* rooms = viewer_->rooms();
55 const int room_id = viewer_->current_room_id();
56 const bool room_id_valid =
57 (room_id >= 0 && room_id < static_cast<int>(rooms->size()));
58
59 const size_t rom_size = viewer_->rom()->vector().size();
60 const bool reserved_region_present =
62 const bool save_enabled =
64 if (!reserved_region_present) {
65 ImGui::TextColored(theme.status_error, ICON_MD_ERROR
66 " WaterFill reserved region missing (use an "
67 "expanded-collision Oracle ROM)");
68 ImGui::TextDisabled(
69 tr("Expected ROM >= 0x%X bytes (WaterFill end). Current ROM is %zu "
70 "bytes."),
72 ImGui::Separator();
73 }
74
75 if (!save_enabled) {
76 ImGui::TextColored(theme.text_warning_yellow, ICON_MD_LOCK
77 " Read-only: Water Fill saving is disabled");
78 ImGui::TextWrapped(
79 tr("Set save_dungeon_water_fill_zones=true in the .yaze project and "
80 "reopen it before authoring zones."));
84 }
85 ImGui::Separator();
86 }
87
88 bool show_overlay = viewer_->show_water_fill_overlay();
89 if (ImGui::Checkbox(tr("Show Water Fill Overlay"), &show_overlay)) {
91 }
92
93 ImGui::Separator();
94 ImGui::TextUnformatted(tr("Authoring"));
95
96 util::FileDialogOptions json_options;
97 json_options.filters.push_back({"Water Fill Zones", "json"});
98 json_options.filters.push_back({"All Files", "*"});
99
100 ImGui::BeginDisabled(!reserved_region_present || !save_enabled);
101 if (ImGui::Button(ICON_MD_UPLOAD " Import Zones...")) {
102 std::string path =
104 if (!path.empty()) {
105 try {
106 std::string contents = util::LoadFile(path);
107 auto zones_or = zelda3::LoadWaterFillZonesFromJsonString(contents);
108 if (!zones_or.ok()) {
109 last_io_error_ = std::string(zones_or.status().message());
110 last_io_status_.clear();
111 } else {
112 auto zones = std::move(zones_or.value());
113 for (const auto& z : zones) {
114 if (z.room_id < 0 ||
115 z.room_id >= static_cast<int>(rooms->size())) {
116 continue;
117 }
118 ApplyZoneToRoom(z, &(*rooms)[z.room_id]);
119 }
121 last_io_status_ = absl::StrFormat("Imported %zu zone(s) from %s",
122 zones.size(), path.c_str());
123 last_io_error_.clear();
124 }
125 } catch (const std::exception& e) {
126 last_io_error_ = e.what();
127 last_io_status_.clear();
128 }
129 }
130 }
131 ImGui::SameLine();
132 if (ImGui::Button(ICON_MD_TUNE " Normalize Masks Now")) {
133 auto zones = CollectZones(*rooms);
134 auto st = zelda3::NormalizeWaterFillZoneMasks(&zones);
135 if (!st.ok()) {
136 last_io_error_ = std::string(st.message());
137 last_io_status_.clear();
138 } else {
139 int changed = 0;
140 for (const auto& z : zones) {
141 auto& r = (*rooms)[z.room_id];
142 if (r.water_fill_sram_bit_mask() != z.sram_bit_mask) {
143 r.set_water_fill_sram_bit_mask(z.sram_bit_mask);
144 ++changed;
145 }
146 }
147 if (changed > 0) {
149 }
151 absl::StrFormat("Normalized masks (%d room(s) updated)", changed);
152 last_io_error_.clear();
153 }
154 }
155 ImGui::EndDisabled();
156
157 ImGui::SameLine();
158 if (ImGui::Button(ICON_MD_DOWNLOAD " Export Zones...")) {
159 auto zones = CollectZones(*rooms);
160 auto json_or = zelda3::DumpWaterFillZonesToJsonString(zones);
161 if (!json_or.ok()) {
162 last_io_error_ = std::string(json_or.status().message());
163 last_io_status_.clear();
164 } else {
166 "water_fill_zones.json", "json");
167 if (!path.empty()) {
168 std::ofstream file(path);
169 if (!file.is_open()) {
171 absl::StrFormat("Cannot write file: %s", path.c_str());
172 last_io_status_.clear();
173 } else {
174 file << *json_or;
175 file.close();
176 last_io_status_ = absl::StrFormat("Exported %zu zone(s) to %s",
177 zones.size(), path.c_str());
178 last_io_error_.clear();
179 }
180 }
181 }
182 }
183
184 if (!last_io_error_.empty()) {
185 ImGui::TextColored(theme.status_error, ICON_MD_ERROR " %s",
186 last_io_error_.c_str());
187 } else if (!last_io_status_.empty()) {
188 ImGui::TextColored(theme.status_success, ICON_MD_CHECK_CIRCLE " %s",
189 last_io_status_.c_str());
190 }
191
192 ImGui::TextWrapped(tr(
193 "Import/export uses a room-indexed JSON format. Normalize masks before "
194 "saving to avoid duplicate SRAM bits."));
195
196 ImGui::Separator();
197 if (!room_id_valid) {
198 ImGui::TextDisabled(ICON_MD_INFO " Invalid room ID.");
199 } else {
200 auto& room = (*rooms)[room_id];
201 const bool room_loaded = room.IsLoaded();
202 if (!room_loaded) {
203 ImGui::TextDisabled(
205 " Room not loaded yet (open it to paint and validate sprites).");
206 }
207
208 if (!interaction_) {
209 ImGui::TextDisabled(
210 tr("Painting requires an active interaction context."));
211 } else {
212 // Brush controls are shared across paint modes.
213 auto& state = interaction_->mode_manager().GetModeState();
214 int brush_radius = std::clamp(state.paint_brush_radius, 0, 8);
215 if (ImGui::SliderInt(tr("Brush Radius"), &brush_radius, 0, 8)) {
216 state.paint_brush_radius = brush_radius;
217 }
218 ImGui::SameLine();
219 ImGui::TextDisabled(tr("%dx%d"), (brush_radius * 2) + 1,
220 (brush_radius * 2) + 1);
221
222 bool is_painting = (interaction_->mode_manager().GetMode() ==
224 const bool can_paint =
225 reserved_region_present && room_loaded && save_enabled;
226 ImGui::BeginDisabled(!can_paint);
227 if (ImGui::Checkbox(tr("Paint Mode"), &is_painting)) {
228 if (is_painting) {
232 } else {
234 }
235 }
236 ImGui::EndDisabled();
237
238 if (is_painting) {
239 ImGui::TextColored(theme.text_warning_yellow,
240 tr("Left-drag paints; Alt-drag erases"));
241 }
242 }
243
244 const int tile_count = room.WaterFillTileCount();
245 ImGui::Separator();
246 ImGui::Text(tr("Zone Tiles: %d"), tile_count);
247 if (tile_count > 255) {
248 ImGui::TextColored(theme.status_error,
249 ICON_MD_ERROR " Too many tiles (max 255 per room)");
250 }
251
252 if (room_loaded) {
253 bool has_switch_sprite = false;
254 for (const auto& spr : room.GetSprites()) {
255 if (spr.id() == 0x04 || spr.id() == 0x21) {
256 has_switch_sprite = true;
257 break;
258 }
259 }
260 if (!has_switch_sprite) {
261 ImGui::TextColored(
262 theme.text_warning_yellow, ICON_MD_WARNING
263 " No PullSwitch (0x04) / PushSwitch (0x21) sprite found");
264 }
265 } else {
266 ImGui::TextDisabled(tr("Sprite checks require the room to be loaded."));
267 }
268
269 ImGui::Separator();
270 uint8_t mask = room.water_fill_sram_bit_mask();
271 std::string preview =
272 (mask == 0) ? "Auto (0x00)" : absl::StrFormat("0x%02X", mask);
273 ImGui::BeginDisabled(!reserved_region_present || !save_enabled);
274 if (ImGui::BeginCombo(tr("SRAM Bit Mask ($7EF411)"), preview.c_str())) {
275 auto option = [&](const char* label, uint8_t val) {
276 const bool selected = (mask == val);
277 if (ImGui::Selectable(label, selected)) {
278 room.set_water_fill_sram_bit_mask(val);
279 mask = val;
280 }
281 };
282
283 option("Auto (0x00)", 0x00);
284 option("Bit 0 (0x01)", 0x01);
285 option("Bit 1 (0x02)", 0x02);
286 option("Bit 2 (0x04)", 0x04);
287 option("Bit 3 (0x08)", 0x08);
288 option("Bit 4 (0x10)", 0x10);
289 option("Bit 5 (0x20)", 0x20);
290 option("Bit 6 (0x40)", 0x40);
291 option("Bit 7 (0x80)", 0x80);
292
293 ImGui::EndCombo();
294 }
295
296 ImGui::Separator();
297 if (ImGui::Button(tr("Clear Water Fill Zone"))) {
298 room.ClearWaterFillZone();
299 }
300 ImGui::EndDisabled();
301
302 ImGui::TextWrapped(
303 tr("Water fill zones are serialized as compact tile offset lists. "
304 "Keep zones under 255 tiles per room."));
305 }
306
307 // Overview: show all rooms that currently have zone data, plus global
308 // constraints (max 8 rooms / unique SRAM masks).
309 ImGui::Separator();
310 if (ImGui::CollapsingHeader(tr("Zone Overview"),
311 ImGuiTreeNodeFlags_DefaultOpen)) {
312 struct ZoneRow {
313 int room_id = 0;
314 int tiles = 0;
315 uint8_t mask = 0;
316 bool dirty = false;
317 };
318
319 std::vector<ZoneRow> rows;
320 rows.reserve(8);
321 std::unordered_map<uint8_t, int> mask_counts;
322 int rooms_over_tile_limit = 0;
323 int rooms_unassigned_mask = 0;
324
325 for (int rid = 0; rid < static_cast<int>(rooms->size()); ++rid) {
326 auto& r = (*rooms)[rid];
327 const int tiles = r.WaterFillTileCount();
328 if (tiles <= 0)
329 continue;
330 rows.push_back(ZoneRow{rid, tiles, r.water_fill_sram_bit_mask(),
331 r.water_fill_dirty()});
332 if (tiles > 255) {
333 rooms_over_tile_limit++;
334 }
335 if (r.water_fill_sram_bit_mask() == 0) {
336 rooms_unassigned_mask++;
337 } else {
338 mask_counts[r.water_fill_sram_bit_mask()]++;
339 }
340 }
341
342 std::sort(rows.begin(), rows.end(),
343 [](const ZoneRow& a, const ZoneRow& b) {
344 return a.room_id < b.room_id;
345 });
346
347 int duplicate_masks = 0;
348 for (const auto& [mask, count] : mask_counts) {
349 if (mask != 0 && count > 1) {
350 duplicate_masks++;
351 }
352 }
353
354 ImGui::Text(tr("Rooms with zones: %zu / 8"), rows.size());
355 if (rows.size() > 8) {
356 ImGui::TextColored(theme.status_error,
357 ICON_MD_ERROR " Too many rooms with zones (max 8)");
358 }
359 if (rooms_over_tile_limit > 0) {
360 ImGui::TextColored(theme.status_error,
361 ICON_MD_ERROR " %d room(s) exceed 255 tiles",
362 rooms_over_tile_limit);
363 }
364 if (duplicate_masks > 0) {
365 ImGui::TextColored(theme.status_error,
367 " Duplicate SRAM bit masks detected (%d mask(s))",
368 duplicate_masks);
369 }
370 if (rooms_unassigned_mask > 0) {
371 ImGui::TextColored(theme.text_warning_yellow,
373 " %d room(s) use Auto mask (assigned on save)",
374 rooms_unassigned_mask);
375 }
376
377 if (ImGui::BeginTable("##WaterFillZoneOverview", 6,
378 ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg |
379 ImGuiTableFlags_SizingFixedFit)) {
380 ImGui::TableSetupColumn("Room");
381 ImGui::TableSetupColumn("Tiles");
382 ImGui::TableSetupColumn("Mask");
383 ImGui::TableSetupColumn("Dirty");
384 ImGui::TableSetupColumn("Dup?");
385 ImGui::TableSetupColumn("Action");
386 ImGui::TableHeadersRow();
387
388 for (const auto& row : rows) {
389 const bool is_current = (row.room_id == room_id);
390 const bool is_dup =
391 (row.mask != 0 && mask_counts.contains(row.mask) &&
392 mask_counts[row.mask] > 1);
393
394 ImGui::TableNextRow();
395 ImGui::TableNextColumn();
396 if (is_current) {
397 ImGui::TextColored(theme.text_info, tr("0x%02X"), row.room_id);
398 } else {
399 ImGui::Text(tr("0x%02X"), row.room_id);
400 }
401
402 ImGui::TableNextColumn();
403 ImGui::Text("%d", row.tiles);
404
405 ImGui::TableNextColumn();
406 if (row.mask == 0) {
407 ImGui::TextDisabled(tr("Auto"));
408 } else {
409 ImGui::Text(tr("0x%02X"), row.mask);
410 }
411
412 ImGui::TableNextColumn();
413 ImGui::TextUnformatted(row.dirty ? "Yes" : "No");
414
415 ImGui::TableNextColumn();
416 if (is_dup) {
417 ImGui::TextColored(theme.status_error, ICON_MD_ERROR);
418 } else {
419 ImGui::TextDisabled("-");
420 }
421
422 ImGui::TableNextColumn();
423 if (viewer_->CanNavigateRooms()) {
424 ImGui::PushID(row.room_id);
425 if (ImGui::SmallButton(tr("Open"))) {
426 viewer_->NavigateToRoom(row.room_id);
427 }
428 ImGui::PopID();
429 } else {
430 ImGui::TextDisabled("-");
431 }
432 }
433
434 ImGui::EndTable();
435 }
436 }
437 }
438
439 private:
440 static std::vector<zelda3::WaterFillZoneEntry> CollectZones(
441 const DungeonRoomStore& rooms) {
442 std::vector<zelda3::WaterFillZoneEntry> zones;
443 zones.reserve(8);
444 for (int room_id = 0; room_id < static_cast<int>(rooms.size()); ++room_id) {
445 const auto& room = rooms[room_id];
446 const int tile_count = room.WaterFillTileCount();
447 if (tile_count <= 0) {
448 continue;
449 }
450
452 z.room_id = room_id;
453 z.sram_bit_mask = room.water_fill_sram_bit_mask();
454 z.fill_offsets.reserve(static_cast<size_t>(tile_count));
455
456 const auto& map = room.water_fill_zone().tiles;
457 for (size_t i = 0; i < map.size(); ++i) {
458 if (map[i] != 0) {
459 z.fill_offsets.push_back(static_cast<uint16_t>(i));
460 }
461 }
462 zones.push_back(std::move(z));
463 }
464 return zones;
465 }
466
468 zelda3::Room* room) {
469 if (room == nullptr) {
470 return;
471 }
472 room->ClearWaterFillZone();
474 for (uint16_t off : z.fill_offsets) {
475 const int x = static_cast<int>(off % 64);
476 const int y = static_cast<int>(off / 64);
477 room->SetWaterFillTile(x, y, true);
478 }
479 }
480
481 std::string last_io_status_;
482 std::string last_io_error_;
483
486};
487
488} // namespace yaze::editor
489
490#endif // YAZE_APP_EDITOR_DUNGEON_PANELS_WATER_FILL_PANEL_H
const auto & vector() const
Definition rom.h:173
bool is_loaded() const
Definition rom.h:155
static Flags & get()
Definition features.h:119
Handles object selection, placement, and interaction within the dungeon canvas.
void SetMode(InteractionMode mode)
Set interaction mode.
InteractionMode GetMode() const
Get current interaction mode.
ModeState & GetModeState()
Get mutable reference to mode state.
void SetCanvasViewer(DungeonCanvasViewer *viewer)
std::string GetIcon() const override
Material Design icon for this panel.
std::string GetId() const override
Unique identifier for this panel.
DungeonObjectInteraction * interaction_
DungeonCanvasViewer * viewer_
void SetInteraction(DungeonObjectInteraction *interaction)
static void ApplyZoneToRoom(const zelda3::WaterFillZoneEntry &z, zelda3::Room *room)
std::string GetDisplayName() const override
Human-readable name shown in menus and title bars.
void Draw(bool *p_open) override
Draw the panel content.
static std::vector< zelda3::WaterFillZoneEntry > CollectZones(const DungeonRoomStore &rooms)
WaterFillPanel(DungeonCanvasViewer *viewer, DungeonObjectInteraction *interaction)
std::string GetEditorCategory() const override
Editor category this panel belongs to.
Base interface for all logical window content components.
static std::string ShowSaveFileDialog(const std::string &default_name="", const std::string &default_extension="")
ShowSaveFileDialog opens a save file dialog and returns the selected filepath. Uses global feature fl...
static std::string ShowOpenFileDialog()
ShowOpenFileDialog opens a file dialog and returns the selected filepath. Uses global feature flag to...
void set_water_fill_sram_bit_mask(uint8_t mask)
Definition room.h:574
void ClearWaterFillZone()
Definition room.h:561
void SetWaterFillTile(int x, int y, bool filled)
Definition room.h:538
#define ICON_MD_INFO
Definition icons.h:993
#define ICON_MD_WARNING
Definition icons.h:2123
#define ICON_MD_LOCK
Definition icons.h:1140
#define ICON_MD_TUNE
Definition icons.h:2022
#define ICON_MD_ERROR
Definition icons.h:686
#define ICON_MD_UPLOAD
Definition icons.h:2048
#define ICON_MD_CHECK_CIRCLE
Definition icons.h:400
#define ICON_MD_DOWNLOAD
Definition icons.h:618
#define ICON_MD_WATER_DROP
Definition icons.h:2131
const AgentUITheme & GetTheme()
Editors are the view controllers for the application.
std::string LoadFile(const std::string &filename)
Loads the entire contents of a file into a string.
Definition file_util.cc:23
absl::StatusOr< std::string > DumpWaterFillZonesToJsonString(const std::vector< WaterFillZoneEntry > &zones)
constexpr int kWaterFillTableEnd
absl::Status NormalizeWaterFillZoneMasks(std::vector< WaterFillZoneEntry > *zones)
absl::StatusOr< std::vector< WaterFillZoneEntry > > LoadWaterFillZonesFromJsonString(const std::string &json_content)
constexpr bool HasWaterFillReservedRegion(std::size_t rom_size)
struct yaze::core::FeatureFlags::Flags::Dungeon dungeon
std::vector< FileDialogFilter > filters
Definition file_util.h:17
std::vector< uint16_t > fill_offsets