Debian Codegraph @

src/game/menu/trackselectionmenu.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2015 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include "trackselectionmenu.hpp"
17
18 #include "database.hpp"
19 #include "game.hpp"
20 #include "mainmenu.hpp"
21 #include "renderer.hpp"
22 #include "scene.hpp"
23 #include "settings.hpp"
24 #include "timing.hpp"
25 #include "track.hpp"
26 #include "trackdata.hpp"
27 #include "tracktile.hpp"
28
29 #include <MenuItem>
30 #include <MenuManager>
31
32 #include <MCAssetManager>
33 #include <MCSurface>
34 #include <MCTextureFont>
35 #include <MCTextureText>
36
37 #include "../common/config.hpp"
38 #include "../common/mapbase.hpp"
39
40 #include <cassert>
41 #include <memory>
42 #include <sstream>
43
44 #include <QObject> // For QObject::tr()
45
46 const float SAIL_AWAY_HONEY_X = 1000;
47
48 const int ANIMATION_STEPS = 15;
49
50 const int ANIMATION_EXP = 3;
51
52 std::string TrackSelectionMenu::MenuId = "trackSelection";
53
54 class TrackItem : public MTFH::MenuItem
55 {
56 public:
57 TrackItem(int width, int height, std::shared_ptr<Track> track)
58 : MenuItem(width, height)
59 , m_game(Game::instance())
60 , m_track(track)
61 , m_font(MCAssetManager::textureFontManager().font(m_game.fontName()))
62 , m_star(MCAssetManager::surfaceManager().surface("star"))
63 , m_glow(MCAssetManager::surfaceManager().surface("starGlow"))
64 , m_star_half(MCAssetManager::surfaceManager().surface("starHalf"))
65 , m_star_half_r(MCAssetManager::surfaceManager().surface("starHalfR"))
66 , m_glow_half(MCAssetManager::surfaceManager().surface("starHalfGlow"))
67 , m_lock(MCAssetManager::surfaceManager().surface("lock"))
68 {
69 updateData();
70
71 auto && program = Renderer::instance().program("menu");
72 m_star->setShaderProgram(program);
73 m_glow->setShaderProgram(program);
74 m_star_half->setShaderProgram(program);
75 m_star_half_r->setShaderProgram(program);
76 m_glow_half->setShaderProgram(program);
77 m_lock->setShaderProgram(program);
78 }
79
80 std::shared_ptr<Track> track() const
81 {
82 return m_track;
83 }
84
85 // This ensures that data has been updated when player returns to the menu after a race
86 virtual void setFocused(bool focused) override
87 {
88 MenuItem::setFocused(focused);
89 if (focused)
90 {
91 updateData();
92 }
93 }
94
95 //! \reimp
96 virtual void render() override;
97
98 private:
99 void updateData();
100
101 void renderTiles();
102
103 void renderTitle();
104
105 void renderStars();
106
107 void renderLock();
108
109 void renderTrackProperties();
110
111 Game & m_game;
112
113 std::shared_ptr<Track> m_track;
114
115 MCTextureFont & m_font;
116
117 MCSurfacePtr m_star;
118
119 MCSurfacePtr m_glow;
120
121 MCSurfacePtr m_star_half;
122
123 MCSurfacePtr m_star_half_r;
124
125 MCSurfacePtr m_glow_half;
126
127 MCSurfacePtr m_lock;
128
129 int m_lapRecord;
130
131 int m_raceRecord;
132
133 int m_bestPos;
134 };
135
136 void TrackItem::updateData()
137 {
138 const int notSet = -1;
139 const auto lapRecord = Database::instance().loadLapRecord(*m_track);
140 m_lapRecord = lapRecord.second ? lapRecord.first : notSet;
141 const auto raceRecord = Database::instance().loadRaceRecord(
142 *m_track, m_game.lapCount(), m_game.difficultyProfile().difficulty());
143 m_raceRecord = raceRecord.second ? raceRecord.first : notSet;
144 const auto bestPos = Database::instance().loadBestPos(
145 *m_track, m_game.lapCount(), m_game.difficultyProfile().difficulty());
146 m_bestPos = bestPos.second ? bestPos.first : notSet;
147 }
148
149 void TrackItem::renderTiles()
150 {
151 const MapBase & rMap = m_track->trackData().map();
152
153 const auto previewW = width();
154 const auto previewH = height();
155
156 // Set tileW and tileH so that they are squares
157 float tileW = previewW / rMap.cols();
158 float tileH = previewH / rMap.rows();
159
160 if (tileW > tileH)
161 {
162 tileW = tileH;
163 }
164 else
165 {
166 tileH = tileW;
167 }
168
169 // Center the preview
170 float initX;
171 if (rMap.cols() % 2 == 0)
172 {
173 initX = x() - rMap.cols() * tileW / 2 + tileW / 4 + menu()->x();
174 }
175 else
176 {
177 initX = x() - rMap.cols() * tileW / 2 + menu()->x();
178 }
179
180 // Loop through the visible tile matrix and draw the tiles
181 float tileY = y() - rMap.rows() * tileH / 2 + menu()->y();
182 for (size_t j = 0; j < rMap.rows(); j++)
183 {
184 float tileX = initX;
185 for (size_t i = 0; i < rMap.cols(); i++)
186 {
187 const auto tile = std::static_pointer_cast<TrackTile>(rMap.getTile(i, j));
188 const auto surface = tile->previewSurface();
189 if (surface && !tile->excludeFromMinimap())
190 {
191 surface->setShaderProgram(Renderer::instance().program("menu"));
192 surface->bind();
193
194 if (m_track->trackData().isLocked())
195 {
196 surface->setColor(MCGLColor(0.5, 0.5, 0.5));
197 }
198 else
199 {
200 surface->setColor(MCGLColor(1.0, 1.0, 1.0));
201 }
202
203 surface->setSize(tileH, tileW);
204 surface->render(
205 nullptr,
206 MCVector3dF(tileX + tileW / 2, tileY + tileH / 2), tile->rotation());
207 }
208
209 tileX += tileW;
210 }
211
212 tileY += tileH;
213 }
214 }
215
216 void TrackItem::renderTitle()
217 {
218 MCTextureText text(L"");
219
220 const int shadowY = -2;
221 const int shadowX = 2;
222
223 std::wstringstream ss;
224 ss << m_track->trackData().name().toUpper().toStdWString();
225 text.setText(ss.str());
226 text.setGlyphSize(30, 30);
227 text.setShadowOffset(shadowX, shadowY);
228 text.render(menu()->x() + x() - text.width(m_font) / 2, menu()->y() + y() + height() / 2 + text.height(m_font), nullptr, m_font);
229 }
230
231 void TrackItem::renderStars()
232 {
233 if (!m_track->trackData().isLocked())
234 {
235 const auto starW = m_star->width();
236 const auto starH = m_star->height();
237 const auto startX = menu()->x() + x() - 5 * starW + starW / 2;
238 const MCGLColor yellow(1.0, 1.0, 0.0);
239 const MCGLColor grey(.75, .75, .75);
240
241 const int numStars = 10;
242 for (int i = 0; i < numStars; i++)
243 {
244 const MCVector3dF starPos(startX + i * starW, menu()->y() + y() - height() / 2 + starH / 2, 0);
245
246 // The range of m_bestPos is 1..NUM_CARS
247 if (m_bestPos != -1 && numStars - i >= m_bestPos)
248 {
249 m_star->setColor(yellow);
250 m_glow->render(nullptr, starPos, 0);
251 m_star->render(nullptr, starPos, 0);
252 }
253 // Award a half star for rank 11
254 else if (m_bestPos == 11 && i == 0)
255 {
256 m_star_half->setColor(yellow);
257 m_star_half_r->setColor(grey);
258 m_glow_half->render(nullptr, starPos, 0);
259 m_star_half_r->render(nullptr, starPos, 0);
260 m_star_half->render(nullptr, starPos, 0);
261 }
262 else
263 {
264 m_star->setColor(grey);
265 m_star->render(nullptr, starPos, 0);
266 }
267 }
268 }
269 }
270
271 void TrackItem::renderLock()
272 {
273 if (m_track->trackData().isLocked())
274 {
275 m_lock->render(nullptr, { menu()->x() + x(), menu()->y() + y(), 0 }, 0);
276 }
277 }
278
279 void TrackItem::renderTrackProperties()
280 {
281 std::wstringstream ss;
282 MCTextureText text(L"");
283 text.setGlyphSize(20, 20);
284 const int shadowY = -2;
285 const int shadowX = 2;
286 text.setShadowOffset(shadowX, shadowY);
287
288 std::vector<MCTextureText> texts;
289
290 // Render track properties
291 ss.str(L"");
292 ss << QObject::tr(" Laps: ").toStdWString() << Game::instance().lapCount();
293 text.setText(ss.str());
294 text.setGlyphSize(20, 20);
295 float maxWidth = text.width(m_font);
296 texts.push_back(text);
297
298 ss.str(L"");
299 ss << QObject::tr(" Length: ").toStdWString()
300 << static_cast<int>(m_track->trackData().route().geometricLength() * MCWorld::metersPerUnit());
301 text.setText(ss.str());
302 maxWidth = std::fmax(maxWidth, text.width(m_font));
303 texts.push_back(text);
304
305 ss.str(L"");
306 ss << QObject::tr(" Lap Record: ").toStdWString() << Timing::msecsToString(m_lapRecord);
307 text.setText(ss.str());
308 maxWidth = std::fmax(maxWidth, text.width(m_font));
309 if (!m_track->trackData().isLocked())
310 texts.push_back(text);
311
312 ss.str(L"");
313 ss << QObject::tr("Race Record: ").toStdWString() << Timing::msecsToString(m_raceRecord);
314 text.setText(ss.str());
315 maxWidth = std::fmax(maxWidth, text.width(m_font));
316 if (!m_track->trackData().isLocked())
317 texts.push_back(text);
318
319 const float yPos = menu()->y() + y() - height() / 2;
320 const float lineHeight = text.height(m_font);
321 int line = 2;
322 for (auto && text : texts)
323 {
324 const auto textX = menu()->x() + x();
325 text.render(textX - maxWidth / 2, yPos - lineHeight * line, nullptr, m_font);
326 line++;
327 }
328
329 line++;
330
331 if (m_track->trackData().isLocked())
332 {
333 ss.str(L"");
334 if (m_game.hasComputerPlayers())
335 {
336 //: Try to keep the translation as short as possible.
337 ss << QObject::tr("Finish previous track in TOP-6 to unlock!").toStdWString();
338 }
339 else
340 {
341 //: "it" = a locked track. Try to keep the translation as short as possible.
342 ss << QObject::tr("Unlock it in one/two player race!").toStdWString();
343 }
344 text.setText(ss.str());
345 maxWidth = std::fmax(maxWidth, text.width(m_font));
346 const auto textX = menu()->x() + x();
347 text.render(textX - maxWidth / 2, yPos - lineHeight * line, nullptr, m_font);
348 }
349 }
350
351 void TrackItem::render()
352 {
353 renderTiles();
354
355 renderTitle();
356
357 renderStars();
358
359 renderLock();
360
361 renderTrackProperties();
362 }
363
364 TrackSelectionMenu::TrackSelectionMenu(int width, int height, Scene & scene)
365 : SurfaceMenu("trackSelectionBack", MenuId, width, height, Menu::Style::ShowMany, true, true, true)
366 , m_scene(scene)
367 {
368 setWrapAround(false);
369 }
370
371 void TrackSelectionMenu::addTrack(std::shared_ptr<Track> track)
372 {
373 const auto item = std::make_shared<TrackItem>(width() / 2, height() / 2, track);
374 item->setPos(width() / 2, height() / 2);
375 addItem(item);
376 setCurrentIndex(0);
377 setItemsToShow({ 0 });
378 }
379
380 void TrackSelectionMenu::left()
381 {
382 const int prevIndex = currentIndex();
383
384 if (prevIndex > 0)
385 {
386 currentItem()->setPos(width() / 2, height() / 2, width() + SAIL_AWAY_HONEY_X, height() / 2);
387 currentItem()->resetAnimationCurve(ANIMATION_STEPS, ANIMATION_EXP);
388
389 Menu::left();
390
391 currentItem()->setPos(-SAIL_AWAY_HONEY_X, height() / 2, width() / 2, height() / 2);
392 currentItem()->resetAnimationCurve(ANIMATION_STEPS, ANIMATION_EXP);
393
394 setItemsToShow({ prevIndex, currentIndex() });
395 }
396 }
397
398 void TrackSelectionMenu::right()
399 {
400 const int prevIndex = currentIndex();
401
402 if (prevIndex + 1 < static_cast<int>(itemCount()))
403 {
404 currentItem()->setPos(width() / 2, height() / 2, -SAIL_AWAY_HONEY_X, height() / 2);
405 currentItem()->resetAnimationCurve(ANIMATION_STEPS, ANIMATION_EXP);
406
407 Menu::right();
408
409 currentItem()->setPos(width() + SAIL_AWAY_HONEY_X, height() / 2, width() / 2, height() / 2);
410 currentItem()->resetAnimationCurve(ANIMATION_STEPS, ANIMATION_EXP);
411
412 setItemsToShow({ prevIndex, currentIndex() });
413 }
414 }
415
416 void TrackSelectionMenu::up()
417 {
418 left();
419 }
420
421 void TrackSelectionMenu::down()
422 {
423 right();
424 }
425
426 void TrackSelectionMenu::exit()
427 {
428 MTFH::MenuManager::instance().popToMenu(MainMenu::MenuId);
429 }
430
431 void TrackSelectionMenu::selectCurrentItem()
432 {
433 Menu::selectCurrentItem();
434 auto && selectedTrack = std::static_pointer_cast<TrackItem>(currentItem())->track();
435 if (!selectedTrack->trackData().isLocked())
436 {
437 m_selectedTrack = selectedTrack;
438 m_scene.setActiveTrack(m_selectedTrack);
439 setIsDone(true);
440 }
441 }
442
443 std::shared_ptr<Track> TrackSelectionMenu::selectedTrack() const
444 {
445 return m_selectedTrack;
446 }