Refactor message handling and improve message editor functionality
- Combined TextCommands and SpecialChars into a single vector for streamlined matching in FindMatchingElement. - Removed unnecessary logging in ParseMessageToData for cleaner error handling. - Updated FindRealDictionaryEntry function signature for improved readability. - Enhanced DrawCurrentMessage to strip newline characters from input before parsing. - Added command parameter input in DrawTextCommands for better user interaction. - Cleaned up unused ToString methods in MessageData and TextElement structures.
This commit is contained in:
@@ -48,7 +48,10 @@ std::optional<TextElement> FindMatchingSpecial(uint8_t value) {
|
||||
|
||||
ParsedElement FindMatchingElement(const std::string &str) {
|
||||
std::smatch match;
|
||||
for (auto &text_element : TextCommands) {
|
||||
std::vector<TextElement> commands_and_chars = TextCommands;
|
||||
commands_and_chars.insert(commands_and_chars.end(), SpecialChars.begin(),
|
||||
SpecialChars.end());
|
||||
for (auto &text_element : commands_and_chars) {
|
||||
match = text_element.MatchMe(str);
|
||||
if (match.size() > 0) {
|
||||
if (text_element.HasArgument) {
|
||||
@@ -104,7 +107,6 @@ std::vector<uint8_t> ParseMessageToData(std::string str) {
|
||||
std::vector<uint8_t> bytes;
|
||||
std::string temp_string = str;
|
||||
int pos = 0;
|
||||
|
||||
while (pos < temp_string.size()) {
|
||||
// Get next text fragment.
|
||||
if (temp_string[pos] == '[') {
|
||||
@@ -138,7 +140,6 @@ std::vector<uint8_t> ParseMessageToData(std::string str) {
|
||||
uint8_t bb = FindMatchingCharacter(temp_string[pos++]);
|
||||
|
||||
if (bb != 0xFF) {
|
||||
util::logf("Error parsing message: %s", temp_string);
|
||||
bytes.push_back(bb);
|
||||
}
|
||||
}
|
||||
@@ -190,8 +191,8 @@ std::string ReplaceAllDictionaryWords(std::string str,
|
||||
return temp;
|
||||
}
|
||||
|
||||
DictionaryEntry FindRealDictionaryEntry(uint8_t value,
|
||||
std::vector<DictionaryEntry> dictionary) {
|
||||
DictionaryEntry FindRealDictionaryEntry(
|
||||
uint8_t value, std::vector<DictionaryEntry> dictionary) {
|
||||
for (const auto &entry : dictionary) {
|
||||
if (entry.ID + DICTOFF == value) {
|
||||
return entry;
|
||||
|
||||
@@ -83,8 +83,8 @@ constexpr uint8_t kLine3 = 0x76;
|
||||
std::vector<DictionaryEntry> BuildDictionaryEntries(Rom *rom);
|
||||
std::string ReplaceAllDictionaryWords(std::string str,
|
||||
std::vector<DictionaryEntry> dictionary);
|
||||
DictionaryEntry FindRealDictionaryEntry(uint8_t value,
|
||||
std::vector<DictionaryEntry> dictionary);
|
||||
DictionaryEntry FindRealDictionaryEntry(
|
||||
uint8_t value, std::vector<DictionaryEntry> dictionary);
|
||||
|
||||
// Inserted into commands to protect them from dictionary replacements.
|
||||
const std::string CHEESE = "\uBEBE";
|
||||
@@ -119,10 +119,6 @@ struct MessageData {
|
||||
ContentsParsed = other.ContentsParsed;
|
||||
}
|
||||
|
||||
std::string ToString() const {
|
||||
return absl::StrFormat("%0X - %s", ID, ContentsParsed);
|
||||
}
|
||||
|
||||
std::string OptimizeMessageForDictionary(
|
||||
std::string message_string,
|
||||
const std::vector<DictionaryEntry> &dictionary) {
|
||||
@@ -197,10 +193,6 @@ struct TextElement {
|
||||
}
|
||||
}
|
||||
|
||||
std::string ToString() const {
|
||||
return absl::StrFormat("%s %s", GenericToken, Description);
|
||||
}
|
||||
|
||||
std::smatch MatchMe(std::string dfrag) const {
|
||||
std::regex pattern(StrictPattern);
|
||||
std::smatch match;
|
||||
|
||||
@@ -143,7 +143,10 @@ void MessageEditor::DrawCurrentMessage() {
|
||||
Button(absl::StrCat("Message ", current_message_.ID).c_str());
|
||||
if (InputTextMultiline("##MessageEditor", &message_text_box_.text,
|
||||
ImVec2(ImGui::GetContentRegionAvail().x, 0))) {
|
||||
current_message_.Data = ParseMessageToData(message_text_box_.text);
|
||||
std::string temp = message_text_box_.text;
|
||||
// Strip newline characters.
|
||||
temp.erase(std::remove(temp.begin(), temp.end(), '\n'), temp.end());
|
||||
current_message_.Data = ParseMessageToData(temp);
|
||||
DrawMessagePreview();
|
||||
}
|
||||
Separator();
|
||||
@@ -159,7 +162,7 @@ void MessageEditor::DrawCurrentMessage() {
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
gui::BeginPadding(1);
|
||||
BeginChild("CurrentGfxFont", ImVec2(344, 0), true,
|
||||
BeginChild("CurrentGfxFont", ImVec2(348, 0), true,
|
||||
ImGuiWindowFlags_NoScrollWithMouse);
|
||||
current_font_gfx16_canvas_.DrawBackground();
|
||||
gui::EndPadding();
|
||||
@@ -202,13 +205,14 @@ void MessageEditor::DrawFontAtlas() {
|
||||
void MessageEditor::DrawExpandedMessageSettings() {
|
||||
ImGui::BeginChild("##ExpandedMessageSettings", ImVec2(0, 100), true,
|
||||
ImGuiWindowFlags_AlwaysVerticalScrollbar);
|
||||
// Input for the address of the expanded messages
|
||||
ImGui::InputText("Address", &expanded_message_address_,
|
||||
ImGuiInputTextFlags_CharsHexadecimal);
|
||||
|
||||
ImGui::Text("Expanded Messages");
|
||||
static std::string expanded_message_path = "";
|
||||
if (ImGui::Button("Load Expanded Message")) {
|
||||
// Load the expanded message from the address.
|
||||
// TODO: Implement this.
|
||||
expanded_message_path = core::FileDialogWrapper::ShowOpenFileDialog();
|
||||
if (!expanded_message_path.empty()) {
|
||||
// Load the expanded message from the path.
|
||||
// TODO: Implement this.
|
||||
}
|
||||
}
|
||||
EndChild();
|
||||
}
|
||||
@@ -217,9 +221,12 @@ void MessageEditor::DrawTextCommands() {
|
||||
ImGui::BeginChild("##TextCommands",
|
||||
ImVec2(0, ImGui::GetWindowContentRegionMax().y / 3), true,
|
||||
ImGuiWindowFlags_AlwaysVerticalScrollbar);
|
||||
static uint8_t command_parameter = 0;
|
||||
gui::InputHexByte("Command Parameter", &command_parameter);
|
||||
for (const auto& text_element : TextCommands) {
|
||||
if (Button(text_element.GenericToken.c_str())) {
|
||||
message_text_box_.text.append(text_element.GenericToken);
|
||||
message_text_box_.text.append(
|
||||
text_element.GetParamToken(command_parameter));
|
||||
}
|
||||
SameLine();
|
||||
TextWrapped("%s", text_element.Description.c_str());
|
||||
|
||||
@@ -62,16 +62,14 @@ class MessageEditor : public Editor {
|
||||
|
||||
private:
|
||||
Rom* rom_;
|
||||
bool data_loaded_ = false;
|
||||
bool case_sensitive_ = false;
|
||||
bool match_whole_word_ = false;
|
||||
|
||||
std::string expanded_message_address_ = "";
|
||||
std::string search_text_ = "";
|
||||
|
||||
std::array<uint8_t, 0x4000> raw_font_gfx_data_;
|
||||
std::vector<std::string> parsed_messages_;
|
||||
std::vector<MessageData> list_of_texts_;
|
||||
std::vector<MessageData> expanded_messages_;
|
||||
|
||||
MessageData current_message_;
|
||||
MessagePreview message_preview_;
|
||||
|
||||
@@ -32,14 +32,16 @@ void MessagePreview::DrawTileToPreview(int x, int y, int srcx, int srcy,
|
||||
}
|
||||
}
|
||||
|
||||
void MessagePreview::DrawStringToPreview(std::string str) {
|
||||
for (const auto c : str) {
|
||||
void MessagePreview::DrawStringToPreview(const std::string& str) {
|
||||
for (const auto& c : str) {
|
||||
DrawCharacterToPreview(c);
|
||||
}
|
||||
}
|
||||
|
||||
void MessagePreview::DrawCharacterToPreview(char c) {
|
||||
DrawCharacterToPreview(FindMatchingCharacter(c));
|
||||
std::vector<uint8_t> text;
|
||||
text.push_back(FindMatchingCharacter(c));
|
||||
DrawCharacterToPreview(text);
|
||||
}
|
||||
|
||||
void MessagePreview::DrawCharacterToPreview(const std::vector<uint8_t>& text) {
|
||||
@@ -87,7 +89,8 @@ void MessagePreview::DrawCharacterToPreview(const std::vector<uint8_t>& text) {
|
||||
} else if (value == 0x6A) {
|
||||
// Includes parentheses to be longer, since player names can be up to 6
|
||||
// characters.
|
||||
DrawStringToPreview("(NAME)");
|
||||
const std::string name = "(NAME)";
|
||||
DrawStringToPreview(name);
|
||||
} else if (value >= DICTOFF && value < (DICTOFF + 97)) {
|
||||
int pos = value - DICTOFF;
|
||||
if (pos < 0 || pos >= all_dictionaries_.size()) {
|
||||
|
||||
@@ -20,7 +20,7 @@ struct MessagePreview {
|
||||
void DrawTileToPreview(int x, int y, int srcx, int srcy, int pal,
|
||||
int sizex = 1, int sizey = 1);
|
||||
|
||||
void DrawStringToPreview(std::string str);
|
||||
void DrawStringToPreview(const std::string& str);
|
||||
void DrawCharacterToPreview(char c);
|
||||
void DrawCharacterToPreview(const std::vector<uint8_t>& text);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user