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
|
#ifndef INTERPRETER_MISCOPCODES_H_INCLUDED
#define INTERPRETER_MISCOPCODES_H_INCLUDED
#include <algorithm>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include "defines.hpp"
#include "opcodes.hpp"
#include "runtime.hpp"
#include <components/misc/messageformatparser.hpp>
namespace Interpreter
{
class RuntimeMessageFormatter : public Misc::MessageFormatParser
{
private:
std::string mFormattedMessage;
Runtime& mRuntime;
protected:
void visitedPlaceholder(
Placeholder placeholder, char padding, int width, int precision, Notation notation) override
{
std::ostringstream out;
out.fill(padding);
if (width != -1)
out.width(width);
if (precision != -1)
out.precision(precision);
switch (placeholder)
{
case StringPlaceholder:
{
int index = mRuntime[0].mInteger;
mRuntime.pop();
out << mRuntime.getStringLiteral(index);
mFormattedMessage += out.str();
}
break;
case IntegerPlaceholder:
{
Type_Integer value = mRuntime[0].mInteger;
mRuntime.pop();
out << value;
mFormattedMessage += out.str();
}
break;
case FloatPlaceholder:
{
float value = mRuntime[0].mFloat;
mRuntime.pop();
if (notation == FixedNotation)
{
out << std::fixed << value;
mFormattedMessage += out.str();
}
else if (notation == ShortestNotation)
{
out << value;
std::string standard = out.str();
out.str(std::string());
out.clear();
out << std::scientific << value;
std::string scientific = out.str();
mFormattedMessage += standard.length() < scientific.length() ? standard : scientific;
}
else
{
out << std::scientific << value;
mFormattedMessage += out.str();
}
}
break;
default:
break;
}
}
void visitedCharacter(char c) override { mFormattedMessage += c; }
public:
RuntimeMessageFormatter(Runtime& runtime)
: mRuntime(runtime)
{
}
void process(std::string_view message) override
{
mFormattedMessage.clear();
MessageFormatParser::process(message);
}
std::string getFormattedMessage() const { return mFormattedMessage; }
};
inline std::string formatMessage(std::string_view message, Runtime& runtime)
{
RuntimeMessageFormatter formatter(runtime);
formatter.process(message);
std::string formattedMessage = formatter.getFormattedMessage();
formattedMessage = fixDefinesMsgBox(formattedMessage, runtime.getContext());
return formattedMessage;
}
class OpMessageBox : public Opcode1
{
public:
void execute(Runtime& runtime, unsigned int arg0) override
{
// message
int index = runtime[0].mInteger;
runtime.pop();
std::string_view message = runtime.getStringLiteral(index);
// buttons
std::vector<std::string> buttons;
for (std::size_t i = 0; i < arg0; ++i)
{
index = runtime[0].mInteger;
runtime.pop();
buttons.emplace_back(runtime.getStringLiteral(index));
}
std::reverse(buttons.begin(), buttons.end());
// handle additional parameters
std::string formattedMessage = formatMessage(message, runtime);
runtime.getContext().messageBox(formattedMessage, buttons);
}
};
class OpReport : public Opcode0
{
public:
void execute(Runtime& runtime) override
{
// message
int index = runtime[0].mInteger;
runtime.pop();
std::string_view message = runtime.getStringLiteral(index);
// handle additional parameters
std::string formattedMessage = formatMessage(message, runtime);
runtime.getContext().report(formattedMessage);
}
};
}
#endif
|