| 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 "menu.hpp" |
| 17 | #include "menumanager.hpp" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <cassert> |
| 21 | #include <iostream> |
| 22 | namespace MTFH { |
| 23 | |
| 24 | Menu::Menu(std::string id, int width, int height, Menu::Style style) |
| 25 | : m_id(id) |
| 26 | , m_width(width) |
| 27 | , m_height(height) |
| 28 | , m_x(0) |
| 29 | , m_y(0) |
| 30 | , m_targetX(0) |
| 31 | , m_targetY(0) |
| 32 | , m_currentIndex(-1) |
| 33 | , m_selectedIndex(-1) |
| 34 | , m_style(style) |
| 35 | , m_isDone(false) |
| 36 | , m_wrapAround(true) |
| 37 | , m_animationCurve(15, 3) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | std::string Menu::id() const |
| 42 | { |
| 43 | return m_id; |
| 44 | } |
| 45 | |
| 46 | void Menu::addItem(MenuItemPtr menuItem) |
| 47 | { |
| 48 | m_items.push_back(menuItem); |
| 49 | m_currentIndex = static_cast<int>(m_items.size()) - 1; |
| 50 | m_selectedIndex = m_currentIndex; |
| 51 | menuItem->setMenu(this); |
| 52 | menuItem->setIndex(static_cast<int>(m_items.size()) - 1); |
| 53 | |
| 54 | updateFocus(); |
| 55 | } |
| 56 | |
| 57 | void Menu::reverseItems() |
| 58 | { |
| 59 | std::reverse(m_items.begin(), m_items.end()); |
| 60 | m_currentIndex = static_cast<int>(m_items.size()) - 1 - m_currentIndex; |
| 61 | m_selectedIndex = m_currentIndex; |
| 62 | |
| 63 | for (size_t i = 0; i < m_items.size(); i++) |
| 64 | { |
| 65 | m_items.at(i)->setIndex(static_cast<int>(i)); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void Menu::addMouseItem(Menu::MouseItemType type, MenuItemPtr menuItem) |
| 70 | { |
| 71 | MouseItem mouseItem; |
| 72 | mouseItem.item = menuItem; |
| 73 | mouseItem.type = type; |
| 74 | m_mouseItems.push_back(mouseItem); |
| 75 | } |
| 76 | |
| 77 | MenuItemPtr Menu::currentItem() const |
| 78 | { |
| 79 | if (m_items.size()) |
| 80 | { |
| 81 | return m_items.at(static_cast<size_t>(m_currentIndex)); |
| 82 | } |
| 83 | |
| 84 | return nullptr; |
| 85 | } |
| 86 | |
| 87 | MenuItemPtr Menu::selectedItem() const |
| 88 | { |
| 89 | if (m_items.size()) |
| 90 | { |
| 91 | return m_items.at(static_cast<size_t>(m_selectedIndex)); |
| 92 | } |
| 93 | |
| 94 | return nullptr; |
| 95 | } |
| 96 | |
| 97 | MenuItemPtr Menu::item(size_t index) const |
| 98 | { |
| 99 | if (index < m_items.size()) |
| 100 | { |
| 101 | return m_items.at(index); |
| 102 | } |
| 103 | |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
| 107 | size_t Menu::itemCount() const |
| 108 | { |
| 109 | return m_items.size(); |
| 110 | } |
| 111 | |
| 112 | void Menu::render() |
| 113 | { |
| 114 | renderItems(); |
| 115 | renderMouseItems(); |
| 116 | } |
| 117 | |
| 118 | void Menu::renderItems() |
| 119 | { |
| 120 | if (!m_items.size()) |
| 121 | { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | if (m_style == Menu::Style::VerticalList) |
| 126 | { |
| 127 | // Calculate total height |
| 128 | float totalHeight = 0; |
| 129 | for (auto && item : m_items) |
| 130 | { |
| 131 | totalHeight += item->height(); |
| 132 | } |
| 133 | |
| 134 | // Render centered items |
| 135 | float startY = y() + m_height / 2 - totalHeight / 2 + totalHeight / m_items.size() / 2; |
| 136 | for (auto && item : m_items) |
| 137 | { |
| 138 | item->setPos(x() + m_width / 2, startY); |
| 139 | item->render(); |
| 140 | startY += item->height(); |
| 141 | } |
| 142 | } |
| 143 | else if (m_style == Menu::Style::HorizontalList) |
| 144 | { |
| 145 | // Calculate total width |
| 146 | float totalWidth = 0; |
| 147 | for (auto && item : m_items) |
| 148 | { |
| 149 | totalWidth += item->width(); |
| 150 | } |
| 151 | |
| 152 | // Render centered items |
| 153 | float startX = x() + m_width / 2 - totalWidth / 2 + totalWidth / m_items.size() / 2; |
| 154 | for (auto && item : m_items) |
| 155 | { |
| 156 | item->setPos(startX, y() + m_height / 2); |
| 157 | item->render(); |
| 158 | startX += item->width(); |
| 159 | } |
| 160 | } |
| 161 | else if (m_style == Menu::Style::ShowOne) |
| 162 | { |
| 163 | const auto item = m_items.at(static_cast<size_t>(m_currentIndex)); |
| 164 | item->setPos(x() + m_width / 2, y() + m_height / 2); |
| 165 | item->render(); |
| 166 | } |
| 167 | else if (m_style == Menu::Style::ShowMany) |
| 168 | { |
| 169 | for (auto index : m_itemsToShow) |
| 170 | { |
| 171 | if (index >= 0 && index < static_cast<int>(m_items.size())) |
| 172 | { |
| 173 | m_items.at(static_cast<size_t>(index))->render(); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | bool Menu::isNextAllowed() const |
| 180 | { |
| 181 | return m_wrapAround || static_cast<size_t>(m_currentIndex) + 1 < m_items.size(); |
| 182 | } |
| 183 | |
| 184 | bool Menu::isPrevAllowed() const |
| 185 | { |
| 186 | return m_wrapAround || m_currentIndex > 0; |
| 187 | } |
| 188 | |
| 189 | void Menu::renderMouseItems() |
| 190 | { |
| 191 | for (auto && item : m_mouseItems) |
| 192 | { |
| 193 | switch (item.type) |
| 194 | { |
| 195 | case Menu::MouseItemType::Quit: |
| 196 | item.item->setPos(x() + m_width - item.item->width(), y() + m_height - item.item->height()); |
| 197 | item.item->render(); |
| 198 | break; |
| 199 | |
| 200 | case Menu::MouseItemType::Prev: |
| 201 | if (isPrevAllowed()) |
| 202 | { |
| 203 | item.item->setPos(x() + item.item->width(), y() + m_height / 2); |
| 204 | item.item->render(); |
| 205 | } |
| 206 | break; |
| 207 | |
| 208 | case Menu::MouseItemType::Next: |
| 209 | if (isNextAllowed()) |
| 210 | { |
| 211 | item.item->setPos(x() + m_width - item.item->width(), y() + m_height / 2); |
| 212 | item.item->render(); |
| 213 | } |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void Menu::incrementIndex(int increment) |
| 220 | { |
| 221 | if (m_wrapAround) |
| 222 | { |
| 223 | setCurrentIndexWrapAround(m_currentIndex + increment); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | setCurrentIndex(m_currentIndex + increment); |
| 228 | } |
| 229 | |
| 230 | updateFocus(); |
| 231 | } |
| 232 | |
| 233 | void Menu::up() |
| 234 | { |
| 235 | incrementIndex(1); |
| 236 | } |
| 237 | |
| 238 | void Menu::down() |
| 239 | { |
| 240 | incrementIndex(-1); |
| 241 | } |
| 242 | |
| 243 | void Menu::left() |
| 244 | { |
| 245 | incrementIndex(-1); |
| 246 | } |
| 247 | |
| 248 | void Menu::right() |
| 249 | { |
| 250 | incrementIndex(1); |
| 251 | } |
| 252 | |
| 253 | void Menu::selectCurrentItem() |
| 254 | { |
| 255 | if (m_items.size()) |
| 256 | { |
| 257 | for (auto item : m_items) |
| 258 | { |
| 259 | item->setSelected(false); |
| 260 | } |
| 261 | |
| 262 | m_items.at(static_cast<size_t>(m_currentIndex))->setSelected(true); |
| 263 | m_selectedIndex = m_currentIndex; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | bool Menu::checkIfHit(MenuItemPtr item, int x, int y) |
| 268 | { |
| 269 | const int x1 = static_cast<int>(item->x() - item->width() / 2); |
| 270 | const int x2 = static_cast<int>(item->x() + item->width() / 2); |
| 271 | const int y1 = static_cast<int>(item->y() - item->height() / 2); |
| 272 | const int y2 = static_cast<int>(item->y() + item->height() / 2); |
| 273 | |
| 274 | return x >= x1 && x <= x2 && y >= y1 && y <= y2; |
| 275 | } |
| 276 | |
| 277 | bool Menu::handleMousePressOnMouseItem(int x, int y) |
| 278 | { |
| 279 | for (auto && mouseItem : m_mouseItems) |
| 280 | { |
| 281 | auto item = mouseItem.item; |
| 282 | switch (mouseItem.type) |
| 283 | { |
| 284 | case MouseItemType::Quit: |
| 285 | if (checkIfHit(item, x, y)) |
| 286 | { |
| 287 | item->setFocused(true); |
| 288 | return true; |
| 289 | } |
| 290 | break; |
| 291 | |
| 292 | case MouseItemType::Prev: |
| 293 | if (isPrevAllowed() && checkIfHit(item, x, y)) |
| 294 | { |
| 295 | item->setFocused(true); |
| 296 | return true; |
| 297 | } |
| 298 | break; |
| 299 | |
| 300 | case MouseItemType::Next: |
| 301 | if (isNextAllowed() && checkIfHit(item, x, y)) |
| 302 | { |
| 303 | item->setFocused(true); |
| 304 | return true; |
| 305 | } |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | bool Menu::handleMousePressOnItem(int x, int y) |
| 314 | { |
| 315 | for (auto && mouseItem : m_mouseItems) |
| 316 | { |
| 317 | if (const auto item = mouseItem.item; checkIfHit(item, x, y)) |
| 318 | { |
| 319 | item->setFocused(true); |
| 320 | return true; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if (m_style != Menu::Style::ShowOne) |
| 325 | { |
| 326 | for (size_t i = 0; i < m_items.size(); i++) |
| 327 | { |
| 328 | if (auto item = m_items.at(i); checkIfHit(item, x, y)) |
| 329 | { |
| 330 | setCurrentIndex(static_cast<int>(i)); |
| 331 | return true; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | bool Menu::handleMousePress(int x, int y, int screenWidth, int screenHeight) |
| 340 | { |
| 341 | x = x * width() / screenWidth; |
| 342 | y = y * height() / screenHeight; |
| 343 | |
| 344 | return handleMousePressOnMouseItem(x, y) || handleMousePressOnItem(x, y); |
| 345 | } |
| 346 | |
| 347 | bool Menu::handleMouseReleaseOnMouseItem(int x, int y) |
| 348 | { |
| 349 | for (auto && mouseItem : m_mouseItems) |
| 350 | { |
| 351 | mouseItem.item->setFocused(false); |
| 352 | } |
| 353 | |
| 354 | for (auto && mouseItem : m_mouseItems) |
| 355 | { |
| 356 | if (auto item = mouseItem.item; checkIfHit(item, x, y)) |
| 357 | { |
| 358 | switch (mouseItem.type) |
| 359 | { |
| 360 | case Menu::MouseItemType::Quit: |
| 361 | exit(); |
| 362 | return true; |
| 363 | |
| 364 | case Menu::MouseItemType::Prev: |
| 365 | if (isPrevAllowed()) |
| 366 | { |
| 367 | left(); |
| 368 | return true; |
| 369 | } |
| 370 | break; |
| 371 | |
| 372 | case Menu::MouseItemType::Next: |
| 373 | if (isNextAllowed()) |
| 374 | { |
| 375 | right(); |
| 376 | return true; |
| 377 | } |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | bool Menu::handleMouseReleaseOnItem(int x, int y) |
| 387 | { |
| 388 | if (m_style == Menu::Style::ShowOne) |
| 389 | { |
| 390 | if (const auto item = currentItem(); checkIfHit(item, x, y)) |
| 391 | { |
| 392 | selectCurrentItem(); |
| 393 | return true; |
| 394 | } |
| 395 | } |
| 396 | else |
| 397 | { |
| 398 | for (size_t i = 0; i < m_items.size(); i++) |
| 399 | { |
| 400 | if (const auto item = m_items.at(i); checkIfHit(item, x, y)) |
| 401 | { |
| 402 | if (static_cast<int>(i) == m_currentIndex) |
| 403 | { |
| 404 | selectCurrentItem(); |
| 405 | } |
| 406 | |
| 407 | return true; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | bool Menu::handleMouseRelease(int x, int y, int screenWidth, int screenHeight) |
| 416 | { |
| 417 | x = x * width() / screenWidth; |
| 418 | y = y * height() / screenHeight; |
| 419 | |
| 420 | return handleMouseReleaseOnMouseItem(x, y) || handleMouseReleaseOnItem(x, y); |
| 421 | } |
| 422 | |
| 423 | void Menu::updateFocus() |
| 424 | { |
| 425 | if (m_items.size()) |
| 426 | { |
| 427 | for (auto item : m_items) |
| 428 | { |
| 429 | item->setFocused(false); |
| 430 | } |
| 431 | |
| 432 | m_items.at(static_cast<size_t>(m_currentIndex))->setFocused(true); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | int Menu::width() const |
| 437 | { |
| 438 | return m_width; |
| 439 | } |
| 440 | |
| 441 | int Menu::height() const |
| 442 | { |
| 443 | return m_height; |
| 444 | } |
| 445 | |
| 446 | void Menu::setPos(float x, float y) |
| 447 | { |
| 448 | m_x = x; |
| 449 | m_y = y; |
| 450 | m_targetX = x; |
| 451 | m_targetY = y; |
| 452 | } |
| 453 | |
| 454 | void Menu::setPos(float x, float y, float targetX, float targetY) |
| 455 | { |
| 456 | m_x = x; |
| 457 | m_y = y; |
| 458 | m_targetX = targetX; |
| 459 | m_targetY = targetY; |
| 460 | } |
| 461 | |
| 462 | float Menu::x() const |
| 463 | { |
| 464 | return m_x; |
| 465 | } |
| 466 | |
| 467 | float Menu::y() const |
| 468 | { |
| 469 | return m_y; |
| 470 | } |
| 471 | |
| 472 | float Menu::targetY() const |
| 473 | { |
| 474 | return m_targetY; |
| 475 | } |
| 476 | |
| 477 | float Menu::targetX() const |
| 478 | { |
| 479 | return m_targetX; |
| 480 | } |
| 481 | |
| 482 | int Menu::currentIndex() const |
| 483 | { |
| 484 | return m_currentIndex; |
| 485 | } |
| 486 | |
| 487 | void Menu::setCurrentIndex(int index) |
| 488 | { |
| 489 | if (const int numItems = static_cast<int>(m_items.size()); !numItems) |
| 490 | { |
| 491 | m_currentIndex = -1; |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | m_currentIndex = index; |
| 496 | m_currentIndex = m_currentIndex < 0 ? 0 : m_currentIndex; |
| 497 | m_currentIndex = m_currentIndex >= numItems ? numItems - 1 : m_currentIndex; |
| 498 | |
| 499 | updateFocus(); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | void Menu::setCurrentIndexWrapAround(int index) |
| 504 | { |
| 505 | if (const int numItems = static_cast<int>(m_items.size()); !numItems) |
| 506 | { |
| 507 | m_currentIndex = -1; |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | m_currentIndex = index; |
| 512 | m_currentIndex = m_currentIndex < 0 ? numItems - 1 : m_currentIndex; |
| 513 | m_currentIndex = m_currentIndex >= numItems ? 0 : m_currentIndex; |
| 514 | |
| 515 | updateFocus(); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | void Menu::setItemsToShow(const std::vector<int> & itemsToShow) |
| 520 | { |
| 521 | m_itemsToShow = itemsToShow; |
| 522 | } |
| 523 | |
| 524 | bool Menu::isDone() const |
| 525 | { |
| 526 | return m_isDone; |
| 527 | } |
| 528 | |
| 529 | void Menu::setIsDone(bool done) |
| 530 | { |
| 531 | m_isDone = done; |
| 532 | } |
| 533 | |
| 534 | void Menu::enter() |
| 535 | { |
| 536 | m_isDone = false; |
| 537 | |
| 538 | m_animationCurve.reset(); |
| 539 | |
| 540 | updateFocus(); |
| 541 | } |
| 542 | |
| 543 | void Menu::pushEnter() |
| 544 | { |
| 545 | m_isDone = false; |
| 546 | |
| 547 | m_animationCurve.reset(); |
| 548 | |
| 549 | updateFocus(); |
| 550 | } |
| 551 | |
| 552 | void Menu::pushExit() |
| 553 | { |
| 554 | m_animationCurve.reset(); |
| 555 | } |
| 556 | |
| 557 | void Menu::popEnter() |
| 558 | { |
| 559 | m_isDone = false; |
| 560 | |
| 561 | m_animationCurve.reset(); |
| 562 | |
| 563 | updateFocus(); |
| 564 | } |
| 565 | |
| 566 | void Menu::popExit() |
| 567 | { |
| 568 | m_animationCurve.reset(); |
| 569 | } |
| 570 | |
| 571 | void Menu::setWrapAround(bool wrapAround) |
| 572 | { |
| 573 | m_wrapAround = wrapAround; |
| 574 | } |
| 575 | |
| 576 | void Menu::exit() |
| 577 | { |
| 578 | MenuManager::instance().popMenu(); |
| 579 | } |
| 580 | |
| 581 | void Menu::stepTime(int msecs) |
| 582 | { |
| 583 | for (auto item : m_items) |
| 584 | { |
| 585 | item->stepTime(msecs); |
| 586 | } |
| 587 | |
| 588 | positionAnimation(msecs); |
| 589 | } |
| 590 | |
| 591 | void Menu::positionAnimation(int) |
| 592 | { |
| 593 | m_animationCurve.step(); |
| 594 | |
| 595 | m_x = m_x + (m_targetX - m_x) * m_animationCurve.value(); |
| 596 | m_y = m_y + (m_targetY - m_y) * m_animationCurve.value(); |
| 597 | } |
| 598 | |
| 599 | Menu::~Menu() = default; |
| 600 | |
| 601 | } // namespace MTFH |