Skip to content

Commit

Permalink
feat: buttons now show as selected when controller is focused on them
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaskowicz1 committed May 13, 2024
1 parent 788a695 commit 1d14d68
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
4 changes: 2 additions & 2 deletions include/engine/utilities/input_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ enum controller_inputs {
};

enum input_types {
KeyboardAndMouse = 0,
Controller = 1,
KEYBOARDANDMOUSE = 0,
CONTROLLER = 1,
};

class input_manager {
Expand Down
40 changes: 22 additions & 18 deletions src/engine/utilities/input_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ input::input_manager::input_manager(GLFWwindow* window) {

static input_manager *mgr = this;

glfwSetKeyCallback(window, [](GLFWwindow *window, int key, int scancode, int action, int mods) {
mgr->current_input_type = input_types::KeyboardAndMouse;
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);
}
});

glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos) {
mgr->current_input_type = input_types::KEYBOARDANDMOUSE;
});

std::cout << "Input manager has setup successfully!" << "\n";
}

Expand All @@ -41,7 +45,7 @@ void input::input_manager::input_loop() {
on_controller_input(controller_inputs::INPUT_RIGHT);
}

current_input_type = input_types::Controller;
current_input_type = input_types::CONTROLLER;
}

if(abs(y_axis) >= deadzone) {
Expand All @@ -51,7 +55,7 @@ void input::input_manager::input_loop() {
on_controller_input(controller_inputs::INPUT_DOWN);
}

current_input_type = input_types::Controller;
current_input_type = input_types::CONTROLLER;
}
}

Expand All @@ -60,72 +64,72 @@ void input::input_manager::input_loop() {
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
current_input_type = input_types::CONTROLLER;
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/engine/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ void window::window_loop() {
selected_option = opt.first;
}

if(input_man->current_input_type == input::CONTROLLER) {
if(selected_option == opt.first) {
ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetColorU32(ImGuiCol_ButtonHovered));
}
}

std::string option_text(opt.second.name);
if (ImGui::Button(option_text.c_str(), ImVec2(300, 30))) {
first_frame = true;
Expand All @@ -418,7 +424,13 @@ void window::window_loop() {
break;
}

if(input_man->current_input_type == input::Controller) {
if(input_man->current_input_type == input::CONTROLLER) {
if (selected_option == opt.first) {
ImGui::PopStyleColor();
}
}

if(input_man->current_input_type == input::CONTROLLER) {
if(selected_option == opt.first) {
ImGui::SetKeyboardFocusHere();
}
Expand Down

0 comments on commit 1d14d68

Please sign in to comment.