BaseGUI.cpp 3.4 KB

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