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 5.0KB

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