| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2012 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 "timingoverlay.hpp" |
| 17 | |
| 18 | #include "car.hpp" |
| 19 | #include "game.hpp" |
| 20 | #include "race.hpp" |
| 21 | #include "scene.hpp" |
| 22 | #include "timing.hpp" |
| 23 | |
| 24 | #include <MCAssetManager> |
| 25 | #include <MCCamera> |
| 26 | |
| 27 | #include <cassert> |
| 28 | #include <sstream> |
| 29 | |
| 30 | #include <QObject> // For QObject::tr() |
| 31 | #include <QTimer> |
| 32 | |
| 33 | static const int RACE_TIME_POS = 1; |
| 34 | static const int CURRENT_LAP_TIME_POS = 2; |
| 35 | static const int LAST_LAP_TIME_POS = 3; |
| 36 | static const int RECORD_LAP_TIME_POS = 4; |
| 37 | static const int GLYPH_W_TIMES = 15; |
| 38 | static const int GLYPH_H_TIMES = 15; |
| 39 | static const int GLYPH_W_POS = 20; |
| 40 | static const int GLYPH_H_POS = 20; |
| 41 | |
| 42 | static const MCGLColor RED(1.0, 0.0, 0.0); |
| 43 | static const MCGLColor GREEN(0.0, 1.0, 0.0); |
| 44 | static const MCGLColor YELLOW(1.0, 1.0, 0.0); |
| 45 | static const MCGLColor WHITE(1.0, 1.0, 1.0); |
| 46 | |
| 47 | TimingOverlay::TimingOverlay() |
| 48 | : m_fontManager { MCAssetManager::textureFontManager() } |
| 49 | , m_font { m_fontManager.font(Game::instance().fontName()) } |
| 50 | , m_text { L"" } |
| 51 | , m_posTexts({ QObject::tr("---").toStdWString(), |
| 52 | QObject::tr("1st").toStdWString(), |
| 53 | QObject::tr("2nd").toStdWString(), |
| 54 | QObject::tr("3rd").toStdWString(), |
| 55 | QObject::tr("4th").toStdWString(), |
| 56 | QObject::tr("5th").toStdWString(), |
| 57 | QObject::tr("6th").toStdWString(), |
| 58 | QObject::tr("7th").toStdWString(), |
| 59 | QObject::tr("8th").toStdWString(), |
| 60 | QObject::tr("9th").toStdWString(), |
| 61 | QObject::tr("10th").toStdWString(), |
| 62 | QObject::tr("11th").toStdWString(), |
| 63 | QObject::tr("12th").toStdWString() }) |
| 64 | { |
| 65 | assert(Scene::carCount() + 1 <= m_posTexts.size()); |
| 66 | m_text.setShadowOffset(2, -2); |
| 67 | } |
| 68 | |
| 69 | void TimingOverlay::setCarToFollow(const Car & car) |
| 70 | { |
| 71 | m_car = &car; |
| 72 | m_carStatusView.setCarToFollow(car); |
| 73 | } |
| 74 | |
| 75 | void TimingOverlay::setRace(RaceS race) |
| 76 | { |
| 77 | m_race = race; |
| 78 | |
| 79 | connect(m_race.get(), &Race::lapRecordAchieved, this, &TimingOverlay::setLapRecord); |
| 80 | connect(m_race.get(), &Race::raceRecordAchieved, this, &TimingOverlay::setRaceRecord); |
| 81 | connect(m_race.get(), &Race::tiresChanged, this, static_cast<void (TimingOverlay::*)()>(&TimingOverlay::blinkCarStatus)); |
| 82 | } |
| 83 | |
| 84 | void TimingOverlay::setLapRecord(int) |
| 85 | { |
| 86 | blinkLapRecord(); |
| 87 | } |
| 88 | |
| 89 | void TimingOverlay::setRaceRecord(int) |
| 90 | { |
| 91 | blinkRaceRecord(); |
| 92 | } |
| 93 | |
| 94 | void TimingOverlay::blinkLapRecord() |
| 95 | { |
| 96 | static int count = 0; |
| 97 | if (count < 10) |
| 98 | { |
| 99 | QTimer::singleShot(250, this, &TimingOverlay::blinkLapRecord); |
| 100 | m_showLapRecordTime = !m_showLapRecordTime; |
| 101 | count++; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | m_showLapRecordTime = true; |
| 106 | count = 0; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void TimingOverlay::blinkRaceRecord() |
| 111 | { |
| 112 | static int count = 0; |
| 113 | if (count < 10) |
| 114 | { |
| 115 | QTimer::singleShot(250, this, &TimingOverlay::blinkRaceRecord); |
| 116 | m_showRaceTime = !m_showRaceTime; |
| 117 | count++; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | m_showRaceTime = true; |
| 122 | count = 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void TimingOverlay::blinkCarStatus() |
| 127 | { |
| 128 | static int count = 0; |
| 129 | if (count < 10) |
| 130 | { |
| 131 | QTimer::singleShot(250, this, static_cast<void (TimingOverlay::*)()>(&TimingOverlay::blinkCarStatus)); |
| 132 | m_showCarStatus = !m_showCarStatus; |
| 133 | count++; |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | m_showCarStatus = true; |
| 138 | count = 0; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void TimingOverlay::blinkCarStatus(const Car & car) |
| 143 | { |
| 144 | if (m_car == &car) |
| 145 | { |
| 146 | blinkCarStatus(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void TimingOverlay::render() |
| 151 | { |
| 152 | if (m_car && m_race) |
| 153 | { |
| 154 | renderCurrentLap(); |
| 155 | renderPosition(); |
| 156 | renderSpeed(); |
| 157 | renderRaceTime(); |
| 158 | renderCurrentLapTime(); |
| 159 | renderLastLapTime(); |
| 160 | renderRecordLapTime(); |
| 161 | renderCarStatusView(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void TimingOverlay::renderCurrentLap() |
| 166 | { |
| 167 | const auto leadersLap = m_race->timing().lock()->leadersLap() + 1; |
| 168 | const auto laps = m_race->lapCount(); |
| 169 | |
| 170 | m_text.setGlyphSize(GLYPH_W_POS, GLYPH_H_POS); |
| 171 | |
| 172 | // Render the current lap number |
| 173 | std::wstringstream ss; |
| 174 | ss << QObject::tr(" LAP:").toStdWString() << (leadersLap <= laps ? leadersLap : laps) << "/" << laps; |
| 175 | |
| 176 | m_text.setText(ss.str()); |
| 177 | m_text.setColor(WHITE); |
| 178 | m_text.render(0, static_cast<float>(height()) - m_text.height(m_font), nullptr, m_font); |
| 179 | } |
| 180 | |
| 181 | void TimingOverlay::renderPosition() |
| 182 | { |
| 183 | const auto pos = m_race->position(m_car->index()); |
| 184 | const auto lap = m_race->timing().lock()->lap(m_car->index()) + 1; |
| 185 | const auto leadersLap = m_race->timing().lock()->leadersLap() + 1; |
| 186 | |
| 187 | m_text.setGlyphSize(GLYPH_W_POS, GLYPH_H_POS); |
| 188 | |
| 189 | std::wstringstream ss; |
| 190 | //: Position/rank |
| 191 | ss << QObject::tr(" POS:").toStdWString(); |
| 192 | ss << m_posTexts.at(pos); |
| 193 | |
| 194 | if (const int lapDiff = static_cast<int>(leadersLap) - static_cast<int>(lap); lapDiff > 0) |
| 195 | { |
| 196 | ss << "+" |
| 197 | << lapDiff |
| 198 | << (lapDiff == 1 ? QObject::tr("LAP").toStdWString() : QObject::tr("LAPS").toStdWString()); |
| 199 | } |
| 200 | |
| 201 | m_text.setText(ss.str()); |
| 202 | m_text.setColor(YELLOW); |
| 203 | m_text.render(0, static_cast<float>(height()) - m_text.height(m_font) * 2, nullptr, m_font); |
| 204 | } |
| 205 | |
| 206 | void TimingOverlay::renderSpeed() |
| 207 | { |
| 208 | // Minimaps replace speedometer in the split modes |
| 209 | if (!Game::instance().hasTwoHumanPlayers()) |
| 210 | { |
| 211 | int speed = m_car->speedInKmh(); |
| 212 | speed = speed < 0 ? 0 : speed; |
| 213 | |
| 214 | std::wstringstream ss; |
| 215 | ss << " " << speed; |
| 216 | if (speed < 100) |
| 217 | { |
| 218 | m_text.setColor(WHITE); |
| 219 | } |
| 220 | else if (speed < 200) |
| 221 | { |
| 222 | m_text.setColor(YELLOW); |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | m_text.setColor(RED); |
| 227 | } |
| 228 | |
| 229 | m_text.setText(ss.str()); |
| 230 | m_text.setGlyphSize(40, 40); |
| 231 | |
| 232 | const float h = static_cast<float>(m_text.height(m_font)); |
| 233 | m_text.render(0, h, nullptr, m_font); |
| 234 | |
| 235 | m_text.setText(QObject::tr(" KM/H").toStdWString()); |
| 236 | m_text.setGlyphSize(20, 20); |
| 237 | m_text.setColor(WHITE); |
| 238 | m_text.render(0, 2 * m_text.height(m_font) + h, nullptr, m_font); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void TimingOverlay::renderCurrentLapTime() |
| 243 | { |
| 244 | const int lastLapTime = m_race->timing().lock()->lastLapTime(m_car->index()); |
| 245 | const int currentLapTime = m_race->timing().lock()->currentLapTime(m_car->index()); |
| 246 | |
| 247 | std::wstringstream ss; |
| 248 | ss << QObject::tr("LAP:").toStdWString(); |
| 249 | if (m_race->timing().lock()->raceCompleted(m_car->index())) |
| 250 | { |
| 251 | ss << m_race->timing().lock()->msecsToString(lastLapTime); |
| 252 | |
| 253 | m_text.setColor(WHITE); |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | ss << m_race->timing().lock()->msecsToString(currentLapTime); |
| 258 | |
| 259 | // Set color to WHITE, if lastLapTime is not set. |
| 260 | if (lastLapTime == -1 || currentLapTime == lastLapTime) |
| 261 | { |
| 262 | m_text.setColor(WHITE); |
| 263 | } |
| 264 | // Set color to GREEN, if current time is ahead of the last lap time. |
| 265 | else if (currentLapTime < lastLapTime) |
| 266 | { |
| 267 | m_text.setColor(GREEN); |
| 268 | } |
| 269 | // Set color to RED (current time is slower than the last lap time). |
| 270 | else |
| 271 | { |
| 272 | m_text.setColor(RED); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | m_text.setText(ss.str()); |
| 277 | m_text.setGlyphSize(GLYPH_W_TIMES, GLYPH_H_TIMES); |
| 278 | m_text.render( |
| 279 | static_cast<float>(width()) - m_text.width(m_font), |
| 280 | static_cast<float>(height()) - m_text.height(m_font) * CURRENT_LAP_TIME_POS, |
| 281 | nullptr, |
| 282 | m_font); |
| 283 | } |
| 284 | |
| 285 | void TimingOverlay::renderLastLapTime() |
| 286 | { |
| 287 | const int lastLapTime = m_race->timing().lock()->lastLapTime(m_car->index()); |
| 288 | |
| 289 | m_text.setGlyphSize(GLYPH_W_TIMES, GLYPH_H_TIMES); |
| 290 | m_text.setColor(WHITE); |
| 291 | |
| 292 | std::wstringstream ss; |
| 293 | //: Last lap time |
| 294 | ss << QObject::tr("L:").toStdWString() << m_race->timing().lock()->msecsToString(lastLapTime); |
| 295 | m_text.setText(ss.str()); |
| 296 | m_text.render( |
| 297 | static_cast<float>(width()) - m_text.width(m_font), |
| 298 | static_cast<float>(height()) - m_text.height(m_font) * LAST_LAP_TIME_POS, |
| 299 | nullptr, |
| 300 | m_font); |
| 301 | } |
| 302 | |
| 303 | void TimingOverlay::renderRecordLapTime() |
| 304 | { |
| 305 | if (m_showLapRecordTime) |
| 306 | { |
| 307 | const int recordLapTime = m_race->timing().lock()->lapRecord(); |
| 308 | |
| 309 | m_text.setGlyphSize(GLYPH_W_TIMES, GLYPH_H_TIMES); |
| 310 | m_text.setColor(WHITE); |
| 311 | |
| 312 | std::wstringstream ss; |
| 313 | //: Lap record time |
| 314 | ss << QObject::tr("R:").toStdWString() << m_race->timing().lock()->msecsToString(recordLapTime); |
| 315 | m_text.setText(ss.str()); |
| 316 | m_text.render( |
| 317 | static_cast<float>(width()) - m_text.width(m_font), |
| 318 | static_cast<float>(height()) - m_text.height(m_font) * RECORD_LAP_TIME_POS, |
| 319 | nullptr, |
| 320 | m_font); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void TimingOverlay::renderRaceTime() |
| 325 | { |
| 326 | if (m_showRaceTime) |
| 327 | { |
| 328 | const auto raceTime = m_race->timing().lock()->raceTime(m_car->index()); |
| 329 | |
| 330 | m_text.setGlyphSize(GLYPH_W_TIMES, GLYPH_H_TIMES); |
| 331 | m_text.setColor(WHITE); |
| 332 | |
| 333 | std::wstringstream ss; |
| 334 | //: Total race time |
| 335 | ss << QObject::tr("TOT:").toStdWString() << m_race->timing().lock()->msecsToString(static_cast<int>(raceTime.count())); |
| 336 | m_text.setText(ss.str()); |
| 337 | m_text.render( |
| 338 | static_cast<float>(width()) - m_text.width(m_font), |
| 339 | static_cast<float>(height()) - m_text.height(m_font) * RACE_TIME_POS, |
| 340 | nullptr, |
| 341 | m_font); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | void TimingOverlay::renderCarStatusView() |
| 346 | { |
| 347 | if (m_showCarStatus) |
| 348 | { |
| 349 | m_carStatusView.setPos(width() - m_carStatusView.width(), m_carStatusView.height() + 10); |
| 350 | m_carStatusView.render(); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | bool TimingOverlay::update() |
| 355 | { |
| 356 | return true; |
| 357 | } |