Skip to content

Commit

Permalink
DynRPG TextPlugin: Update to latest Player code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Mar 1, 2022
1 parent 8d54fea commit 34d6cfe
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 340 deletions.
427 changes: 139 additions & 288 deletions src/dynrpg_textplugin.cpp

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/dynrpg_textplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _EASYRPG_DYNRPG_TEXTPLUGIN_H_
#define _EASYRPG_DYNRPG_TEXTPLUGIN_H_
#ifndef EP_DYNRPG_TEXTPLUGIN_H
#define EP_DYNRPG_TEXTPLUGIN_H

#include "dynrpg.h"

namespace DynRpg {
class TextPlugin : public DynRpgPlugin {
public:
TextPlugin() : DynRpgPlugin("DynTextPlugin") {}
~TextPlugin();

std::string GetIdentifier();
void RegisterFunctions();
void Update();
void Load(const std::vector<uint8_t>&);
std::vector<uint8_t> Save();
void RegisterFunctions() override;
void Update() override;
void Load(const std::vector<uint8_t>&) override;
std::vector<uint8_t> Save() override;
};
}

Expand Down
75 changes: 69 additions & 6 deletions src/game_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,9 @@ bool Game_Message::CanShowMessage(bool foreground) {
return window ? window->GetAllowNextMessage(foreground) : false;
}


