Browse Source

mouse support

Kajetan Johannes Hammerle 3 years ago
parent
commit
cc650ea739
4 changed files with 68 additions and 6 deletions
  1. 42 4
      input/Buttons.cpp
  2. 15 2
      input/Buttons.h
  3. 8 0
      rendering/Window.cpp
  4. 3 0
      rendering/Window.h

+ 42 - 4
input/Buttons.cpp

@@ -1,9 +1,12 @@
 #include "input/Buttons.h"
 #include "input/Buttons.h"
 
 
-Buttons::Axis::Axis() : less(0.0f), greater(0.0f), lessIndex(-1), greaterIndex(-1) {
+Buttons::Axis::Axis()
+    : less(0.0f), greater(0.0f), lessIndex(-1), greaterIndex(-1) {
 }
 }
 
 
-Buttons::Buttons(const Window& window) : window(window), dummy(0, "Dummy"), activeController(-1), gamepadToButton(-1) {
+Buttons::Buttons(const Window& window)
+    : window(window), dummy(0, "Dummy"), activeController(-1),
+      gamepadToButton(-1) {
 }
 }
 
 
 Button& Buttons::add(int key, const char* name) {
 Button& Buttons::add(int key, const char* name) {
@@ -13,6 +16,13 @@ Button& Buttons::add(int key, const char* name) {
     return buttons[buttons.getLength() - 1];
     return buttons[buttons.getLength() - 1];
 }
 }
 
 
+Button& Buttons::addMouse(int mouse, const char* name) {
+    if(mouseButtons.add(mouse, name)) {
+        return dummy;
+    }
+    return mouseButtons[mouseButtons.getLength() - 1];
+}
+
 void Buttons::mapGamepadButton(const Button& button, int mapping) {
 void Buttons::mapGamepadButton(const Button& button, int mapping) {
     gamepadToButton[mapping] = searchButton(button);
     gamepadToButton[mapping] = searchButton(button);
 }
 }
@@ -35,12 +45,38 @@ void Buttons::tick() {
     for(int i = 0; i < buttons.getLength(); i++) {
     for(int i = 0; i < buttons.getLength(); i++) {
         buttons[i].tick(window.isKeyDown(buttons[i].key) || down[i]);
         buttons[i].tick(window.isKeyDown(buttons[i].key) || down[i]);
     }
     }
+    for(int i = 0; i < mouseButtons.getLength(); i++) {
+        mouseButtons[i].tick(window.isMouseDown(mouseButtons[i].key));
+    }
+    lastMouseX = mouseX;
+    lastMouseY = mouseY;
+    window.getMousePosition(mouseX, mouseY);
 }
 }
 
 
 const ButtonList& Buttons::get() const {
 const ButtonList& Buttons::get() const {
     return buttons;
     return buttons;
 }
 }
 
 
