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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*!
  2. * \file src/Menu.cpp
  3. * \brief Menu 'overlay'
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cctype>
  8. #include "global.h"
  9. #include "Console.h"
  10. #include "Menu.h"
  11. #include "OpenRaider.h"
  12. #include "utils/strings.h"
  13. #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR) && defined(HAVE_READDIR_R) && defined(HAVE_CLOSEDIR) && defined(HAVE_DT_DIR)
  14. #include <dirent.h>
  15. #define USE_DIRENT
  16. #elif defined(HAVE_WINDOWS_H) && defined(HAVE_FINDFIRSTFILE) && defined(HAVE_FINDNEXTFILE) && defined(HAVE_FINDCLOSE)
  17. #include <windows.h>
  18. #define USE_FINDFILE
  19. #else
  20. #error No support for recursive folder traversal
  21. #endif
  22. Menu::Menu() {
  23. mVisible = false;
  24. mCursor = 0;
  25. mMin = 0;
  26. mainText.text = bufferString(VERSION);
  27. mainText.color[0] = 0xFF;
  28. mainText.color[1] = 0xFF;
  29. mainText.color[2] = 0xFF;
  30. mainText.color[3] = 0xFF;
  31. mainText.scale = 1.2f;
  32. mainText.y = 10;
  33. mainText.w = 0;
  34. mainText.h = 0;
  35. mMapListFilled = false;
  36. mFirstPass = false;
  37. }
  38. Menu::~Menu() {
  39. delete [] mainText.text;
  40. while (mMapList.size() > 0) {
  41. delete [] mMapList.back();
  42. mMapList.pop_back();
  43. }
  44. }
  45. void Menu::setVisible(bool visible) {
  46. mVisible = visible;
  47. }
  48. bool Menu::isVisible() {
  49. return mVisible;
  50. }
  51. void Menu::loadPakFolderRecursive(const char *dir) {
  52. assert(dir != NULL);
  53. assert(dir[0] != '\0');
  54. #ifdef USE_DIRENT
  55. struct dirent entry;
  56. struct dirent *ep = NULL;
  57. DIR *pakDir;
  58. pakDir = opendir(dir);
  59. if (pakDir != NULL) {
  60. readdir_r(pakDir, &entry, &ep);
  61. while (ep != NULL) {
  62. if (ep->d_type == DT_DIR) {
  63. if ((strcmp(".", ep->d_name) != 0)
  64. && (strcmp("..", ep->d_name) != 0)) {
  65. char *tmp = bufferString("%s%s", dir, ep->d_name);
  66. char *next = fullPath(tmp, '/');
  67. loadPakFolderRecursive(next);
  68. delete next;
  69. delete tmp;
  70. }
  71. } else {
  72. char *fullPathMap = bufferString("%s%s", dir, ep->d_name);
  73. char *lowerPath = bufferString("%s", fullPathMap);
  74. for (char *p = lowerPath; *p; ++p) *p = (char)tolower(*p);
  75. // Check for valid extension
  76. if (stringEndsWith(lowerPath, ".phd")
  77. || stringEndsWith(lowerPath, ".tr2")
  78. || stringEndsWith(lowerPath, ".tr4")
  79. || stringEndsWith(lowerPath, ".trc")) {
  80. int error = TombRaider::checkMime(fullPathMap);
  81. if (error == 0) {
  82. // Just load relative filename
  83. mMapList.push_back(bufferString("%s", (fullPathMap + strlen(getOpenRaider().mPakDir) + 1)));
  84. } else {
  85. getConsole().print("Error: pak file '%s' %s",
  86. fullPathMap, (error == -1) ? "not found" : "invalid");
  87. }
  88. }
  89. delete [] lowerPath;
  90. delete [] fullPathMap;
  91. }
  92. readdir_r(pakDir, &entry, &ep);
  93. }
  94. closedir(pakDir);
  95. } else {
  96. getConsole().print("Could not open PAK dir %s!", dir);
  97. }
  98. #elif defined(USE_FINDFILE)
  99. std::vector<char *> list;
  100. list.push_back(bufferString("%s", dir));
  101. do {
  102. WIN32_FIND_DATA fd;
  103. char *tmp = bufferString("%s\\*", list.at(0));
  104. HANDLE hFind = FindFirstFile(tmp, &fd);
  105. do {
  106. if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  107. list.push_back(bufferString("%s\\%s", list.at(0), fd.cFileName));
  108. } else {
  109. char *fullPathMap = bufferString("%s\\%s", list.at(0), fd.cFileName);
  110. char *lowerPath = bufferString("%s", fullPathMap);
  111. for (char *p = lowerPath; *p; ++p) *p = (char)tolower(*p);
  112. // Check for valid extension
  113. if (stringEndsWith(lowerPath, ".phd")
  114. || stringEndsWith(lowerPath, ".tr2")
  115. || stringEndsWith(lowerPath, ".tr4")
  116. || stringEndsWith(lowerPath, ".trc")) {
  117. int error = TombRaider::checkMime(fullPathMap);
  118. if (error == 0) {
  119. // Just load relative filename
  120. mMapList.push_back(bufferString("%s", (fullPathMap + strlen(getOpenRaider().mPakDir) + 1)));
  121. } else {
  122. getConsole().print("Error: pak file '%s' %s",
  123. fullPathMap, (error == -1) ? "not found" : "invalid");
  124. }
  125. }
  126. delete [] lowerPath;
  127. delete [] fullPathMap;
  128. }
  129. } while (FindNextFile(hFind, &fd) != 0);
  130. FindClose(hFind);
  131. delete [] tmp;
  132. delete [] list.at(0);
  133. list.erase(list.begin());
  134. } while (list.size() > 0);
  135. #endif
  136. }
  137. void Menu::fillMapList() {
  138. char *tmp = fullPath(getOpenRaider().mPakDir, '/');
  139. loadPakFolderRecursive(tmp);
  140. delete [] tmp;
  141. mMapListFilled = true;
  142. }
  143. void Menu::displayMapList() {
  144. // Estimate displayable number of items
  145. int items = (getWindow().mHeight - 110) / 25;
  146. // Select which part of the list to show
  147. int min, max;
  148. if (((int)mCursor - (items / 2)) > 0)
  149. min = mCursor - (items / 2);
  150. else
  151. min = 0;
  152. if ((mCursor + (items / 2)) < mMapList.size())
  153. max = mCursor + (items / 2);
  154. else
  155. max = mMapList.size();
  156. while ((max - min) < items) {
  157. if (min > 0)
  158. min--;
  159. else if (max < ((int)mMapList.size()))
  160. max++;
  161. else
  162. break;
  163. }
  164. mMin = min;
  165. for (int i = 0; i < (max - min); i++) {
  166. char *map = mMapList[i + min];
  167. if ((i + min) == (int)mCursor)
  168. getWindow().drawText(25, 100 + (25 * i), 0.75f, RED, "%s", map);
  169. else
  170. getWindow().drawText(25, 100 + (25 * i), 0.75f, OR_BLUE, "%s", map);
  171. }
  172. }
  173. void Menu::display() {
  174. if (mVisible) {
  175. // Draw half-transparent *overlay*
  176. glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
  177. glDisable(GL_TEXTURE_2D);
  178. glRecti(0, 0, getWindow().mWidth, getWindow().mHeight);
  179. glEnable(GL_TEXTURE_2D);
  180. // Draw heading text, using WindowString so we can get the
  181. // width of the drawn text to center it
  182. mainText.x = (getWindow().mWidth / 2) - (mainText.w / 2);
  183. getWindow().writeString(mainText);
  184. if (!mMapListFilled) {
  185. getWindow().drawText(25, (getWindow().mHeight / 2) - 20, 0.75f, OR_BLUE, "Generating map list...");
  186. } else {
  187. if (mMapList.size() == 0) {
  188. getWindow().drawText(25, (getWindow().mHeight / 2) - 20, 0.75f, RED, "No maps found! See README.md");
  189. } else {
  190. // draw *play button* above list
  191. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  192. glDisable(GL_TEXTURE_2D);
  193. glRecti(25, 25, 100, 75);
  194. glEnable(GL_TEXTURE_2D);
  195. getWindow().drawText(40, 35, 0.75f, BLACK, "Play");
  196. displayMapList();
  197. }
  198. }
  199. // Fill map list after first render pass,
  200. // so menu *loading screen* is visible
  201. if (mFirstPass) {
  202. if (!mMapListFilled) {
  203. fillMapList();
  204. }
  205. } else {
  206. mFirstPass = true;
  207. }
  208. }
  209. }
  210. void Menu::play() {
  211. char *tmp = bufferString("load %s", mMapList[mCursor]);
  212. if (getOpenRaider().command(tmp) == 0) {
  213. setVisible(false);
  214. } else {
  215. //! \todo Display something if an error occurs
  216. }
  217. delete [] tmp;
  218. }
  219. void Menu::handleKeyboard(KeyboardButton key, bool pressed) {
  220. if (!pressed)
  221. return;
  222. if (key == upKey) {
  223. if (mCursor > 0)
  224. mCursor--;
  225. else
  226. mCursor = mMapList.size() - 1;
  227. } else if (key == downKey) {
  228. if (mCursor < (mMapList.size() - 1))
  229. mCursor++;
  230. else
  231. mCursor = 0;
  232. } else if (key == rightKey) {
  233. int i = 10;
  234. if (mCursor > (mMapList.size() - 11))
  235. i = mMapList.size() - 1 - mCursor;
  236. while (i-- > 0)
  237. handleKeyboard(downKey, true);
  238. } else if (key == leftKey) {
  239. int i = 10;
  240. if (mCursor < 10)
  241. i = mCursor;
  242. while (i-- > 0)
  243. handleKeyboard(upKey, true);
  244. } else if (key == enterKey) {
  245. play();
  246. }
  247. }
  248. void Menu::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  249. int items = (getWindow().mHeight - 110) / 25;
  250. if (released || (button != leftmouseKey))
  251. return;
  252. if ((y >= 100) && (y <= (unsigned int)(100 + (25 * items)))) {
  253. y -= 100;
  254. mCursor = mMin + (y / 25);
  255. } else if ((y >= 25) && (y <= 100) && (x >= 25) && (x <= 125)) {
  256. // Play button
  257. play();
  258. }
  259. }