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++) {
55 if (room_size.room_size_pointer != 0x0A8000) {
60 rooms_[i].LoadRoomGraphics();
64 auto palette_id =
rom()->
ReadWord(0xDEC4B + dungeon_palette_ptr);
65 if (palette_id.status() != absl::OkStatus()) {
68 int p_id = palette_id.value() / 180;
69 auto color = dungeon_man_pal_group[p_id][3];
75 for (
int i = 0; i < 0x07; ++i) {
79 for (
int i = 0; i < 0x85; ++i) {
95 return absl::OkStatus();
104 if (ImGui::BeginTabBar(
"##DungeonEditorTabBar")) {
105 if (ImGui::BeginTabItem(
"Room Editor")) {
109 if (ImGui::BeginTabItem(
"Usage Statistics")) {
118 return absl::OkStatus();
123 [
this](
int block) -> absl::Status {
127 return absl::OkStatus();
132 [
this, &sprites_aux1_pal_group](
int block) -> absl::Status {
136 return absl::OkStatus();
138 return absl::OkStatus();
142 std::map<int, std::vector<int>> rooms_by_bank;
144 int bank = room.second >> 16;
145 rooms_by_bank[bank].push_back(room.second);
149 for (
auto &bank_rooms : rooms_by_bank) {
151 std::sort(bank_rooms.second.begin(), bank_rooms.second.end());
153 for (
size_t i = 0; i < bank_rooms.second.size(); ++i) {
154 int room_ptr = bank_rooms.second[i];
159 [room_ptr](
const auto &entry) {
160 return entry.second == room_ptr;
164 if (room_ptr != 0x0A8000) {
165 if (i < bank_rooms.second.size() - 1) {
168 room_sizes_[room_id] = bank_rooms.second[i + 1] - room_ptr;
171 int bank_end_address = (bank_rooms.first << 16) | 0xFFFF;
172 room_sizes_[room_id] = bank_end_address - room_ptr + 1;
196 TableSetupColumn(
"Room Selector");
197 TableSetupColumn(
"Canvas", ImGuiTableColumnFlags_WidthStretch,
198 ImGui::GetContentRegionAvail().x);
199 TableSetupColumn(
"Object Selector");
204 if (ImGui::BeginTabBar(
"##DungeonRoomTabBar")) {
221 return absl::OkStatus();
225 if (BeginTable(
"DWToolset", 13, ImGuiTableFlags_SizingFixedFit,
227 TableSetupColumn(
"#undoTool");
228 TableSetupColumn(
"#redoTool");
229 TableSetupColumn(
"#separator");
230 TableSetupColumn(
"#anyTool");
232 TableSetupColumn(
"#bg1Tool");
233 TableSetupColumn(
"#bg2Tool");
234 TableSetupColumn(
"#bg3Tool");
235 TableSetupColumn(
"#separator");
236 TableSetupColumn(
"#spriteTool");
237 TableSetupColumn(
"#itemTool");
238 TableSetupColumn(
"#doorTool");
239 TableSetupColumn(
"#blockTool");
281 if (ImGui::IsItemHovered()) {
282 ImGui::SetTooltip(
"Sprites");
289 if (ImGui::IsItemHovered()) {
290 ImGui::SetTooltip(
"Items");
297 if (ImGui::IsItemHovered()) {
298 ImGui::SetTooltip(
"Doors");
305 if (ImGui::IsItemHovered()) {
306 ImGui::SetTooltip(
"Blocks");
319 if (
rom()->is_loaded()) {
323 if (ImGuiID child_id = ImGui::GetID((
void *)(intptr_t)9);
324 BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
325 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
330 each_room_name.data());
331 if (ImGui::IsItemClicked()) {
345using ImGui::Separator;
348 if (
rom()->is_loaded()) {
378 Text(
"Camera Boundaries");
380 Text(
"\t\t\t\t\tNorth East South West");
399 if (BeginChild(
"EntranceSelector", ImVec2(0, 0),
true,
400 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
401 for (
int i = 0; i < 0x85 + 7; i++) {
406 if (ImGui::IsItemClicked()) {
419 static int next_tab_id = 0;
441 ImGuiTabItemFlags_None)) {
482 canvas_.DrawBackground(ImVec2(0x200, 0x200));
492 const auto height = 0x40;
493 const int num_sheets = 0x10;
494 room_gfx_canvas_.DrawBackground(ImVec2(0x100 + 1, num_sheets * height + 1));
499 int current_block = 0;
500 for (
int block : blocks) {
501 int offset = height * (current_block + 1);
503 if (current_block >= 1) {
519 if (BeginTabBar(
"##TabBar", ImGuiTabBarFlags_FittingPolicyScroll)) {
520 if (BeginTabItem(
"Room Graphics")) {
521 if (ImGuiID child_id = ImGui::GetID((
void *)(intptr_t)3);
522 BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
523 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
530 if (BeginTabItem(
"Object Renderer")) {
541 TableSetupColumn(
"Dungeon Objects", ImGuiTableColumnFlags_WidthStretch,
542 ImGui::GetContentRegionAvail().x);
543 TableSetupColumn(
"Canvas");
546 BeginChild(
"DungeonObjectButtons", ImVec2(250, 0),
true);
548 int selected_object = 0;
550 for (
const auto object_name : zelda3::Type1RoomObjectNames) {
551 if (ImGui::Selectable(object_name.data(), selected_object == i)) {
566 BeginChild(
"DungeonObjectCanvas", ImVec2(276, 0x10 * 0x40 + 1),
true);
583 static MemoryEditor mem_edit;
591 for (
const auto &room :
rooms_) {
613 const absl::flat_hash_map<uint16_t, int> &usage_map, uint16_t &selected_set,
614 int spriteset_offset) {
616 std::vector<std::pair<uint16_t, int>> sorted_usage(usage_map.begin(),
618 std::sort(sorted_usage.begin(), sorted_usage.end(),
619 [](
const auto &a,
const auto &b) { return a.first < b.first; });
621 for (
const auto &[set, count] : sorted_usage) {
622 std::string display_str;
623 if (spriteset_offset != 0x00) {
624 display_str = absl::StrFormat(
"%#02x, %#02x: %d", set,
625 (set + spriteset_offset), count);
628 absl::StrFormat(
"%#02x: %d", (set + spriteset_offset), count);
630 if (ImGui::Selectable(display_str.c_str(), selected_set == set)) {
643 int spriteset_offset = 0x00) {
644 std::vector<int> unused_sets;
645 for (
int i = 0; i < max_set; i++) {
646 if (usage_map.find(i) == usage_map.end()) {
647 unused_sets.push_back(i);
650 for (
const auto &set : unused_sets) {
651 if (spriteset_offset != 0x00) {
652 Text(
"%#02x, %#02x", set, (set + spriteset_offset));
661 if (Button(
"Refresh")) {
671 ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
672 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
673 if (BeginTable(
"DungeonUsageStatsTable", 8,
675 ImGui::GetContentRegionAvail())) {
676 TableSetupColumn(
"Blockset Usage");
677 TableSetupColumn(
"Unused Blockset");
678 TableSetupColumn(
"Palette Usage");
679 TableSetupColumn(
"Unused Palette");
680 TableSetupColumn(
"Spriteset Usage");
681 TableSetupColumn(
"Unused Spriteset");
682 TableSetupColumn(
"Usage Grid");
683 TableSetupColumn(
"Group Preview");
685 ImGui::PopStyleVar(2);
688 BeginChild(
"BlocksetUsageScroll", ImVec2(0, 0),
true,
689 ImGuiWindowFlags_HorizontalScrollbar);
694 BeginChild(
"UnusedBlocksetScroll", ImVec2(0, 0),
true,
695 ImGuiWindowFlags_HorizontalScrollbar);
700 BeginChild(
"PaletteUsageScroll", ImVec2(0, 0),
true,
701 ImGuiWindowFlags_HorizontalScrollbar);
706 BeginChild(
"UnusedPaletteScroll", ImVec2(0, 0),
true,
707 ImGuiWindowFlags_HorizontalScrollbar);
713 BeginChild(
"SpritesetUsageScroll", ImVec2(0, 0),
true,
714 ImGuiWindowFlags_HorizontalScrollbar);
719 BeginChild(
"UnusedSpritesetScroll", ImVec2(0, 0),
true,
720 ImGuiWindowFlags_HorizontalScrollbar);
725 BeginChild(
"UsageGrid", ImVec2(0, 0),
true,
726 ImGuiWindowFlags_HorizontalScrollbar);
727 Text(
"%s", absl::StrFormat(
"Total size of all rooms: %d hex format: %#06x",
746 int totalSquares = 296;
747 int squaresWide = 16;
748 int squaresTall = (totalSquares + squaresWide - 1) /
751 for (
int row = 0; row < squaresTall; ++row) {
754 for (
int col = 0; col < squaresWide; ++col) {
756 if (row * squaresWide + col >= totalSquares) {
760 const auto &room =
rooms_[row * squaresWide + col];
765 color.x = color.x / 255;
766 color.y = color.y / 255;
767 color.z = color.z / 255;
769 if (
room_sizes_[row * squaresWide + col] > 0xFFFF) {
770 color = ImVec4(1.0f, 0.0f, 0.0f, 1.0f);
773 color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
775 ImGui::PushStyleColor(ImGuiCol_Button, color);
777 ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
785 ImGui::PushStyleColor(
787 ImVec4(1.0f, 0.5f, 0.0f, 1.0f));
789 if (Button(absl::StrFormat(
"%#x",
room_sizes_[row * squaresWide + col])
796 if (ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
798 absl::StrFormat(
"RoomContextMenu%d", row * squaresWide + col)
801 ImGui::PopStyleColor(2);
806 ImGui::PopStyleColor();
810 if (ImGui::IsItemHovered()) {
812 ImGui::BeginTooltip();
813 Text(
"Room ID: %d", row * squaresWide + col);
814 Text(
"Blockset: %#02x", room.blockset);
815 Text(
"Spriteset: %#02x", room.spriteset);
816 Text(
"Palette: %#02x", room.palette);
817 Text(
"Floor1: %#02x", room.floor1);
818 Text(
"Floor2: %#02x", room.floor2);
819 Text(
"Message ID: %#04x", room.message_id_);
820 Text(
"Size: %#016llx",
room_sizes_[row * squaresWide + col]);
821 Text(
"Size Pointer: %#016llx",
static GraphicsSheetManager & GetInstance()
std::array< gfx::Bitmap, kNumGfxSheets > & gfx_sheets()
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)
Used to update a bitmap on the screen.
void RenderBitmap(gfx::Bitmap *bitmap)
Used to render a bitmap to the screen.
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::vector< zelda3::RoomEntrance > entrances_
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_
std::vector< zelda3::Room > rooms_
void RenderSetUsage(const absl::flat_hash_map< uint16_t, int > &usage_map, uint16_t &selected_set, int spriteset_offset=0x00)
uint16_t selected_spriteset_
std::vector< gfx::Bitmap * > room_gfx_sheets_
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
zelda3::DungeonObjectRenderer object_renderer_
gui::Canvas room_gfx_canvas_
gfx::SnesPalette full_palette_
gui::Canvas object_canvas_
void DrawDungeonTabView()
std::vector< int64_t > room_sizes_
void CalculateUsageStats()
std::vector< int64_t > room_size_pointers_
absl::Status Load() override
std::array< gfx::Bitmap, kNumGfxSheets > graphics_bin_
void DrawObjectRenderer()
ImVector< int > active_rooms_
absl::Status UpdateDungeonRoomView()
absl::Status Undo() override
static Renderer & GetInstance()
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)