-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
546a7a6
commit 788a695
Showing
4 changed files
with
410 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,80 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include "GLFW/glfw3.h" | ||
|
||
namespace input { | ||
|
||
enum controller_inputs { | ||
INPUT_UP = 0, | ||
INPUT_DOWN = 1, | ||
INPUT_LEFT = 2, | ||
INPUT_RIGHT = 3, | ||
CONTROLLER_BUTTON_UP = 4, | ||
CONTROLLER_BUTTON_DOWN = 5, | ||
CONTROLLER_BUTTON_LEFT = 6, | ||
CONTROLLER_BUTTON_RIGHT = 7, | ||
CONTROLLER_BUTTON_OPTION = 8, | ||
CONTROLLER_BUTTON_START = 9, | ||
CONTROLLER_BUTTON_LEFT_JOYSTICK = 10, | ||
CONTROLLER_BUTTON_RIGHT_JOYSTICK = 11, | ||
CONTROLLER_BUTTON_LEFT_BUMPER = 12, | ||
CONTROLLER_BUTTON_LB = 12, | ||
CONTROLLER_BUTTON_RIGHT_BUMPER = 13, | ||
CONTROLLER_BUTTON_RB = 13, | ||
CONTROLLER_BUTTON_DPAD_UP = 14, | ||
CONTROLLER_BUTTON_DPAD_DOWN = 15, | ||
CONTROLLER_BUTTON_DPAD_LEFT = 16, | ||
CONTROLLER_BUTTON_DPAD_RIGHT = 17, | ||
}; | ||
|
||
enum input_types { | ||
KeyboardAndMouse = 0, | ||
Controller = 1, | ||
}; | ||
|
||
class input_manager { | ||
|
||
public: | ||
/** | ||
* @brief input_manager constructor. | ||
* @param window The window reference to setup the input_manager. | ||
*/ | ||
input_manager(GLFWwindow* window); | ||
|
||
~input_manager(); | ||
|
||
void input_loop(); | ||
|
||
/** | ||
* @brief Keyboard press event | ||
* | ||
* @param key The key that was pressed (look at `GLFW_KEY_`). | ||
* | ||
*/ | ||
std::function<void(int key)> on_keyboard_press{}; | ||
|
||
/** | ||
* @brief Controller input event | ||
* | ||
* @param input The input of the controller as `controller_inputs`. | ||
*/ | ||
std::function<void(controller_inputs input)> on_controller_input{}; | ||
|
||
/** | ||
* @brief Controller button press event | ||
* | ||
* @param key The controller button as `controller_inputs`. | ||
*/ | ||
std::function<void(controller_inputs key)> on_controller_button_press{}; | ||
|
||
std::vector<int> keys_held{}; | ||
|
||
input_types current_input_type{}; | ||
|
||
private: | ||
|
||
float deadzone = 0.2; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,132 @@ | ||
#include <iostream> | ||
#include "utilities/input_manager.h" | ||
|
||
input::input_manager::input_manager(GLFWwindow* window) { | ||
if(!window) { | ||
return; | ||
} | ||
|
||
std::cout << "Setting up input manager..." << "\n"; | ||
|
||
static input_manager *mgr = this; | ||
|
||
glfwSetKeyCallback(window, [](GLFWwindow *window, int key, int scancode, int action, int mods) { | ||
mgr->current_input_type = input_types::KeyboardAndMouse; | ||
if(action == GLFW_PRESS) { | ||
mgr->keys_held.emplace_back(key); | ||
mgr->on_keyboard_press(key); | ||
} | ||
}); | ||
|
||
std::cout << "Input manager has setup successfully!" << "\n"; | ||
} | ||
|
||
input::input_manager::~input_manager() { | ||
std::cout << "Input manager killed." << "\n"; | ||
} | ||
|
||
void input::input_manager::input_loop() { | ||
if(glfwJoystickPresent(GLFW_JOYSTICK_1) == GLFW_TRUE) { | ||
int count; | ||
const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &count); | ||
|
||
const float x_axis = axes[0]; | ||
const float y_axis = axes[1]; | ||
|
||
if(on_controller_input) { | ||
if(abs(x_axis) >= deadzone) { | ||
if(x_axis < 0) { | ||
on_controller_input(controller_inputs::INPUT_LEFT); | ||
} else { | ||
on_controller_input(controller_inputs::INPUT_RIGHT); | ||
} | ||
|
||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(abs(y_axis) >= deadzone) { | ||
if(y_axis < 0) { | ||
on_controller_input(controller_inputs::INPUT_UP); | ||
} else { | ||
on_controller_input(controller_inputs::INPUT_DOWN); | ||
} | ||
|
||
current_input_type = input_types::Controller; | ||
} | ||
} | ||
|
||
const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &count); | ||
|
||
if(on_controller_button_press) { | ||
if(buttons[0] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_DOWN); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[1] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_RIGHT); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[2] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_LEFT); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[3] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_UP); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[4] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_LEFT_BUMPER); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[5] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_RIGHT_BUMPER); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[6] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_OPTION); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[7] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_START); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[8] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_LEFT_JOYSTICK); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[9] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_RIGHT_JOYSTICK); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[10] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_DPAD_UP); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[11] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_DPAD_RIGHT); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[12] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_DPAD_DOWN); | ||
current_input_type = input_types::Controller; | ||
} | ||
|
||
if(buttons[13] == GLFW_PRESS) { | ||
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_DPAD_LEFT); | ||
current_input_type = input_types::Controller; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.