Browse Source

cursor for textinput, core update, refactored string rendering

Kajetan Johannes Hammerle 3 years ago
parent
commit
4988c44d06

+ 1 - 1
client/Game.cpp

@@ -87,7 +87,7 @@ void Game::renderOverlay(float lag, ShaderMatrix& sm, Renderer& r) {
         .append(fps.getUpdatesPerSecond())
         .append(" &999TPS: &722")
         .append(tps.getUpdatesPerSecond());
-    r.drawString(10, 10, s);
+    r.drawString(Vector2(10.0f, 10.0f), s);
 }
 
 bool Game::isRunning() const {

+ 1 - 2
client/Main.cpp

@@ -19,8 +19,7 @@ int main() {
         std::cout << client.getError() << '\n';
         return 0;
     }
-    Size size(1024, 620);
-    WindowOptions options(4, 0, size, false, "test");
+    WindowOptions options(4, 0, {1024, 620}, false, "test");
     TextInput* textInput = nullptr;
     Window w(textInput, options);
     if(w.getError().has()) {

+ 23 - 12
client/gui/BaseGUI.cpp

@@ -1,7 +1,7 @@
 #include "client/gui/BaseGUI.h"
 
-static const Color4 INPUT_BACKGROUND(0xFF, 0xFF, 0xFF, 0x80);
-static const Color4 INPUT_BACKGROUND_2(0xFF, 0xFF, 0xFF, 0xC0);
+static const Color4 INPUT_BACKGROUND(0x00, 0x00, 0x00, 0xA0);
+static const Color4 INPUT_BACKGROUND_2(0x00, 0x00, 0x00, 0x80);
 const Vector2 BaseGUI::FIXED_SIZE(400.0f, 300.0f);
 
 BaseGUI::BaseGUI(const Size& size, TextInput*& textInput,
@@ -30,29 +30,34 @@ void BaseGUI::updateScale(ShaderMatrix& sm) {
     sm.scale(scale).update();
 }
 
-void BaseGUI::render(float lag, ShaderMatrix& sm, Renderer& r) {
-    (void)lag;
-    (void)sm;
-
+void BaseGUI::render(float, ShaderMatrix&, Renderer& r) {
     Vector2 mousePos = controller.getMouse() / scale;
     for(Input& input : inputs) {
+        input.text.setLimit(r.charsInSpace(input.size[0] - 20.0f));
         if(isIn(input.pos, input.size, mousePos)) {
             r.drawRectangle(input.pos, input.size, INPUT_BACKGROUND_2);
         } else {
             r.drawRectangle(input.pos, input.size, INPUT_BACKGROUND);
         }
         StringBuffer<256> text(input.text);
-        r.drawString(input.pos[0], input.pos[1], text);
+        Vector2 size = r.getStringSize(text);
+        Vector2 pos = input.pos + (input.size - size) * 0.5f;
+        drawString(r, pos, text);
+        if(textInput == &input.text) {
+            Vector2 cursor = r.getStringSize(text, input.text.getCursor());
+            drawString(r, Vector2(pos[0] + cursor[0], pos[1] + 2.0f), "&292_");
+            drawString(r, Vector2(pos[0] + cursor[0], pos[1] + 3.0f), "&292_");
+        }
     }
-    r.drawString(0, 50, StringBuffer<50>(controller.getMouse()));
 }
 
-float BaseGUI::round(float f) const {
-    return static_cast<int>(f * scale) / scale;
+Vector2 BaseGUI::round(const Vector2& v) const {
+    return Vector2(static_cast<int>(v[0] * scale) / scale,
+                   static_cast<int>(v[1] * scale) / scale);
 }
 
-void BaseGUI::drawCenteredString(Renderer& r, float x, float y, const char* s) {
-    r.drawString(round(x - r.drawStringWidth(s) * 0.5f), round(y), s);
+void BaseGUI::drawString(Renderer& r, const Vector2& pos, const char* s) {
+    r.drawString(round(pos), s);
 }
 
 BaseGUI::Input& BaseGUI::addInput() {
@@ -60,6 +65,12 @@ BaseGUI::Input& BaseGUI::addInput() {
     return inputs[inputs.getLength() - 1];
 }
 
+void BaseGUI::drawCenteredString(Renderer& r, const Vector2& pos,
+                                 const Vector2& size, const char* text) {
+    Vector2 textSize = r.getStringSize(text);
+    drawString(r, pos + (size - textSize) * 0.5f, text);
+}
+
 bool BaseGUI::isIn(const Vector2& pos, const Vector2& size,
                    const Vector2& point) const {
     Vector2 end = pos + size;

+ 4 - 2
client/gui/BaseGUI.h

@@ -32,9 +32,11 @@ struct BaseGUI final {
     void tick();
     void updateScale(ShaderMatrix& sm);
     void render(float lag, ShaderMatrix& sm, Renderer& r);
-    float round(float f) const;
-    void drawCenteredString(Renderer& r, float x, float y, const char* s);
+    Vector2 round(const Vector2& v) const;
+    void drawString(Renderer& r, const Vector2& pos, const char* s);
     Input& addInput();
+    void drawCenteredString(Renderer& r, const Vector2& pos,
+                            const Vector2& size, const char* text);
     bool isIn(const Vector2& pos, const Vector2& size,
               const Vector2& point) const;
 };

+ 2 - 3
client/gui/StartGUI.cpp

@@ -13,7 +13,6 @@ void StartGUI::render(float lag, ShaderMatrix& sm, Renderer& r) {
 
     Vector2 size = Vector2(BaseGUI::FIXED_SIZE[0] - 80.0f, 110.0f);
     Vector2 pos = (base.scaledSize - size) * 0.5f;
-    Vector2 mid = pos + size * 0.5f;
 
     address.pos = pos + Vector2(40.0f, 30.0f);
     address.size = Vector2(size[0] - 80.0f, 30.0f);
@@ -22,7 +21,7 @@ void StartGUI::render(float lag, ShaderMatrix& sm, Renderer& r) {
     test.size = Vector2(size[0] - 80.0f, 30.0f);
 
     r.drawRectangle(pos, size, Color4(0x50, 0x50, 0x50, 0xFF));
-    base.drawCenteredString(r, mid[0], pos[1] + 10.0f, "Connect to server ...");
-
+    base.drawCenteredString(r, pos, Vector2(size[0], 30.0f),
+                            "Connect to server ...");
     base.render(lag, sm, r);
 }

+ 73 - 50
client/rendering/Renderer.cpp

@@ -2,76 +2,95 @@
 #include "gaming-core/rendering/Attributes.h"
 #include "gaming-core/utils/Buffer.h"
 
-Renderer::Renderer() : buffer(16), font("resources/font8x8.png") {
+Renderer::Renderer()
+    : buffer(16), font("resources/font8x8.png"), text(nullptr), index(0),
+      maxIndex(0), x(0.0f), y(0.0f), vertices(0) {
     vertexBuffer.setAttributes(
         Attributes().addFloat(2).addFloat(2).addColor4());
     vertexBuffer.setStreamData(1024 * 1024);
 }
 
-void Renderer::drawString(float x, float y, const char* text) {
+void Renderer::setupStringRead(const char* s, int limit) {
+    text = s;
+    index = 0;
+    maxIndex = limit;
+}
+
+void Renderer::setupString(const Vector2& pos, const char* s, int limit) {
     buffer.clear();
-    int index = 0;
-    int vertices = 0;
-    Color4 color(0xFF, 0xFF, 0xFF, 0x00);
-    while(text[index] != '\0') {
-        char32_t c = text[index];
-        if(c > 128 && text[index + 1] != '\0') {
-            index++;
-            c = (text[index] & 0x3F) | ((c & 0x1F) << 6);
-        }
-        if(c == '&') {
-            if(text[index + 1] == '\0' || text[index + 2] == '\0' ||
-               text[index + 3] == '\0') {
-                break;
-            }
-            color[0] = ((text[index + 1] - '0') * 255) / 9;
-            color[1] = ((text[index + 2] - '0') * 255) / 9;
-            color[2] = ((text[index + 3] - '0') * 255) / 9;
-            index += 4;
-            continue;
-        }
+    setupStringRead(s, limit);
+    x = pos[0];
+    y = pos[1];
+    vertices = 0;
+    color = {0xFF, 0xFF, 0xFF, 0x00};
+}
 
-        float minX = (c & 0xF) * (1.0f / 16.0f) + 1.0f / 128.0f;
-        float minY = (c >> 4) * (1.0f / 16.0f);
-        float maxX = minX + (1.0f / 16.0f) - 2.0f / 128.0f;
-        float maxY = minY + (1.0f / 16.0f);
+bool Renderer::hasStringData() const {
+    return (index < maxIndex || maxIndex < 0) && text[index] != '\0';
+}
 
-        buffer.add(x).add(y).add(minX).add(minY).add(color);
-        buffer.add(x).add(y + 8.0f).add(minX).add(maxY).add(color);
-        buffer.add(x + 6.0f).add(y).add(maxX).add(minY).add(color);
-        buffer.add(x + 6.0f).add(y + 8.0f).add(maxX).add(maxY).add(color);
+char32_t Renderer::readUnicode() {
+    char32_t c = text[index];
+    index++;
+    if(c > 128 && hasStringData()) {
+        c = (text[index] & 0x3F) | ((c & 0x1F) << 6);
+        index++;
+    }
+    return c;
+}
 
-        x += 6;
+bool Renderer::readColor(char32_t c) {
+    if(c != '&') {
+        return false;
+    }
+    for(int i = 0; i < 3; i++) {
+        if(!hasStringData()) {
+            return true;
+        }
+        color[i] = ((text[index] - '0') * 255) / 9;
         index++;
-        vertices += 4;
     }
+    return true;
+}
+
+void Renderer::addChar(char32_t c) {
+    float minX = (c & 0xF) * (1.0f / 16.0f) + (1.0f / 128.0f);
+    float minY = (c >> 4) * (1.0f / 16.0f);
+    float maxX = minX + (1.0f / 16.0f) - (2.0f / 128.0f);
+    float maxY = minY + (1.0f / 16.0f);
+    buffer.add(x).add(y).add(minX).add(minY).add(color);
+    buffer.add(x).add(y + 8.0f).add(minX).add(maxY).add(color);
+    buffer.add(x + 6.0f).add(y).add(maxX).add(minY).add(color);
+    buffer.add(x + 6.0f).add(y + 8.0f).add(maxX).add(maxY).add(color);
+    x += 6;
+    vertices += 4;
+}
 
+void Renderer::drawString(const Vector2& pos, const char* text, int limit) {
+    setupString(pos, text, limit);
+    while(hasStringData()) {
+        char32_t c = readUnicode();
+        if(readColor(c)) {
+            continue;
+        }
+        addChar(c);
+    }
     font.bindTo(0);
     update();
     vertexBuffer.drawStrip(vertices);
 }
 
-float Renderer::drawStringWidth(const char* text) {
-    float width = 0.0f;
-    int index = 0;
-    while(text[index] != '\0') {
-        char32_t c = text[index];
-        if(c > 128 && text[index + 1] != '\0') {
-            index++;
-            c = (text[index] & 0x3F) | ((c & 0x1F) << 6);
-        }
-        if(c == '&') {
-            if(text[index + 1] == '\0' || text[index + 2] == '\0' ||
-               text[index + 3] == '\0') {
-                break;
-            }
-            index += 4;
+Vector2 Renderer::getStringSize(const char* text, int limit) {
+    setupStringRead(text, limit);
+    Vector2 size(0.0f, 8.0f);
+    while(hasStringData()) {
+        char32_t c = readUnicode();
+        if(readColor(c)) {
             continue;
         }
-        width += 6.0f;
-        index++;
+        size[0] += 6.0f;
     }
-    return width;
+    return size;
 }
 
 void Renderer::drawRectangle(const Vector2& pos, const Vector2& size,
@@ -92,4 +111,8 @@ void Renderer::drawRectangle(const Vector2& pos, const Vector2& size,
 void Renderer::update() {
     vertexBuffer.updateData(
         0, std::min(buffer.getLength(), vertexBuffer.getSize()), buffer);
+}
+
+int Renderer::charsInSpace(float width) {
+    return std::abs(width / 6.0f);
 }

+ 17 - 2
client/rendering/Renderer.h

@@ -11,15 +11,30 @@ class Renderer final {
     VertexBuffer vertexBuffer;
     Buffer buffer;
     FileTexture font;
+    Color4 color;
+    const char* text;
+    int index;
+    int maxIndex;
+    float x;
+    float y;
+    int vertices;
 
 public:
     Renderer();
 
-    void drawString(float x, float y, const char* text);
-    float drawStringWidth(const char* text);
+    void drawString(const Vector2& pos, const char* text, int limit = -1);
+    Vector2 getStringSize(const char* text, int limit = -1);
     void drawRectangle(const Vector2& pos, const Vector2& size, Color4 c);
+    int charsInSpace(float width);
 
 private:
+    void setupStringRead(const char* s, int limit);
+    void setupString(const Vector2& pos, const char* s, int limit);
+    bool hasStringData() const;
+    char32_t readUnicode();
+    bool readColor(char32_t c);
+    void addChar(char32_t c);
+
     void update();
 };
 

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit f771b8a0406ce86272f32374811aca8b70dac424
+Subproject commit 35425beaa48eab218b807971ff8d8294eb3ca6f3