Chat.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "client/Chat.h"
  2. #include "client/Controls.h"
  3. #include "client/FontRenderer.h"
  4. #include "common/NetworkPackets.h"
  5. #include "data/RingBuffer.h"
  6. #include "network/Client.h"
  7. #include "rendering/GL.h"
  8. static constexpr int LINES = 20;
  9. static RingBuffer<Chat::Message, LINES> chat;
  10. static bool active = false;
  11. static int alpha = 255;
  12. static int getAlpha() {
  13. return alpha > 255 ? 255 : alpha;
  14. }
  15. static void enable() {
  16. Window::Input::enable();
  17. Window::freeCursor();
  18. active = true;
  19. }
  20. static void disable() {
  21. Window::Input::disable();
  22. Window::trapCursor();
  23. active = false;
  24. }
  25. void Chat::tick() {
  26. if(active) {
  27. alpha = 500;
  28. if(Controls::wasReleased(Controls::enter)) {
  29. OutPacket out(100);
  30. out.writeU8(static_cast<uint8>(ToServerPacket::CHAT));
  31. for(uint32 u : Window::Input::getUnicode()) {
  32. out.writeU32(u);
  33. }
  34. Client::send(out, PacketSendMode::RELIABLE);
  35. Window::Input::reset();
  36. disable();
  37. }
  38. if(Controls::wasReleased(Controls::escape)) {
  39. disable();
  40. }
  41. } else {
  42. alpha = std::max(alpha - 5, 0);
  43. if(Controls::isDown(Controls::chat)) {
  44. enable();
  45. }
  46. }
  47. }
  48. void Chat::render() {
  49. GL::disableDepthTesting();
  50. GL::enableBlending();
  51. Vector2 size = Window::getSize().toFloat();
  52. IntVector2 padding(2.0f, 2.0f);
  53. FontRenderer::bind();
  54. FontRenderer::setProjection(Matrix());
  55. Matrix v;
  56. v.scale(Vector3(2.0f / size[0], -2.0f / size[1], 1.0f));
  57. v.translate(Vector3(-1.0f, 1.0f, 0.0f));
  58. FontRenderer::setView(v);
  59. int scale = ((size[1] - padding[1]) / 9.0f) / (LINES + 2);
  60. float y = size[1] / scale - 9.0f * (chat.getLength() + 2) - padding[1];
  61. float max = 0.0f;
  62. for(int i = 0; i < chat.getLength(); i++) {
  63. max = std::max(max, FontRenderer::getWidth(chat[i], -1));
  64. }
  65. Matrix model;
  66. model.translateTo(Vector3(1.0f, 1.0f, 0.0f)).scale(scale);
  67. FontRenderer::setModel(model);
  68. FontRenderer::draw(
  69. StringBuffer<50>("FPS: ").append(Window::getFramesPerSecond()), 255);
  70. model.translateTo(Vector3(padding[0] - 2.0f, y - 2.0f, 0.0f)).scale(scale);
  71. FontRenderer::setModel(model);
  72. if(chat.getLength() > 0) {
  73. FontRenderer::draw(Vector2(max + 4.0f, 9.0f * chat.getLength() + 4.0f),
  74. Color4(0, 0, 0, getAlpha() / 2));
  75. }
  76. for(int i = 0; i < chat.getLength(); i++) {
  77. model.translateTo(Vector3(padding[0], i * 9.0f + y, 0.0f)).scale(scale);
  78. FontRenderer::setModel(model);
  79. FontRenderer::draw(chat[i], getAlpha());
  80. }
  81. if(!active) {
  82. return;
  83. }
  84. float height = (chat.getLength() + 1) * 9.0f + y;
  85. StringBuffer<256> s;
  86. Window::Input::toString(s);
  87. float width = FontRenderer::getWidth(s, -1) + 8.0f;
  88. model.translateTo(Vector3(padding[0] - 2.0f, height - 2.0f, 0.0f))
  89. .scale(scale);
  90. FontRenderer::setModel(model);
  91. FontRenderer::draw(Vector2(width + 4.0f, 12.0f), Color4(0, 0, 0, 127));
  92. model.translateTo(Vector3(padding[0], height, 0.0f)).scale(scale);
  93. FontRenderer::setModel(model);
  94. FontRenderer::draw(s, 255);
  95. s.clear();
  96. const List<uint32>& unicode = Window::Input::getUnicode();
  97. for(int i = 0; i < Window::Input::getCursor(); i++) {
  98. s.appendUnicode(unicode[i]);
  99. }
  100. width = FontRenderer::getWidth(s, -1);
  101. model.translateTo(Vector3(padding[0] + width, height + 2.0f, 0.0f))
  102. .scale(scale);
  103. FontRenderer::setModel(model);
  104. FontRenderer::draw("_", 255);
  105. }
  106. bool Chat::isActive() {
  107. return active;
  108. }
  109. void Chat::add(const Message& msg) {
  110. if(chat.add(msg)) {
  111. chat.remove();
  112. chat.add(msg);
  113. }
  114. alpha = 500;
  115. }