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.
This commit is contained in:
scawful
2025-05-08 19:40:29 -04:00
parent fcb6a46bb1
commit 2901c9a486
4 changed files with 51 additions and 59 deletions

View File

@@ -3,6 +3,7 @@
#include <optional> #include <optional>
#include <string> #include <string>
#include "app/snes.h"
#include "util/hex.h" #include "util/hex.h"
#include "util/log.h" #include "util/log.h"
@@ -376,11 +377,8 @@ void ReadAllTextData(Rom *rom, std::vector<MessageData> &list_of_texts_) {
// Check for dictionary. // Check for dictionary.
int dictionary = FindDictionaryEntry(current_byte); int dictionary = FindDictionaryEntry(current_byte);
if (dictionary >= 0) { if (dictionary >= 0) {
current_raw_message.append("["); current_raw_message.append(absl::StrFormat("[%s:%s]", DICTIONARYTOKEN,
current_raw_message.append(DICTIONARYTOKEN); util::HexByte(dictionary)));
current_raw_message.append(":");
current_raw_message.append(util::HexByte(dictionary));
current_raw_message.append("]");
uint32_t address = Get24LocalFromPC( uint32_t address = Get24LocalFromPC(
rom->mutable_data(), kPointersDictionaries + (dictionary * 2)); rom->mutable_data(), kPointersDictionaries + (dictionary * 2));
@@ -406,26 +404,5 @@ void ReadAllTextData(Rom *rom, std::vector<MessageData> &list_of_texts_) {
} }
} }
std::vector<std::string> ImportMessageData(std::string_view filename) {
std::vector<std::string> 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 editor
} // namespace yaze } // namespace yaze

View File

@@ -317,8 +317,6 @@ constexpr int kTextData2End = 0x773FF;
// Reads all text data from the ROM and returns a vector of MessageData objects. // Reads all text data from the ROM and returns a vector of MessageData objects.
void ReadAllTextData(Rom *rom, std::vector<MessageData> &list_of_texts_); void ReadAllTextData(Rom *rom, std::vector<MessageData> &list_of_texts_);
std::vector<std::string> ImportMessageData(std::string_view filename);
} // namespace editor } // namespace editor
} // namespace yaze } // namespace yaze

View File

