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

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