+const MouseList& Buttons::getMouse() const {
+    return mouseButtons;
+}
+
+double Buttons::getLastMouseX() const {
+    return lastMouseX;
+}
+
+double Buttons::getLastMouseY() const {
+    return lastMouseY;
+}
+
+double Buttons::getMouseX() const {
+    return mouseX;
+}
+
+double Buttons::getMouseY() const {
+    return mouseY;
+}
+
 bool Buttons::searchForGamepad() {
 bool Buttons::searchForGamepad() {
     if(activeController != -1) {
     if(activeController != -1) {
         return true;
         return true;
@@ -65,9 +101,11 @@ void Buttons::checkGamepad(DownArray& down) {
         }
         }
     }
     }
     for(int i = 0; i <= GLFW_GAMEPAD_AXIS_LAST; i++) {
     for(int i = 0; i <= GLFW_GAMEPAD_AXIS_LAST; i++) {
-        if(gamepadAxisToButton[i].greaterIndex != -1 && state.axes[i] > gamepadAxisToButton[i].greater) {
+        if(gamepadAxisToButton[i].greaterIndex != -1 &&
+           state.axes[i] > gamepadAxisToButton[i].greater) {
             down[gamepadAxisToButton[i].greaterIndex] = true;
             down[gamepadAxisToButton[i].greaterIndex] = true;
-        } else if(gamepadAxisToButton[i].lessIndex != -1 && state.axes[i] < gamepadAxisToButton[i].less) {
+        } else if(gamepadAxisToButton[i].lessIndex != -1 &&
+                  state.axes[i] < gamepadAxisToButton[i].less) {
             down[gamepadAxisToButton[i].lessIndex] = true;
             down[gamepadAxisToButton[i].lessIndex] = true;
         }
         }
     }
     }

+ 15 - 2
input/Buttons.h

@@ -2,11 +2,12 @@
 #define BUTTONS_H
 #define BUTTONS_H
 
 
 #include "input/Button.h"
 #include "input/Button.h"
+#include "rendering/Window.h"
 #include "utils/Array.h"
 #include "utils/Array.h"
 #include "utils/ArrayList.h"
 #include "utils/ArrayList.h"
-#include "rendering/Window.h"
 
 
 typedef ArrayList<Button, 32> ButtonList;
 typedef ArrayList<Button, 32> ButtonList;
+typedef ArrayList<Button, 1 + GLFW_MOUSE_BUTTON_LAST> MouseList;
 
 
 class Buttons final {
 class Buttons final {
     typedef Array<bool, 32> DownArray;
     typedef Array<bool, 32> DownArray;
@@ -15,6 +16,11 @@ class Buttons final {
     Button dummy;
     Button dummy;
     int activeController;
     int activeController;
     ButtonList buttons;
     ButtonList buttons;
+    MouseList mouseButtons;
+    double lastMouseX;
+    double lastMouseY;
+    double mouseX;
+    double mouseY;
 
 
     struct Axis {
     struct Axis {
         float less;
         float less;
@@ -25,19 +31,26 @@ class Buttons final {
         Axis();
         Axis();
     };
     };
 
 
-    Array<int, 1 + GLFW_GAMEPAD_BUTTON_LAST> gamepadToButton;
     Array<Axis, 1 + GLFW_GAMEPAD_AXIS_LAST> gamepadAxisToButton;
     Array<Axis, 1 + GLFW_GAMEPAD_AXIS_LAST> gamepadAxisToButton;
+    Array<int, 1 + GLFW_GAMEPAD_BUTTON_LAST> gamepadToButton;
 
 
 public:
 public:
     Buttons(const Window& window);
     Buttons(const Window& window);
 
 
     Button& add(int key, const char* name);
     Button& add(int key, const char* name);
+    Button& addMouse(int mouse, const char* name);
     void mapGamepadButton(const Button& button, int mapping);
     void mapGamepadButton(const Button& button, int mapping);
     void mapGamepadAxis(const Button& button, float value, int index);
     void mapGamepadAxis(const Button& button, float value, int index);
 
 
     void tick();
     void tick();
 
 
     const ButtonList& get() const;
     const ButtonList& get() const;
+    const MouseList& getMouse() const;
+
+    double getLastMouseX() const;
+    double getLastMouseY() const;
+    double getMouseX() const;
+    double getMouseY() const;
 
 
 private:
 private:
     bool searchForGamepad();
     bool searchForGamepad();

+ 8 - 0
rendering/Window.cpp

@@ -77,6 +77,14 @@ bool Window::isKeyDown(int key) const {
     return glfwGetKey(window, key) == GLFW_PRESS;
     return glfwGetKey(window, key) == GLFW_PRESS;
 }
 }
 
 
+bool Window::isMouseDown(int mouse) const {
+    return glfwGetMouseButton(window, mouse) == GLFW_PRESS;
+}
+
+void Window::getMousePosition(double& x, double& y) const {
+    glfwGetCursorPos(window, &x, &y);
+}
+
 void Window::keyCallback(GLFWwindow* w, int key, int scancode, int action,
 void Window::keyCallback(GLFWwindow* w, int key, int scancode, int action,
                          int mods) {
                          int mods) {
     void* p = glfwGetWindowUserPointer(w);
     void* p = glfwGetWindowUserPointer(w);

+ 3 - 0
rendering/Window.h

@@ -30,6 +30,9 @@ public:
     Size getFramebufferSize() const;
     Size getFramebufferSize() const;
     bool isKeyDown(int key) const;
     bool isKeyDown(int key) const;
 
 
+    bool isMouseDown(int mouse) const;
+    void getMousePosition(double& x, double& y) const;
+
 private:
 private:
     static void keyCallback(GLFWwindow* w, int key, int scancode, int action,
     static void keyCallback(GLFWwindow* w, int key, int scancode, int action,
                             int mods);
                             int mods);