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.

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