/
MenuHighScores.cpp
127 lines (103 loc) · 4.79 KB
/
MenuHighScores.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 <string>
#include <math.h>
#include "MenuHighScores.h"
#include "HighScores.h"
#include "TextureManager.h"
#include "FontManager.h"
#include "App.h"
#include "Utils.h"
namespace typing
{
const std::string HighScoresMenu::MENU_NAME("HighScoresMenu");
const std::string HighScoresMenu::FONT("fonts/menufont.fnt");
const std::string HighScoresMenu::BACKGROUND("textures/menu/background.tga");
const float HighScoresMenu::BACK_BUTTON_HEIGHT = 32.0f;
const float HighScoresMenu::BACK_BUTTON_PAD = 2.0f;
void HighScoresMenu::Init()
{
TEXTURES.Add(BACKGROUND);
FONTS.Add(FONT);
MenuItem::Init();
AddMenuItem(MenuItemPtr(new MenuItem(juzutil::Vector2(APP.GetScreenWidth() / 2.0f,
APP.GetScreenHeight() - BACK_BUTTON_HEIGHT - BACK_BUTTON_PAD),
"Back", MENUITEM_BACK, BACK_BUTTON_HEIGHT, true)));
m_startTime = 0.0f;
}
MenuScreen::NextAction HighScoresMenu::Update()
{
if (m_startTime == 0.0f)
{
m_startTime = APP.GetTime();
}
return ACTION_NONE;
}
void HighScoresMenu::Draw()
{
const float BACKGROUND_MARGIN = 50.0f;
const float BACKGROUND_WIDTH = APP.GetScreenWidth() - BACKGROUND_MARGIN * 2.0f;
const float SECTION_SPACING = 3.0f;
const float TITLE_HEIGHT = 64.0f;
const float TITLE_BACKGROUND_PAD = 4.0f;
const float HEADING_HEIGHT = 24.0f;
const float HEADING_BACKGROUND_PAD = 2.0f;
const float ENTRY_HEIGHT = 32.0f;
const float ENTRY_BACKGROUND_PAD = 2.0f;
const float ENTRY_TIME = 0.1f;
const float NAME_X = 55.0f;
const float SCORE_X = 150.0f;
const float STREAK_X = APP.GetScreenWidth() - 150.0f;
// Background
DrawTexturedRect(BACKGROUND, 0.0f, 0.0f, APP.GetScreenWidth(), APP.GetScreenHeight());
// Title "High Scores"
DrawRect(ColourRGBA(0.0f, 0.0f, 0.0f, 0.5f), BACKGROUND_MARGIN, 0.0f,
BACKGROUND_WIDTH, TITLE_HEIGHT + TITLE_BACKGROUND_PAD * 2.0f);
FONTS.Print(FONT, APP.GetScreenWidth() / 2.0f, TITLE_BACKGROUND_PAD, TITLE_HEIGHT, ColourRGBA::White(),
Font::ALIGN_CENTER, "High Scores");
float y = TITLE_HEIGHT + TITLE_BACKGROUND_PAD * 2.0f + SECTION_SPACING;
// Score table headings
DrawRect(ColourRGBA(0.0f, 0.0f, 0.0f, 0.5f), BACKGROUND_MARGIN, y,
BACKGROUND_WIDTH, HEADING_HEIGHT + HEADING_BACKGROUND_PAD * 2.0f);
FONTS.Print(FONT, NAME_X, y + HEADING_BACKGROUND_PAD, HEADING_HEIGHT, ColourRGBA::White(),
Font::ALIGN_LEFT, "Player");
FONTS.Print(FONT, SCORE_X, y + HEADING_BACKGROUND_PAD, HEADING_HEIGHT, ColourRGBA::White(),
Font::ALIGN_LEFT, "Score");
FONTS.Print(FONT, STREAK_X, y + HEADING_BACKGROUND_PAD, HEADING_HEIGHT, ColourRGBA::White(),
Font::ALIGN_LEFT, "Streak");
y += HEADING_HEIGHT + HEADING_BACKGROUND_PAD * 2.0f + SECTION_SPACING;
// Scores
float entryEndY = APP.GetScreenHeight() - BACK_BUTTON_HEIGHT - BACK_BUTTON_PAD * 2.0f - SECTION_SPACING;
DrawRect(ColourRGBA(0.0f, 0.0f, 0.0f, 0.5f), BACKGROUND_MARGIN, y,
BACKGROUND_WIDTH, entryEndY - y);
float entryPad = (((entryEndY - y - ENTRY_BACKGROUND_PAD * 2.0f)) - ENTRY_HEIGHT * SCORES.GetHighScoreCount()) / (SCORES.GetHighScoreCount() - 1);
y += ENTRY_BACKGROUND_PAD;
const unsigned int numEntries = static_cast<unsigned int>(floor((APP.GetTime() - m_startTime) / ENTRY_TIME));
float blue = 0;
const float blueInc = 1.0f / SCORES.GetHighScoreCount();
HighScores::ScoreIterator iter = SCORES.begin();
for(unsigned int i = 0; i < numEntries && iter != SCORES.end(); ++i, ++iter)
{
ColourRGBA col(1.0f, 1.0f, blue, 1.0f);
FONTS.Print(FONT, NAME_X, y, ENTRY_HEIGHT, col, Font::ALIGN_LEFT,
(*iter)->name);
FONTS.Print(FONT, SCORE_X, y, ENTRY_HEIGHT, col, Font::ALIGN_LEFT,
std::to_string((*iter)->score));
FONTS.Print(FONT, STREAK_X, y, ENTRY_HEIGHT, col, Font::ALIGN_LEFT,
std::to_string((*iter)->streak));
y += ENTRY_HEIGHT + entryPad;
blue += blueInc;
}
// Back button
y = entryEndY + SECTION_SPACING;
DrawRect(ColourRGBA(0.0f, 0.0f, 0.0f, 0.5f), BACKGROUND_MARGIN, y,
BACKGROUND_WIDTH, APP.GetScreenHeight() - y);
MenuScreen::Draw();
}
MenuScreen::NextAction HighScoresMenu::OnMenuItemChoose(unsigned int)
{
return ACTION_PREV;
}
const std::string HighScoresMenu::NextMenu() const
{
return "";
}
}