Browse Source

input experiments

Kajetan Johannes Hammerle 3 years ago
parent
commit
91bab12fc1

+ 10 - 12
client/Game.cpp

@@ -2,22 +2,23 @@
 #include "common/network/Packets.h"
 #include "gaming-core/utils/Utils.h"
 
-Game::Game(TextInput& textInput, const Controller& controller, const Clock& fps,
-           const Clock& tps, RenderSettings& settings, const Size& size,
-           Client& client)
-    : textInput(textInput), controller(controller), fps(fps), tps(tps),
-      renderSettings(settings), size(size), client(client), state(State::START),
+Game::Game(TextInput*& textInput, const Controller& controller,
+           const Clock& fps, const Clock& tps, RenderSettings& settings,
+           const Size& size, Client& client)
+    : controller(controller), fps(fps), tps(tps), renderSettings(settings),
+      size(size), client(client), state(State::START),
       baseGUI(size, textInput, controller), startGUI(baseGUI),
       world(blockRegistry), worldRenderer(world), connected(false) {
     pos = Vector3(16.0f, 30.0f, -10.0f);
     rotation = Quaternion(Vector3(1.0f, 0.0f, 0.0f), 30) * rotation;
     rotation = Quaternion(Vector3(0.0f, 1.0f, 0.0f), 30) * rotation;
-
-    textInput.setActive(true);
 }
 
 void Game::tick() {
-    if(!connected && controller.down.wasReleased()) {
+    switch(state) {
+        case State::START: startGUI.tick(); break;
+    }
+    /*if(!connected && controller.down.wasReleased()) {
         if(client.connect("127.0.0.1", 11196, 3000)) {
             std::cout << client.getError() << '\n';
         } else {
@@ -25,7 +26,7 @@ void Game::tick() {
         }
     } else {
         client.consumeEvents(*this);
-    }
+    }*/
 
     lastRotation = rotation;
     lastPos = pos;
@@ -87,9 +88,6 @@ void Game::renderOverlay(float lag, ShaderMatrix& sm, Renderer& r) {
         .append(" &999TPS: &722")
         .append(tps.getUpdatesPerSecond());
     r.drawString(10, 10, s);
-    s.clear();
-    textInput.getInput(s);
-    r.drawString(10, 30, s);
 }
 
 bool Game::isRunning() const {

+ 1 - 2
client/Game.h

@@ -18,7 +18,7 @@ class Game final {
 public:
     enum State { START };
 
-    Game(TextInput& textInput, const Controller& controller, const Clock& fps,
+    Game(TextInput*& textInput, const Controller& controller, const Clock& fps,
          const Clock& tps, RenderSettings& renderSettings, const Size& size,
          Client& client);
 
@@ -33,7 +33,6 @@ public:
     void onPacket(InPacket& in);
 
 private:
-    TextInput& textInput;
     const Controller& controller;
     const Clock& fps;
     const Clock& tps;

+ 1 - 1
client/Main.cpp

@@ -38,7 +38,7 @@ int main() {
 
     Size size(1024, 620);
     WindowOptions options(4, 0, size, false, "test");
-    TextInput textInput;
+    TextInput* textInput = nullptr;
     Window window(textInput, options);
     if(window.hasError() || GLEW::init()) {
         return 0;

+ 58 - 2
client/gui/BaseGUI.cpp

@@ -1,12 +1,68 @@
 #include "client/gui/BaseGUI.h"
 
-BaseGUI::BaseGUI(const Size& size, TextInput& textInput,
+static const Color4 INPUT_BACKGROUND(0xFF, 0xFF, 0xFF, 0x80);
+static const Color4 INPUT_BACKGROUND_2(0xFF, 0xFF, 0xFF, 0xC0);
+const Vector2 BaseGUI::FIXED_SIZE(400.0f, 300.0f);
+
+BaseGUI::BaseGUI(const Size& size, TextInput*& textInput,
                  const Controller& controller)
     : size(size), textInput(textInput), controller(controller) {
 }
 
+void BaseGUI::tick() {
+    if(controller.leftClick.wasReleased()) {
+        Vector2 mousePos = controller.getMouse() / scale;
+        for(Input& input : inputs) {
+            if(isIn(input.pos, input.size, mousePos)) {
+                textInput = &input.text;
+                textInput->setActive(true);
+                break;
+            }
+        }
+    }
+}
+
+void BaseGUI::updateScale(ShaderMatrix& sm) {
+    scale = static_cast<int>(
+        std::min(size.width / FIXED_SIZE[0], size.height / FIXED_SIZE[1]));
+    scale = scale < 1.0f ? 1.0f : scale;
+    scaledSize = Vector2(size.width / scale, size.height / scale);
+    sm.scale(scale).update();
+}
+
 void BaseGUI::render(float lag, ShaderMatrix& sm, Renderer& r) {
     (void)lag;
     (void)sm;
-    (void)r;
+
+    Vector2 mousePos = controller.getMouse() / scale;
+    for(Input& input : inputs) {
+        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);
+    }
+    r.drawString(0, 50, StringBuffer<50>(controller.getMouse()));
+}
+
+float BaseGUI::round(float f) const {
+    return static_cast<int>(f * 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);
+}
+
+BaseGUI::Input& BaseGUI::addInput() {
+    inputs.add();
+    return inputs[inputs.getLength() - 1];
+}
+
+bool BaseGUI::isIn(const Vector2& pos, const Vector2& size,
+                   const Vector2& point) const {
+    Vector2 end = pos + size;
+    return pos[0] <= point[0] && point[0] <= end[0] && pos[1] <= point[1] &&
+           point[1] <= end[1];
 }

+ 24 - 4
client/gui/BaseGUI.h

@@ -5,18 +5,38 @@
 #include "client/rendering/Renderer.h"
 #include "client/rendering/ShaderMatrix.h"
 #include "gaming-core/input/TextInput.h"
+#include "gaming-core/math/Vector.h"
+#include "gaming-core/utils/List.h"
 #include "gaming-core/utils/Size.h"
 
-class BaseGUI final {
+struct BaseGUI final {
+    static const Vector2 FIXED_SIZE;
+
     const Size& size;
-    TextInput& textInput;
+    TextInput*& textInput;
     const Controller& controller;
 
-public:
-    BaseGUI(const Size& size, TextInput& textInput,
+    float scale;
+    Vector2 scaledSize;
+
+    struct Input final {
+        Vector2 pos;
+        Vector2 size;
+        TextInput text;
+    };
+    List<Input> inputs;
+
+    BaseGUI(const Size& size, TextInput*& textInput,
             const Controller& controller);
 
+    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);
+    Input& addInput();
+    bool isIn(const Vector2& pos, const Vector2& size,
+              const Vector2& point) const;
 };
 
 #endif

+ 21 - 4
client/gui/StartGUI.cpp

@@ -1,11 +1,28 @@
 #include "client/gui/StartGUI.h"
 
-StartGUI::StartGUI(BaseGUI base) : base(base) {
+StartGUI::StartGUI(BaseGUI b)
+    : base(b), address(base.addInput()), test(base.addInput()) {
+}
+
+void StartGUI::tick() {
+    base.tick();
 }
 
 void StartGUI::render(float lag, ShaderMatrix& sm, Renderer& r) {
-    base.render(lag, sm, r);
+    base.updateScale(sm);
+
+    Vector2 size = Vector2(BaseGUI::FIXED_SIZE[0] - 80.0f, 110.0f);
+    Vector2 pos = (base.scaledSize - size) * 0.5f;
+    Vector2 mid = pos + size * 0.5f;
 
-    sm.scale(2.0f).update();
-    r.drawRectangle(0, 0, 100, 200, Color4(0xFF, 0x00, 0xFF, 0xFF));
+    address.pos = pos + Vector2(40.0f, 30.0f);
+    address.size = Vector2(size[0] - 80.0f, 30.0f);
+
+    test.pos = pos + Vector2(40.0f, 70.0f);
+    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.render(lag, sm, r);
 }

+ 4 - 1
client/gui/StartGUI.h

@@ -5,10 +5,13 @@
 
 class StartGUI final {
     BaseGUI base;
+    BaseGUI::Input& address;
+    BaseGUI::Input& test;
 
 public:
-    StartGUI(BaseGUI base);
+    StartGUI(BaseGUI b);
 
+    void tick();
     void render(float lag, ShaderMatrix& sm, Renderer& r);
 };
 

+ 35 - 8
client/rendering/Renderer.cpp

@@ -37,9 +37,9 @@ void Renderer::drawString(float x, float y, const char* text) {
         float maxY = minY + (1.0f / 16.0f);
 
         buffer.add(x).add(y).add(minX).add(minY).add(color);
-        buffer.add(x).add(y + 8).add(minX).add(maxY).add(color);
-        buffer.add(x + 6).add(y).add(maxX).add(minY).add(color);
-        buffer.add(x + 6).add(y + 8).add(maxX).add(maxY).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;
         index++;
@@ -51,13 +51,40 @@ void Renderer::drawString(float x, float y, const char* text) {
     vertexBuffer.drawStrip(vertices);
 }
 
-void Renderer::drawRectangle(float x, float y, float width, float height,
+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;
+            continue;
+        }
+        width += 6.0f;
+        index++;
+    }
+    return width;
+}
+
+void Renderer::drawRectangle(const Vector2& pos, const Vector2& size,
                              Color4 c) {
     buffer.clear();
-    buffer.add(x).add(y).add(0.0f).add(0.0f).add(c);
-    buffer.add(x).add(y + height).add(0.0f).add(0.0f).add(c);
-    buffer.add(x + width).add(y).add(0.0f).add(0.0f).add(c);
-    buffer.add(x + width).add(y + height).add(0.0f).add(0.0f).add(c);
+    buffer.add(pos[0]).add(pos[1]).add(0.0f).add(0.0f).add(c);
+    buffer.add(pos[0]).add(pos[1] + size[1]).add(0.0f).add(0.0f).add(c);
+    buffer.add(pos[0] + size[0]).add(pos[1]).add(0.0f).add(0.0f).add(c);
+    buffer.add(pos[0] + size[0])
+        .add(pos[1] + size[1])
+        .add(0.0f)
+        .add(0.0f)
+        .add(c);
     update();
     vertexBuffer.drawStrip(4);
 }

+ 3 - 1
client/rendering/Renderer.h

@@ -1,6 +1,7 @@
 #ifndef RENDERER_H
 #define RENDERER_H
 
+#include "gaming-core/math/Vector.h"
 #include "gaming-core/rendering/FileTexture.h"
 #include "gaming-core/rendering/VertexBuffer.h"
 #include "gaming-core/utils/Buffer.h"
@@ -15,7 +16,8 @@ public:
     Renderer();
 
     void drawString(float x, float y, const char* text);
-    void drawRectangle(float x, float y, float width, float height, Color4 c);
+    float drawStringWidth(const char* text);
+    void drawRectangle(const Vector2& pos, const Vector2& size, Color4 c);
 
 private:
     void update();

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit 923055a1562c99d025bd0ee7477dd22929f209a7
+Subproject commit 18c11759be38328c95fea4e3ef067f4a0c390c4d