From 2901c9a486f00c82b70793245801d8a7e307b8a8 Mon Sep 17 00:00:00 2001 From: scawful Date: Thu, 8 May 2025 19:40:29 -0400 Subject: [PATCH] Refactor message handling and improve graphics rendering in MessageEditor - Added SNES header include to message_data.cc for better integration. - Simplified dictionary token formatting in ReadAllTextData function. - Removed unused ImportMessageData function from message_data.h. - Streamlined bitmap updates and rendering in MessageEditor, replacing instances of Renderer::GetInstance() with Renderer::Get(). - Enhanced message preview functionality with improved scrolling and drawing logic. - Adjusted canvas sizes for better layout consistency. --- src/app/editor/message/message_data.cc | 29 +--------- src/app/editor/message/message_data.h | 2 - src/app/editor/message/message_editor.cc | 72 +++++++++++++++--------- src/app/editor/message/message_editor.h | 7 +-- 4 files changed, 51 insertions(+), 59 deletions(-) diff --git a/src/app/editor/message/message_data.cc b/src/app/editor/message/message_data.cc index 7dd0db81..0a326e27 100644 --- a/src/app/editor/message/message_data.cc +++ b/src/app/editor/message/message_data.cc @@ -3,6 +3,7 @@ #include #include +#include "app/snes.h" #include "util/hex.h" #include "util/log.h" @@ -376,11 +377,8 @@ void ReadAllTextData(Rom *rom, std::vector &list_of_texts_) { // Check for dictionary. int dictionary = FindDictionaryEntry(current_byte); if (dictionary >= 0) { - current_raw_message.append("["); - current_raw_message.append(DICTIONARYTOKEN); - current_raw_message.append(":"); - current_raw_message.append(util::HexByte(dictionary)); - current_raw_message.append("]"); + current_raw_message.append(absl::StrFormat("[%s:%s]", DICTIONARYTOKEN, + util::HexByte(dictionary))); uint32_t address = Get24LocalFromPC( rom->mutable_data(), kPointersDictionaries + (dictionary * 2)); @@ -406,26 +404,5 @@ void ReadAllTextData(Rom *rom, std::vector &list_of_texts_) { } } -std::vector ImportMessageData(std::string_view filename) { - std::vector messages; - std::ifstream file(filename.data()); - if (!file.is_open()) { - util::logf("Error opening file: %s", filename); - return messages; - } - - // Parse a file with dialogue IDs and convert - std::string line; - while (std::getline(file, line)) { - if (line.empty()) { - continue; - } - - // Get the Dialogue ID and then read until the next header - } - - return messages; -} - } // namespace editor } // namespace yaze diff --git a/src/app/editor/message/message_data.h b/src/app/editor/message/message_data.h index 74c3a5f5..85979306 100644 --- a/src/app/editor/message/message_data.h +++ b/src/app/editor/message/message_data.h @@ -317,8 +317,6 @@ constexpr int kTextData2End = 0x773FF; // Reads all text data from the ROM and returns a vector of MessageData objects. void ReadAllTextData(Rom *rom, std::vector &list_of_texts_); -std::vector ImportMessageData(std::string_view filename); - } // namespace editor } // namespace yaze diff --git a/src/app/editor/message/message_editor.cc b/src/app/editor/message/message_editor.cc index 2505d974..dc79fe03 100644 --- a/src/app/editor/message/message_editor.cc +++ b/src/app/editor/message/message_editor.cc @@ -56,27 +56,27 @@ void MessageEditor::Initialize() { font_preview_colors_.AddColor(gfx::SnesColor(0x03E0)); // Green font_preview_colors_.AddColor(gfx::SnesColor(0x001F)); // Blue - std::vector data(0x4000, 0); for (int i = 0; i < 0x4000; i++) { - data[i] = rom()->data()[kGfxFont + i]; + raw_font_gfx_data_[i] = rom()->data()[kGfxFont + i]; } - font_gfx16_data_ = gfx::SnesTo8bppSheet(data, /*bpp=*/2, /*num_sheets=*/2); + font_gfx16_data_ = + gfx::SnesTo8bppSheet(raw_font_gfx_data_, /*bpp=*/2, /*num_sheets=*/2); // 4bpp - Renderer::GetInstance().CreateAndRenderBitmap( + Renderer::Get().CreateAndRenderBitmap( kFontGfxMessageSize, kFontGfxMessageSize, kFontGfxMessageDepth, font_gfx16_data_, font_gfx_bitmap_, font_preview_colors_); current_font_gfx16_data_.reserve(kCurrentMessageWidth * kCurrentMessageHeight); - for (int i = 0; i < kCurrentMessageWidth * kCurrentMessageHeight; i++) { - current_font_gfx16_data_.push_back(0); - } + std::fill(current_font_gfx16_data_.begin(), current_font_gfx16_data_.end(), + 0); // 8bpp - Renderer::GetInstance().CreateAndRenderBitmap( - kCurrentMessageWidth, kCurrentMessageHeight, 64, current_font_gfx16_data_, - current_font_gfx16_bitmap_, font_preview_colors_); + Renderer::Get().CreateAndRenderBitmap( + kCurrentMessageWidth, kCurrentMessageHeight, 172, + current_font_gfx16_data_, current_font_gfx16_bitmap_, + font_preview_colors_); *font_gfx_bitmap_.mutable_palette() = font_preview_colors_; *current_font_gfx16_bitmap_.mutable_palette() = font_preview_colors_; @@ -137,6 +137,8 @@ void MessageEditor::DrawMessageList() { PushID(message.ID); if (Button(util::HexWord(message.ID).c_str())) { current_message_ = message; + message_text_box_.text = parsed_messages_[message.ID]; + DrawMessagePreview(); } PopID(); TableNextColumn(); @@ -182,9 +184,6 @@ void MessageEditor::DrawCurrentMessage() { ImGui::BeginChild("##MessagePreview", ImVec2(0, 0), true, 1); Text("Message Preview"); - if (Button("Create Preview")) { - DrawMessagePreview(); - } if (Button("View Palette")) { ImGui::OpenPopup("Palette"); } @@ -193,12 +192,32 @@ void MessageEditor::DrawCurrentMessage() { ImGui::EndPopup(); } gui::BeginPadding(1); - BeginChild("CurrentGfxFont", ImVec2(current_font_gfx16_bitmap_.width(), 0), - true, ImGuiWindowFlags_AlwaysVerticalScrollbar); + BeginChild("CurrentGfxFont", ImVec2(340, 0), true, + ImGuiWindowFlags_AlwaysVerticalScrollbar); current_font_gfx16_canvas_.DrawBackground(); gui::EndPadding(); current_font_gfx16_canvas_.DrawContextMenu(); - current_font_gfx16_canvas_.DrawBitmap(current_font_gfx16_bitmap_, 0, 0); + + // Handle mouse wheel scrolling + if (ImGui::IsWindowHovered()) { + float wheel = ImGui::GetIO().MouseWheel; + if (wheel > 0 && shown_lines_ > 0) { + shown_lines_--; + } else if (wheel < 0 && shown_lines_ < text_line_ - 2) { + shown_lines_++; + } + } + + // Draw only the visible portion of the text + current_font_gfx16_canvas_.DrawBitmap( + current_font_gfx16_bitmap_, ImVec2(0, 0), // Destination position + ImVec2(340, + font_gfx_canvas_.canvas_size().y), // Destination size + ImVec2(0, shown_lines_ * 16), // Source position + ImVec2(170, + font_gfx_canvas_.canvas_size().y / 2) // Source size + ); + current_font_gfx16_canvas_.DrawGrid(); current_font_gfx16_canvas_.DrawOverlay(); EndChild(); @@ -235,8 +254,7 @@ void MessageEditor::DrawSpecialCharacters() { } void MessageEditor::DrawExpandedMessageSettings() { - ImGui::BeginChild("##ExpandedMessageSettings", - ImVec2(0, ImGui::GetWindowContentRegionMax().y / 2), true, + ImGui::BeginChild("##ExpandedMessageSettings", ImVec2(0, 100), true, ImGuiWindowFlags_AlwaysVerticalScrollbar); // Input for the address of the expanded messages ImGui::InputText("Address", &expanded_message_address_, @@ -283,9 +301,7 @@ void MessageEditor::DrawTileToPreview(int x, int y, int srcx, int srcy, int pal, int my = yl; // Formula information to get tile index position in the array. - // ((ID / nbrofXtiles) * (imgwidth/2) + (ID - ((ID/16)*16) )) - int tx = ((draw_id / num_x_tiles) * img_width) + - ((draw_id - ((draw_id / 16) * 16)) * 4); + int tx = ((draw_id / num_x_tiles) * img_width) + ((draw_id & 0xF) << 2); uint8_t pixel = font_gfx16_data_[tx + (yl * 64) + xl]; // nx,ny = object position, xx,yy = tile position, xl,yl = pixel @@ -322,8 +338,9 @@ void MessageEditor::DrawCharacterToPreview(const std::vector& text) { } if (value < 100) { - int srcy = value / 16; - int srcx = value - (value & (~0xF)); + // int srcy = value / 16; + int srcy = value >> 4; + int srcx = value & 0xF; if (text_position_ >= 170) { text_position_ = 0; @@ -376,14 +393,15 @@ void MessageEditor::DrawCharacterToPreview(const std::vector& text) { void MessageEditor::DrawMessagePreview() { // From Parsing. text_line_ = 0; - for (int i = 0; i < kFontGfx16Size; i++) { - current_font_gfx16_data_[i] = 0; - } + std::fill(current_font_gfx16_data_.begin(), current_font_gfx16_data_.end(), + 0); text_position_ = 0; DrawCharacterToPreview(current_message_.Data); shown_lines_ = 0; - Renderer::GetInstance().UpdateBitmap(¤t_font_gfx16_bitmap_); + // Update the bitmap with the new data + current_font_gfx16_bitmap_.mutable_data() = current_font_gfx16_data_; + Renderer::Get().UpdateBitmap(¤t_font_gfx16_bitmap_); } absl::Status MessageEditor::Save() { diff --git a/src/app/editor/message/message_editor.h b/src/app/editor/message/message_editor.h index 0853906c..cae6a378 100644 --- a/src/app/editor/message/message_editor.h +++ b/src/app/editor/message/message_editor.h @@ -22,7 +22,7 @@ constexpr int kNumMessages = 396; constexpr int kCurrentMessageWidth = 172; constexpr int kCurrentMessageHeight = 4096; constexpr int kFontGfxMessageSize = 128; -constexpr int kFontGfxMessageDepth = 8; +constexpr int kFontGfxMessageDepth = 64; constexpr int kFontGfx16Size = 172 * 4096; constexpr uint8_t kWidthArraySize = 100; @@ -85,6 +85,7 @@ class MessageEditor : public Editor { std::array width_array = {0}; std::vector font_gfx16_data_; + std::array raw_font_gfx_data_; std::vector current_font_gfx16_data_; std::vector parsed_messages_; std::vector list_of_texts_; @@ -98,9 +99,7 @@ class MessageEditor : public Editor { gui::Canvas font_gfx_canvas_{"##FontGfxCanvas", ImVec2(128, 128)}; gui::Canvas current_font_gfx16_canvas_{"##CurrentMessageGfx", - ImVec2(172, 4096)}; - gui::Canvas tile_editor_canvas_{"##TileEditorCanvas", ImVec2(256, 256)}; - gui::Canvas tile_preview_canvas_{"##TilePreviewCanvas", ImVec2(64, 64)}; + ImVec2(172 * 2, 4096)}; gui::TextBox message_text_box_;