Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Menu.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*!
  2. * \file src/Menu.cpp
  3. * \brief Menu 'overlay'
  4. *
  5. * \author xythobuz
  6. */
  7. #ifdef __APPLE__
  8. #include <OpenGL/gl.h>
  9. #else
  10. #include <GL/gl.h>
  11. #endif
  12. #include "config.h"
  13. #include "global.h"
  14. #include "main.h"
  15. #include "Menu.h"
  16. #include "utils/strings.h"
  17. Menu::Menu() {
  18. mVisible = false;
  19. mCursor = 0;
  20. mMin = 0;
  21. mainText.text = bufferString(VERSION);
  22. mainText.color[0] = 0xFF;
  23. mainText.color[1] = 0xFF;
  24. mainText.color[2] = 0xFF;
  25. mainText.color[3] = 0xFF;
  26. mainText.scale = 1.2f;
  27. mainText.y = 10;
  28. mainText.w = 0;
  29. mainText.h = 0;
  30. }
  31. Menu::~Menu() {
  32. delete [] mainText.text;
  33. }
  34. void Menu::setVisible(bool visible) {
  35. mVisible = visible;
  36. }
  37. bool Menu::isVisible() {
  38. return mVisible;
  39. }
  40. void Menu::displayMapList() {
  41. // Estimate displayable number of items
  42. int items = (gOpenRaider->mWindow->mHeight - 110) / 25;
  43. // Select which part of the list to show
  44. int min, max;
  45. if (((int)mCursor - (items / 2)) > 0)
  46. min = mCursor - (items / 2);
  47. else
  48. min = 0;
  49. if ((mCursor + (items / 2)) < gOpenRaider->mMapList.size())
  50. max = mCursor + (items / 2);
  51. else
  52. max = gOpenRaider->mMapList.size();
  53. while ((max - min) < items) {
  54. if (min > 0)
  55. min--;
  56. else if (max < ((int)gOpenRaider->mMapList.size()))
  57. max++;
  58. else
  59. break;
  60. }
  61. mMin = min;
  62. for (int i = 0; i < (max - min); i++) {
  63. char *map = gOpenRaider->mMapList[i + min];
  64. if ((i + min) == (int)mCursor)
  65. gOpenRaider->mWindow->drawText(25, 100 + (25 * i), 0.75f, RED, "%s", map);
  66. else
  67. gOpenRaider->mWindow->drawText(25, 100 + (25 * i), 0.75f, OR_BLUE, "%s", map);
  68. }
  69. }
  70. void Menu::display() {
  71. Window *window = gOpenRaider->mWindow;
  72. if (mVisible) {
  73. // Draw half-transparent *overlay*
  74. glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
  75. glDisable(GL_TEXTURE_2D);
  76. glRecti(0, 0, window->mWidth, window->mHeight);
  77. glEnable(GL_TEXTURE_2D);
  78. // Draw heading text
  79. mainText.x = (window->mWidth / 2) - (mainText.w / 2);
  80. window->writeString(&mainText);
  81. if (!gOpenRaider->mMapListFilled) {
  82. gOpenRaider->mWindow->drawText(25, (window->mHeight / 2) - 20, 0.75f, OR_BLUE, "Generating map list...");
  83. } else {
  84. if (gOpenRaider->mMapList.size() == 0) {
  85. gOpenRaider->mWindow->drawText(25, (window->mHeight / 2) - 20, 0.75f, RED, "No maps found! See README.md");
  86. } else {
  87. // draw *play button* above list
  88. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  89. glDisable(GL_TEXTURE_2D);
  90. glRecti(25, 25, 100, 75);
  91. glEnable(GL_TEXTURE_2D);
  92. gOpenRaider->mWindow->drawText(40, 35, 0.75f, BLACK, "Play");
  93. displayMapList();
  94. }
  95. }
  96. }
  97. }
  98. void Menu::handleKeyboard(KeyboardButton key, bool pressed) {
  99. if (!pressed)
  100. return;
  101. if (key == up) {
  102. if (mCursor > 0)
  103. mCursor--;
  104. else
  105. mCursor = gOpenRaider->mMapList.size() - 1;
  106. } else if (key == down) {
  107. if (mCursor < (gOpenRaider->mMapList.size() - 1))
  108. mCursor++;
  109. else
  110. mCursor = 0;
  111. } else if (key == right) {
  112. int i = 10;
  113. if (mCursor > (gOpenRaider->mMapList.size() - 11))
  114. i = gOpenRaider->mMapList.size() - 1 - mCursor;
  115. while (i-- > 0)
  116. handleKeyboard(down, true);
  117. } else if (key == left) {
  118. int i = 10;
  119. if (mCursor < 10)
  120. i = mCursor;
  121. while (i-- > 0)
  122. handleKeyboard(up, true);
  123. } else if (key == enter) {
  124. char *tmp = bufferString("load %s", gOpenRaider->mMapList[mCursor]);
  125. if (gOpenRaider->command(tmp) == 0)
  126. setVisible(false);
  127. delete [] tmp;
  128. }
  129. }
  130. void Menu::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  131. int items = (gOpenRaider->mWindow->mHeight - 110) / 25;
  132. if ((!released) || (button != leftmouse))
  133. return;
  134. if ((y >= 100) && (y <= (unsigned int)(100 + (25 * items)))) {
  135. y -= 100;
  136. mCursor = mMin + (y / 25);
  137. } else if ((y >= 25) && (y <= 100) && (x >= 25) && (x <= 125)) {
  138. // Play button
  139. mCursor = 0;
  140. }
  141. }