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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*!
  2. * \file src/Menu.cpp
  3. * \brief Menu 'overlay'
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Console.h"
  9. #include "Font.h"
  10. #include "Window.h"
  11. #include "Menu.h"
  12. #include "MenuFolder.h"
  13. Menu &getMenu() {
  14. static MenuFolder gMenu;
  15. return gMenu;
  16. }
  17. void Menu::showDialog(std::string msg, std::string btn1, std::string btn2,
  18. std::function<int (bool state)> callback) {
  19. // Only show one dialog at a time
  20. assert(dialogText.length() == 0);
  21. assert(dialogButton1.length() == 0);
  22. assert(dialogButton2.length() == 0);
  23. assert(msg.length() > 0);
  24. assert(btn1.length() > 0);
  25. dialogText = msg;
  26. dialogButton1 = btn1;
  27. dialogButton2 = btn2;
  28. dialogState = false;
  29. dialogFunction = callback;
  30. getConsole() << dialogText << Console::endl;
  31. }
  32. void Menu::ackDialog() {
  33. dialogText = "";
  34. dialogButton1 = "";
  35. dialogButton2 = "";
  36. if (dialogFunction) {
  37. int error = dialogFunction(dialogState);
  38. if (error != 0) {
  39. showDialog("Error processing dialog callback!", "OK", "");
  40. }
  41. }
  42. dialogState = false;
  43. }
  44. bool Menu::handleKeyboardDialog(KeyboardButton key, bool pressed) {
  45. if (!pressed)
  46. return (dialogText.length() > 0);
  47. if (dialogText.length() > 0) {
  48. if (dialogButton2.length() == 0) {
  49. if (key == enterKey) {
  50. ackDialog();
  51. }
  52. } else {
  53. if (key == enterKey) {
  54. ackDialog();
  55. } else if (key == leftKey) {
  56. dialogState = !dialogState;
  57. } else if (key == rightKey) {
  58. dialogState = !dialogState;
  59. } else if (key == upKey) {
  60. dialogState = !dialogState;
  61. } else if (key == downKey) {
  62. dialogState = !dialogState;
  63. }
  64. }
  65. return true;
  66. }
  67. return false;
  68. }
  69. bool Menu::handleMouseClickDialog(unsigned int x, unsigned int y,
  70. KeyboardButton button, bool released) {
  71. //! \todo Allow mouse usage of Menu dialogs!
  72. return (dialogText.length() > 0);
  73. }
  74. bool Menu::handleMouseScrollDialog(int xrel, int yrel) {
  75. //! \todo Allow mouse usage of Menu dialogs!
  76. return (dialogText.length() > 0);
  77. }
  78. void Menu::displayDialog() {
  79. if (dialogText.length() > 0) {
  80. unsigned int wMax = ((unsigned int)(::getWindow().getWidth() * 0.66f));
  81. unsigned int w0 = getFont().widthText(1.0f, dialogText) + 20;
  82. if (w0 > wMax)
  83. w0 = wMax;
  84. unsigned int h0 = getFont().heightText(1.0f, w0, dialogText) + 10;
  85. assert(dialogButton1.length() > 0);
  86. unsigned int w1 = getFont().widthText(1.0f, dialogButton1) + 20;
  87. if (w1 > wMax)
  88. w1 = wMax;
  89. unsigned int h1 = getFont().heightText(1.0f, w1, dialogButton1) + 10;
  90. unsigned int wOverlay = wMax, hOverlay, w2 = 0, h2 = 0;
  91. if (dialogButton2.length() > 0) {
  92. // Show text and two buttons
  93. w2 = getFont().widthText(1.0f, dialogButton2) + 20;
  94. if (w2 > wMax)
  95. w2 = wMax;
  96. h2 = getFont().heightText(1.0f, w2, dialogButton2) + 10;
  97. if (w0 > (w1 + w2)) {
  98. if (w0 < wMax) {
  99. wOverlay = w0;
  100. }
  101. } else if (w0 < (w1 + w2)) {
  102. if ((w1 + w2) < wMax) {
  103. wOverlay = (w1 + w2);
  104. }
  105. }
  106. if ((w1 + w2) <= wMax) {
  107. hOverlay = h0 + ((h1 + h2) / 2);
  108. } else {
  109. hOverlay = h0 + h1 + h2;
  110. }
  111. } else {
  112. // Show text and one button
  113. if (w0 > w1) {
  114. if (w0 < wMax) {
  115. wOverlay = w0;
  116. }
  117. } else if (w0 < w1) {
  118. if (w1 < wMax) {
  119. wOverlay = w1;
  120. }
  121. }
  122. hOverlay = h0 + h1;
  123. }
  124. unsigned int xOverlay = (::getWindow().getWidth() - wOverlay) / 2;
  125. unsigned int yOverlay = (::getWindow().getHeight() - hOverlay) / 2;
  126. glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
  127. glDisable(GL_TEXTURE_2D);
  128. glRecti(xOverlay, yOverlay, xOverlay + wOverlay, yOverlay + hOverlay);
  129. glEnable(GL_TEXTURE_2D);
  130. getFont().drawTextWrapped(xOverlay + 10, yOverlay + 5, 1.0f, BLUE, w0, dialogText);
  131. if (dialogButton2.length() > 0) {
  132. if ((w1 + w2) <= wMax) {
  133. getFont().drawTextWrapped(xOverlay + 10, yOverlay + 10 + h0, 1.0f,
  134. dialogState ? BLUE : RED, w1, dialogButton1);
  135. getFont().drawTextWrapped(xOverlay + 10 + w1, yOverlay + 10 + h0,
  136. 1.0f, dialogState ? RED : BLUE, w2, dialogButton2);
  137. } else {
  138. getFont().drawTextWrapped((::getWindow().getWidth() - w1) / 2,
  139. yOverlay + 10 + h0, 1.0f, dialogState ? BLUE : RED, w1, dialogButton1);
  140. getFont().drawTextWrapped((::getWindow().getWidth() - w2) / 2,
  141. yOverlay + 10 + h0 + h1, 1.0f, dialogState ? RED : BLUE, w2, dialogButton2);
  142. }
  143. } else {
  144. getFont().drawTextWrapped((::getWindow().getWidth() - w1) / 2,
  145. yOverlay + 10 + h0, 1.0f, RED, w1, dialogButton1);
  146. }
  147. }
  148. }