Skip to content

Commit b812401

Browse files
committed
- don#t use get_primary input to get navigation descriptions for game over or pause scene, use underlying input:
- make every game controller have an underlying input - all have a really one, except replay input, that uses the primary input, so that you get the description for that!
1 parent 41b0238 commit b812401

File tree

13 files changed

+59
-27
lines changed

13 files changed

+59
-27
lines changed

src/input/controller_input.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ namespace input {
7272
ControllerInput* m_underlying_input;
7373

7474

75-
protected:
76-
[[nodiscard]] const ControllerInput* underlying_input() const;
77-
7875
public:
7976
ControllerGameInput(
8077
ControllerSettings settings,
@@ -86,6 +83,8 @@ namespace input {
8683

8784
[[nodiscard]] std::string describe_menu_event(MenuEvent event) const override;
8885

86+
[[nodiscard]] const ControllerInput* underlying_input() const override;
87+
8988
protected:
9089
[[nodiscard]] helper::optional<InputEvent> sdl_event_to_input_event(const SDL_Event& event) const override;
9190
};

src/input/game_input.hpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ namespace input {
3030
enum class MenuEvent : u8 { OpenSettings, Pause };
3131

3232

33+
enum class HoldableKey : u8 {
34+
Left,
35+
Right,
36+
};
37+
38+
// forward declaration
39+
struct Input;
40+
3341
struct GameInput {
3442
public:
3543
using OnEventCallback = std::function<void(InputEvent, SimulationStep)>;
3644

3745
private:
38-
enum class HoldableKey : u8 {
39-
Left,
40-
Right,
41-
};
42-
4346
static constexpr u64 delayed_auto_shift_frames = 10;
4447
static constexpr u64 auto_repeat_rate_frames = 2;
4548

@@ -62,6 +65,15 @@ namespace input {
6265
}
6366

6467
public:
68+
GameInput(const GameInput&) = delete;
69+
GameInput& operator=(const GameInput&) = delete;
70+
71+
GameInput(GameInput&&) = default;
72+
GameInput& operator=(GameInput&&) = default;
73+
74+
virtual ~GameInput() = default;
75+
76+
6577
virtual void update(SimulationStep simulation_step_index);
6678
virtual void late_update(SimulationStep /*simulation_step*/){};
6779

@@ -86,12 +98,7 @@ namespace input {
8698
m_on_event_callback = std::move(on_event_callback);
8799
}
88100

89-
GameInput(const GameInput&) = delete;
90-
GameInput& operator=(const GameInput&) = delete;
91101

92-
GameInput(GameInput&&) = default;
93-
GameInput& operator=(GameInput&&) = default;
94-
95-
virtual ~GameInput() = default;
102+
[[nodiscard]] virtual const Input* underlying_input() const = 0;
96103
};
97104
} // namespace input

src/input/input.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,17 @@ input::InputManager::get_game_input( //NOLINT(readability-convert-member-functio
385385
return helper::unexpected<std::string>{ "No input was found, failed to get by settings and by primary input" };
386386
}
387387

388-
[[nodiscard]] const std::unique_ptr<input::Input>& input::InputManager::get_primary_input() {
388+
[[nodiscard]] const input::Input* input::InputManager::get_primary_input() {
389389

390390
for (const auto& input : m_inputs) {
391391
if (const auto pointer_input = utils::is_child_class<PrimaryInputType>(input); pointer_input.has_value()) {
392-
return input;
392+
return input.get();
393393
}
394394
}
395395

396396
// this should always be true, since in the initialization the first one, that is ALWAYS added is the KeyboardInput
397397
assert(not m_inputs.empty() && "at least one input has to be given");
398-
return m_inputs.at(0);
398+
return m_inputs.at(0).get();
399399
}
400400

401401
input::PointerInput::PointerInput(const std::string& name) : Input{ name, input::InputType::Pointer } { }

src/input/input.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ namespace input {
109109
ServiceProvider* service_provider
110110
);
111111

112-
[[nodiscard]] const std::unique_ptr<input::Input>& get_primary_input();
112+
[[nodiscard]] const input::Input* get_primary_input();
113113
};
114114

115115

src/input/input_creator.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ namespace {
7171

7272
for (u8 tetrion_index = 0; tetrion_index < static_cast<u8>(tetrion_headers.size()); ++tetrion_index) {
7373

74-
auto input = std::make_unique<ReplayGameInput>(recording_reader);
74+
const auto* primary_input = service_provider->input_manager().get_primary_input();
75+
76+
auto input = std::make_unique<ReplayGameInput>(recording_reader, primary_input);
7577

7678
const auto& header = tetrion_headers.at(tetrion_index);
7779

src/input/joystick_input.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,6 @@ namespace input {
261261
private:
262262
JoystickInput* m_underlying_input;
263263

264-
protected:
265-
[[nodiscard]] const JoystickInput* underlying_input() const;
266-
267264
public:
268265
JoystickGameInput(EventDispatcher* event_dispatcher, JoystickInput* underlying_input);
269266

@@ -275,6 +272,8 @@ namespace input {
275272
const JoystickSettings& settings
276273
);
277274

275+
[[nodiscard]] const JoystickInput* underlying_input() const override;
276+
278277
protected:
279278
template<typename T>
280279
[[nodiscard]] static helper::expected<AbstractJoystickSettings<T>, std::string>

src/input/keyboard_input.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ helper::optional<InputEvent> input::KeyboardGameInput::sdl_event_to_input_event(
149149
input::KeyboardGameInput::KeyboardGameInput(const KeyboardSettings& settings, EventDispatcher* event_dispatcher)
150150
: GameInput{ GameInputType::Keyboard },
151151
m_settings{ settings },
152-
m_event_dispatcher{ event_dispatcher } {
152+
m_event_dispatcher{ event_dispatcher },
153+
m_underlying_input{} {
153154
m_event_dispatcher->register_listener(this);
154155
}
155156

@@ -216,3 +217,7 @@ sdl::Key json_helper::get_key(const nlohmann::json& obj, const std::string& name
216217
utils::unreachable();
217218
}
218219
}
220+
221+
[[nodiscard]] const input::KeyboardInput* input::KeyboardGameInput::underlying_input() const {
222+
return &m_underlying_input;
223+
}

src/input/keyboard_input.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace input {
5959
KeyboardSettings m_settings;
6060
std::vector<SDL_Event> m_event_buffer;
6161
EventDispatcher* m_event_dispatcher;
62+
KeyboardInput m_underlying_input;
6263

6364
public:
6465
KeyboardGameInput(const KeyboardSettings& settings, EventDispatcher* event_dispatcher);
@@ -80,6 +81,8 @@ namespace input {
8081

8182
[[nodiscard]] std::string describe_menu_event(MenuEvent event) const override;
8283

84+
[[nodiscard]] const KeyboardInput* underlying_input() const override;
85+
8386
private:
8487
[[nodiscard]] helper::optional<InputEvent> sdl_event_to_input_event(const SDL_Event& event) const;
8588
};

src/input/replay_input.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
#include "helper/magic_enum_wrapper.hpp"
44
#include "helper/optional.hpp"
55

6-
input::ReplayGameInput::ReplayGameInput(std::shared_ptr<recorder::RecordingReader> recording_reader)
6+
input::ReplayGameInput::ReplayGameInput(
7+
std::shared_ptr<recorder::RecordingReader> recording_reader,
8+
const Input* underlying_input
9+
)
710
: GameInput{ GameInputType::Recording },
8-
m_recording_reader{ std::move(recording_reader) } { }
11+
m_recording_reader{ std::move(recording_reader) },
12+
m_underlying_input{ underlying_input } { }
913

1014
void input::ReplayGameInput::update(const SimulationStep simulation_step_index) {
1115
while (true) {
@@ -89,3 +93,7 @@ void input::ReplayGameInput::late_update(const SimulationStep simulation_step_in
8993
[[nodiscard]] bool input::ReplayGameInput::is_end_of_recording() const {
9094
return m_next_record_index >= m_recording_reader->num_records();
9195
}
96+
97+
[[nodiscard]] const input::Input* input::ReplayGameInput::underlying_input() const {
98+
return m_underlying_input;
99+
}

src/input/replay_input.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ namespace input {
1313
std::shared_ptr<recorder::RecordingReader> m_recording_reader;
1414
usize m_next_record_index{ 0 };
1515
usize m_next_snapshot_index{ 0 };
16+
const Input* m_underlying_input;
1617

1718
public:
18-
explicit ReplayGameInput(std::shared_ptr<recorder::RecordingReader> recording_reader);
19+
ReplayGameInput(std::shared_ptr<recorder::RecordingReader> recording_reader, const Input* underlying_input);
1920

2021
void update(SimulationStep simulation_step_index) override;
2122
void late_update(SimulationStep simulation_step_index) override;
@@ -25,6 +26,8 @@ namespace input {
2526
[[nodiscard]] std::string describe_menu_event(MenuEvent event) const override;
2627

2728
[[nodiscard]] bool is_end_of_recording() const;
29+
30+
[[nodiscard]] const Input* underlying_input() const override;
2831
};
2932

3033
} // namespace input

0 commit comments

Comments
 (0)