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) {
|
ParsedElement FindMatchingElement(const std::string &str) {
|
||||||
std::smatch match;
|
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);
|
match = text_element.MatchMe(str);
|
||||||
if (match.size() > 0) {
|
if (match.size() > 0) {
|
||||||
if (text_element.HasArgument) {
|
if (text_element.HasArgument) {
|
||||||
@@ -104,7 +107,6 @@ std::vector<uint8_t> ParseMessageToData(std::string str) {
|
|||||||
std::vector<uint8_t> bytes;
|
std::vector<uint8_t> bytes;
|
||||||
std::string temp_string = str;
|
std::string temp_string = str;
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
|
|
||||||
while (pos < temp_string.size()) {
|
while (pos < temp_string.size()) {
|
||||||
// Get next text fragment.
|
// Get next text fragment.
|
||||||
if (temp_string[pos] == '[') {
|
if (temp_string[pos] == '[') {
|
||||||
@@ -138,7 +140,6 @@ std::vector<uint8_t> ParseMessageToData(std::string str) {
|
|||||||
uint8_t bb = FindMatchingCharacter(temp_string[pos++]);
|
uint8_t bb = FindMatchingCharacter(temp_string[pos++]);
|
||||||
|
|
||||||
if (bb != 0xFF) {
|
if (bb != 0xFF) {
|
||||||
util::logf("Error parsing message: %s", temp_string);
|
|
||||||
bytes.push_back(bb);
|
bytes.push_back(bb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -190,8 +191,8 @@ std::string ReplaceAllDictionaryWords(std::string str,
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
DictionaryEntry FindRealDictionaryEntry(uint8_t value,
|
DictionaryEntry FindRealDictionaryEntry(
|
||||||
std::vector<DictionaryEntry> dictionary) {
|
uint8_t value, std::vector<DictionaryEntry> dictionary) {
|
||||||
for (const auto &entry : dictionary) {
|
for (const auto &entry : dictionary) {
|
||||||
if (entry.ID + DICTOFF == value) {
|
if (entry.ID + DICTOFF == value) {
|
||||||
return entry;
|
return entry;
|
||||||
|
|||||||
@@ -83,8 +83,8 @@ constexpr uint8_t kLine3 = 0x76;
|
|||||||
std::vector<DictionaryEntry> BuildDictionaryEntries(Rom *rom);
|
std::vector<DictionaryEntry> BuildDictionaryEntries(Rom *rom);
|
||||||
std::string ReplaceAllDictionaryWords(std::string str,
|
std::string ReplaceAllDictionaryWords(std::string str,
|
||||||
std::vector<DictionaryEntry> dictionary);
|
std::vector<DictionaryEntry> dictionary);
|
||||||
DictionaryEntry FindRealDictionaryEntry(uint8_t value,
|
DictionaryEntry FindRealDictionaryEntry(
|
||||||
std::vector<DictionaryEntry> dictionary);
|
uint8_t value, std::vector<DictionaryEntry> dictionary);
|
||||||
|
|
||||||
// Inserted into commands to protect them from dictionary replacements.
|
// Inserted into commands to protect them from dictionary replacements.
|
||||||
const std::string CHEESE = "\uBEBE";
|
const std::string CHEESE = "\uBEBE";
|
||||||
@@ -119,10 +119,6 @@ struct MessageData {
|
|||||||
ContentsParsed = other.ContentsParsed;
|
ContentsParsed = other.ContentsParsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ToString() const {
|
|
||||||
return absl::StrFormat("%0X - %s", ID, ContentsParsed);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string OptimizeMessageForDictionary(
|
std::string OptimizeMessageForDictionary(
|
||||||
std::string message_string,
|
std::string message_string,
|
||||||
const std::vector<DictionaryEntry> &dictionary) {
|
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::smatch MatchMe(std::string dfrag) const {
|
||||||
std::regex pattern(StrictPattern);
|
std::regex pattern(StrictPattern);
|
||||||
std::smatch match;
|
std::smatch match;
|
||||||
|
|||||||
@@ -143,7 +143,10 @@ void MessageEditor::DrawCurrentMessage() {
|
|||||||
Button(absl::StrCat("Message ", current_message_.ID).c_str());
|
Button(absl::StrCat("Message ", current_message_.ID).c_str());
|
||||||
if (InputTextMultiline("##MessageEditor", &message_text_box_.text,
|
if (InputTextMultiline("##MessageEditor", &message_text_box_.text,
|
||||||
ImVec2(ImGui::GetContentRegionAvail().x, 0))) {
|
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();
|
DrawMessagePreview();
|
||||||
}
|
}
|
||||||
Separator();
|
Separator();
|
||||||
@@ -159,7 +162,7 @@ void MessageEditor::DrawCurrentMessage() {
|
|||||||
ImGui::EndPopup();
|
ImGui::EndPopup();
|
||||||
}
|
}
|
||||||
gui::BeginPadding(1);
|
gui::BeginPadding(1);
|
||||||
BeginChild("CurrentGfxFont", ImVec2(344, 0), true,
|
BeginChild("CurrentGfxFont", ImVec2(348, 0), true,
|
||||||
ImGuiWindowFlags_NoScrollWithMouse);
|
ImGuiWindowFlags_NoScrollWithMouse);
|
||||||
current_font_gfx16_canvas_.DrawBackground();
|
current_font_gfx16_canvas_.DrawBackground();
|
||||||
gui::EndPadding();
|
gui::EndPadding();
|
||||||
@@ -202,13 +205,14 @@ void MessageEditor::DrawFontAtlas() {
|
|||||||
void MessageEditor::DrawExpandedMessageSettings() {
|
void MessageEditor::DrawExpandedMessageSettings() {
|
||||||
ImGui::BeginChild("##ExpandedMessageSettings", ImVec2(0, 100), true,
|
ImGui::BeginChild("##ExpandedMessageSettings", ImVec2(0, 100), true,
|
||||||
ImGuiWindowFlags_AlwaysVerticalScrollbar);
|
ImGuiWindowFlags_AlwaysVerticalScrollbar);
|
||||||
// Input for the address of the expanded messages
|
ImGui::Text("Expanded Messages");
|
||||||
ImGui::InputText("Address", &expanded_message_address_,
|
static std::string expanded_message_path = "";
|
||||||
ImGuiInputTextFlags_CharsHexadecimal);
|
|
||||||
|
|
||||||
if (ImGui::Button("Load Expanded Message")) {
|
if (ImGui::Button("Load Expanded Message")) {
|
||||||
// Load the expanded message from the address.
|
expanded_message_path = core::FileDialogWrapper::ShowOpenFileDialog();
|
||||||
// TODO: Implement this.
|
if (!expanded_message_path.empty()) {
|
||||||
|
// Load the expanded message from the path.
|
||||||
|
// TODO: Implement this.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
EndChild();
|
EndChild();
|
||||||
}
|
}
|
||||||
@@ -217,9 +221,12 @@ void MessageEditor::DrawTextCommands() {
|
|||||||
ImGui::BeginChild("##TextCommands",
|
ImGui::BeginChild("##TextCommands",
|
||||||
ImVec2(0, ImGui::GetWindowContentRegionMax().y / 3), true,
|
ImVec2(0, ImGui::GetWindowContentRegionMax().y / 3), true,
|
||||||
ImGuiWindowFlags_AlwaysVerticalScrollbar);
|
ImGuiWindowFlags_AlwaysVerticalScrollbar);
|
||||||
|
static uint8_t command_parameter = 0;
|
||||||
|
gui::InputHexByte("Command Parameter", &command_parameter);
|
||||||
for (const auto& text_element : TextCommands) {
|
for (const auto& text_element : TextCommands) {
|
||||||
if (Button(text_element.GenericToken.c_str())) {
|
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();
|
SameLine();
|
||||||
TextWrapped("%s", text_element.Description.c_str());
|
TextWrapped("%s", text_element.Description.c_str());
|
||||||
|
|||||||
@@ -62,16 +62,14 @@ class MessageEditor : public Editor {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Rom* rom_;
|
Rom* rom_;
|
||||||
bool data_loaded_ = false;
|
|
||||||
bool case_sensitive_ = false;
|
bool case_sensitive_ = false;
|
||||||
bool match_whole_word_ = false;
|
bool match_whole_word_ = false;
|
||||||
|
|
||||||
std::string expanded_message_address_ = "";
|
|
||||||
std::string search_text_ = "";
|
std::string search_text_ = "";
|
||||||
|
|
||||||
std::array<uint8_t, 0x4000> raw_font_gfx_data_;
|
std::array<uint8_t, 0x4000> raw_font_gfx_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_;
|
||||||
|
std::vector<MessageData> expanded_messages_;
|
||||||
|
|
||||||
MessageData current_message_;
|
MessageData current_message_;
|
||||||
MessagePreview message_preview_;
|
MessagePreview message_preview_;
|
||||||
|
|||||||
@@ -32,14 +32,16 @@ void MessagePreview::DrawTileToPreview(int x, int y, int srcx, int srcy,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MessagePreview::DrawStringToPreview(std::string str) {
|
void MessagePreview::DrawStringToPreview(const std::string& str) {
|
||||||
for (const auto c : str) {
|
for (const auto& c : str) {
|
||||||
DrawCharacterToPreview(c);
|
DrawCharacterToPreview(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MessagePreview::DrawCharacterToPreview(char 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) {
|
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) {
|
} else if (value == 0x6A) {
|
||||||
// Includes parentheses to be longer, since player names can be up to 6
|
// Includes parentheses to be longer, since player names can be up to 6
|
||||||
// characters.
|
// characters.
|
||||||
DrawStringToPreview("(NAME)");
|
const std::string name = "(NAME)";
|
||||||
|
DrawStringToPreview(name);
|
||||||
} else if (value >= DICTOFF && value < (DICTOFF + 97)) {
|
} else if (value >= DICTOFF && value < (DICTOFF + 97)) {
|
||||||
int pos = value - DICTOFF;
|
int pos = value - DICTOFF;
|
||||||
if (pos < 0 || pos >= all_dictionaries_.size()) {
|
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,
|
void DrawTileToPreview(int x, int y, int srcx, int srcy, int pal,
|
||||||
int sizex = 1, int sizey = 1);
|
int sizex = 1, int sizey = 1);
|
||||||
|
|
||||||
void DrawStringToPreview(std::string str);
|
void DrawStringToPreview(const std::string& str);
|
||||||
void DrawCharacterToPreview(char c);
|
void DrawCharacterToPreview(char c);
|
||||||
void DrawCharacterToPreview(const std::vector<uint8_t>& text);
|
void DrawCharacterToPreview(const std::vector<uint8_t>& text);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user