5#include <unordered_map>
8#include "absl/status/status.h"
9#include "absl/strings/str_format.h"
27#include "imgui/imgui.h"
28#include "imgui_memory_editor.h"
48 [
this]() { DrawOverworldCanvas(); });
50 [
this]() { status_ = DrawTileSelector(); });
52 if (rom_->is_loaded()) {
53 status_ = UpdateUsageStats();
57 [
this]() { DrawToolset(); });
59 if (rom_->is_loaded()) {
60 status_ = tile16_editor_.Update();
64 if (rom_->is_loaded()) {
65 status_ = gfx_group_editor_.Update();
77 return absl::OkStatus();
93 static bool use_work_area =
true;
94 static ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration |
95 ImGuiWindowFlags_NoMove |
96 ImGuiWindowFlags_NoSavedSettings;
97 const ImGuiViewport *viewport = ImGui::GetMainViewport();
98 ImGui::SetNextWindowPos(use_work_area ? viewport->WorkPos : viewport->Pos);
99 ImGui::SetNextWindowSize(use_work_area ? viewport->WorkSize : viewport->Size);
110 static bool show_gfx_group =
false;
111 static bool show_properties =
false;
190 std::vector<uint8_t> png_data;
192 if (png_data.size() > 0) {
196 "Failed to convert overworld map surface to PNG");
206 [&]() { Checkbox(
"Properties", &show_properties); });
214 ImGuiWindowFlags_MenuBar);
219 if (show_gfx_group) {
225 if (show_properties) {
226 ImGui::Begin(
"Properties", &show_properties);
232 if (!ImGui::IsAnyItemActive()) {
233 if (ImGui::IsKeyDown(ImGuiKey_1)) {
235 }
else if (ImGui::IsKeyDown(ImGuiKey_2)) {
237 }
else if (ImGui::IsKeyDown(ImGuiKey_3)) {
239 }
else if (ImGui::IsKeyDown(ImGuiKey_4)) {
241 }
else if (ImGui::IsKeyDown(ImGuiKey_5)) {
243 }
else if (ImGui::IsKeyDown(ImGuiKey_6)) {
245 }
else if (ImGui::IsKeyDown(ImGuiKey_7)) {
247 }
else if (ImGui::IsKeyDown(ImGuiKey_8)) {
254 "##WorldId",
"##GfxId",
"##PalId",
"##SprGfxId",
255 "##5thCol",
"##6thCol",
"##7thCol",
"##8thCol"};
260 ImGui::TableSetupColumn(name);
263 ImGui::SetNextItemWidth(120.f);
270 ->mutable_area_graphics(),
281 ->mutable_area_palette(),
314 ImGui::SetNextItemWidth(100.f);
321 HOVER_HINT(
"Enable Mosaic effect for the current map");
330 ImGui::TableSetupColumn(name);
333 ImGui::SetNextItemWidth(120.f);
336 static const std::array<std::string, 8> kCustomMapSettingsColumnNames = {
337 "TileGfx0",
"TileGfx1",
"TileGfx2",
"TileGfx3",
338 "TileGfx4",
"TileGfx5",
"TileGfx6",
"TileGfx7"};
339 for (
int i = 0; i < 8; ++i) {
344 ->mutable_custom_tileset(i),
356 ->mutable_area_palette(),
389 ImGui::SetNextItemWidth(100.f);
396 HOVER_HINT(
"Enable Mosaic effect for the current map");
405 for (
int i = 0; i < 0x40; i++) {
438 int mouse_x = mouse_position.x;
439 int mouse_y = mouse_position.y;
445 auto &selected_world =
448 :
overworld_.mutable_map_tiles()->special_world;
450 int index_x = superX * 32 + tile16_x;
451 int index_y = superY * 32 + tile16_y;
457 const ImVec2 &click_position,
const std::vector<uint8_t> &tile_data) {
465 ImVec2 start_position;
495 if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) ||
496 ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
497 auto &selected_world =
501 :
overworld_.mutable_map_tiles()->special_world;
512 if (start_x > end_x) std::swap(start_x, end_x);
513 if (start_y > end_y) std::swap(start_y, end_y);
515 constexpr int local_map_size = 512;
517 constexpr int tiles_per_local_map = local_map_size /
kTile16Size;
519 for (
int y = start_y, i = 0; y <= end_y; y +=
kTile16Size) {
520 for (
int x = start_x; x <= end_x; x +=
kTile16Size, ++i) {
522 int local_map_x = x / local_map_size;
523 int local_map_y = y / local_map_size;
530 int index_x = local_map_x * tiles_per_local_map + tile16_x;
531 int index_y = local_map_y * tiles_per_local_map + tile16_y;
532 int tile16_id =
overworld_.GetTileFromPosition(
534 selected_world[index_x][index_y] = tile16_id;
553 static std::vector<int> tile16_ids;
556 if (tile16_ids.size() != 0) {
562 tile16_ids.push_back(
overworld_.GetTileFromPosition(each));
572 const auto mouse_position = ImGui::GetIO().MousePos;
573 const int large_map_size = 1024;
593 const int highlight_parent =
594 overworld_.overworld_map(current_highlighted_map)->parent();
595 const int parent_map_x = highlight_parent % 8;
596 const int parent_map_y = highlight_parent / 8;
601 const int current_map_x = current_highlighted_map % 8;
602 const int current_map_y = current_highlighted_map / 8;
609 ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
616 return absl::OkStatus();
620 if (ImGui::IsMouseDragging(ImGuiMouseButton_Middle)) {
626 if (ImGui::IsMouseReleased(ImGuiMouseButton_Middle) &&
674 if (ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows) &&
675 ImGui::IsMouseDragging(ImGuiMouseButton_Middle)) {
676 ImGui::SetScrollX(ImGui::GetScrollX() + ImGui::GetIO().MouseWheelH * 16.0f);
677 ImGui::SetScrollY(ImGui::GetScrollY() + ImGui::GetIO().MouseWheel * 16.0f);
695 int grid_x =
static_cast<int>(tile_pos.x / 32);
696 int grid_y =
static_cast<int>(tile_pos.y / 32);
697 int id = grid_x + grid_y * 8;
713 return absl::OkStatus();
722 int offset = 0x40 * (key + 1);
727 auto texture = value.texture();
729 (ImTextureID)(intptr_t)texture,
767 return absl::OkStatus();
772 ImGuiTabBarFlags_FittingPolicyScroll)) {
773 if (BeginTabItem(
"Tile16")) {
777 if (BeginTabItem(
"Tile8")) {
785 if (BeginTabItem(
"Area Graphics")) {
791 return absl::OkStatus();
800 auto color = ImVec4(255, 255, 0, 100);
802 color = ImVec4(255, 255, 255, 200);
812 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
817 ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
830 auto deleted_entrance_id =
overworld_.deleted_entrances().back();
832 auto &entrance =
overworld_.entrances()[deleted_entrance_id];
834 entrance.entrance_id_ = deleted_entrance_id;
837 entrance.deleted =
false;
841 const auto is_hovering =
844 if (!is_hovering && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
845 ImGui::OpenPopup(
"Entrance Inserter");
853 overworld_.mutable_deleted_entrances()->emplace_back(
862 for (
auto &each : *
overworld_.mutable_exits()) {
866 ImVec4(255, 255, 255, 150));
874 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
879 ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
884 ImGui::OpenPopup(
"Exit editor");
900 if (!hovering && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
901 ImGui::OpenPopup(
"Exit Inserter");
913 for (
auto &item : *
overworld_.mutable_all_items()) {
917 ow_map_canvas_.DrawRect(item.x_, item.y_, 16, 16, ImVec4(255, 0, 0, 150));
927 if (hovering && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
933 std::string item_name =
"";
937 item_name = absl::StrFormat(
"0x%02X", item.id_);
950 if (!hovering && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
951 ImGui::OpenPopup(
"Item Inserter");
964 if (!sprite.deleted()) {
965 int map_x = sprite.map_x();
966 int map_y = sprite.map_y();
968 ImVec4(255, 0, 255, 150));
975 ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
987 ow_map_canvas_.DrawText(absl::StrFormat(
"%s", sprite.name()), map_x,
999 if (!hovering && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
1000 ImGui::OpenPopup(
"Sprite Inserter");
1030 return absl::OkStatus();
1034 util::logf(
"Loading overworld.");
1039 util::logf(
"Loading overworld graphics.");
1045 util::logf(
"Loading overworld tileset.");
1048 0x80, 0x2000, 0x08,
overworld_.tile16_blockset_data(),
1053 auto tile16_blockset_data =
overworld_.tile16_blockset_data();
1054 util::logf(
"Loading overworld tile16 graphics.");
1060 util::logf(
"Loading overworld maps.");
1064 auto palette =
overworld_.current_area_palette();
1069 }
catch (
const std::bad_alloc &e) {
1070 std::cout <<
"Error: " << e.what() << std::endl;
1079 return absl::OkStatus();
1084 const int depth = 0x10;
1085 for (
int i = 0; i < 3; i++)
1086 for (
auto const &sprite : *
overworld_.mutable_sprites(i)) {
1087 int width = sprite.width();
1088 int height = sprite.height();
1089 if (width == 0 || height == 0) {
1096 *sprite.preview_graphics());
1100 return absl::OkStatus();
1104 overworld_.mutable_overworld_map(map_index)->LoadAreaGraphics();
1113 overworld_.mutable_overworld_map(map_index)->bitmap_data());
1114 maps_bmp_[map_index].set_modified(
true);
1119 std::vector<std::future<void>> futures;
1122 auto refresh_map_async = [
this](
int map_index) {
1131 for (
int i = 1; i < 4; i++) {
1132 int sibling_index =
overworld_.overworld_map(source_map_id)->parent() + i;
1133 if (i >= 2) sibling_index += 6;
1135 std::async(std::launch::async, refresh_map_async, sibling_index));
1136 indices[i] = sibling_index;
1139 indices[0] = source_map_id;
1141 std::async(std::launch::async, refresh_map_async, source_map_id));
1143 for (
auto &each : futures) {
1147 int n = is_large ? 4 : 1;
1149 for (
int i = 0; i < n; ++i) {
1157 const auto current_map_palette =
overworld_.current_area_palette();
1161 for (
int i = 1; i < 4; i++) {
1163 if (i >= 2) sibling_index += 6;
1165 overworld_.mutable_overworld_map(sibling_index)->LoadPalette());
1166 maps_bmp_[sibling_index].SetPalette(current_map_palette);
1171 return absl::OkStatus();
1176 if (current_ow_map.is_large_map()) {
1178 for (
int i = 1; i < 4; i++) {
1179 int sibling_index = current_ow_map.parent() + i;
1183 auto &map = *
overworld_.mutable_overworld_map(sibling_index);
1184 map.set_area_graphics(current_ow_map.area_graphics());
1185 map.set_area_palette(current_ow_map.area_palette());
1190 map.set_message_id(current_ow_map.message_id());
1198 return absl::OkStatus();
1205 const auto tile16_data =
overworld_.tile16_blockset_data();
1209 return absl::OkStatus();
1213 static bool init_properties =
false;
1215 if (!init_properties) {
1216 for (
int i = 0; i < 0x40; i++) {
1217 std::string area_graphics_str = absl::StrFormat(
1218 "%02hX",
overworld_.overworld_map(i)->area_graphics());
1220 ->push_back(area_graphics_str);
1222 area_graphics_str = absl::StrFormat(
1223 "%02hX",
overworld_.overworld_map(i + 0x40)->area_graphics());
1225 ->push_back(area_graphics_str);
1227 std::string area_palette_str =
1228 absl::StrFormat(
"%02hX",
overworld_.overworld_map(i)->area_palette());
1230 ->push_back(area_palette_str);
1232 area_palette_str = absl::StrFormat(
1233 "%02hX",
overworld_.overworld_map(i + 0x40)->area_palette());
1235 ->push_back(area_palette_str);
1236 std::string sprite_gfx_str = absl::StrFormat(
1237 "%02hX",
overworld_.overworld_map(i)->sprite_graphics(1));
1239 ->push_back(sprite_gfx_str);
1241 sprite_gfx_str = absl::StrFormat(
1242 "%02hX",
overworld_.overworld_map(i)->sprite_graphics(2));
1244 ->push_back(sprite_gfx_str);
1246 sprite_gfx_str = absl::StrFormat(
1247 "%02hX",
overworld_.overworld_map(i + 0x40)->sprite_graphics(1));
1249 ->push_back(sprite_gfx_str);
1251 sprite_gfx_str = absl::StrFormat(
1252 "%02hX",
overworld_.overworld_map(i + 0x40)->sprite_graphics(2));
1254 ->push_back(sprite_gfx_str);
1256 std::string sprite_palette_str = absl::StrFormat(
1257 "%02hX",
overworld_.overworld_map(i)->sprite_palette(1));
1259 ->push_back(sprite_palette_str);
1261 sprite_palette_str = absl::StrFormat(
1262 "%02hX",
overworld_.overworld_map(i)->sprite_palette(2));
1264 ->push_back(sprite_palette_str);
1266 sprite_palette_str = absl::StrFormat(
1267 "%02hX",
overworld_.overworld_map(i + 0x40)->sprite_palette(1));
1269 ->push_back(sprite_palette_str);
1271 sprite_palette_str = absl::StrFormat(
1272 "%02hX",
overworld_.overworld_map(i + 0x40)->sprite_palette(2));
1274 ->push_back(sprite_palette_str);
1276 init_properties =
true;
1279 Text(
"Area Gfx LW/DW");
1287 Text(
"Sprite Gfx LW/DW");
1301 Text(
"Area Pal LW/DW");
1308 static bool show_gfx_group =
false;
1309 Checkbox(
"Show Gfx Group Editor", &show_gfx_group);
1310 if (show_gfx_group) {
1318 if (BeginTable(
"UsageStatsTable", 3,
kOWEditFlags, ImVec2(0, 0))) {
1319 TableSetupColumn(
"Entrances");
1320 TableSetupColumn(
"Grid", ImGuiTableColumnFlags_WidthStretch,
1321 ImGui::GetContentRegionAvail().x);
1322 TableSetupColumn(
"Usage", ImGuiTableColumnFlags_WidthFixed, 256);
1327 if (BeginChild(
"UnusedSpritesetScroll", ImVec2(0, 0),
true,
1328 ImGuiWindowFlags_HorizontalScrollbar)) {
1329 for (
int i = 0; i < 0x81; i++) {
1330 auto entrance_name =
rom_->resource_label()->CreateOrGetLabel(
1333 std::string str = absl::StrFormat(
"%#x - %s", i, entrance_name);
1336 ? ImGuiSelectableFlags_Disabled
1342 if (IsItemHovered()) {
1344 Text(
"Entrance ID: %d", i);
1345 Text(
"Map ID: %d",
overworld_.entrances().at(i).map_id_);
1346 Text(
"Entrance ID: %d",
overworld_.entrances().at(i).entrance_id_);
1347 Text(
"X: %d",
overworld_.entrances().at(i).x_);
1348 Text(
"Y: %d",
overworld_.entrances().at(i).y_);
1350 overworld_.entrances().at(i).deleted ?
"Yes" :
"No");
1365 return absl::OkStatus();
1370 int total_squares = 128;
1371 int squares_wide = 8;
1372 int squares_tall = (total_squares + squares_wide - 1) /
1376 for (
int row = 0; row < squares_tall; ++row) {
1379 for (
int col = 0; col < squares_wide; ++col) {
1380 if (row * squares_wide + col >= total_squares) {
1388 PushStyleColor(ImGuiCol_Button,
1389 ImVec4(1.0f, 0.5f, 0.0f,
1394 if (Button(
"##square", ImVec2(20, 20))) {
1406 if (IsItemHovered()) {
1419 int relative_x = (int)
ow_map_canvas_.drawn_tile_position().x % 512;
1420 int relative_y = (int)
ow_map_canvas_.drawn_tile_position().y % 512;
1421 Text(
"Current Tile16 Drawn Position (Relative): %d, %d", relative_x,
1425 Text(
"Light World Map Tiles: %d",
1426 (
int)
overworld_.mutable_map_tiles()->light_world.size());
1427 Text(
"Dark World Map Tiles: %d",
1428 (
int)
overworld_.mutable_map_tiles()->dark_world.size());
1429 Text(
"Special World Map Tiles: %d",
1430 (
int)
overworld_.mutable_map_tiles()->special_world.size());
1432 static bool view_lw_map_tiles =
false;
1433 static MemoryEditor mem_edit;
1435 if (Button(
"View Light World Map Tiles")) {
1436 view_lw_map_tiles = !view_lw_map_tiles;
1439 if (view_lw_map_tiles) {
1440 mem_edit.DrawContents(
1451 return absl::OkStatus();
absl::Status Clear() override
gui::zeml::Node layout_node_
zelda3::OverworldItem current_item_
bool overworld_canvas_fullscreen_
bool map_blockset_loaded_
absl::Status UpdateUsageStats()
zelda3::OverworldEntranceTileTypes entrance_tiletypes_
zelda3::OverworldEntrance current_entrance_
zelda3::Sprite current_sprite_
absl::Status CheckForCurrentMap()
Check for changes to the overworld map. Calls RefreshOverworldMap and RefreshTile16Blockset on the cu...
void DrawOverworldEdits()
gfx::Bitmap current_gfx_bmp_
gfx::Tilemap tile16_blockset_
bool middle_mouse_dragging_
Tile16Editor tile16_editor_
gui::Canvas ow_map_canvas_
void DrawOverworldExits(ImVec2 zero, ImVec2 scrolling)
absl::Status Undo() override
void DrawOverworldItems()
zelda3::GameEntity * dragged_entity_
std::array< gfx::Bitmap, zelda3::kNumOverworldMaps > maps_bmp_
void RefreshOverworldMap()
void CheckForOverworldEdits()
Check for changes to the overworld map.
zelda3::OverworldExit current_exit_
void RefreshMapProperties()
void RenderUpdatedMapBitmap(const ImVec2 &click_position, const std::vector< uint8_t > &tile_data)
gui::Canvas current_gfx_canvas_
void DrawCustomOverworldMapSettings()
absl::Status DrawTileSelector()
void Initialize() override
absl::Status Redo() override
void DrawFullscreenCanvas()
void DrawOverworldProperties()
absl::Status RefreshMapPalette()
void DrawOverworldMapSettings()
void DrawOverworldCanvas()
gui::Canvas blockset_canvas_
absl::Status RefreshTile16Blockset()
void CheckForSelectRectangle()
Draw and create the tile16 IDs that are currently selected.
void RefreshChildMap(int i)
absl::Status Load() override
std::vector< gfx::Bitmap > sprite_previews_
absl::Status DrawAreaGraphics()
GfxGroupEditor gfx_group_editor_
absl::Status Update() final
void DrawOverworldSprites()
EditingMode previous_mode
gui::Table toolset_table_
gui::Canvas graphics_bin_canvas_
gfx::BitmapTable current_graphics_set_
zelda3::Overworld overworld_
absl::Status LoadGraphics()
Load the Bitmap objects for each OverworldMap.
absl::Status LoadSpriteGraphics()
absl::Status Save() override
void DrawOverworldEntrances(ImVec2 canvas_p, ImVec2 scrolling, bool holes=false)
zelda3::GameEntity * current_entity_
gui::Canvas properties_canvas_
gfx::SnesPalette palette_
gfx::Bitmap tile16_blockset_bmp_
absl::Status DrawTile16Selector()
Represents a bitmap image.
void WriteToPixel(int position, uint8_t value)
Write a value to a pixel at the given position.
void set_modified(bool modified)
#define ICON_MD_GRID_VIEW
#define ICON_MD_MORE_VERT
#define ICON_MD_OPEN_IN_FULL
#define ICON_MD_TABLE_CHART
#define ICON_MD_DOOR_BACK
#define ICON_MD_MUSIC_NOTE
#define ICON_MD_DOOR_FRONT
#define ICON_MD_ADD_LOCATION
#define ICON_MD_PEST_CONTROL_RODENT
#define ICON_MD_CONTENT_COPY
#define ICON_MD_PAN_TOOL_ALT
#define PRINT_IF_ERROR(expression)
#define RETURN_IF_ERROR(expression)
#define ASSIGN_OR_RETURN(type_variable_name, expression)
#define HOVER_HINT(string)
void CopyImageToClipboard(const std::vector< uint8_t > &data)
Editors are the view controllers for the application.
void DrawExitInserterPopup()
constexpr absl::string_view kOWMapTable
constexpr ImGuiTableFlags kOWMapFlags
constexpr unsigned int kOverworldMapSize
bool DrawSpriteEditorPopup(zelda3::Sprite &sprite)
void DrawItemInsertPopup()
bool DrawEntranceInserterPopup()
void DrawSpriteInserterPopup()
constexpr absl::string_view kWorldList
constexpr absl::string_view kGamePartComboString
void HandleEntityDragging(zelda3::GameEntity *entity, ImVec2 canvas_p0, ImVec2 scrolling, bool &is_dragging_entity, zelda3::GameEntity *&dragged_entity, zelda3::GameEntity *¤t_entity, bool free_movement)
bool DrawOverworldEntrancePopup(zelda3::OverworldEntrance &entrance)
bool IsMouseHoveringOverEntity(const zelda3::GameEntity &entity, ImVec2 canvas_p0, ImVec2 scrolling)
absl::Status DisplayPalette(gfx::SnesPalette &palette, bool loaded)
bool DrawItemEditorPopup(zelda3::OverworldItem &item)
constexpr int kTile16Size
constexpr ImGuiTableFlags kOWEditFlags
constexpr float kInputFieldSize
constexpr absl::string_view kTileSelectorTab
constexpr std::array< const char *, 8 > kMapSettingsColumnNames
bool DrawExitEditorPopup(zelda3::OverworldExit &exit)
Tilemap CreateTilemap(std::vector< uint8_t > &data, int width, int height, int tile_size, int num_tiles, SnesPalette &palette)
void UpdateTilemap(Tilemap &tilemap, const std::vector< uint8_t > &data)
std::vector< uint8_t > GetTilemapData(Tilemap &tilemap, int tile_id)
void Render(Node &node)
Render a zeml tree.
std::string LoadFile(const std::string &filename)
void Bind(Node *node, std::function< void()> callback)
Bind a callback to a node.
Node Parse(const std::string &yazon_input, const std::map< std::string, void * > &data_bindings)
Parse a zeml string.
void BeginChildBothScrollbars(int id)
bool InputHexWord(const char *label, uint16_t *data, float input_width, bool no_step)
void AddTableColumn(Table &table, const std::string &label, GuiElement element)
void DrawTable(Table ¶ms)
void EndWindowWithDisplaySettings()
void BeginWindowWithDisplaySettings(const char *id, bool *active, const ImVec2 &size, ImGuiWindowFlags flags)
bool InputHexByte(const char *label, uint8_t *data, float input_width, bool no_step)
void BeginChildWithScrollbar(const char *str_id)
std::string HexByte(uint8_t byte, HexStringParams params)
constexpr std::string_view kEntranceNames[]
constexpr int kNumTile16Individual
constexpr int kNumOverworldMaps
const std::vector< std::string > kSecretItemNames
absl::StatusOr< OverworldEntranceTileTypes > LoadEntranceTileTypes(Rom *rom)
Main namespace for the application.