static Game_Message::ParseParamResult ParseParamImpl(
const char upper,
const char lower,
Game_Message::ParseParamResult Game_Message::ParseParamImpl(
char upper,
char lower,
const char* iter,
const char* end,
uint32_t escape_char,
Expand Down Expand Up @@ -251,6 +250,72 @@ static Game_Message::ParseParamResult ParseParamImpl(
return { iter, value };
}

Game_Message::ParseParamStringResult Game_Message::ParseStringParamImpl(
char upper,
char lower,
const char* iter,
const char* end,
uint32_t escape_char,
bool skip_prefix,
int max_recursion)
{
if (!skip_prefix) {
const auto begin = iter;
if (iter == end) {
return { begin, "" };
}
auto ret = Utils::UTF8Next(iter, end);
// Invalid commands
if (ret.ch != escape_char) {
return { begin, "" };
}
iter = ret.next;
if (iter == end || (*iter != upper && *iter != lower)) {
return { begin, "" };
}
++iter;
}

// If no bracket, return empty string
if (iter == end || *iter != '[') {
return { iter, "" };
}

std::string value;
++iter;

while (iter != end && *iter != ']') {
// Fast inline isdigit()
value += *iter;

if (max_recursion > 0) {
auto ret = Utils::UTF8Next(iter, end);
auto ch = ret.ch;
iter = ret.next;

// Recursive variable case.
if (ch == escape_char) {
if (iter != end && (*iter == 'V' || *iter == 'v')) {
++iter;

auto ret = ParseParamImpl('V', 'v', iter, end, escape_char, true, max_recursion - 1);
iter = ret.next;
int var_val = Main_Data::game_variables->Get(ret.value);

value += std::to_string(var_val);
continue;
}
}
}
}

if (iter != end) {
++iter;
}

return { iter, value };
}

Game_Message::ParseParamResult Game_Message::ParseVariable(const char* iter, const char* end, uint32_t escape_char, bool skip_prefix, int max_recursion) {
return ParseParamImpl('V', 'v', iter, end, escape_char, skip_prefix, max_recursion - 1);
}
Expand All @@ -266,5 +331,3 @@ Game_Message::ParseParamResult Game_Message::ParseSpeed(const char* iter, const
Game_Message::ParseParamResult Game_Message::ParseActor(const char* iter, const char* end, uint32_t escape_char, bool skip_prefix, int max_recursion) {
return ParseParamImpl('N', 'n', iter, end, escape_char, skip_prefix, max_recursion);
}


12 changes: 11 additions & 1 deletion src/game_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ namespace Game_Message {
int value = 0;
};

/** Struct returned by parameter parsing methods */
struct ParseParamStringResult {
/** iterator to the next character after parsed content */
const char* next = nullptr;
/** value that was parsed */
std::string value;
};

/** Parse a \v[] variable string
*
* @param iter start of utf8 string
Expand Down Expand Up @@ -155,7 +163,9 @@ namespace Game_Message {
* @return \refer ParseParamResult
*/
ParseParamResult ParseActor(const char* iter, const char* end, uint32_t escape_char, bool skip_prefix = false, int max_recursion = default_max_recursion);
}

Game_Message::ParseParamResult ParseParamImpl(char upper, char lower, const char* iter, const char* end, uint32_t escape_char, bool skip_prefix = false, int max_recursion = default_max_recursion);
Game_Message::ParseParamStringResult ParseStringParamImpl(char upper, char lower, const char* iter, const char* end, uint32_t escape_char, bool skip_prefix = false, int max_recursion = default_max_recursion);
}

#endif
4 changes: 2 additions & 2 deletions src/game_pictures.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ class Game_Pictures {
void OnMapScrolled(int dx, int dy);

struct Picture {
Picture(int id) { data.ID = id; }
Picture(lcf::rpg::SavePicture data);
explicit Picture(int id) { data.ID = id; }
explicit Picture(lcf::rpg::SavePicture data);

Sprite_Picture* sprite = nullptr;
lcf::rpg::SavePicture data;
Expand Down
77 changes: 47 additions & 30 deletions src/pending_message.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* 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_actors.h"
Expand All @@ -17,26 +34,26 @@ static void RemoveControlChars(std::string& s) {
s.erase(iter, s.end());
}

int PendingMessage::PushLineImpl(std::string msg) {
int PendingMessage::PushLineImpl(std::string msg, CommandInserter cmd_fn) {
RemoveControlChars(msg);
msg = ApplyTextInsertingCommands(std::move(msg), Player::escape_char);
msg = ApplyTextInsertingCommands(std::move(msg), Player::escape_char, cmd_fn);
texts.push_back(std::move(msg));
return texts.size();
}

int PendingMessage::PushLine(std::string msg) {
int PendingMessage::PushLine(std::string msg, CommandInserter cmd_fn) {
assert(!HasChoices());
assert(!HasNumberInput());
return PushLineImpl(std::move(msg));
return PushLineImpl(std::move(msg), cmd_fn);
}

int PendingMessage::PushChoice(std::string msg, bool enabled) {
int PendingMessage::PushChoice(std::string msg, bool enabled, CommandInserter cmd_fn) {
assert(!HasNumberInput());
if (!HasChoices()) {
choice_start = NumLines();
}
choice_enabled[GetNumChoices()] = enabled;
return PushLineImpl(std::move(msg));
return PushLineImpl(std::move(msg), cmd_fn);
}

int PendingMessage::PushNumInput(int variable_id, int num_digits) {
Expand Down Expand Up @@ -69,7 +86,7 @@ void PendingMessage::SetChoiceResetColors(bool value) {
choice_reset_color = value;
}

std::string PendingMessage::ApplyTextInsertingCommands(std::string input, uint32_t escape_char) {
std::string PendingMessage::ApplyTextInsertingCommands(std::string input, uint32_t escape_char, CommandInserter cmd_fn) {
if (input.empty()) {
return input;
}
Expand Down Expand Up @@ -103,29 +120,8 @@ std::string PendingMessage::ApplyTextInsertingCommands(std::string input, uint32
const auto ch = *iter;
++iter;

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);
} else{
output.append(ToString(actor->GetName()));
}

start_copy = iter;
} 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);
output.append(std::to_string(variable_value));

start_copy = iter;
}
output.append(cmd_fn(ch, &iter, end, escape_char));
start_copy = iter;
}

if (start_copy == input.data()) {
Expand All @@ -138,4 +134,25 @@ std::string PendingMessage::ApplyTextInsertingCommands(std::string input, uint32
return output;
}

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);
} 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 "";
};
11 changes: 7 additions & 4 deletions src/pending_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
class PendingMessage {
public:
using ChoiceContinuation = std::function<AsyncOp(int)>;
using CommandInserter = std::function<std::string(char,const char**,const char*,uint32_t)>;
static std::string DefaultCommandInserter(char ch, const char** iter, const char* end, uint32_t escape_char);

int PushLine(std::string msg);
int PushChoice(std::string msg, bool enabled = true);
int PushLine(std::string msg, CommandInserter cmd_fn = DefaultCommandInserter);
int PushChoice(std::string msg, bool enabled = true, CommandInserter cmd_fn = DefaultCommandInserter);
int PushNumInput(int variable_id, int num_digits);
void PushPageEnd();

Expand Down Expand Up @@ -63,10 +65,11 @@ class PendingMessage {

void SetIsEventMessage(bool value) { is_event_message = value; }
bool IsEventMessage() const { return is_event_message; }

private:
int PushLineImpl(std::string msg);
int PushLineImpl(std::string msg, CommandInserter cmd_fn);

std::string ApplyTextInsertingCommands(std::string input, uint32_t escape_char);
std::string ApplyTextInsertingCommands(std::string input, uint32_t escape_char, CommandInserter cmd_fn);

private:
ChoiceContinuation choice_continuation;
Expand Down
1 change: 0 additions & 1 deletion src/scene_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ void Scene_Map::Update() {
return;
}

DynRpg::Update();
MapUpdateAsyncContext actx;
UpdateStage1(actx);
}
Expand Down
4 changes: 3 additions & 1 deletion src/sprite_picture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ void Sprite_Picture::OnPictureShow() {
priority = Drawable::GetPriorityForMapLayer(pic.data.map_layer);
}
if (priority > 0) {
SetZ(priority + z_mask + pic_id);
// Small offset (10) to ensure there is space for graphics that are
// drawn at the image position (e.g. DynRPG Text Plugin)
SetZ(priority + z_mask + pic_id * 10);
}
}
}
Expand Down

0 comments on commit 34d6cfe

Please sign in to comment.