Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Menu.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*!
  2. * \file src/Menu.cpp
  3. * \brief Menu 'overlay'
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Font.h"
  9. #include "Log.h"
  10. #include "Window.h"
  11. #include "Menu.h"
  12. #include "MenuFolder.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 = getFont().widthText(1.0f, dialogText) + 20;
  78. if (w0 > wMax)
  79. w0 = wMax;
  80. unsigned int h0 = getFont().heightText(1.0f, w0, dialogText) + 10;
  81. assert(dialogButton1.length() > 0);
  82. unsigned int w1 = getFont().widthText(1.0f, dialogButton1) + 20;
  83. if (w1 > wMax)
  84. w1 = wMax;
  85. unsigned int h1 = getFont().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 = getFont().widthText(1.0f, dialogButton2) + 20;
  90. if (w2 > wMax)
  91. w2 = wMax;
  92. h2 = getFont().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. 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. getFont().drawTextWrapped(xOverlay + 10, yOverlay + 5, 1.0f, BLUE, w0, dialogText);
  127. if (dialogButton2.length() > 0) {
  128. if ((w1 + w2) <= wMax) {
  129. getFont().drawTextWrapped(xOverlay + 10, yOverlay + 10 + h0, 1.0f,
  130. dialogState ? BLUE : RED, w1, dialogButton1);
  131. getFont().drawTextWrapped(xOverlay + 10 + w1, yOverlay + 10 + h0,
  132. 1.0f, dialogState ? RED : BLUE, w2, dialogButton2);
  133. } else {
  134. getFont().drawTextWrapped((::getWindow().getWidth() - w1) / 2,
  135. yOverlay + 10 + h0, 1.0f, dialogState ? BLUE : RED, w1, dialogButton1);
  136. getFont().drawTextWrapped((::getWindow().getWidth() - w2) / 2,
  137. yOverlay + 10 + h0 + h1, 1.0f, dialogState ? RED : BLUE, w2, dialogButton2);
  138. }
  139. } else {
  140. getFont().drawTextWrapped((::getWindow().getWidth() - w1) / 2,
  141. yOverlay + 10 + h0, 1.0f, RED, w1, dialogButton1);
  142. }
  143. }
  144. }