1 /**
2  * Copyright:       Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved
3  * Authors:         $(Gabriel Gheorghe)
4  * License:         $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007)
5  * Source:          $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/input/event.d)
6  * Documentation:
7  * Coverage:
8 **/
9 module liberty.input.event;
10 
11 import bindbc.glfw;
12 
13 import liberty.math.vector;
14 import liberty.core.engine;
15 import liberty.input.keyboard.constants;
16 import liberty.input.mouse.constants;
17 import liberty.input.impl;
18 import liberty.core.platform;
19 import liberty.graphics.engine;
20 
21 package(liberty) class EventManager {
22   private {
23     static bool firstMouse = true;
24     static float lastX;
25     static float lastY;
26   }
27 
28   static void initialize() {
29     lastX = Platform.getWindow().getHeight() / 2.0f;
30     lastY = Platform.getWindow().getWidth() / 2.0f;
31   }
32 
33   extern (C) static void mouseCallback(GLFWwindow* window, double xPos, double yPos) nothrow {
34     try {
35       if (firstMouse) {
36         lastX = xPos;
37         lastY = yPos;
38         firstMouse = false;
39       }
40 
41       const xOffset = xPos - lastX;
42       const yOffset = lastY - yPos;
43 
44       Input
45         .getMouse()
46         .setPreviousPostion(Vector2F(lastX, lastY))
47         .setPosition(Vector2F(xPos, yPos));
48 
49       lastX = xPos;
50       lastY = yPos;
51 
52       CoreEngine.scene.camera.processMouseMovement(xOffset, yOffset);
53     } catch (Exception e) {}
54   }
55 
56   extern (C) static void joystickCallback(int joy, int event) nothrow {
57     try {
58       if (event == GLFW_CONNECTED) {
59         Input
60           .getJoystick()
61           .setConnected(true)
62           .processButtons();
63       } else if (event == GLFW_DISCONNECTED)
64         Input
65           .getJoystick()
66           .setConnected(false);
67       }
68     catch (Exception e) {}
69   }
70 
71   extern (C) static void scrollCallback(GLFWwindow* window, double xOffset, double yOffset) nothrow {
72     try {
73       CoreEngine.scene.camera.processMouseScroll(yOffset);
74     } catch (Exception e) {}
75   }
76 
77   extern (C) static void frameBufferResizeCallback(GLFWwindow* window, int width, int height) nothrow {
78     try {
79       GfxEngine.resizeFrameBufferViewport(width, height);
80     } catch (Exception e) {}
81   }
82 
83   static void updateLastMousePosition() {
84     Input
85       .getMouse()
86       .setLastPosition(Vector2F(lastX, lastY));
87   }
88 }