BaseGUI.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "client/gui/BaseGUI.h"
  2. #include "client/Game.h"
  3. #include "client/input/Controller.h"
  4. #include "client/rendering/Engine.h"
  5. #include "rendering/Window.h"
  6. static const Color4 INPUT_BACKGROUND(0x00, 0x00, 0x00, 0xA0);
  7. static const Color4 INPUT_BACKGROUND_2(0x00, 0x00, 0x00, 0x80);
  8. const Vector2 BaseGUI::FIXED_SIZE(400.0f, 300.0f);
  9. BaseGUI::Base::Base() : hovered(false), pressed(false) {
  10. }
  11. void BaseGUI::tickBase(Base& b) {
  12. b.hovered =
  13. isIn(b.pos, b.size, Window::Controls::getMousePosition() / scale);
  14. b.pressed = b.hovered && Controller::wasReleased(Controller::leftClick);
  15. }
  16. void BaseGUI::tick() {
  17. if(Controller::wasReleased(Controller::leftClick)) {
  18. activeInput = -1;
  19. }
  20. for(Label& label : labels) {
  21. tickBase(label.base);
  22. }
  23. Window::Input::disable();
  24. int i = 0;
  25. for(Input& input : inputs) {
  26. tickBase(input.base);
  27. if(input.base.pressed) {
  28. Window::Input::fill(input.text);
  29. activeInput = i;
  30. }
  31. if(i == activeInput) {
  32. Window::Input::enable();
  33. input.text.clear();
  34. Window::Input::toString(input.text);
  35. }
  36. i++;
  37. }
  38. for(Button& button : buttons) {
  39. tickBase(button.base);
  40. }
  41. }
  42. void BaseGUI::updateScale() {
  43. const Size& size = Window::getSize();
  44. scale = static_cast<int>(
  45. std::min(size.width / FIXED_SIZE[0], size.height / FIXED_SIZE[1]));
  46. scale = scale < 1.0f ? 1.0f : scale;
  47. scaledSize = Vector2(size.width / scale, size.height / scale);
  48. Engine::matrix.scale(scale).update();
  49. }
  50. void BaseGUI::renderBase(const Base& base) {
  51. Engine::renderer.renderRectangle(base.pos, base.size,
  52. base.hovered ? INPUT_BACKGROUND_2
  53. : INPUT_BACKGROUND);
  54. }
  55. void BaseGUI::renderLabels() {
  56. for(const Label& label : labels) {
  57. renderCenteredString(label.base, label.text);
  58. }
  59. }
  60. void BaseGUI::renderInputs() {
  61. int index = 0;
  62. for(Input& i : inputs) {
  63. Window::Input::setLimit(
  64. Engine::renderer.charsInSpace(i.base.size[0] - 20.0f));
  65. renderBase(i.base);
  66. StringBuffer<256> text(i.text);
  67. Vector2 pos = renderCenteredString(i.base, text);
  68. if(activeInput == index) {
  69. Vector2 c = Engine::renderer.getStringSize(
  70. text, Window::Input::getCursor());
  71. renderString(Vector2(pos[0] + c[0], pos[1] + 2.0f), "&292_");
  72. renderString(Vector2(pos[0] + c[0], pos[1] + 3.0f), "&292_");
  73. }
  74. index++;
  75. }
  76. }
  77. void BaseGUI::renderButtons() {
  78. for(const Button& button : buttons) {
  79. renderBase(button.base);
  80. renderCenteredString(button.base, button.text);
  81. }
  82. }
  83. void BaseGUI::render() {
  84. renderLabels();
  85. renderInputs();
  86. renderButtons();
  87. }
  88. Vector2 BaseGUI::round(const Vector2& v) const {
  89. return Vector2(static_cast<int>(v[0] * scale) / scale,
  90. static_cast<int>(v[1] * scale) / scale);
  91. }
  92. void BaseGUI::renderString(const Vector2& pos, const char* s) {
  93. Engine::renderer.renderString(round(pos), s);
  94. }
  95. BaseGUI::Label& BaseGUI::addLabel(const char* text) {
  96. labels.add({Base(), text});
  97. return labels[labels.getLength() - 1];
  98. }
  99. BaseGUI::Input& BaseGUI::addInput() {
  100. inputs.add();
  101. return inputs[inputs.getLength() - 1];
  102. }
  103. BaseGUI::Button& BaseGUI::addButton(const char* text) {
  104. buttons.add({Base(), text});
  105. return buttons[buttons.getLength() - 1];
  106. }
  107. Vector2 BaseGUI::renderCenteredString(const Base& b, const char* text) {
  108. Vector2 pos =
  109. b.pos + (b.size - Engine::renderer.getStringSize(text)) * 0.5f;
  110. renderString(pos, text);
  111. return pos;
  112. }
  113. bool BaseGUI::isIn(const Vector2& pos, const Vector2& size,
  114. const Vector2& point) const {
  115. Vector2 end = pos + size;
  116. return pos[0] <= point[0] && point[0] <= end[0] && pos[1] <= point[1] &&
  117. point[1] <= end[1];
  118. }