diff options
author | StapleButter <thetotalworm@gmail.com> | 2017-09-30 19:27:47 +0200 |
---|---|---|
committer | StapleButter <thetotalworm@gmail.com> | 2017-09-30 19:27:47 +0200 |
commit | 9f486de76a061326f06e51e36424a6be2574ee8d (patch) | |
tree | 4e6ba66d7aa058765ef1095e730a4942e92a580b /src/libui_sdl/DlgInputConfig.cpp | |
parent | acc23f47cc54e1b0fee67b2c3b56e10ddf47e486 (diff) |
allow entering joystick config
Diffstat (limited to 'src/libui_sdl/DlgInputConfig.cpp')
-rw-r--r-- | src/libui_sdl/DlgInputConfig.cpp | 116 |
1 files changed, 114 insertions, 2 deletions
diff --git a/src/libui_sdl/DlgInputConfig.cpp b/src/libui_sdl/DlgInputConfig.cpp index 910486b..a60a0f9 100644 --- a/src/libui_sdl/DlgInputConfig.cpp +++ b/src/libui_sdl/DlgInputConfig.cpp @@ -20,6 +20,7 @@ #include <stdio.h> #include <string.h> +#include <SDL2/SDL.h> #include "libui/ui.h" #include "../types.h" @@ -28,6 +29,9 @@ #include "DlgInputConfig.h" +extern SDL_Joystick* Joystick; + + namespace DlgInputConfig { @@ -89,7 +93,7 @@ void OnAreaDragBroken(uiAreaHandler* handler, uiArea* area) int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt) { - if (pollid < 0 || pollid > 12) + if (pollid < 0) return 0; if (evt->Scancode == 0x38) // ALT @@ -97,6 +101,25 @@ int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt) if (evt->Modifiers == 0x2) // ALT+key return 0; + if (pollid > 12) + { + if (pollid < 0x100) return 0; + int id = pollid & 0xFF; + if (id > 12) return 0; + if (evt->Scancode != 0x1) return 0; // ESC + + char keyname[16]; + JoyMappingName(joymap[id], keyname); + uiButtonSetText(pollbtn, keyname); + uiControlEnable(uiControl(pollbtn)); + + pollid = -1; + + uiControlSetFocus(uiControl(pollbtn)); + + return 1; + } + if (!evt->Up) { // set key. @@ -116,13 +139,68 @@ int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt) return 1; } +Uint32 JoyPoll(Uint32 interval, void* param) +{ + if (pollid < 0x100) return 0; + int id = pollid & 0xFF; + if (id > 12) return 0; + + SDL_JoystickUpdate(); + + SDL_Joystick* joy = Joystick; + if (!joy) return 0; + + int nbuttons = SDL_JoystickNumButtons(joy); + for (int i = 0; i < nbuttons; i++) + { + if (SDL_JoystickGetButton(joy, i)) + { + joymap[id] = i; + + char keyname[16]; + JoyMappingName(joymap[id], keyname); + uiButtonSetText(pollbtn, keyname); + uiControlEnable(uiControl(pollbtn)); + + pollid = -1; + + uiControlSetFocus(uiControl(pollbtn)); + return 0; + } + } + + u8 blackhat = SDL_JoystickGetHat(joy, 0); + if (blackhat) + { + if (blackhat & 0x1) blackhat = 0x1; + else if (blackhat & 0x2) blackhat = 0x2; + else if (blackhat & 0x4) blackhat = 0x4; + else blackhat = 0x8; + + joymap[id] = 0x100 | blackhat; + + char keyname[16]; + JoyMappingName(joymap[id], keyname); + uiButtonSetText(pollbtn, keyname); + uiControlEnable(uiControl(pollbtn)); + + pollid = -1; + + uiControlSetFocus(uiControl(pollbtn)); + return 0; + } + + return 100; +} + void OnKeyStartConfig(uiButton* btn, void* data) { if (pollid != -1) { // TODO: handle this better? - uiControlSetFocus(uiControl(keypresscatcher)); + if (pollid <= 12) + uiControlSetFocus(uiControl(keypresscatcher)); return; } @@ -136,12 +214,43 @@ void OnKeyStartConfig(uiButton* btn, void* data) uiControlSetFocus(uiControl(keypresscatcher)); } +void OnJoyStartConfig(uiButton* btn, void* data) +{ + if (pollid != -1) + { + // TODO: handle this better? + if (pollid <= 12) + uiControlSetFocus(uiControl(keypresscatcher)); + return; + } + + int id = *(int*)data; + pollid = id | 0x100; + pollbtn = btn; + + uiButtonSetText(btn, "[press button]"); + uiControlDisable(uiControl(btn)); + + SDL_AddTimer(100, JoyPoll, NULL); + uiControlSetFocus(uiControl(keypresscatcher)); +} + int OnCloseWindow(uiWindow* window, void* blarg) { return 1; } +void OnGetFocus(uiWindow* window, void* blarg) +{ + if (pollid >= 0) + uiControlSetFocus(uiControl(keypresscatcher)); +} + +void OnLoseFocus(uiWindow* window, void* blarg) +{ +} + void OnCancel(uiButton* btn, void* blarg) { uiControlDestroy(uiControl(win)); @@ -167,6 +276,8 @@ void Open() win = uiNewWindow("Input config - melonDS", 600, 400, 0); uiWindowSetMargined(win, 1); uiWindowOnClosing(win, OnCloseWindow, NULL); + uiWindowOnGetFocus(win, OnGetFocus, NULL); + uiWindowOnLoseFocus(win, OnLoseFocus, NULL); areahandler.Draw = OnAreaDraw; areahandler.MouseEvent = OnAreaMouseEvent; @@ -227,6 +338,7 @@ void Open() uiButton* btn = uiNewButton(keyname); uiBoxAppend(box, uiControl(btn), 1); + uiButtonOnClicked(btn, OnJoyStartConfig, &keyorder[i]); } } |