Browse Source

Add cursor hiding functions

master
C. J. Howard 1 year ago
parent
commit
b7c7284a62
7 changed files with 98 additions and 2 deletions
  1. +10
    -0
      src/app/input-manager.hpp
  2. +17
    -1
      src/app/sdl/sdl-input-manager.cpp
  3. +2
    -0
      src/app/sdl/sdl-input-manager.hpp
  4. +6
    -0
      src/game/context.hpp
  5. +1
    -1
      src/game/controls.cpp
  6. +59
    -0
      src/game/state/boot.cpp
  7. +3
    -0
      src/game/state/main-menu.cpp

+ 10
- 0
src/app/input-manager.hpp View File

@ -50,6 +50,16 @@ public:
*/ */
virtual void update() = 0; virtual void update() = 0;
/**
* Makes the cursor visible.
*/
virtual void show_cursor() = 0;
/**
* Makes the cursor invisible.
*/
virtual void hide_cursor() = 0;
/** /**
* Returns the event queue associated with registered input devices. * Returns the event queue associated with registered input devices.
*/ */

+ 17
- 1
src/app/sdl/sdl-input-manager.cpp View File

@ -298,7 +298,7 @@ void sdl_input_manager::update()
debug::log::info("Disconnected gamepad \"{}\"", controller_name); debug::log::info("Disconnected gamepad \"{}\"", controller_name);
} }
break; break;
} }
@ -311,4 +311,20 @@ void sdl_input_manager::update()
this->event_queue.flush(); this->event_queue.flush();
} }
void sdl_input_manager::show_cursor()
{
if (SDL_ShowCursor(SDL_ENABLE) < 0)
{
debug::log::error("Failed to show cursor: \"{}\"", SDL_GetError());
}
}
void sdl_input_manager::hide_cursor()
{
if (SDL_ShowCursor(SDL_DISABLE) < 0)
{
debug::log::error("Failed to hide cursor: \"{}\"", SDL_GetError());
}
}
} // namespace app } // namespace app

+ 2
- 0
src/app/sdl/sdl-input-manager.hpp View File

@ -43,6 +43,8 @@ public:
virtual ~sdl_input_manager(); virtual ~sdl_input_manager();
virtual void update(); virtual void update();
virtual void show_cursor();
virtual void hide_cursor();
private: private:
input::keyboard keyboard; input::keyboard keyboard;

+ 6
- 0
src/game/context.hpp View File

@ -154,6 +154,12 @@ struct context
// Input management and input event handling // Input management and input event handling
app::input_manager* input_manager; app::input_manager* input_manager;
std::shared_ptr<::event::subscription> application_quit_subscription; std::shared_ptr<::event::subscription> application_quit_subscription;
std::shared_ptr<::event::subscription> gamepad_axis_moved_subscription;
std::shared_ptr<::event::subscription> gamepad_button_pressed_subscription;
std::shared_ptr<::event::subscription> mouse_button_pressed_subscription;
std::shared_ptr<::event::subscription> mouse_moved_subscription;
std::shared_ptr<::event::subscription> mouse_scrolled_subscription;
bool gamepad_active;
// Localization and internationalization // Localization and internationalization
std::uint16_t language_index; std::uint16_t language_index;

+ 1
- 1
src/game/controls.cpp View File

@ -181,7 +181,7 @@ void setup_menu_controls(game::context& ctx)
// Set activation threshold for menu navigation controls to mitigate drifting gamepad axes // Set activation threshold for menu navigation controls to mitigate drifting gamepad axes
auto menu_control_threshold = [](float x) -> bool auto menu_control_threshold = [](float x) -> bool
{ {
return x > 0.1f;
return x > 0.5f;
}; };
ctx.menu_up_control.set_threshold_function(menu_control_threshold); ctx.menu_up_control.set_threshold_function(menu_control_threshold);
ctx.menu_down_control.set_threshold_function(menu_control_threshold); ctx.menu_down_control.set_threshold_function(menu_control_threshold);

+ 59
- 0
src/game/state/boot.cpp View File

@ -468,6 +468,65 @@ void boot::setup_input()
ctx.closed = true; ctx.closed = true;
} }
); );
// Gamepad deactivation function
auto deactivate_gamepad = [&ctx = this->ctx](const auto& event)
{
if (ctx.gamepad_active)
{
ctx.gamepad_active = false;
ctx.input_manager->show_cursor();
}
};
// Setup gamepad activation callbacks
ctx.gamepad_axis_moved_subscription = ctx.input_manager->get_event_queue().subscribe<input::gamepad_axis_moved_event>
(
[&](const auto& event)
{
if (!ctx.gamepad_active && std::abs(event.position) > 0.1f)
{
ctx.gamepad_active = true;
ctx.input_manager->hide_cursor();
}
}
);
ctx.gamepad_button_pressed_subscription = ctx.input_manager->get_event_queue().subscribe<input::gamepad_button_pressed_event>
(
[&ctx = this->ctx](const auto& event)
{
if (!ctx.gamepad_active)
{
ctx.gamepad_active = true;
ctx.input_manager->hide_cursor();
}
}
);
// Setup gamepad deactivation callbacks
ctx.mouse_button_pressed_subscription = ctx.input_manager->get_event_queue().subscribe<input::mouse_button_pressed_event>
(
deactivate_gamepad
);
ctx.mouse_moved_subscription = ctx.input_manager->get_event_queue().subscribe<input::mouse_moved_event>
(
deactivate_gamepad
);
ctx.mouse_scrolled_subscription = ctx.input_manager->get_event_queue().subscribe<input::mouse_scrolled_event>
(
deactivate_gamepad
);
// Activate gamepad if one is connected
if (!ctx.input_manager->get_gamepads().empty())
{
ctx.gamepad_active = true;
ctx.input_manager->hide_cursor();
}
else
{
ctx.gamepad_active = false;
}
} }
void boot::load_strings() void boot::load_strings()

+ 3
- 0
src/game/state/main-menu.cpp View File

@ -214,6 +214,9 @@ main_menu::main_menu(game::context& ctx, bool fade_in):
// Fade to black then quit // Fade to black then quit
ctx.fade_transition->transition(config::quit_fade_out_duration, false, ease<float>::out_cubic, false, [&ctx](){ctx.closed=true;}); ctx.fade_transition->transition(config::quit_fade_out_duration, false, ease<float>::out_cubic, false, [&ctx](){ctx.closed=true;});
// Quit immediately
//ctx.function_queue.push([&ctx](){ctx.closed=true;});
}; };
// Build list of menu select callbacks // Build list of menu select callbacks

Loading…
Cancel
Save