Skip to content

Commit

Permalink
Make debug key configurable, pass through imgui
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelOtter committed Sep 5, 2024
1 parent df28f4b commit 5321a49
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 3 deletions.
3 changes: 2 additions & 1 deletion example/test_app/src/test_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ int main(int argc, char** argv) {
#ifdef GROWL_DESKTOP
auto adapter = Growl::GameAdapter{
std::make_unique<Growl::TestAppGame>(),
Growl::Config{"Growl Test App", 1000, 1000, true}};
Growl::Config{
"Growl Test App", 1000, 1000, true, Growl::Key::FunctionF12}};
adapter.run();
#elif GROWL_IOS
NSString* appDelegateClassName;
Expand Down
3 changes: 3 additions & 0 deletions src/core/include/growl/core/config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "growl/core/input/keyboard.h"
#include <string>
namespace Growl {

Expand All @@ -9,6 +10,8 @@ struct Config {
int window_width = 1000;
int window_height = 1000;
bool window_centered = true;

Key debug_mode_key = Key::FunctionF12;
};

} // namespace Growl
143 changes: 142 additions & 1 deletion src/plugins/sdl2/src/sdl2_keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void SDL2SystemAPI::handleKeyboardEvent(SDL_Event& event) {
inputProcessor->onEvent(e);
}
if (event.type == SDL_KEYUP &&
event.key.keysym.scancode == SDL_SCANCODE_F12) {
event.key.keysym.scancode == debug_mode_key) {
api.setImguiVisible(!api.imguiVisible());
}
}
Expand Down Expand Up @@ -175,3 +175,144 @@ Key SDL2SystemAPI::getKey(SDL_KeyboardEvent& event) {
return Key::Unknown;
}
}

SDL_Scancode SDL2SystemAPI::getScancode(Key key) {
switch (key) {
// Arrows
case Key::ArrowUp:
return SDL_SCANCODE_UP;
case Key::ArrowDown:
return SDL_SCANCODE_DOWN;
case Key::ArrowLeft:
return SDL_SCANCODE_LEFT;
case Key::ArrowRight:
return SDL_SCANCODE_RIGHT;
// Numbers
case Key::Number0:
return SDL_SCANCODE_0;
case Key::Number1:
return SDL_SCANCODE_1;
case Key::Number2:
return SDL_SCANCODE_2;
case Key::Number3:
return SDL_SCANCODE_3;
case Key::Number4:
return SDL_SCANCODE_4;
case Key::Number5:
return SDL_SCANCODE_5;
case Key::Number6:
return SDL_SCANCODE_6;
case Key::Number7:
return SDL_SCANCODE_7;
case Key::Number8:
return SDL_SCANCODE_8;
case Key::Number9:
return SDL_SCANCODE_9;

// Letters
case Key::LetterA:
return SDL_SCANCODE_A;
case Key::LetterB:
return SDL_SCANCODE_B;
case Key::LetterC:
return SDL_SCANCODE_C;
case Key::LetterD:
return SDL_SCANCODE_D;
case Key::LetterE:
return SDL_SCANCODE_E;
case Key::LetterF:
return SDL_SCANCODE_F;
case Key::LetterG:
return SDL_SCANCODE_G;
case Key::LetterH:
return SDL_SCANCODE_H;
case Key::LetterI:
return SDL_SCANCODE_I;
case Key::LetterJ:
return SDL_SCANCODE_J;
case Key::LetterK:
return SDL_SCANCODE_K;
case Key::LetterL:
return SDL_SCANCODE_L;
case Key::LetterM:
return SDL_SCANCODE_M;
case Key::LetterN:
return SDL_SCANCODE_N;
case Key::LetterO:
return SDL_SCANCODE_O;
case Key::LetterP:
return SDL_SCANCODE_P;
case Key::LetterQ:
return SDL_SCANCODE_Q;
case Key::LetterR:
return SDL_SCANCODE_R;
case Key::LetterS:
return SDL_SCANCODE_S;
case Key::LetterT:
return SDL_SCANCODE_T;
case Key::LetterU:
return SDL_SCANCODE_U;
case Key::LetterV:
return SDL_SCANCODE_V;
case Key::LetterW:
return SDL_SCANCODE_W;
case Key::LetterX:
return SDL_SCANCODE_X;
case Key::LetterY:
return SDL_SCANCODE_Y;
case Key::LetterZ:
return SDL_SCANCODE_Z;

case Key::FunctionF1:
return SDL_SCANCODE_F1;
case Key::FunctionF2:
return SDL_SCANCODE_F2;
case Key::FunctionF3:
return SDL_SCANCODE_F3;
case Key::FunctionF4:
return SDL_SCANCODE_F4;
case Key::FunctionF5:
return SDL_SCANCODE_F5;
case Key::FunctionF6:
return SDL_SCANCODE_F6;
case Key::FunctionF7:
return SDL_SCANCODE_F7;
case Key::FunctionF8:
return SDL_SCANCODE_F8;
case Key::FunctionF9:
return SDL_SCANCODE_F9;
case Key::FunctionF10:
return SDL_SCANCODE_F10;
case Key::FunctionF11:
return SDL_SCANCODE_F11;
case Key::FunctionF12:
return SDL_SCANCODE_F12;
case Key::FunctionF13:
return SDL_SCANCODE_F13;
case Key::FunctionF14:
return SDL_SCANCODE_F14;
case Key::FunctionF15:
return SDL_SCANCODE_F15;
case Key::FunctionF16:
return SDL_SCANCODE_F16;
case Key::FunctionF17:
return SDL_SCANCODE_F17;
case Key::FunctionF18:
return SDL_SCANCODE_F18;
case Key::FunctionF19:
return SDL_SCANCODE_F19;
case Key::FunctionF20:
return SDL_SCANCODE_F20;
case Key::FunctionF21:
return SDL_SCANCODE_F21;
case Key::FunctionF22:
return SDL_SCANCODE_F22;
case Key::FunctionF23:
return SDL_SCANCODE_F23;
case Key::FunctionF24:
return SDL_SCANCODE_F24;

default:
return SDL_SCANCODE_UNKNOWN;
}
}
4 changes: 3 additions & 1 deletion src/plugins/sdl2/src/sdl2_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ SDL2SystemAPI::createWindow(const Config& config) {
return Error(std::make_unique<SDL2Error>(SDL_GetError()));
}

debug_mode_key = getScancode(config.debug_mode_key);

return std::unique_ptr<Window>(std::make_unique<SDL2Window>(win));
}

Expand Down Expand Up @@ -90,7 +92,7 @@ void SDL2SystemAPI::tick() {
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
if (scene_focused) {
if (scene_focused || event.key.keysym.scancode == debug_mode_key) {
handleKeyboardEvent(event);
}
break;
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/sdl2/src/sdl2_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class SDL2SystemAPI : public SystemAPIInternal {

KeyEventType getKeyEventType(SDL_KeyboardEvent& event);
Key getKey(SDL_KeyboardEvent& event);
SDL_Scancode getScancode(Key key);

ControllerEventType getControllerEventType(SDL_Event& event);
ControllerButton getButton(SDL_Event& event);
Expand All @@ -77,6 +78,7 @@ class SDL2SystemAPI : public SystemAPIInternal {
std::unique_ptr<SDL2Controller> controller;
int resize_width = 0;
int resize_height = 0;
SDL_Scancode debug_mode_key;

#ifdef GROWL_IMGUI
ImGuiIO* imgui_io;
Expand Down

0 comments on commit 5321a49

Please sign in to comment.