-
Notifications
You must be signed in to change notification settings - Fork 1
/
Phrase.cpp
127 lines (106 loc) · 4.5 KB
/
Phrase.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
#include "Phrase.h"
#include "FontManager.h"
#include "Utils.h"
#include "Game.h"
namespace typing
{
const std::string Phrase::PHRASE_FONT("fonts/menufont.fnt");
const float Phrase::PHRASE_HEIGHT = 18.0f;
const float Phrase::PHRASE_BORDER_GAP = 3.0f;
const float Phrase::PHRASE_BORDER_LINE_LENGTH = 3.0f;
// Nudge the phrase up a bit so that it doesn't cover the entity
const float Phrase::PHRASE_Y_OFFSET = 15.0f;
void Phrase::Init()
{
FONTS.Add(PHRASE_FONT);
}
bool Phrase::OnType(char c, float time)
{
if (m_phraseIndex < m_phrase.length() && m_phrase[m_phraseIndex] == c)
{
if (m_phraseIndex++ == 0)
{
m_startTime = time;
}
m_lastCorrectTypeTime = time;
return true;
}
return false;
}
// This assumes that the view has already been set up in ortho projection.
void Phrase::Draw(const juzutil::Vector3& origin,
PhraseDrawOption option)
{
if (!m_phrase.empty()) {
const float height =
PHRASE_HEIGHT * APP.GetOption<float>("text-scale");
const juzutil::Vector2 coords =
GAME.GetCam().PerspectiveProject(origin);
std::string typed;
std::string remaining;
typed.assign(m_phrase, 0, m_phraseIndex);
remaining.assign(m_phrase,
m_phraseIndex,
m_phrase.length() - m_phraseIndex);
const float typedWidth = FONTS.GetLineWidth(
PHRASE_FONT, height, typed);
const float remainingWidth = FONTS.GetLineWidth(
PHRASE_FONT, height, remaining);
const float totalWidth = typedWidth + remainingWidth;
float x = coords[0];
float y = coords[1] - height / 2.0f - PHRASE_BORDER_GAP -
PHRASE_Y_OFFSET;
glPushMatrix();
glTranslatef(x, y, 0.0f);
if (option == PHRASE_DRAW_BACKWARDS) {
glScalef(-1.0f, 1.0f, 1.0f);
}
glTranslatef(-totalWidth / 2.0f,
-height / 2.0f - PHRASE_BORDER_GAP,
0.0f);
// Draw the backing
DrawRect(ColourRGBA(0.0f, 0.0f, 0.0f, 0.5f),
-PHRASE_BORDER_GAP,
-PHRASE_BORDER_GAP,
totalWidth + PHRASE_BORDER_GAP * 2,
height + PHRASE_BORDER_GAP * 2);
// Draw the text
ColourRGBA textColour;
if (option == PHRASE_DRAW_BLOCKED) {
textColour[ColourRGBA::COLOUR_RED] = 0.4f;
textColour[ColourRGBA::COLOUR_GREEN] = 0.4f;
textColour[ColourRGBA::COLOUR_BLUE] = 0.4f;
textColour[ColourRGBA::COLOUR_ALPHA] = 1.0f;
} else {
textColour = ColourRGBA::White();
}
FONTS.Print(PHRASE_FONT,
0.0f, 0.0f, height,
ColourRGBA::Red(),
Font::ALIGN_LEFT,
typed);
if (option != PHRASE_DRAW_HIDDEN) {
FONTS.Print(PHRASE_FONT,
typedWidth, 0.0f, height,
textColour,
Font::ALIGN_LEFT,
remaining);
}
// Draw the corners of the backing
x = -PHRASE_BORDER_GAP;
y = -PHRASE_BORDER_GAP;
DrawLine(textColour, x, y, x, y + PHRASE_BORDER_LINE_LENGTH);
DrawLine(textColour, x, y, x + PHRASE_BORDER_LINE_LENGTH, y);
x += totalWidth + PHRASE_BORDER_GAP * 2;
DrawLine(textColour, x, y, x, y + PHRASE_BORDER_LINE_LENGTH);
DrawLine(textColour, x, y, x - PHRASE_BORDER_LINE_LENGTH, y);
y += height + PHRASE_BORDER_GAP * 2;
DrawLine(textColour, x, y, x, y - PHRASE_BORDER_LINE_LENGTH);
DrawLine(textColour, x, y, x - PHRASE_BORDER_LINE_LENGTH, y);
x -= totalWidth + PHRASE_BORDER_GAP * 2;
DrawLine(textColour, x, y, x, y - PHRASE_BORDER_LINE_LENGTH);
DrawLine(textColour, x, y, x + PHRASE_BORDER_LINE_LENGTH, y);
glPopMatrix();
}
}
}