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

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