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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*!
  2. * \file src/Menu.cpp
  3. * \brief Menu 'overlay'
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cctype>
  8. #include <cstring>
  9. #include "global.h"
  10. #include "Console.h"
  11. #include "Font.h"
  12. #include "OpenRaider.h"
  13. #include "utils/strings.h"
  14. #include "TombRaider.h"
  15. #include "Window.h"
  16. #include "Menu.h"
  17. Menu::Menu() {
  18. mVisible = false;
  19. mCursor = 0;
  20. mMin = 0;
  21. mapFolder = nullptr;
  22. hiddenState = false;
  23. }
  24. Menu::~Menu() {
  25. delete mapFolder;
  26. mapFolder = nullptr;
  27. }
  28. int Menu::initialize() {
  29. return initialize(getOpenRaider().mPakDir);
  30. }
  31. int Menu::initialize(std::string folder) {
  32. return initialize(new Folder(folder, hiddenState));
  33. }
  34. int Menu::initialize(Folder *folder) {
  35. if (mapFolder != nullptr)
  36. delete mapFolder;
  37. mapFolder = folder;
  38. mMin = mCursor = 0;
  39. mapFolder->executeRemoveFiles([](File &f) {
  40. // Filter files based on file name
  41. if ((f.getName().length() > 4)
  42. && (f.getName().compare(f.getName().length() - 4, 4, ".phd") != 0)
  43. && (f.getName().compare(f.getName().length() - 4, 4, ".tr2") != 0)
  44. && (f.getName().compare(f.getName().length() - 4, 4, ".tr4") != 0)
  45. && (f.getName().compare(f.getName().length() - 4, 4, ".trc") != 0)) {
  46. return true; // delete file from list
  47. }
  48. // Check maps for validity
  49. int error = TombRaider::checkMime(f.getPath().c_str());
  50. if (error != 0) {
  51. getConsole() << "Error: pak file '" << f.getName().c_str()
  52. << "' " << ((error == -1) ? "not found" : "invalid") << Console::endl;
  53. return true; // delete file from list
  54. }
  55. return false; // keep file on list
  56. });
  57. if ((mapFolder->fileCount() + mapFolder->folderCount()) > 0)
  58. mCursor = 1; // Don't select ".." by default
  59. return 0;
  60. }
  61. void Menu::setVisible(bool visible) {
  62. mVisible = visible;
  63. }
  64. bool Menu::isVisible() {
  65. return mVisible;
  66. }
  67. void Menu::display() {
  68. if (!mVisible)
  69. return;
  70. // Draw half-transparent *overlay*
  71. glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
  72. glDisable(GL_TEXTURE_2D);
  73. glRecti(0, 0, (GLint)getWindow().getWidth(), (GLint)getWindow().getHeight());
  74. glEnable(GL_TEXTURE_2D);
  75. // Draw heading
  76. getFont().drawTextCentered(0, 10, 1.2f, BLUE, getWindow().getWidth(), "%s", VERSION);
  77. // Estimate displayable number of items
  78. int items = (getWindow().getHeight() - 60) / 25;
  79. // Print list of "..", folders, files
  80. for (long i = mMin; (i < (mMin + items))
  81. && (i < (mapFolder->folderCount() + mapFolder->fileCount() + 1)); i++) {
  82. if (i == 0) {
  83. getFont().drawText(25, 50, 0.75f, (mCursor == i) ? RED : BLUE, "..");
  84. } else {
  85. getFont().drawText(25, (unsigned int)(50 + (25 * (i - mMin))), 0.75f,
  86. (mCursor == i) ? RED : BLUE, "%s",
  87. ((i - 1) < mapFolder->folderCount()) ?
  88. (mapFolder->getFolder(i - 1).getName() + "/").c_str()
  89. : mapFolder->getFile(i - 1 - mapFolder->folderCount()).getName().c_str());
  90. }
  91. }
  92. }
  93. void Menu::play() {
  94. if (mCursor == 0) {
  95. if (initialize(mapFolder->getParent().getPath()) != 0) {
  96. //! \todo Display something if an error occurs
  97. }
  98. } else if ((mCursor - 1) < mapFolder->folderCount()) {
  99. if (initialize(mapFolder->getFolder(mCursor - 1).getPath()) != 0) {
  100. //! \todo Display something if an error occurs
  101. }
  102. } else {
  103. std::string tmp = "load ";
  104. tmp += mapFolder->getFile((unsigned long)mCursor - 1 - mapFolder->folderCount()).getPath();
  105. if (getOpenRaider().command(tmp.c_str()) == 0) {
  106. setVisible(false);
  107. } else {
  108. //! \todo Display something if an error occurs
  109. }
  110. }
  111. }
  112. void Menu::handleKeyboard(KeyboardButton key, bool pressed) {
  113. if (!pressed)
  114. return;
  115. assert(mapFolder != nullptr);
  116. int items = (getWindow().getHeight() - 60) / 25;
  117. if (key == upKey) {
  118. if (mCursor > 0)
  119. mCursor--;
  120. else
  121. mCursor = (long)(mapFolder->folderCount() + mapFolder->fileCount());
  122. } else if (key == downKey) {
  123. if (mCursor < (long)(mapFolder->folderCount() + mapFolder->fileCount()))
  124. mCursor++;
  125. else
  126. mCursor = 0;
  127. } else if (key == enterKey) {
  128. play();
  129. } else if (key == dotKey) {
  130. hiddenState = !hiddenState;
  131. initialize(mapFolder->getPath());
  132. }
  133. if (mCursor > (mMin + items - 1)) {
  134. mMin = mCursor - items + 1;
  135. } else if (mCursor < mMin) {
  136. mMin = mCursor;
  137. }
  138. }
  139. void Menu::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  140. int items = (getWindow().getHeight() - 60) / 25;
  141. if (released || (button != leftmouseKey))
  142. return;
  143. if ((y >= 50) && (y <= (unsigned int)(50 + (25 * items)))) {
  144. y -= 50;
  145. if (mCursor == (mMin + (y / 25)))
  146. play();
  147. else
  148. mCursor = mMin + (y / 25);
  149. }
  150. }
  151. void Menu::handleMouseScroll(int xrel, int yrel) {
  152. assert((xrel != 0) || (yrel != 0));
  153. assert(mapFolder != nullptr);
  154. int items = (getWindow().getHeight() - 60) / 25;
  155. if ((mapFolder->folderCount() + mapFolder->fileCount() + 1) > items) {
  156. if (yrel < 0) {
  157. if (mMin < (mapFolder->folderCount() + mapFolder->fileCount() + 1 - items))
  158. mMin++;
  159. } else if (yrel > 0) {
  160. if (mMin > 0)
  161. mMin--;
  162. }
  163. if (mCursor < mMin) {
  164. mCursor = mMin;
  165. } else if (mCursor > (mMin + items - 1)) {
  166. mCursor = mMin + items - 1;
  167. }
  168. }
  169. }