From 7c69ce016c6e56b64251c9d14084b6a1c3458a24 Mon Sep 17 00:00:00 2001 From: scawful Date: Fri, 9 Aug 2024 20:59:20 -0400 Subject: [PATCH] cleanup message editor --- src/app/editor/message/message_data.h | 3 + src/app/editor/message/message_editor.cc | 111 ++++++++++++----------- src/app/editor/message/message_editor.h | 50 +++++----- 3 files changed, 85 insertions(+), 79 deletions(-) diff --git a/src/app/editor/message/message_data.h b/src/app/editor/message/message_data.h index 9b1422a3..9b42e729 100644 --- a/src/app/editor/message/message_data.h +++ b/src/app/editor/message/message_data.h @@ -140,6 +140,9 @@ struct TextElement { } bool Empty() { return ID == 0; } + + // Comparison operator + bool operator==(const TextElement& other) const { return ID == other.ID; } }; struct ParsedElement { diff --git a/src/app/editor/message/message_editor.cc b/src/app/editor/message/message_editor.cc index 0e350cd9..79849691 100644 --- a/src/app/editor/message/message_editor.cc +++ b/src/app/editor/message/message_editor.cc @@ -46,7 +46,7 @@ using ImGui::Text; using ImGui::TextWrapped; using ImGui::TreeNode; -static ParsedElement FindMatchingElement(std::string str) { +ParsedElement MessageEditor::FindMatchingElement(std::string str) { std::smatch match; for (auto& textElement : TextCommands) { match = textElement.MatchMe(str); @@ -60,9 +60,12 @@ static ParsedElement FindMatchingElement(std::string str) { } } - match = DictionaryElement.MatchMe(str); + const auto dictionary_element = + TextElement(0x80, DICTIONARYTOKEN, true, "Dictionary"); + + match = dictionary_element.MatchMe(str); if (match.size() > 0) { - return ParsedElement(DictionaryElement, + return ParsedElement(dictionary_element, DICTOFF + std::stoi(match[1].str(), nullptr, 16)); } return ParsedElement(); @@ -79,25 +82,30 @@ std::string ReplaceAllDictionaryWords(std::string str) { return temp; } -std::vector ParseMessageToData(std::string str) { +std::vector MessageEditor::ParseMessageToData(std::string str) { std::vector bytes; - std::string tempString = str; + std::string temp_string = str; int pos = 0; - while (pos < tempString.size()) { + while (pos < temp_string.size()) { // Get next text fragment. - if (tempString[pos] == '[') { - int next = tempString.find(']', pos); + if (temp_string[pos] == '[') { + int next = temp_string.find(']', pos); if (next == -1) { break; } ParsedElement parsedElement = - FindMatchingElement(tempString.substr(pos, next - pos + 1)); + FindMatchingElement(temp_string.substr(pos, next - pos + 1)); + + const auto dictionary_element = + TextElement(0x80, DICTIONARYTOKEN, true, "Dictionary"); + if (!parsedElement.Active) { - break; // TODO: handle badness. - // } else if (parsedElement.Parent == DictionaryElement) { - // bytes.push_back(parsedElement.Value); + core::logf("Error parsing message: %s", temp_string); + break; + } else if (parsedElement.Parent == dictionary_element) { + bytes.push_back(parsedElement.Value); } else { bytes.push_back(parsedElement.Parent.ID); @@ -109,10 +117,10 @@ std::vector ParseMessageToData(std::string str) { pos = next + 1; continue; } else { - uint8_t bb = MessageEditor::FindMatchingCharacter(tempString[pos++]); + uint8_t bb = MessageEditor::FindMatchingCharacter(temp_string[pos++]); if (bb != 0xFF) { - // TODO: handle badness. + core::logf("Error parsing message: %s", temp_string); bytes.push_back(bb); } } @@ -124,7 +132,7 @@ std::vector ParseMessageToData(std::string str) { absl::Status MessageEditor::Update() { if (rom()->is_loaded() && !data_loaded_) { RETURN_IF_ERROR(Initialize()); - CurrentMessage = ListOfTexts[1]; + current_message_ = list_of_texts_[1]; data_loaded_ = true; } @@ -153,10 +161,10 @@ absl::Status MessageEditor::Update() { void MessageEditor::DrawMessageList() { if (InputText("Search", &search_text_)) { - DisplayedMessages.clear(); - for (const auto& message : ListOfTexts) { + displayed_messages_.clear(); + for (const auto& message : list_of_texts_) { if (absl::StrContains(message.ContentsParsed, search_text_)) { - DisplayedMessages.push_back(message); + displayed_messages_.push_back(message); } } } @@ -172,18 +180,18 @@ void MessageEditor::DrawMessageList() { TableHeadersRow(); - for (const auto& message : ListOfTexts) { + for (const auto& message : list_of_texts_) { TableNextColumn(); if (Button(core::UppercaseHexWord(message.ID).c_str())) { - CurrentMessage = message; + current_message_ = message; DrawMessagePreview(); } TableNextColumn(); - TextWrapped("%s", ParsedMessages[message.ID].c_str()); + TextWrapped("%s", parsed_messages_[message.ID].c_str()); TableNextColumn(); TextWrapped( "%s", - core::UppercaseHexLong(ListOfTexts[message.ID].Address).c_str()); + core::UppercaseHexLong(list_of_texts_[message.ID].Address).c_str()); } EndTable(); @@ -193,10 +201,11 @@ void MessageEditor::DrawMessageList() { } void MessageEditor::DrawCurrentMessage() { - Button(absl::StrCat("Message ", CurrentMessage.ID).c_str()); - if (InputTextMultiline("##MessageEditor", &ParsedMessages[CurrentMessage.ID], + Button(absl::StrCat("Message ", current_message_.ID).c_str()); + if (InputTextMultiline("##MessageEditor", + &parsed_messages_[current_message_.ID], ImVec2(ImGui::GetContentRegionAvail().x, 0))) { - CurrentMessage.Data = ParseMessageToData(message_text_box_.text); + current_message_.Data = ParseMessageToData(message_text_box_.text); DrawMessagePreview(); } Separator(); @@ -260,11 +269,11 @@ absl::Status MessageEditor::Initialize() { for (int i = 0; i < 0x4000; i++) { data[i] = rom()->data()[kGfxFont + i]; } - font_gfx16_data = gfx::SnesTo8bppSheet(data, /*bpp=*/2); + font_gfx16_data_ = gfx::SnesTo8bppSheet(data, /*bpp=*/2); // 4bpp RETURN_IF_ERROR(Renderer::GetInstance().CreateAndRenderBitmap( - 128, 128, 8, font_gfx16_data, font_gfx_bitmap_, font_preview_colors_)) + 128, 128, 8, font_gfx16_data_, font_gfx_bitmap_, font_preview_colors_)) current_font_gfx16_data_.reserve(172 * 4096); for (int i = 0; i < 172 * 4096; i++) { @@ -283,11 +292,11 @@ absl::Status MessageEditor::Initialize() { *font_gfx_bitmap_.mutable_palette() = color_palette; - for (const auto& message : ListOfTexts) { - DisplayedMessages.push_back(message); + for (const auto& message : list_of_texts_) { + displayed_messages_.push_back(message); } - for (const auto& each_message : ListOfTexts) { + for (const auto& each_message : list_of_texts_) { // Each string has a [:XX] char encoded // The corresponding character is found in CharEncoder unordered_map std::string parsed_message = ""; @@ -316,7 +325,7 @@ absl::Status MessageEditor::Initialize() { } } } - ParsedMessages.push_back(parsed_message); + parsed_messages_.push_back(parsed_message); } DrawMessagePreview(); @@ -370,7 +379,7 @@ void MessageEditor::ReadAllTextData() { MessageData(message_id++, pos, current_message_raw, temp_bytes_raw, current_message_parsed, temp_bytes_parsed); - ListOfTexts.push_back(message); + list_of_texts_.push_back(message); temp_bytes_raw.clear(); temp_bytes_parsed.clear(); @@ -536,7 +545,7 @@ void MessageEditor::DrawTileToPreview(int x, int y, int srcx, int srcy, int pal, // Formula information to get tile index position in the array. // ((ID / nbrofXtiles) * (imgwidth/2) + (ID - ((ID/16)*16) )) int tx = ((drawid / 16) * 512) + ((drawid - ((drawid / 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 // position @@ -575,25 +584,25 @@ void MessageEditor::DrawCharacterToPreview(const std::vector& text) { int srcy = value / 16; int srcx = value - (value & (~0xF)); - if (text_pos >= 170) { - text_pos = 0; - text_line++; + if (text_position_ >= 170) { + text_position_ = 0; + text_line_++; } - DrawTileToPreview(text_pos, text_line * 16, srcx, srcy, 0, 1, 2); - text_pos += width_array[value]; + DrawTileToPreview(text_position_, text_line_ * 16, srcx, srcy, 0, 1, 2); + text_position_ += width_array[value]; } else if (value == kLine1) { - text_pos = 0; - text_line = 0; + text_position_ = 0; + text_line_ = 0; } else if (value == kScrollVertical) { - text_pos = 0; - text_line += 1; + text_position_ = 0; + text_line_ += 1; } else if (value == kLine2) { - text_pos = 0; - text_line = 1; + text_position_ = 0; + text_line_ = 1; } else if (value == kLine3) { - text_pos = 0; - text_line = 2; + text_position_ = 0; + text_line_ = 2; } else if (value == 0x6B || value == 0x6D || value == 0x6E || value == 0x77 || value == 0x78 || value == 0x79 || value == 0x7A) { @@ -619,13 +628,13 @@ void MessageEditor::DrawCharacterToPreview(const std::vector& text) { void MessageEditor::DrawMessagePreview() // From Parsing. { - text_line = 0; + text_line_ = 0; for (int i = 0; i < (172 * 4096); i++) { current_font_gfx16_data_[i] = 0; } - text_pos = 0; - DrawCharacterToPreview(CurrentMessage.Data); - shown_lines = 0; + text_position_ = 0; + DrawCharacterToPreview(current_message_.Data); + shown_lines_ = 0; } absl::Status MessageEditor::Cut() { @@ -677,7 +686,7 @@ absl::Status MessageEditor::Save() { int pos = kTextData; bool in_second_bank = false; - for (const auto& message : ListOfTexts) { + for (const auto& message : list_of_texts_) { for (const auto value : message.Data) { RETURN_IF_ERROR(rom()->Write(pos, value)); diff --git a/src/app/editor/message/message_editor.h b/src/app/editor/message/message_editor.h index 35330084..14e5bc1a 100644 --- a/src/app/editor/message/message_editor.h +++ b/src/app/editor/message/message_editor.h @@ -40,10 +40,6 @@ const uint8_t BANKID = 0x80; constexpr uint8_t kBlockTerminator = 0x80; -std::vector ParseMessageToData(std::string str); - -static ParsedElement FindMatchingElement(std::string str); - constexpr uint8_t kScrollVertical = 0x73; constexpr uint8_t kLine1 = 0x74; constexpr uint8_t kLine2 = 0x75; @@ -184,6 +180,9 @@ static TextElement DictionaryElement = class MessageEditor : public Editor, public SharedRom { public: + static std::vector ParseMessageToData(std::string str); + static ParsedElement FindMatchingElement(std::string str); + struct DictionaryEntry { uint8_t ID; std::string Contents; @@ -237,7 +236,6 @@ class MessageEditor : public Editor, public SharedRom { absl::Status Save(); void Delete(); void SelectAll(); - // void RegisterTests(ImGuiTestEngine* e) override; TextElement FindMatchingCommand(uint8_t byte); TextElement FindMatchingSpecial(uint8_t value); @@ -261,40 +259,36 @@ class MessageEditor : public Editor, public SharedRom { static const std::vector AllDicts; - uint8_t width_array[100]; - std::string romname = ""; - - int text_line = 0; - int text_pos = 0; - int shown_lines = 0; - int selected_tile = 0; - + private: bool skip_next = false; bool from_form = false; - - std::vector ListOfTexts; - std::vector DisplayedMessages; - std::vector ParsedMessages; - - MessageData CurrentMessage; - - private: - static const TextElement DictionaryElement; - bool data_loaded_ = false; + + int text_line_ = 0; + int text_position_ = 0; + int shown_lines_ = 0; + int selected_tile = 0; int current_message_id_ = 0; + uint8_t width_array[100]; + std::string search_text_ = ""; - gui::Canvas font_gfx_canvas_{"##FontGfxCanvas", ImVec2(128, 128)}; - gui::Canvas current_font_gfx16_canvas_{"##CurrentMessageGfx", - ImVec2(172, 4096)}; + std::vector list_of_texts_; + std::vector displayed_messages_; + std::vector parsed_messages_; + + MessageData current_message_; + + Bytes font_gfx16_data_; + Bytes current_font_gfx16_data_; gfx::Bitmap font_gfx_bitmap_; gfx::Bitmap current_font_gfx16_bitmap_; - Bytes font_gfx16_data; - Bytes current_font_gfx16_data_; + gui::Canvas font_gfx_canvas_{"##FontGfxCanvas", ImVec2(128, 128)}; + gui::Canvas current_font_gfx16_canvas_{"##CurrentMessageGfx", + ImVec2(172, 4096)}; gfx::SnesPalette font_preview_colors_;