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.

UI.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*!
  2. * \file src/UI.cpp
  3. * \brief UI interface manager
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include "global.h"
  9. #include "Console.h"
  10. #include "Debug.h"
  11. #include "Menu.h"
  12. #include "OpenRaider.h"
  13. #include "TextureManager.h"
  14. #include "Window.h"
  15. std::vector<UI*> UI::windows;
  16. UI::~UI() {
  17. }
  18. int UI::initialize() { return 0; }
  19. void UI::eventsFinished() { }
  20. void UI::display() { }
  21. void UI::handleKeyboard(KeyboardButton key, bool pressed) { }
  22. void UI::handleText(char *text, bool notFinished) { }
  23. void UI::handleAction(ActionEvents action, bool isFinished) { }
  24. void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) { }
  25. void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) { }
  26. void UI::handleMouseScroll(int xrel, int yrel) { }
  27. void UI::passKeyboard(KeyboardButton key, bool pressed) {
  28. if (pressed) {
  29. if (getOpenRaider().keyBindings[menuAction] == key) {
  30. if (getMenu().isOnTop()) {
  31. getMenu().makeInvisible();
  32. } else {
  33. getMenu().moveToTop();
  34. }
  35. } else if (getOpenRaider().keyBindings[consoleAction] == key) {
  36. if (getConsole().isOnTop()) {
  37. getConsole().makeInvisible();
  38. } else {
  39. getConsole().moveToTop();
  40. }
  41. } else if (getOpenRaider().keyBindings[debugAction] == key) {
  42. if (getDebug().isOnTop()) {
  43. getDebug().makeInvisible();
  44. } else {
  45. getDebug().moveToTop();
  46. }
  47. }
  48. }
  49. auto maxIterator = std::max_element(windows.begin(), windows.end(), compareUIs);
  50. (*maxIterator)->handleKeyboard(key, pressed);
  51. for (int i = forwardAction; i < ActionEventCount; i++) {
  52. if (getOpenRaider().keyBindings[i] == key) {
  53. (*maxIterator)->handleAction((ActionEvents)i, !pressed);
  54. }
  55. }
  56. bool mousegrab = (*maxIterator)->zPos == 0;
  57. if (mousegrab != getWindow().getMousegrab())
  58. getWindow().setMousegrab(mousegrab);
  59. }
  60. void UI::passText(char *text, bool notFinished) {
  61. auto maxIterator = std::max_element(windows.begin(), windows.end(), compareUIs);
  62. (*maxIterator)->handleText(text, notFinished);
  63. }
  64. void UI::passMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  65. auto maxIterator = std::max_element(windows.begin(), windows.end(), compareUIs);
  66. (*maxIterator)->handleMouseClick(x, y, button, released);
  67. for (int i = forwardAction; i < ActionEventCount; i++) {
  68. if (getOpenRaider().keyBindings[i] == button) {
  69. (*maxIterator)->handleAction((ActionEvents)i, released);
  70. }
  71. }
  72. }
  73. void UI::passMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  74. auto maxIterator = std::max_element(windows.begin(), windows.end(), compareUIs);
  75. (*maxIterator)->handleMouseMotion(xrel, yrel, xabs, yabs);
  76. }
  77. void UI::passMouseScroll(int xrel, int yrel) {
  78. auto maxIterator = std::max_element(windows.begin(), windows.end(), compareUIs);
  79. (*maxIterator)->handleMouseScroll(xrel, yrel);
  80. }
  81. void UI::addWindow(UI* window) {
  82. windows.push_back(window);
  83. }
  84. void UI::removeWindow(UI *window) {
  85. findInList(window, [](unsigned long i){
  86. windows.erase(windows.begin() + i);
  87. });
  88. }
  89. bool UI::compareUIs(UI* a, UI* b) {
  90. return a->zPos < b->zPos;
  91. }
  92. bool UI::isOnTop(unsigned long windowID) {
  93. assert(windowID < windows.size());
  94. auto maxIterator = std::max_element(windows.begin(), windows.end(), compareUIs);
  95. unsigned long maxPos = (unsigned long)(maxIterator - windows.begin());
  96. return (maxPos == windowID);
  97. }
  98. void UI::moveToTop(unsigned long windowID) {
  99. assert(windowID < windows.size());
  100. auto maxIterator = std::max_element(windows.begin(), windows.end(), compareUIs);
  101. long max = (*maxIterator)->zPos;
  102. unsigned long maxPos = (unsigned long)(maxIterator - windows.begin());
  103. if (maxPos != windowID) {
  104. windows.at(windowID)->zPos = max + 1;
  105. }
  106. }
  107. void UI::makeInvisible(unsigned long windowID) {
  108. assert(windowID < windows.size());
  109. windows.at(windowID)->zPos = -1;
  110. }
  111. void UI::findInList(UI *w, std::function<void (unsigned long i)> func) {
  112. for (unsigned long i = 0; i < windows.size(); i++) {
  113. auto UIptr = &(*windows.at(i));
  114. if (w == UIptr) {
  115. func(i);
  116. return;
  117. }
  118. }
  119. assert(false); // called UI was not registered
  120. }
  121. bool UI::isOnTop() {
  122. bool top = false;
  123. findInList(this, [&](unsigned long i) {
  124. top = UI::isOnTop(i);
  125. });
  126. return top;
  127. }
  128. void UI::moveToTop() {
  129. findInList(this, [](unsigned long i) {
  130. UI::moveToTop(i);
  131. });
  132. }
  133. void UI::makeInvisible() {
  134. findInList(this, [](unsigned long i) {
  135. UI::makeInvisible(i);
  136. });
  137. }
  138. int UI::passInitialize() {
  139. for (auto &x : windows) {
  140. int error = x->initialize();
  141. if (error != 0)
  142. return error;
  143. }
  144. return 0;
  145. }
  146. void UI::passEvents() {
  147. for (auto &x : windows) {
  148. x->eventsFinished();
  149. }
  150. }
  151. void UI::passDisplay() {
  152. std::sort(windows.begin(), windows.end(), compareUIs);
  153. for (auto &x : windows) {
  154. if (x->zPos >= 0) {
  155. x->display();
  156. }
  157. }
  158. }