yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
input_handler.cc
Go to the documentation of this file.
2
5#include "imgui/imgui.h"
6
7namespace yaze {
8namespace emu {
9namespace ui {
10
12 input::InputManager* manager,
13 const std::function<void(const input::InputConfig&)>& on_config_changed) {
14 if (!manager || !manager->backend())
15 return;
16
17 auto config = manager->GetConfig();
18
19 ImGui::TextColored(ImVec4(1.0f, 0.8f, 0.2f, 1.0f),
20 ICON_MD_INFO " Keyboard Configuration");
21 ImGui::Separator();
22
23 ImGui::Text("Backend: %s", manager->backend()->GetBackendName().c_str());
24 ImGui::Separator();
25
26 ImGui::TextWrapped(
27 "Configure keyboard bindings for SNES controller emulation. "
28 "Click a button and press a key to rebind.");
29 ImGui::Spacing();
30
31 auto RenderKeyBind = [&](const char* label, int* key) {
32 ImGui::Text("%s:", label);
33 ImGui::SameLine(150);
34
35 // Show current key
36 const char* key_name = SDL_GetKeyName(*key);
37 ImGui::PushID(label);
38 if (ImGui::Button(key_name, ImVec2(120, 0))) {
39 ImGui::OpenPopup("Rebind");
40 }
41
42 if (ImGui::BeginPopup("Rebind")) {
43 ImGui::Text("Press any key...");
44 ImGui::Separator();
45
46 // Poll for key press (cross-version compatible)
47 SDL_Event event;
48 if (SDL_PollEvent(&event) &&
49 event.type == platform::kEventKeyDown) {
50 SDL_Keycode keycode = platform::GetKeyFromEvent(event);
51 if (keycode != SDLK_UNKNOWN && keycode != SDLK_ESCAPE) {
52 *key = keycode;
53 ImGui::CloseCurrentPopup();
54 }
55 }
56
57 if (ImGui::Button("Cancel", ImVec2(-1, 0))) {
58 ImGui::CloseCurrentPopup();
59 }
60
61 ImGui::EndPopup();
62 }
63 ImGui::PopID();
64 };
65
66 // Face Buttons
67 if (ImGui::CollapsingHeader("Face Buttons", ImGuiTreeNodeFlags_DefaultOpen)) {
68 RenderKeyBind("A Button", &config.key_a);
69 RenderKeyBind("B Button", &config.key_b);
70 RenderKeyBind("X Button", &config.key_x);
71 RenderKeyBind("Y Button", &config.key_y);
72 }
73
74 // D-Pad
75 if (ImGui::CollapsingHeader("D-Pad", ImGuiTreeNodeFlags_DefaultOpen)) {
76 RenderKeyBind("Up", &config.key_up);
77 RenderKeyBind("Down", &config.key_down);
78 RenderKeyBind("Left", &config.key_left);
79 RenderKeyBind("Right", &config.key_right);
80 }
81
82 // Shoulder Buttons
83 if (ImGui::CollapsingHeader("Shoulder Buttons")) {
84 RenderKeyBind("L Button", &config.key_l);
85 RenderKeyBind("R Button", &config.key_r);
86 }
87
88 // Start/Select
89 if (ImGui::CollapsingHeader("Start/Select")) {
90 RenderKeyBind("Start", &config.key_start);
91 RenderKeyBind("Select", &config.key_select);
92 }
93
94 ImGui::Spacing();
95 ImGui::Separator();
96
97 // Input mode
98 ImGui::Checkbox("Continuous Polling", &config.continuous_polling);
99 if (ImGui::IsItemHovered()) {
100 ImGui::SetTooltip(
101 "Recommended: ON for games (detects held buttons)\nOFF for event-based "
102 "input");
103 }
104
105 // Input capture behavior
106 if (ImGui::Checkbox("Allow input while typing in UI",
107 &config.ignore_imgui_text_input)) {
108 // nothing else here; tooltip below
109 }
110 if (ImGui::IsItemHovered()) {
111 ImGui::SetTooltip(
112 "When enabled, game input is not blocked by ImGui text fields.\n"
113 "Useful if emulator input is blocked while typing in other panels.");
114 }
115
116 ImGui::Spacing();
117
118 // Apply button
119 if (ImGui::Button("Apply Changes", ImVec2(-1, 30))) {
120 manager->SetConfig(config);
121 config = manager->GetConfig();
122 if (on_config_changed) {
123 on_config_changed(config);
124 }
125 }
126
127 ImGui::Spacing();
128
129 // Defaults
130 if (ImGui::Button("Reset to Defaults", ImVec2(-1, 30))) {
131 config = input::InputConfig(); // Reset to defaults
133 manager->SetConfig(config);
134 config = manager->GetConfig();
135 if (on_config_changed) {
136 on_config_changed(config);
137 }
138 }
139
140 ImGui::Spacing();
141 ImGui::Separator();
142 ImGui::TextColored(ImVec4(0.5f, 0.5f, 0.5f, 1.0f), "Default Bindings:");
143 ImGui::BulletText("A/B/X/Y: X/Z/S/A");
144 ImGui::BulletText("L/R: D/C");
145 ImGui::BulletText("D-Pad: Arrow Keys");
146 ImGui::BulletText("Start/Select: Enter/RShift");
147}
148
149} // namespace ui
150} // namespace emu
151} // namespace yaze
virtual std::string GetBackendName() const =0
Get backend name for debugging.
void SetConfig(const InputConfig &config)
InputConfig GetConfig() const
#define ICON_MD_INFO
Definition icons.h:993
void ApplyDefaultKeyBindings(InputConfig &config)
Apply default keyboard bindings if unset.
void RenderKeyboardConfig(input::InputManager *manager, const std::function< void(const input::InputConfig &)> &on_config_changed)
Render keyboard configuration UI.
constexpr auto kEventKeyDown
Definition sdl_compat.h:53
SDL_Keycode GetKeyFromEvent(const SDL_Event &event)
Get keyboard state from SDL event.
Definition sdl_compat.h:115
SDL2/SDL3 compatibility layer.
Input configuration (platform-agnostic key codes)