forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpending_message.cpp
171 lines (145 loc) · 4.4 KB
/
pending_message.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pending_message.h"
#include "game_variables.h"
#include "game_strings.h"
#include "game_actors.h"
#include "game_message.h"
#include "game_switches.h"
#include <lcf/data.h>
#include "output.h"
#include "utils.h"
#include "player.h"
#include "main_data.h"
#include <cassert>
#include <cctype>
#include <algorithm>
#include <utility>
static void RemoveControlChars(std::string& s) {
// RPG_RT ignores any control characters within messages.
auto iter = std::remove_if(s.begin(), s.end(), [](const char c) { return Utils::IsControlCharacter(c); });
s.erase(iter, s.end());
}
PendingMessage::PendingMessage(PendingMessage::CommandInserter cmd_fn) :
command_inserter(std::move(cmd_fn)) {
// no-op
};
int PendingMessage::PushLineImpl(std::string msg) {
RemoveControlChars(msg);
msg = ApplyTextInsertingCommands(std::move(msg), Player::escape_char, command_inserter);
texts.push_back(std::move(msg));
return texts.size();
}
int PendingMessage::PushLine(std::string msg) {
assert(!HasChoices());
assert(!HasNumberInput());
return PushLineImpl(std::move(msg));
}
int PendingMessage::PushChoice(std::string msg, bool enabled) {
assert(!HasNumberInput());
if (!HasChoices()) {
choice_start = NumLines();
}
choice_enabled[GetNumChoices()] = enabled;
return PushLineImpl(std::move(msg));
}
int PendingMessage::PushNumInput(int variable_id, int num_digits) {
assert(!HasChoices());
assert(!HasNumberInput());
num_input_variable = variable_id;
num_input_digits = num_digits;
return NumLines();
}
void PendingMessage::PushPageEnd() {
assert(!HasChoices());
assert(!HasNumberInput());
if (texts.empty()) {
texts.push_back("");
}
texts.back().push_back('\f');
}
void PendingMessage::SetWordWrapped(bool value) {
assert(texts.empty());
word_wrapped = value;
}
void PendingMessage::SetChoiceCancelType(int value) {
choice_cancel_type = value;
}
void PendingMessage::SetChoiceResetColors(bool value) {
choice_reset_color = value;
}
std::string PendingMessage::ApplyTextInsertingCommands(std::string input, uint32_t escape_char, const CommandInserter& cmd_fn) {
if (input.empty()) {
return input;
}
std::string output;
const char* iter = input.data();
const auto end = input.data() + input.size();
const char* start_copy = iter;
while (iter != end) {
auto ret = Utils::UTF8Next(iter, end);
if (ret.ch != escape_char) {
iter = ret.next;
continue;
}
// utf8 parsing failed
if (ret.ch == 0) {
break;
}
output.append(start_copy, iter - start_copy);
start_copy = iter;
iter = ret.next;
if (iter == end) {
break;
}
const auto ch = *iter;
++iter;
auto fn_res = cmd_fn(ch, &iter, end, escape_char);
if (fn_res) {
output.append(*fn_res);
start_copy = iter;
}
}
if (start_copy == input.data()) {
// Fast path - no substitutions occured, so just move the input into the return value.
output = std::move(input);
} else {
output.append(start_copy, end - start_copy);
}
return output;
}
std::optional<std::string> PendingMessage::DefaultCommandInserter(char ch, const char** iter, const char* end, uint32_t escape_char) {
if (ch == 'N' || ch == 'n') {
auto parse_ret = Game_Message::ParseActor(*iter, end, escape_char, true);
*iter = parse_ret.next;
int value = parse_ret.value;
const auto* actor = Main_Data::game_actors->GetActor(value);
if (!actor) {
Output::Warning("Invalid Actor Id {} in message text", value);
return "";
} else {
return ToString(actor->GetName());
}
} else if (ch == 'V' || ch == 'v') {
auto parse_ret = Game_Message::ParseVariable(*iter, end, escape_char, true);
*iter = parse_ret.next;
int value = parse_ret.value;
int variable_value = Main_Data::game_variables->Get(value);
return std::to_string(variable_value);
}
return std::nullopt;
}