@@ -56,27 +56,27 @@ void MessageEditor::Initialize() {
font_preview_colors_.AddColor(gfx::SnesColor(0x03E0)); // Green font_preview_colors_.AddColor(gfx::SnesColor(0x03E0)); // Green
font_preview_colors_.AddColor(gfx::SnesColor(0x001F)); // Blue font_preview_colors_.AddColor(gfx::SnesColor(0x001F)); // Blue
std::vector<uint8_t> data(0x4000, 0);
for (int i = 0; i < 0x4000; i++) { 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 // 4bpp
Renderer::GetInstance().CreateAndRenderBitmap( Renderer::Get().CreateAndRenderBitmap(
kFontGfxMessageSize, kFontGfxMessageSize, kFontGfxMessageDepth, kFontGfxMessageSize, kFontGfxMessageSize, kFontGfxMessageDepth,
font_gfx16_data_, font_gfx_bitmap_, font_preview_colors_); font_gfx16_data_, font_gfx_bitmap_, font_preview_colors_);
current_font_gfx16_data_.reserve(kCurrentMessageWidth * current_font_gfx16_data_.reserve(kCurrentMessageWidth *
kCurrentMessageHeight); kCurrentMessageHeight);
for (int i = 0; i < kCurrentMessageWidth * kCurrentMessageHeight; i++) { std::fill(current_font_gfx16_data_.begin(), current_font_gfx16_data_.end(),
current_font_gfx16_data_.push_back(0); 0);
}
// 8bpp // 8bpp
Renderer::GetInstance().CreateAndRenderBitmap( Renderer::Get().CreateAndRenderBitmap(
kCurrentMessageWidth, kCurrentMessageHeight, 64, current_font_gfx16_data_, kCurrentMessageWidth, kCurrentMessageHeight, 172,
current_font_gfx16_bitmap_, font_preview_colors_); current_font_gfx16_data_, current_font_gfx16_bitmap_,
font_preview_colors_);
*font_gfx_bitmap_.mutable_palette() = font_preview_colors_; *font_gfx_bitmap_.mutable_palette() = font_preview_colors_;
*current_font_gfx16_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); PushID(message.ID);
if (Button(util::HexWord(message.ID).c_str())) { if (Button(util::HexWord(message.ID).c_str())) {
current_message_ = message; current_message_ = message;
message_text_box_.text = parsed_messages_[message.ID];
DrawMessagePreview();
} }
PopID(); PopID();
TableNextColumn(); TableNextColumn();
@@ -182,9 +184,6 @@ void MessageEditor::DrawCurrentMessage() {
ImGui::BeginChild("##MessagePreview", ImVec2(0, 0), true, 1); ImGui::BeginChild("##MessagePreview", ImVec2(0, 0), true, 1);
Text("Message Preview"); Text("Message Preview");
if (Button("Create Preview")) {
DrawMessagePreview();
}
if (Button("View Palette")) { if (Button("View Palette")) {
ImGui::OpenPopup("Palette"); ImGui::OpenPopup("Palette");
} }
@@ -193,12 +192,32 @@ void MessageEditor::DrawCurrentMessage() {
ImGui::EndPopup(); ImGui::EndPopup();
} }
gui::BeginPadding(1); gui::BeginPadding(1);
BeginChild("CurrentGfxFont", ImVec2(current_font_gfx16_bitmap_.width(), 0), BeginChild("CurrentGfxFont", ImVec2(340, 0), true,
true, ImGuiWindowFlags_AlwaysVerticalScrollbar); ImGuiWindowFlags_AlwaysVerticalScrollbar);
current_font_gfx16_canvas_.DrawBackground(); current_font_gfx16_canvas_.DrawBackground();
gui::EndPadding(); gui::EndPadding();
current_font_gfx16_canvas_.DrawContextMenu(); 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_.DrawGrid();
current_font_gfx16_canvas_.DrawOverlay(); current_font_gfx16_canvas_.DrawOverlay();
EndChild(); EndChild();
@@ -235,8 +254,7 @@ void MessageEditor::DrawSpecialCharacters() {
} }
void MessageEditor::DrawExpandedMessageSettings() { void MessageEditor::DrawExpandedMessageSettings() {
ImGui::BeginChild("##ExpandedMessageSettings", ImGui::BeginChild("##ExpandedMessageSettings", ImVec2(0, 100), true,
ImVec2(0, ImGui::GetWindowContentRegionMax().y / 2), true,
ImGuiWindowFlags_AlwaysVerticalScrollbar); ImGuiWindowFlags_AlwaysVerticalScrollbar);
// Input for the address of the expanded messages // Input for the address of the expanded messages
ImGui::InputText("Address", &expanded_message_address_, 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; int my = yl;
// Formula information to get tile index position in the array. // 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 & 0xF) << 2);
int tx = ((draw_id / num_x_tiles) * img_width) +
((draw_id - ((draw_id / 16) * 16)) * 4);
uint8_t pixel = font_gfx16_data_[tx + (yl * 64) + xl]; uint8_t pixel = font_gfx16_data_[tx + (yl * 64) + xl];
// nx,ny = object position, xx,yy = tile position, xl,yl = pixel // nx,ny = object position, xx,yy = tile position, xl,yl = pixel
@@ -322,8 +338,9 @@ void MessageEditor::DrawCharacterToPreview(const std::vector<uint8_t>& text) {
} }
if (value < 100) { if (value < 100) {
int srcy = value / 16; // int srcy = value / 16;
int srcx = value - (value & (~0xF)); int srcy = value >> 4;
int srcx = value & 0xF;
if (text_position_ >= 170) { if (text_position_ >= 170) {
text_position_ = 0; text_position_ = 0;
@@ -376,14 +393,15 @@ void MessageEditor::DrawCharacterToPreview(const std::vector<uint8_t>& text) {
void MessageEditor::DrawMessagePreview() { void MessageEditor::DrawMessagePreview() {
// From Parsing. // From Parsing.
text_line_ = 0; text_line_ = 0;
for (int i = 0; i < kFontGfx16Size; i++) { std::fill(current_font_gfx16_data_.begin(), current_font_gfx16_data_.end(),
current_font_gfx16_data_[i] = 0; 0);
}
text_position_ = 0; text_position_ = 0;
DrawCharacterToPreview(current_message_.Data); DrawCharacterToPreview(current_message_.Data);
shown_lines_ = 0; shown_lines_ = 0;
Renderer::GetInstance().UpdateBitmap(&current_font_gfx16_bitmap_); // Update the bitmap with the new data
current_font_gfx16_bitmap_.mutable_data() = current_font_gfx16_data_;
Renderer::Get().UpdateBitmap(&current_font_gfx16_bitmap_);
} }
absl::Status MessageEditor::Save() { absl::Status MessageEditor::Save() {

View File

@@ -22,7 +22,7 @@ constexpr int kNumMessages = 396;
constexpr int kCurrentMessageWidth = 172; constexpr int kCurrentMessageWidth = 172;
constexpr int kCurrentMessageHeight = 4096; constexpr int kCurrentMessageHeight = 4096;
constexpr int kFontGfxMessageSize = 128; constexpr int kFontGfxMessageSize = 128;
constexpr int kFontGfxMessageDepth = 8; constexpr int kFontGfxMessageDepth = 64;
constexpr int kFontGfx16Size = 172 * 4096; constexpr int kFontGfx16Size = 172 * 4096;
constexpr uint8_t kWidthArraySize = 100; constexpr uint8_t kWidthArraySize = 100;
@@ -85,6 +85,7 @@ class MessageEditor : public Editor {
std::array<uint8_t, kWidthArraySize> width_array = {0}; std::array<uint8_t, kWidthArraySize> width_array = {0};
std::vector<uint8_t> font_gfx16_data_; std::vector<uint8_t> font_gfx16_data_;
std::array<uint8_t, 0x4000> raw_font_gfx_data_;
std::vector<uint8_t> current_font_gfx16_data_; std::vector<uint8_t> current_font_gfx16_data_;
std::vector<std::string> parsed_messages_; std::vector<std::string> parsed_messages_;
std::vector<MessageData> list_of_texts_; std::vector<MessageData> list_of_texts_;
@@ -98,9 +99,7 @@ class MessageEditor : public Editor {
gui::Canvas font_gfx_canvas_{"##FontGfxCanvas", ImVec2(128, 128)}; gui::Canvas font_gfx_canvas_{"##FontGfxCanvas", ImVec2(128, 128)};
gui::Canvas current_font_gfx16_canvas_{"##CurrentMessageGfx", gui::Canvas current_font_gfx16_canvas_{"##CurrentMessageGfx",
ImVec2(172, 4096)}; ImVec2(172 * 2, 4096)};
gui::Canvas tile_editor_canvas_{"##TileEditorCanvas", ImVec2(256, 256)};
gui::Canvas tile_preview_canvas_{"##TilePreviewCanvas", ImVec2(64, 64)};
gui::TextBox message_text_box_; gui::TextBox message_text_box_;