Open Source Tomb Raider Engine
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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