3#include "absl/container/flat_hash_map.h"
13#include "imgui/imgui.h"
14#include "imgui_memory_editor.h"
22using ImGui::BeginChild;
23using ImGui::BeginTabBar;
24using ImGui::BeginTabItem;
25using ImGui::BeginTable;
28using ImGui::EndTabBar;
29using ImGui::EndTabItem;
30using ImGui::RadioButton;
32using ImGui::TableHeadersRow;
33using ImGui::TableNextColumn;
34using ImGui::TableNextRow;
35using ImGui::TableSetupColumn;
39 ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable |
40 ImGuiTableFlags_Hideable | ImGuiTableFlags_BordersOuter |
41 ImGuiTableFlags_BordersV;
48 for (
int i = 0; i < 0x100 + 40; i++) {
54 if (room_size.room_size_pointer != 0x0A8000) {
61 auto palette_id =
rom()->
ReadWord(0xDEC4B + dungeon_palette_ptr);
62 if (palette_id.status() != absl::OkStatus()) {
65 int p_id = palette_id.value() / 180;
66 auto color = dungeon_man_pal_group[p_id][3];
72 for (
int i = 0; i < 0x07; ++i) {
76 for (
int i = 0; i < 0x85; ++i) {
87 return absl::OkStatus();
96 if (ImGui::BeginTabBar(
"##DungeonEditorTabBar")) {
97 if (ImGui::BeginTabItem(
"Room Editor")) {
101 if (ImGui::BeginTabItem(
"Usage Statistics")) {
110 return absl::OkStatus();
116 [
this](
int block) -> absl::Status {
121 return absl::OkStatus();
127 [
this, &sprites_aux1_pal_group](
int block) -> absl::Status {
132 return absl::OkStatus();
134 return absl::OkStatus();
138 std::map<int, std::vector<int>> rooms_by_bank;
140 int bank = room.second >> 16;
141 rooms_by_bank[bank].push_back(room.second);
145 for (
auto &bank_rooms : rooms_by_bank) {
147 std::sort(bank_rooms.second.begin(), bank_rooms.second.end());
149 for (
size_t i = 0; i < bank_rooms.second.size(); ++i) {
150 int room_ptr = bank_rooms.second[i];
155 [room_ptr](
const auto &entry) {
156 return entry.second == room_ptr;
160 if (room_ptr != 0x0A8000) {
161 if (i < bank_rooms.second.size() - 1) {
164 room_sizes_[room_id] = bank_rooms.second[i + 1] - room_ptr;
167 int bank_end_address = (bank_rooms.first << 16) | 0xFFFF;
168 room_sizes_[room_id] = bank_end_address - room_ptr + 1;
192 TableSetupColumn(
"Room Selector");
193 TableSetupColumn(
"Canvas", ImGuiTableColumnFlags_WidthStretch,
194 ImGui::GetContentRegionAvail().x);
195 TableSetupColumn(
"Object Selector");
200 if (ImGui::BeginTabBar(
"##DungeonRoomTabBar")) {
217 return absl::OkStatus();
221 if (BeginTable(
"DWToolset", 13, ImGuiTableFlags_SizingFixedFit,
223 TableSetupColumn(
"#undoTool");
224 TableSetupColumn(
"#redoTool");
225 TableSetupColumn(
"#separator");
226 TableSetupColumn(
"#anyTool");
228 TableSetupColumn(
"#bg1Tool");
229 TableSetupColumn(
"#bg2Tool");
230 TableSetupColumn(
"#bg3Tool");
231 TableSetupColumn(
"#separator");
232 TableSetupColumn(
"#spriteTool");
233 TableSetupColumn(
"#itemTool");
234 TableSetupColumn(
"#doorTool");
235 TableSetupColumn(
"#blockTool");
277 if (ImGui::IsItemHovered()) {
278 ImGui::SetTooltip(
"Sprites");
285 if (ImGui::IsItemHovered()) {
286 ImGui::SetTooltip(
"Items");
293 if (ImGui::IsItemHovered()) {
294 ImGui::SetTooltip(
"Doors");
301 if (ImGui::IsItemHovered()) {
302 ImGui::SetTooltip(
"Blocks");
315 if (
rom()->is_loaded()) {
319 if (ImGuiID child_id = ImGui::GetID((
void *)(intptr_t)9);
320 BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
321 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
326 each_room_name.data());
327 if (ImGui::IsItemClicked()) {
341using ImGui::Separator;
344 if (
rom()->is_loaded()) {
374 Text(
"Camera Boundaries");
376 Text(
"\t\t\t\t\tNorth East South West");
395 if (BeginChild(
"EntranceSelector", ImVec2(0, 0),
true,
396 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
397 for (
int i = 0; i < 0x85 + 7; i++) {
402 if (ImGui::IsItemClicked()) {
415 static int next_tab_id = 0;
437 ImGuiTabItemFlags_None)) {
476 if (Button(
"Load Room")) {
477 rooms_[room_id].LoadRoomGraphics(
rooms_[room_id].blockset);
478 rooms_[room_id].RenderRoomGraphics();
481 static bool show_objects =
false;
482 ImGui::Checkbox(
"Show Objects", &show_objects);
493 for (
const auto &
object :
rooms_[room_id].tile_objects_) {
494 canvas_.DrawOutline(
object.x_,
object.y_,
object.width_ * 16,
495 object.height_ * 16);
504 const auto height = 0x40;
510 int current_block = 0;
511 for (
int block : blocks) {
512 int offset = height * (current_block + 1);
514 if (current_block >= 1) {
532 if (BeginTabBar(
"##TabBar", ImGuiTabBarFlags_FittingPolicyScroll)) {
533 if (BeginTabItem(
"Room Graphics")) {
534 if (ImGuiID child_id = ImGui::GetID((
void *)(intptr_t)3);
535 BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
536 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
543 if (BeginTabItem(
"Object Renderer")) {
554 TableSetupColumn(
"Dungeon Objects", ImGuiTableColumnFlags_WidthStretch,
555 ImGui::GetContentRegionAvail().x);
556 TableSetupColumn(
"Canvas");
559 BeginChild(
"DungeonObjectButtons", ImVec2(250, 0),
true);
561 int selected_object = 0;
563 for (
const auto object_name : zelda3::Type1RoomObjectNames) {
564 if (ImGui::Selectable(object_name.data(), selected_object == i)) {
577 BeginChild(
"DungeonObjectCanvas", ImVec2(276, 0x10 * 0x40 + 1),
true);
591 static MemoryEditor mem_edit;
599 for (
const auto &room :
rooms_) {
621 const absl::flat_hash_map<uint16_t, int> &usage_map, uint16_t &selected_set,
622 int spriteset_offset) {
624 std::vector<std::pair<uint16_t, int>> sorted_usage(usage_map.begin(),
626 std::sort(sorted_usage.begin(), sorted_usage.end(),
627 [](
const auto &a,
const auto &b) { return a.first < b.first; });
629 for (
const auto &[set, count] : sorted_usage) {
630 std::string display_str;
631 if (spriteset_offset != 0x00) {
632 display_str = absl::StrFormat(
"%#02x, %#02x: %d", set,
633 (set + spriteset_offset), count);
636 absl::StrFormat(
"%#02x: %d", (set + spriteset_offset), count);
638 if (ImGui::Selectable(display_str.c_str(), selected_set == set)) {
651 int spriteset_offset = 0x00) {
652 std::vector<int> unused_sets;
653 for (
int i = 0; i < max_set; i++) {
654 if (usage_map.find(i) == usage_map.end()) {
655 unused_sets.push_back(i);
658 for (
const auto &set : unused_sets) {
659 if (spriteset_offset != 0x00) {
660 Text(
"%#02x, %#02x", set, (set + spriteset_offset));
669 if (Button(
"Refresh")) {
679 ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
680 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
681 if (BeginTable(
"DungeonUsageStatsTable", 8,
683 ImGui::GetContentRegionAvail())) {
684 TableSetupColumn(
"Blockset Usage");
685 TableSetupColumn(
"Unused Blockset");
686 TableSetupColumn(
"Palette Usage");
687 TableSetupColumn(
"Unused Palette");
688 TableSetupColumn(
"Spriteset Usage");
689 TableSetupColumn(
"Unused Spriteset");
690 TableSetupColumn(
"Usage Grid");
691 TableSetupColumn(
"Group Preview");
693 ImGui::PopStyleVar(2);
696 BeginChild(
"BlocksetUsageScroll", ImVec2(0, 0),
true,
697 ImGuiWindowFlags_HorizontalScrollbar);
702 BeginChild(
"UnusedBlocksetScroll", ImVec2(0, 0),
true,
703 ImGuiWindowFlags_HorizontalScrollbar);
708 BeginChild(
"PaletteUsageScroll", ImVec2(0, 0),
true,
709 ImGuiWindowFlags_HorizontalScrollbar);
714 BeginChild(
"UnusedPaletteScroll", ImVec2(0, 0),
true,
715 ImGuiWindowFlags_HorizontalScrollbar);
721 BeginChild(
"SpritesetUsageScroll", ImVec2(0, 0),
true,
722 ImGuiWindowFlags_HorizontalScrollbar);
727 BeginChild(
"UnusedSpritesetScroll", ImVec2(0, 0),
true,
728 ImGuiWindowFlags_HorizontalScrollbar);
733 BeginChild(
"UsageGrid", ImVec2(0, 0),
true,
734 ImGuiWindowFlags_HorizontalScrollbar);
735 Text(
"%s", absl::StrFormat(
"Total size of all rooms: %d hex format: %#06x",
754 int totalSquares = 296;
755 int squaresWide = 16;
756 int squaresTall = (totalSquares + squaresWide - 1) /
759 for (
int row = 0; row < squaresTall; ++row) {
762 for (
int col = 0; col < squaresWide; ++col) {
764 if (row * squaresWide + col >= totalSquares) {
768 const auto &room =
rooms_[row * squaresWide + col];
773 color.x = color.x / 255;
774 color.y = color.y / 255;
775 color.z = color.z / 255;
777 if (
room_sizes_[row * squaresWide + col] > 0xFFFF) {
778 color = ImVec4(1.0f, 0.0f, 0.0f, 1.0f);
781 color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
783 ImGui::PushStyleColor(ImGuiCol_Button, color);
785 ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
793 ImGui::PushStyleColor(
795 ImVec4(1.0f, 0.5f, 0.0f, 1.0f));
797 if (Button(absl::StrFormat(
"%#x",
room_sizes_[row * squaresWide + col])
804 if (ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
806 absl::StrFormat(
"RoomContextMenu%d", row * squaresWide + col)
809 ImGui::PopStyleColor(2);
814 ImGui::PopStyleColor();
818 if (ImGui::IsItemHovered()) {
820 ImGui::BeginTooltip();
821 Text(
"Room ID: %d", row * squaresWide + col);
822 Text(
"Blockset: %#02x", room.blockset);
823 Text(
"Spriteset: %#02x", room.spriteset);
824 Text(
"Palette: %#02x", room.palette);
825 Text(
"Floor1: %#02x", room.floor1);
826 Text(
"Floor2: %#02x", room.floor2);
827 Text(
"Message ID: %#04x", room.message_id_);
828 Text(
"Size: %#016llx",
room_sizes_[row * squaresWide + col]);
829 Text(
"Size Pointer: %#016llx",
auto palette_group() const
ResourceLabelManager * resource_label()
std::array< std::array< uint8_t, 4 >, kNumPalettesets > paletteset_ids
absl::StatusOr< uint16_t > ReadWord(int offset)
void UpdateBitmap(gfx::Bitmap *bitmap)
absl::flat_hash_map< uint16_t, int > spriteset_usage_
void LoadDungeonRoomSize()
uint16_t selected_blockset_
absl::Status RefreshGraphics()
absl::Status Update() override
uint64_t current_palette_group_id_
void DrawEntranceSelector()
std::unordered_map< int, ImVec4 > room_palette_
uint16_t current_room_id_
absl::flat_hash_map< uint16_t, int > blockset_usage_
uint64_t current_palette_id_
absl::flat_hash_map< uint16_t, int > palette_usage_
void RenderSetUsage(const absl::flat_hash_map< uint16_t, int > &usage_map, uint16_t &selected_set, int spriteset_offset=0x00)
uint16_t selected_spriteset_
GfxGroupEditor gfx_group_editor_
uint16_t selected_palette_
uint64_t total_room_size_
gfx::SnesPalette current_palette_
gfx::PaletteGroup current_palette_group_
void DrawDungeonCanvas(int room_id)
uint16_t current_entrance_id_
std::unordered_map< int, int > room_size_addresses_
absl::Status Redo() override
void Initialize() override
gui::Canvas room_gfx_canvas_
gfx::SnesPalette full_palette_
gui::Canvas object_canvas_
void DrawDungeonTabView()
std::vector< int64_t > room_sizes_
std::array< zelda3::RoomEntrance, 0x8C > entrances_
void CalculateUsageStats()
std::vector< int64_t > room_size_pointers_
absl::Status Load() override
void DrawObjectRenderer()
ImVector< int > active_rooms_
absl::Status UpdateDungeonRoomView()
absl::Status Undo() override
std::array< zelda3::Room, 0x128 > rooms_
std::array< gfx::Bitmap, 223 > & gfx_sheets()
Dungeon Room Entrance or Spawn Point.
#define ICON_MD_MORE_VERT
#define ICON_MD_FILTER_NONE
#define ICON_MD_SENSOR_DOOR
#define ICON_MD_PEST_CONTROL
#define PRINT_IF_ERROR(expression)
#define RETURN_IF_ERROR(expression)
#define ASSIGN_OR_RETURN(type_variable_name, expression)
void RenderUnusedSets(const absl::flat_hash_map< T, int > &usage_map, int max_set, int spriteset_offset=0x00)
Editors are the view controllers for the application.
constexpr ImGuiTableFlags kDungeonTableFlags
constexpr ImGuiTabItemFlags kDungeonTabFlags
constexpr ImGuiTabBarFlags kDungeonTabBarFlags
constexpr ImGuiTableFlags kDungeonObjectTableFlags
absl::StatusOr< PaletteGroup > CreatePaletteGroupFromLargePalette(SnesPalette &palette, int num_colors)
Take a SNESPalette, divide it into palettes of 8 colors.
bool InputHexWord(const char *label, uint16_t *data, float input_width, bool no_step)
bool InputHex(const char *label, uint64_t *data)
void SelectablePalettePipeline(uint64_t &palette_id, bool &refresh_graphics, gfx::SnesPalette &palette)
bool InputHexByte(const char *label, uint8_t *data, float input_width, bool no_step)
std::string HexByte(uint8_t byte, HexStringParams params)
constexpr std::string_view kEntranceNames[]
RoomSize CalculateRoomSize(Rom *rom, int room_id)
constexpr std::string_view kRoomNames[]
Room LoadRoomFromRom(Rom *rom, int room_id)
Main namespace for the application.
void SelectableLabelWithNameEdit(bool selected, const std::string &type, const std::string &key, const std::string &defaultValue)