mirror of
https://github.com/melonDS-emu/melonDS.git
synced 2025-07-21 21:31:00 -06:00
allow entering joystick config
This commit is contained in:
@ -20,6 +20,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
#include "libui/ui.h"
|
#include "libui/ui.h"
|
||||||
|
|
||||||
#include "../types.h"
|
#include "../types.h"
|
||||||
@ -28,6 +29,9 @@
|
|||||||
#include "DlgInputConfig.h"
|
#include "DlgInputConfig.h"
|
||||||
|
|
||||||
|
|
||||||
|
extern SDL_Joystick* Joystick;
|
||||||
|
|
||||||
|
|
||||||
namespace DlgInputConfig
|
namespace DlgInputConfig
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -89,7 +93,7 @@ void OnAreaDragBroken(uiAreaHandler* handler, uiArea* area)
|
|||||||
|
|
||||||
int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt)
|
int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt)
|
||||||
{
|
{
|
||||||
if (pollid < 0 || pollid > 12)
|
if (pollid < 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (evt->Scancode == 0x38) // ALT
|
if (evt->Scancode == 0x38) // ALT
|
||||||
@ -97,6 +101,25 @@ int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt)
|
|||||||
if (evt->Modifiers == 0x2) // ALT+key
|
if (evt->Modifiers == 0x2) // ALT+key
|
||||||
return 0;
|
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)
|
if (!evt->Up)
|
||||||
{
|
{
|
||||||
// set key.
|
// set key.
|
||||||
@ -116,13 +139,68 @@ int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt)
|
|||||||
return 1;
|
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)
|
void OnKeyStartConfig(uiButton* btn, void* data)
|
||||||
{
|
{
|
||||||
if (pollid != -1)
|
if (pollid != -1)
|
||||||
{
|
{
|
||||||
// TODO: handle this better?
|
// TODO: handle this better?
|
||||||
uiControlSetFocus(uiControl(keypresscatcher));
|
if (pollid <= 12)
|
||||||
|
uiControlSetFocus(uiControl(keypresscatcher));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,12 +214,43 @@ void OnKeyStartConfig(uiButton* btn, void* data)
|
|||||||
uiControlSetFocus(uiControl(keypresscatcher));
|
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)
|
int OnCloseWindow(uiWindow* window, void* blarg)
|
||||||
{
|
{
|
||||||
return 1;
|
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)
|
void OnCancel(uiButton* btn, void* blarg)
|
||||||
{
|
{
|
||||||
uiControlDestroy(uiControl(win));
|
uiControlDestroy(uiControl(win));
|
||||||
@ -167,6 +276,8 @@ void Open()
|
|||||||
win = uiNewWindow("Input config - melonDS", 600, 400, 0);
|
win = uiNewWindow("Input config - melonDS", 600, 400, 0);
|
||||||
uiWindowSetMargined(win, 1);
|
uiWindowSetMargined(win, 1);
|
||||||
uiWindowOnClosing(win, OnCloseWindow, NULL);
|
uiWindowOnClosing(win, OnCloseWindow, NULL);
|
||||||
|
uiWindowOnGetFocus(win, OnGetFocus, NULL);
|
||||||
|
uiWindowOnLoseFocus(win, OnLoseFocus, NULL);
|
||||||
|
|
||||||
areahandler.Draw = OnAreaDraw;
|
areahandler.Draw = OnAreaDraw;
|
||||||
areahandler.MouseEvent = OnAreaMouseEvent;
|
areahandler.MouseEvent = OnAreaMouseEvent;
|
||||||
@ -227,6 +338,7 @@ void Open()
|
|||||||
|
|
||||||
uiButton* btn = uiNewButton(keyname);
|
uiButton* btn = uiNewButton(keyname);
|
||||||
uiBoxAppend(box, uiControl(btn), 1);
|
uiBoxAppend(box, uiControl(btn), 1);
|
||||||
|
uiButtonOnClicked(btn, OnJoyStartConfig, &keyorder[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,6 +57,8 @@ u32 ScreenBuffer[256*384];
|
|||||||
|
|
||||||
bool Touching = false;
|
bool Touching = false;
|
||||||
|
|
||||||
|
SDL_Joystick* Joystick;
|
||||||
|
|
||||||
|
|
||||||
void AudioCallback(void* data, Uint8* stream, int len)
|
void AudioCallback(void* data, Uint8* stream, int len)
|
||||||
{
|
{
|
||||||
@ -93,6 +95,12 @@ int EmuThreadFunc(void* burp)
|
|||||||
SDL_PauseAudioDevice(audio, 0);
|
SDL_PauseAudioDevice(audio, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: support more joysticks
|
||||||
|
if (SDL_NumJoysticks() > 0)
|
||||||
|
Joystick = SDL_JoystickOpen(0);
|
||||||
|
else
|
||||||
|
Joystick = NULL;
|
||||||
|
|
||||||
u32 nframes = 0;
|
u32 nframes = 0;
|
||||||
u32 starttick = SDL_GetTicks();
|
u32 starttick = SDL_GetTicks();
|
||||||
u32 lasttick = starttick;
|
u32 lasttick = starttick;
|
||||||
@ -172,6 +180,8 @@ int EmuThreadFunc(void* burp)
|
|||||||
|
|
||||||
EmuStatus = 0;
|
EmuStatus = 0;
|
||||||
|
|
||||||
|
if (Joystick) SDL_JoystickClose(Joystick);
|
||||||
|
|
||||||
if (audio) SDL_CloseAudioDevice(audio);
|
if (audio) SDL_CloseAudioDevice(audio);
|
||||||
|
|
||||||
NDS::DeInit();
|
NDS::DeInit();
|
||||||
@ -464,6 +474,8 @@ int main(int argc, char** argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SDL_JoystickEventState(SDL_ENABLE);
|
||||||
|
|
||||||
uiInitOptions ui_opt;
|
uiInitOptions ui_opt;
|
||||||
memset(&ui_opt, 0, sizeof(uiInitOptions));
|
memset(&ui_opt, 0, sizeof(uiInitOptions));
|
||||||
const char* ui_err = uiInit(&ui_opt);
|
const char* ui_err = uiInit(&ui_opt);
|
||||||
|
Reference in New Issue
Block a user