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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*!
  2. * \file src/Menu.cpp
  3. * \brief Menu 'overlay'
  4. *
  5. * \author xythobuz
  6. */
  7. #include <assert.h>
  8. #include <dirent.h>
  9. #ifdef __APPLE__
  10. #include <OpenGL/gl.h>
  11. #else
  12. #include <GL/gl.h>
  13. #endif
  14. #include "config.h"
  15. #include "global.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. mMapListFilled = false;
  33. mFirstPass = false;
  34. }
  35. Menu::~Menu() {
  36. delete [] mainText.text;
  37. while (mMapList.size() > 0) {
  38. delete [] mMapList.back();
  39. mMapList.pop_back();
  40. }
  41. }
  42. void Menu::setVisible(bool visible) {
  43. mVisible = visible;
  44. }
  45. bool Menu::isVisible() {
  46. return mVisible;
  47. }
  48. void Menu::loadPakFolderRecursive(const char *dir) {
  49. struct dirent entry;
  50. struct dirent *ep = NULL;
  51. DIR *pakDir;
  52. assert(dir != NULL);
  53. assert(dir[0] != '\0');
  54. pakDir = opendir(dir);
  55. if (pakDir != NULL) {
  56. readdir_r(pakDir, &entry, &ep);
  57. while (ep != NULL) {
  58. if (ep->d_type == DT_DIR) {
  59. if ((strcmp(".", ep->d_name) != 0)
  60. && (strcmp("..", ep->d_name) != 0)) {
  61. char *tmp = bufferString("%s%s", dir, ep->d_name);
  62. char *next = fullPath(tmp, '/');
  63. loadPakFolderRecursive(next);
  64. delete next;
  65. delete tmp;
  66. }
  67. } else {
  68. char *fullPathMap = bufferString("%s%s", dir, ep->d_name);
  69. char *lowerPath = bufferString("%s", fullPathMap);
  70. for (char *p = lowerPath; *p; ++p) *p = (char)tolower(*p);
  71. // Check for valid extension
  72. if (stringEndsWith(lowerPath, ".phd")
  73. || stringEndsWith(lowerPath, ".tr2")
  74. || stringEndsWith(lowerPath, ".tr4")
  75. || stringEndsWith(lowerPath, ".trc")) {
  76. int error = TombRaider::checkMime(fullPathMap);
  77. if (error == 0) {
  78. // Just load relative filename
  79. mMapList.push_back(bufferString("%s", (fullPathMap + strlen(getOpenRaider().mPakDir) + 1)));
  80. } else {
  81. getConsole().print("Error: pak file '%s' %s",
  82. fullPathMap, (error == -1) ? "not found" : "invalid");
  83. }
  84. }
  85. delete [] lowerPath;
  86. delete [] fullPathMap;
  87. }
  88. readdir_r(pakDir, &entry, &ep);
  89. }
  90. closedir(pakDir);
  91. } else {
  92. getConsole().print("Could not open PAK dir %s!", dir);
  93. }
  94. }
  95. void Menu::fillMapList() {
  96. char *tmp = fullPath(getOpenRaider().mPakDir, '/');
  97. loadPakFolderRecursive(tmp);
  98. delete [] tmp;
  99. mMapListFilled = true;
  100. }
  101. void Menu::displayMapList() {
  102. // Estimate displayable number of items
  103. int items = (getWindow().mHeight - 110) / 25;
  104. // Select which part of the list to show
  105. int min, max;
  106. if (((int)mCursor - (items / 2)) > 0)
  107. min = mCursor - (items / 2);
  108. else
  109. min = 0;
  110. if ((mCursor + (items / 2)) < mMapList.size())
  111. max = mCursor + (items / 2);
  112. else
  113. max = mMapList.size();
  114. while ((max - min) < items) {
  115. if (min > 0)
  116. min--;
  117. else if (max < ((int)mMapList.size()))
  118. max++;
  119. else
  120. break;
  121. }
  122. mMin = min;
  123. for (int i = 0; i < (max - min); i++) {
  124. char *map = mMapList[i + min];
  125. if ((i + min) == (int)mCursor)
  126. getWindow().drawText(25, 100 + (25 * i), 0.75f, RED, "%s", map);
  127. else
  128. getWindow().drawText(25, 100 + (25 * i), 0.75f, OR_BLUE, "%s", map);
  129. }
  130. }
  131. void Menu::display() {
  132. if (mVisible) {
  133. // Draw half-transparent *overlay*
  134. glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
  135. glDisable(GL_TEXTURE_2D);
  136. glRecti(0, 0, getWindow().mWidth, getWindow().mHeight);
  137. glEnable(GL_TEXTURE_2D);
  138. // Draw heading text
  139. mainText.x = (getWindow().mWidth / 2) - (mainText.w / 2);
  140. getWindow().writeString(&mainText);
  141. if (!mMapListFilled) {
  142. getWindow().drawText(25, (getWindow().mHeight / 2) - 20, 0.75f, OR_BLUE, "Generating map list...");
  143. } else {
  144. if (mMapList.size() == 0) {
  145. getWindow().drawText(25, (getWindow().mHeight / 2) - 20, 0.75f, RED, "No maps found! See README.md");
  146. } else {
  147. // draw *play button* above list
  148. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  149. glDisable(GL_TEXTURE_2D);
  150. glRecti(25, 25, 100, 75);
  151. glEnable(GL_TEXTURE_2D);
  152. getWindow().drawText(40, 35, 0.75f, BLACK, "Play");
  153. displayMapList();
  154. }
  155. }
  156. // Fill map list after first render pass,
  157. // so menu *loading screen* is visible
  158. if (mFirstPass) {
  159. if (!mMapListFilled) {
  160. fillMapList();
  161. }
  162. } else {
  163. mFirstPass = true;
  164. }
  165. }
  166. }
  167. void Menu::handleKeyboard(KeyboardButton key, bool pressed) {
  168. if (!pressed)
  169. return;
  170. if (key == upKey) {
  171. if (mCursor > 0)
  172. mCursor--;
  173. else
  174. mCursor = mMapList.size() - 1;
  175. } else if (key == downKey) {
  176. if (mCursor < (mMapList.size() - 1))
  177. mCursor++;
  178. else
  179. mCursor = 0;
  180. } else if (key == rightKey) {
  181. int i = 10;
  182. if (mCursor > (mMapList.size() - 11))
  183. i = mMapList.size() - 1 - mCursor;
  184. while (i-- > 0)
  185. handleKeyboard(downKey, true);
  186. } else if (key == leftKey) {
  187. int i = 10;
  188. if (mCursor < 10)
  189. i = mCursor;
  190. while (i-- > 0)
  191. handleKeyboard(upKey, true);
  192. } else if (key == enterKey) {
  193. char *tmp = bufferString("load %s", mMapList[mCursor]);
  194. if (getOpenRaider().command(tmp) == 0) {
  195. setVisible(false);
  196. } else {
  197. //! \todo Display something if an error occurs
  198. }
  199. delete [] tmp;
  200. }
  201. }
  202. void Menu::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  203. int items = (getWindow().mHeight - 110) / 25;
  204. if ((!released) || (button != leftmouseKey))
  205. return;
  206. if ((y >= 100) && (y <= (unsigned int)(100 + (25 * items)))) {
  207. y -= 100;
  208. mCursor = mMin + (y / 25);
  209. } else if ((y >= 25) && (y <= 100) && (x >= 25) && (x <= 125)) {
  210. // Play button
  211. mCursor = 0;
  212. }
  213. }