mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-25 11:52:40 +01:00
Implement some text based markup
This commit is contained in:
parent
fc7642a4d7
commit
98ce9d90e1
@ -30,6 +30,7 @@ struct OnscreenText
|
||||
float osTextStart;
|
||||
float osTextTime;
|
||||
unsigned short osTextStyle;
|
||||
std::string osTextVar;
|
||||
|
||||
enum /*TextStyle*/
|
||||
{
|
||||
|
@ -32,6 +32,8 @@ public:
|
||||
int font;
|
||||
/// Message to be displayed (including markup)
|
||||
std::string text;
|
||||
/// Extra text parameter
|
||||
std::string varText;
|
||||
/// On screen position
|
||||
glm::vec2 screenPosition;
|
||||
/// font size
|
||||
|
@ -118,6 +118,8 @@ TextRenderer::TextRenderer(GameRenderer* renderer)
|
||||
|
||||
glyphData[charToIndex(' ')].widthFrac = 0.4f;
|
||||
glyphData[charToIndex('\'')].widthFrac = 0.5f;
|
||||
glyphData[charToIndex('(')].widthFrac = 0.45f;
|
||||
glyphData[charToIndex(')')].widthFrac = 0.45f;
|
||||
// Assumes contigious a-z character encoding
|
||||
for(char g = 0; g <= ('z'-'a'); g++)
|
||||
{
|
||||
@ -178,9 +180,45 @@ void TextRenderer::renderText(const TextRenderer::TextInfo& ti)
|
||||
std::vector<TextVertex> geo;
|
||||
|
||||
float maxWidth = 0.f;
|
||||
|
||||
auto text = ti.text;
|
||||
|
||||
for( const char& c : ti.text )
|
||||
for( int i = 0; i < text.length(); ++i )
|
||||
{
|
||||
char c = text[i];
|
||||
|
||||
// Handle any markup changes.
|
||||
if( c == '~' && text.length() > i + 1 )
|
||||
{
|
||||
switch( text[i+1] )
|
||||
{
|
||||
case '1':
|
||||
case 'a':
|
||||
text.erase(text.begin()+i, text.begin()+i+3);
|
||||
text.insert(i, ti.varText);
|
||||
break;
|
||||
case 'k': {
|
||||
text.erase(text.begin()+i, text.begin()+i+3);
|
||||
// Extract the key name from the /next/ markup
|
||||
auto keyend = text.find('~', i+1);
|
||||
auto keyname = text.substr(i+1, keyend-i-1);
|
||||
// Since we don't have a key map yet, just print out the name
|
||||
text.erase(text.begin()+i, text.begin()+keyend);
|
||||
text.insert(i, "("+keyname+")");
|
||||
} break;
|
||||
case 'w':
|
||||
text.erase(text.begin()+i, text.begin()+i+3);
|
||||
colour = ti.baseColour;
|
||||
break;
|
||||
case 'h':
|
||||
text.erase(text.begin()+i, text.begin()+i+3);
|
||||
colour = glm::vec3(1.f);
|
||||
break;
|
||||
}
|
||||
|
||||
c = text[i];
|
||||
}
|
||||
|
||||
int glyph = charToIndex(c);
|
||||
if( glyph >= GAME_GLYPHS )
|
||||
{
|
||||
|
@ -331,7 +331,6 @@ void game_print_big_with_number(const ScriptArguments& args)
|
||||
std::string str = args.getWorld()->data->texts.text(id);
|
||||
|
||||
int number = args[1].integer;
|
||||
str += "\n" + std::to_string(number);
|
||||
|
||||
unsigned short style = args[3].integer;
|
||||
|
||||
@ -340,7 +339,8 @@ void game_print_big_with_number(const ScriptArguments& args)
|
||||
str,
|
||||
args.getWorld()->getGameTime(),
|
||||
args[2].integer / 1000.f,
|
||||
style
|
||||
style,
|
||||
std::to_string(number),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -87,39 +87,13 @@ void drawOnScreenText(GameWorld* world, GameRenderer* renderer)
|
||||
ti.screenPosition = glm::vec2(20.f, 20.f);
|
||||
ti.font = 2;
|
||||
ti.size = 20.f;
|
||||
ti.baseColour = glm::vec3(1.f);
|
||||
ti.baseColour = glm::vec3(0.9f);
|
||||
ti.align = TextRenderer::TextInfo::Left;
|
||||
break;
|
||||
}
|
||||
|
||||
ti.text = t.osTextString;
|
||||
|
||||
if( t.osTextStyle == OnscreenText::Help )
|
||||
{
|
||||
// Insert line breaks into the message string.
|
||||
auto m = ti.text;
|
||||
const float boxWidth = 250.f;
|
||||
int lastSpace = 0;
|
||||
float lineLength = 0.f, wordLength = 0.f;
|
||||
for( int c = 0; c < m.length(); ++c )
|
||||
{
|
||||
if(m[c] == ' ')
|
||||
{
|
||||
lastSpace = c;
|
||||
lineLength += wordLength;
|
||||
wordLength = 0.f;
|
||||
}
|
||||
|
||||
wordLength += ti.size;
|
||||
|
||||
if( lineLength + wordLength > boxWidth )
|
||||
{
|
||||
m[lastSpace] = '\n';
|
||||
lineLength = 0.f;
|
||||
}
|
||||
}
|
||||
ti.text = m;
|
||||
}
|
||||
ti.varText = t.osTextVar;
|
||||
|
||||
if( glm::length( shadowOffset ) > 0 )
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user