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

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