BaseGUI.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "client/gui/BaseGUI.h"
  2. static const Color4 INPUT_BACKGROUND(0xFF, 0xFF, 0xFF, 0x80);
  3. static const Color4 INPUT_BACKGROUND_2(0xFF, 0xFF, 0xFF, 0xC0);
  4. const Vector2 BaseGUI::FIXED_SIZE(400.0f, 300.0f);
  5. BaseGUI::BaseGUI(const Size& size, TextInput*& textInput,
  6. const Controller& controller)
  7. : size(size), textInput(textInput), controller(controller) {
  8. }
  9. void BaseGUI::tick() {
  10. if(controller.leftClick.wasReleased()) {
  11. Vector2 mousePos = controller.getMouse() / scale;
  12. for(Input& input : inputs) {
  13. if(isIn(input.pos, input.size, mousePos)) {
  14. textInput = &input.text;
  15. textInput->setActive(true);
  16. break;
  17. }
  18. }
  19. }
  20. }
  21. void BaseGUI::updateScale(ShaderMatrix& sm) {
  22. scale = static_cast<int>(
  23. std::min(size.width / FIXED_SIZE[0], size.height / FIXED_SIZE[1]));
  24. scale = scale < 1.0f ? 1.0f : scale;
  25. scaledSize = Vector2(size.width / scale, size.height / scale);
  26. sm.scale(scale).update();
  27. }
  28. void BaseGUI::render(float lag, ShaderMatrix& sm, Renderer& r) {
  29. (void)lag;
  30. (void)sm;
  31. Vector2 mousePos = controller.getMouse() / scale;
  32. for(Input& input : inputs) {
  33. if(isIn(input.pos, input.size, mousePos)) {
  34. r.drawRectangle(input.pos, input.size, INPUT_BACKGROUND_2);
  35. } else {
  36. r.drawRectangle(input.pos, input.size, INPUT_BACKGROUND);
  37. }
  38. StringBuffer<256> text(input.text);
  39. r.drawString(input.pos[0], input.pos[1], text);
  40. }
  41. r.drawString(0, 50, StringBuffer<50>(controller.getMouse()));
  42. }
  43. float BaseGUI::round(float f) const {
  44. return static_cast<int>(f * scale) / scale;
  45. }
  46. void BaseGUI::drawCenteredString(Renderer& r, float x, float y, const char* s) {
  47. r.drawString(round(x - r.drawStringWidth(s) * 0.5f), round(y), s);
  48. }
  49. BaseGUI::Input& BaseGUI::addInput() {
  50. inputs.add();
  51. return inputs[inputs.getLength() - 1];
  52. }
  53. bool BaseGUI::isIn(const Vector2& pos, const Vector2& size,
  54. const Vector2& point) const {
  55. Vector2 end = pos + size;
  56. return pos[0] <= point[0] && point[0] <= end[0] && pos[1] <= point[1] &&
  57. point[1] <= end[1];
  58. }