1#ifndef YAZE_APP_EDITOR_DUNGEON_PANELS_DUNGEON_ENTRANCES_PANEL_H_
2#define YAZE_APP_EDITOR_DUNGEON_PANELS_DUNGEON_ENTRANCES_PANEL_H_
13#include "absl/status/status.h"
14#include "absl/strings/str_format.h"
23#include "imgui/imgui.h"
44 std::array<zelda3::RoomEntrance, zelda3::kNumDungeonEntranceSlots>*
46 std::array<zelda3::DungeonSpawnPoint, zelda3::kNumDungeonSpawnPoints>*
48 int* current_entrance_id, std::function<
void(
int)> on_entrance_selected)
58 std::string
GetId()
const override {
return "dungeon.entrance_properties"; }
66 std::function<std::optional<DungeonEntranceCameraState>(
int)>;
85 std::function<
void()> data_changed_callback = {}) {
98 std::function<
bool()> automatic,
99 std::function<
void(
int)> select) {
117 ? (*spawn_points_)[selected].main_gfx
118 : (*entrances_)[selected].blockset_;
119 std::string preview = absl::StrFormat(
124 const std::string name =
125 automatic && !context.owner_name.empty() ? context.owner_name
130 context.uses_entrance()
132 "%s: %s / %s", automatic ?
"Auto" :
"Manual", name,
135 "Auto: %s / GFX %02X",
139 context.effective_blockset());
141 ImGui::SetNextItemWidth(width);
142 const bool open = ImGui::BeginCombo(
143 "##GraphicsEntrance", preview.c_str(),
146 if (ImGui::IsItemHovered())
148 "Auto follows the room's dungeon. Choosing an entrance pins its "
149 "graphics until you choose Auto again. This changes only the "
153 if (ImGui::Selectable(
"Auto (follow room)", automatic))
158 ImGui::BeginChild(
"##EntranceChoices",
159 ImVec2(0, ImGui::GetTextLineHeightWithSpacing() * 8));
161 for (
int slot = 0; slot < static_cast<int>(
entrances_->size()); ++slot) {
166 if (ImGui::Selectable(label.c_str(), !automatic && slot == selected)) {
169 ImGui::CloseCurrentPopup();
179 if (slot == selected) {
180 ImGui::SetItemDefaultFocus();
182 ImGui::SetScrollHereY(0.5f);
194 ImGui::TextWrapped(
"Choose an entrance to edit its properties.");
214 void Draw(
bool* p_open)
override {
219 const bool split_layout = ImGui::GetContentRegionAvail().x >= 620.0f;
221 ImGui::BeginTable(
"##EntranceNavigator", 2,
222 ImGuiTableFlags_Resizable |
223 ImGuiTableFlags_BordersInnerV |
224 ImGuiTableFlags_SizingStretchProp)) {
225 ImGui::TableSetupColumn(
"Entrances", ImGuiTableColumnFlags_WidthFixed,
227 ImGui::TableSetupColumn(
"Properties", ImGuiTableColumnFlags_WidthStretch);
228 ImGui::TableNextRow();
229 ImGui::TableNextColumn();
231 ImGui::TableNextColumn();
237 if (ImGui::BeginTabBar(
"##EntranceNavigatorTabs")) {
259 ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows);
264 constexpr char hexadecimal[] =
"0123456789ABCDEF";
265 float glyph_width = 0.0f;
266 for (
int index = 0; index < 16; ++index) {
267 const char glyph[] = {hexadecimal[index],
'\0'};
268 glyph_width = std::max(glyph_width, ImGui::CalcTextSize(glyph).x);
270 return glyph_width * digits + ImGui::GetStyle().FramePadding.x * 2.0f +
276 ImGui::GetCursorScreenPos().x + ImGui::GetContentRegionAvail().x;
277 const float spacing = ImGui::GetStyle().ItemSpacing.x;
278 const float next_width =
280 if (ImGui::GetItemRectMax().x + spacing + next_width <= right)
284 template <
typename T>
286 bool continue_row =
false) {
287 constexpr int digits =
sizeof(T) == 1 ? 2 : 4;
293 const float label_width =
294 std::max(1.0f, ImGui::GetContentRegionAvail().x - width -
295 ImGui::GetStyle().ItemSpacing.x);
296 ImGui::PushTextWrapPos(ImGui::GetCursorPosX() + label_width);
297 bool changed =
false;
298 if constexpr (
sizeof(T) == 1)
302 ImGui::PopTextWrapPos();
320 const auto validation =
322 if (!ImGui::BeginPopupModal(
"Camera Repair Preview##DungeonEntrance",
323 nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
328 "Review the derived camera values before changing this entrance. "
329 "The repair is one undoable action.");
331 if (ImGui::BeginTable(
332 "##CameraRepairValues", 3,
333 ImGuiTableFlags_BordersInner | ImGuiTableFlags_SizingFixedFit)) {
334 ImGui::TableSetupColumn(
"Field");
335 ImGui::TableSetupColumn(
"Current");
336 ImGui::TableSetupColumn(
"Derived");
337 ImGui::TableHeadersRow();
338 const auto draw_word_row = [](
const char* label, uint16_t current,
340 ImGui::TableNextRow();
341 ImGui::TableNextColumn();
342 ImGui::TextUnformatted(label);
343 ImGui::TableNextColumn();
344 ImGui::Text(
"0x%04X", current);
345 ImGui::TableNextColumn();
346 ImGui::Text(
"0x%04X", derived);
349 validation.expected.camera_x);
351 validation.expected.camera_y);
353 validation.expected.trigger_x);
355 validation.expected.trigger_y);
356 ImGui::TableNextRow();
357 ImGui::TableNextColumn();
358 ImGui::TextUnformatted(
"Quadrant");
359 ImGui::TableNextColumn();
361 ImGui::TableNextColumn();
362 if (validation.quadrant_repair_safe) {
363 ImGui::Text(
"0x%02X", validation.expected.quadrant);
365 ImGui::TextUnformatted(
"Preserved");
370 "Boundary pages: [%02X %02X %02X %02X | %02X %02X %02X %02X] "
371 "-> [%02X %02X %02X %02X | %02X %02X %02X %02X]",
380 validation.expected.boundaries[0], validation.expected.boundaries[1],
381 validation.expected.boundaries[2], validation.expected.boundaries[3],
382 validation.expected.boundaries[4], validation.expected.boundaries[5],
383 validation.expected.boundaries[6], validation.expected.boundaries[7]);
389 if (ImGui::Button(
"Apply Safe Repair")) {
390 const absl::Status status =
393 : absl::FailedPreconditionError(
394 "Camera repair callback is unavailable");
399 ImGui::CloseCurrentPopup();
405 ImGui::EndDisabled();
407 if (ImGui::Button(
"Cancel")) {
411 ImGui::CloseCurrentPopup();
421 if (!state.has_value()) {
422 ImGui::TextDisabled(
"Camera validation unavailable.");
426 ImGui::SeparatorText(
"Camera safety");
427 ImGui::PushTextWrapPos(0.0f);
428 if (!validation.geometry_valid()) {
429 ImGui::TextColored(ImVec4(0.95f, 0.35f, 0.30f, 1.0f),
430 "Unsafe camera geometry");
431 for (
const auto& error : validation.errors) {
434 ImGui::TextWrapped(
"%s", error.c_str());
436 if (validation.can_repair()) {
438 "The room and player position are valid, so a previewed repair "
439 "can replace these derived fields.");
441 }
else if (validation.matches_derived()) {
442 ImGui::TextColored(ImVec4(0.35f, 0.80f, 0.45f, 1.0f),
443 "Camera values match the player position.");
446 "Camera geometry is safe, but %zu derived value group(s) differ.",
447 validation.differences.size());
448 for (
const auto& difference : validation.differences) {
451 ImGui::TextWrapped(
"%s", difference.c_str());
455 ImGui::PopTextWrapPos();
457 if (ImGui::Button(
"Preview Camera Repair", ImVec2(-1, 0))) {
461 ImGui::OpenPopup(
"Camera Repair Preview##DungeonEntrance");
464 ImGui::EndDisabled();
481 const std::array<uint8_t*, 8>& boundaries) {
482 bool changed =
false;
484 const float mode_width = ImGui::CalcTextSize(
"Full room").x;
485 const float column_width =
486 std::max(value_width, ImGui::CalcTextSize(
"North").x);
487 const bool narrow = ImGui::GetContentRegionAvail().x <
488 mode_width + column_width * 4.0f +
489 ImGui::GetStyle().CellPadding.x * 10.0f;
490 if (!ImGui::BeginTable(table_id, narrow ? 2 : 5,
491 ImGuiTableFlags_BordersInnerV |
492 ImGuiTableFlags_SizingStretchProp)) {
496 ImGui::TableSetupColumn(
"Boundary", ImGuiTableColumnFlags_WidthStretch);
497 ImGui::TableSetupColumn(
"Value", ImGuiTableColumnFlags_WidthFixed,
500 ImGui::TableSetupColumn(
"Mode", ImGuiTableColumnFlags_WidthFixed,
502 for (
const auto* direction : {
"North",
"East",
"South",
"West"})
503 ImGui::TableSetupColumn(direction);
504 ImGui::TableHeadersRow();
506 constexpr std::array<int, 4> kQuadrantOrder = {0, 6, 2, 4};
507 constexpr std::array<int, 4> kFullRoomOrder = {1, 7, 3, 5};
508 const auto draw_row = [&changed, &boundaries, narrow, value_width,
this](
510 const std::array<int, 4>& order) {
512 ImGui::TableNextRow();
513 ImGui::TableNextColumn();
514 ImGui::TextUnformatted(label);
516 constexpr const char* directions[] = {
"North",
"East",
"South",
"West"};
517 for (
size_t direction = 0; direction < order.size(); ++direction) {
518 const int index = order[direction];
520 ImGui::TableNextRow();
521 ImGui::TableNextColumn();
522 ImGui::TextWrapped(
"%s %s", label, directions[direction]);
524 ImGui::TableNextColumn();
525 ImGui::PushID(index);
529 "##Boundary", absl::StrFormat(
"CameraBoundary%d", index).c_str());
533 draw_row(
"Quadrant", kQuadrantOrder);
534 draw_row(
"Full room", kFullRoomOrder);
541 ImGui::GetContentRegionAvail().x);
546 if (ImGui::BeginChild(
"##EntrancesList", ImVec2(0, 0),
true,
547 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
548 for (
int i = 0; i < kTotalEntries; i++) {
549 std::string entrance_name;
550 if (i < kNumSpawnPoints) {
551 entrance_name = absl::StrFormat(
"Spawn Point %d", i);
553 const int entrance_id = i - kNumSpawnPoints;
554 entrance_name = entrance_id < kNumEntrances
556 : absl::StrFormat(
"Unknown %d", i);
559 const int room_id = i < kNumSpawnPoints ? (*spawn_points_)[i].room_id
560 : (*entrances_)[i].room_;
561 const std::string room_name =
563 const std::string label = absl::StrFormat(
565 entrance_name, room_name, room_id);
571 if (ImGui::Selectable(label.c_str(), is_selected)) {
578 absl::StrFormat(
"entrance_%03X", i));
579 if (ImGui::IsItemHovered())
580 ImGui::SetTooltip(
"%s", label.c_str());
587 auto& spawn = (*spawn_points_)[slot_index];
588 const auto before = spawn;
589 const bool properties_editable =
591 bool changed =
false;
593 ImGui::Text(tr(
"Spawn Point %d"), slot_index);
594 if (!properties_editable) {
595 ImGui::TextWrapped(tr(
596 "Spawn point data is unavailable; reload the ROM before editing."));
598 ImGui::BeginDisabled(!properties_editable);
604 "SpawnDungeonId",
true);
605 changed |=
DrawHexProperty(
"Main GFX", &spawn.main_gfx,
"SpawnMainGfx");
610 changed |=
DrawHexProperty(
"Player X", &spawn.x_coordinate,
"SpawnPlayerX");
612 DrawHexProperty(
"Player Y", &spawn.y_coordinate,
"SpawnPlayerY",
true);
614 DrawHexProperty(
"Overworld Door Tilemap", &spawn.overworld_door_tilemap,
616 ImGui::EndDisabled();
621 const auto camera_before = spawn;
623 const bool camera_open = ImGui::CollapsingHeader(
"Advanced camera data");
626 ImGui::BeginDisabled(!properties_editable);
627 changed |=
DrawHexProperty(
"Camera Trigger X", &spawn.camera_trigger_x,
628 "SpawnCameraTriggerX");
629 changed |=
DrawHexProperty(
"Camera Trigger Y", &spawn.camera_trigger_y,
630 "SpawnCameraTriggerY",
true);
631 changed |=
DrawHexProperty(
"Horizontal Scroll", &spawn.horizontal_scroll,
632 "SpawnHorizontalScroll");
634 "SpawnVerticalScroll",
true);
637 "SpawnQuadrant",
true);
640 "SpawnScrollController",
true);
642 ImGui::SeparatorText(tr(
"Camera Boundaries"));
644 {&spawn.camera_scroll_boundaries[0],
645 &spawn.camera_scroll_boundaries[1],
646 &spawn.camera_scroll_boundaries[2],
647 &spawn.camera_scroll_boundaries[3],
648 &spawn.camera_scroll_boundaries[4],
649 &spawn.camera_scroll_boundaries[5],
650 &spawn.camera_scroll_boundaries[6],
651 &spawn.camera_scroll_boundaries[7]});
652 ImGui::EndDisabled();
659 auto& entrance = (*entrances_)[slot_index];
660 const auto before = entrance;
661 const bool properties_editable =
663 bool changed =
false;
665 ImGui::Text(tr(
"Entrance ID: %04X"), entrance.entrance_id_);
666 ImGui::BeginDisabled(!properties_editable);
667 changed |=
DrawHexProperty(
"Room ID", &entrance.room_,
"RegularRoomId");
669 "RegularDungeonId",
true);
672 changed |=
DrawHexProperty(
"Music", &entrance.music_,
"RegularMusic",
true);
673 changed |=
DrawHexProperty(
"Floor", &entrance.floor_,
"RegularFloor",
true);
679 "RegularPlayerY",
true);
681 ImGui::EndDisabled();
686 const auto camera_before = entrance;
688 const bool camera_open = ImGui::CollapsingHeader(
"Advanced camera data");
691 ImGui::BeginDisabled(!properties_editable);
694 "RegularCameraTriggerX");
697 "RegularCameraTriggerY",
true);
701 "RegularScrollY",
true);
702 changed |=
DrawHexProperty(
"Scroll Quadrant", &entrance.scroll_quadrant_,
703 "RegularScrollQuadrant");
705 ImGui::SeparatorText(tr(
"Camera Boundaries"));
707 "##RegularCameraBoundaries",
708 {&entrance.camera_boundary_qn_, &entrance.camera_boundary_fn_,
709 &entrance.camera_boundary_qs_, &entrance.camera_boundary_fs_,
710 &entrance.camera_boundary_qw_, &entrance.camera_boundary_fw_,
711 &entrance.camera_boundary_qe_, &entrance.camera_boundary_fe_});
712 ImGui::EndDisabled();
721 auto& entrance = (*entrances_)[slot_index];
737 auto& spawn = (*spawn_points_)[slot_index];
753 std::array<zelda3::RoomEntrance, zelda3::kNumDungeonEntranceSlots>*
755 std::array<zelda3::DungeonSpawnPoint, zelda3::kNumDungeonSpawnPoints>*
WindowContent for displaying and editing dungeon entrances.
ImGuiID property_edit_active_id_
void DrawGraphicsContext(float width=-1.0f)
void SetPropertyEditCallbacks(EntrancePropertyEditCallback entrance_callback, SpawnPropertyEditCallback spawn_callback)
bool OwnsNavigationShortcutFocus() const
DungeonEntrancesPanel(std::array< zelda3::RoomEntrance, zelda3::kNumDungeonEntranceSlots > *entrances, std::array< zelda3::DungeonSpawnPoint, zelda3::kNumDungeonSpawnPoints > *spawn_points, int *current_entrance_id, std::function< void(int)> on_entrance_selected)
void DrawCameraRepairPreview()
std::optional< DungeonEntranceCameraState > camera_repair_preview_
std::function< bool()> preview_automatic_
void DrawSpawnPointProperties(int slot_index)
int navigation_shortcut_last_draw_frame_
void Draw(bool *p_open) override
Draw the panel content.
std::function< void(int)> on_entrance_selected_
void SetCameraTools(CameraStateProvider state_provider, CameraRepairCallback repair_callback, std::function< void()> data_changed_callback={})
int GetPriority() const override
Get display priority for menu ordering.
SpawnPropertyEditCallback spawn_property_edit_callback_
void RegisterPropertyInput(const char *field_label, const char *widget_label)
void DrawCameraSafety(int slot_index)
void DrawSelectedProperties()
std::function< std::optional< DungeonEntranceCameraState >(int)> CameraStateProvider
void CommitEntrancePropertyEdit(int slot_index, const zelda3::RoomEntrance &before, bool changed)
bool navigation_shortcut_focus_
bool DrawHexProperty(const char *label, T *value, const char *widget_label, bool continue_row=false)
std::array< zelda3::DungeonSpawnPoint, zelda3::kNumDungeonSpawnPoints > * spawn_points_
float HexPropertyWidth(int digits) const
void SetProject(const project::YazeProject *project)
std::function< void( int, const zelda3::DungeonSpawnPoint &, const zelda3::DungeonSpawnPoint &)> SpawnPropertyEditCallback
std::string GetDisplayName() const override
Human-readable name shown in menus and title bars.
int * current_entrance_id_
std::string GetWorkflowGroup() const override
Optional workflow group for hack-centric actions.
std::array< zelda3::RoomEntrance, zelda3::kNumDungeonEntranceSlots > * entrances_
std::string camera_repair_error_
const project::YazeProject * project_
std::shared_ptr< const int > property_edit_gesture() const
std::string GetEditorCategory() const override
Editor category this panel belongs to.
EntrancePropertyEditCallback entrance_property_edit_callback_
CameraRepairCallback camera_repair_callback_
std::string GetId() const override
Unique identifier for this panel.
ImGuiTextFilter entrance_filter_
std::string GetIcon() const override
Material Design icon for this panel.
std::function< void()> camera_data_changed_callback_
CameraStateProvider camera_state_provider_
std::function< DungeonRenderContext()> preview_context_
void DrawPropertiesInWorkbench()
void DrawRegularEntranceProperties(int slot_index)
void SetGraphicsPreview(std::function< DungeonRenderContext()> context, std::function< bool()> automatic, std::function< void(int)> select)
void ContinuePropertyRow(const char *label, int digits) const
std::function< absl::Status(int)> CameraRepairCallback
std::function< void(int)> select_preview_
std::function< void( int, const zelda3::RoomEntrance &, const zelda3::RoomEntrance &)> EntrancePropertyEditCallback
void ObservePropertyEditGesture()
std::string EntranceLabel(int slot) const
std::shared_ptr< const int > property_edit_gesture_
bool DrawCameraBoundaries(const char *table_id, const std::array< uint8_t *, 8 > &boundaries)
void CommitSpawnPropertyEdit(int slot_index, const zelda3::DungeonSpawnPoint &before, bool changed)
Base interface for all logical window content components.
Dungeon Room Entrance or Spawn Point.
#define ICON_MD_DOOR_FRONT
std::string GetEntranceLabel(const project::YazeProject *project, int entrance_id, int room_id)
std::string GetRoomLabel(const project::YazeProject *project, int room_id)
std::string DungeonEntrancePreviewLabel(int slot)
bool CanEditDungeonEntrance(int slot_index, const zelda3::RoomEntrance &entrance)
bool CanEditDungeonSpawnPoint(int slot_index, const zelda3::DungeonSpawnPoint &spawn)
bool MarkDungeonEntranceDirtyIfEditable(int slot_index, zelda3::RoomEntrance &entrance, bool properties_changed)
bool MarkDungeonSpawnPointDirtyIfEditable(int slot_index, zelda3::DungeonSpawnPoint &spawn, bool properties_changed)
DungeonEntranceCameraValidation ValidateDungeonEntranceCamera(const DungeonEntranceCameraState &state)
void AutoRegisterLastItem(const std::string &widget_type, const std::string &explicit_label, const std::string &description)
Automatically register the last ImGui item.
bool InputHexWord(const char *label, uint16_t *data, float input_width, bool no_step)
bool InputHexByte(const char *label, uint8_t *data, float input_width, bool no_step)
constexpr int kNumRegularDungeonEntrances
constexpr int kNumDungeonSpawnPoints
constexpr int kNumDungeonEntranceSlots
Modern project structure with comprehensive settings consolidation.