Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
KeyBindingScene.cpp
Go to the documentation of this file.
1
9
10namespace rtp::client {
11 namespace scenes {
12
14 // Public API
16
18 Settings& settings,
19 TranslationManager& translationManager,
20 NetworkSyncSystem& network,
21 graphics::UiFactory& uiFactory,
22 std::function<void(GameState)> changeState)
23 : _uiRegistry(UiRegistry),
24 _settings(settings),
25 _translationManager(translationManager),
26 _network(network),
27 _uiFactory(uiFactory),
28 _changeState(changeState)
29 {
30 }
31
33 {
34 log::info("Entering KeyBindingScene");
35
38 {450.0f, 50.0f},
39 _translationManager.get("keybindings.title"),
40 "assets/fonts/main.ttf",
41 60,
42 10,
43 {255, 200, 100}
44 );
45
46 float yPos = 150.0f;
47 const float buttonSpacing = 60.0f;
48
49 struct KeyBindingButton {
50 const char *labelKey;
51 KeyAction action;
52 };
53
54 std::vector<KeyBindingButton> bindings = {
55 {"keybindings.move_up", KeyAction::MoveUp },
56 {"keybindings.move_down", KeyAction::MoveDown },
57 {"keybindings.move_left", KeyAction::MoveLeft },
58 {"keybindings.move_right", KeyAction::MoveRight},
59 {"keybindings.shoot", KeyAction::Shoot },
60 {"keybindings.reload", KeyAction::Reload },
61 };
62
63 for (const auto& binding : bindings) {
66 {200.0f, yPos + 10.0f},
67 _translationManager.get(binding.labelKey),
68 "assets/fonts/main.ttf",
69 24,
70 22,
71 {255, 255, 255}
72 );
73 sf::Keyboard::Key currentKey = _settings.getKey(binding.action);
74 auto e = _uiFactory.createButton(
76 {500.0f, yPos},
77 {200.0f, 40.0f},
78 _settings.getKeyName(currentKey),
79 [this, actionToBind = binding.action]() {
80 _isWaitingForKey = true;
81 _actionToRebind = actionToBind;
82 refreshButtonLabel(actionToBind);
83 }
84 );
85 _actionToButton[binding.action] = e;
86 yPos += buttonSpacing;
87 }
88
91 {490.0f, 650.0f},
92 {300.0f, 60.0f},
93 _translationManager.get("settings.back"),
94 [this]() {
97 }
98 );
99 }
100
102 {
103 }
104
105 void KeyBindingScene::handleEvent(const sf::Event& event)
106 {
107 if (!_isWaitingForKey)
108 return;
109
110 if (const auto* kp = event.getIf<sf::Event::KeyPressed>()) {
111 if (kp->code == sf::Keyboard::Key::Escape) {
112 _isWaitingForKey = false;
113 log::info("Key binding cancelled");
115 return;
116 }
117
119 _settings.save();
120 _isWaitingForKey = false;
121
122 log::info("Key {} bound to action {}",
123 _settings.getKeyName(kp->code),
124 static_cast<int>(_actionToRebind));
125
127 }
128 }
129
131 {
132 (void)dt;
133 }
134
136 // Private API
138
140 {
141 auto it = _actionToButton.find(action);
142 if (it == _actionToButton.end()) {
143 log::warning("No button found for action {}", static_cast<int>(action));
144 return;
145 }
146
147 const ecs::Entity e = it->second;
148
149 const sf::Keyboard::Key key = _settings.getKey(action);
150 const std::string keyName = _settings.getKeyName(key);
151
152 auto buttonsRes = _uiRegistry.get<ecs::components::ui::Button>();
153 if (!buttonsRes) {
154 log::error("Failed to get Button components");
155 return;
156 }
157
158 auto& buttons = buttonsRes.value().get();
159
160 if (!buttons.has(e)) {
161 log::warning("Button component not found for entity {}", static_cast<std::uint64_t>(e));
162 return;
163 }
164
165 buttons[e].text = keyName;
166 }
167
168 } // namespace scenes
169} // namespace rtp::client
System to handle network-related operations on the client side.
Manages game settings and preferences.
Definition Settings.hpp:67
std::string getKeyName(sf::Keyboard::Key key) const
Definition Settings.cpp:185
void setKey(KeyAction action, sf::Keyboard::Key key)
Definition Settings.cpp:180
sf::Keyboard::Key getKey(KeyAction action) const
Definition Settings.cpp:171
bool save(const std::string &filename="config/settings.cfg")
Definition Settings.cpp:339
Manages game translations for all UI elements.
std::string get(const std::string &key) const
Get translated string for a key.
Factory class for creating UI components in the ECS registry.
Definition UiFactory.hpp:72
static ecs::Entity createText(ecs::Registry &registry, const position &position, const std::string &content, const std::string &fontPath, unsigned int fontSize, const std::uint8_t zIndex=0, const color &textColor={255, 255, 255})
Definition UiFactory.cpp:43
static ecs::Entity createButton(ecs::Registry &registry, const position &position, const size &size, const std::string &label, std::function< void()> onClick=nullptr)
Create a button UI component.
Definition UiFactory.cpp:17
void refreshButtonLabel(KeyAction action)
Refresh the label of a button corresponding to a key action.
std::unordered_map< KeyAction, ecs::Entity > _actionToButton
Map of actions to their corresponding button entities.
Settings & _settings
Reference to the application settings.
TranslationManager & _translationManager
Reference to the translation manager.
graphics::UiFactory & _uiFactory
UI Factory for creating UI components.
KeyAction _actionToRebind
Action currently being rebound.
bool _isWaitingForKey
Flag indicating if waiting for key input.
void handleEvent(const sf::Event &event) override
Handle an incoming event.
ecs::Registry & _uiRegistry
Reference to the ECS registry.
ChangeStateFn _changeState
Function to change the game state.
void onExit(void) override
Called when the scene is exited.
KeyBindingScene(ecs::Registry &registry, Settings &settings, TranslationManager &translationManager, NetworkSyncSystem &network, graphics::UiFactory &uiFactory, std::function< void(GameState)> changeState)
void onEnter(void) override
Called when the scene is entered.
Represents an entity in the ECS (Entity-Component-System) architecture.
Definition Entity.hpp:63
auto get(this const Self &self) -> std::expected< std::reference_wrapper< ConstLike< Self, SparseArray< T > > >, rtp::Error >
R-Type client namespace.
KeyAction
Actions that can be bound to keys.
Definition KeyAction.hpp:16
@ Reload
Action for reloading.
@ MoveRight
Action for moving right.
@ MoveDown
Action for moving down.
@ MoveUp
Action for moving up.
@ Shoot
Action for shooting.
@ MoveLeft
Action for moving left.
@ Settings
Settings menu state.
void error(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an error message.
void warning(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log a warning message.
void info(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an informational message.
Component representing a clickable button.
Definition Button.hpp:30