Browse Source

Detect when gamepads are added or removed

pull/484/head^2
Anders Jenbo 6 years ago
parent
commit
f9d4073620
  1. 3
      SourceX/DiabloUI/credits.cpp
  2. 18
      SourceX/DiabloUI/diabloui.cpp
  3. 1
      SourceX/DiabloUI/diabloui.h
  4. 3
      SourceX/DiabloUI/dialogs.cpp
  5. 3
      SourceX/DiabloUI/progress.cpp
  6. 3
      SourceX/DiabloUI/title.cpp
  7. 12
      SourceX/controls/devices/joystick.cpp
  8. 9
      SourceX/controls/menu_controls.cpp
  9. 3
      SourceX/miniwin/misc.cpp
  10. 10
      SourceX/miniwin/misc_msg.cpp

3
SourceX/DiabloUI/credits.cpp

@ -258,8 +258,6 @@ BOOL UiCreditsDialog(int a1)
case SDL_MOUSEBUTTONDOWN:
endMenu = true;
break;
case SDL_QUIT:
exit(0);
default:
switch (GetMenuAction(event)) {
case MenuAction::BACK:
@ -270,6 +268,7 @@ BOOL UiCreditsDialog(int a1)
break;
}
}
UiHandleEvents(&event);
}
} while (!endMenu && !credits_renderer.Finished());
BlackPalette();

18
SourceX/DiabloUI/diabloui.cpp

@ -15,6 +15,7 @@
#include "DiabloUI/fonts.h"
#include "DiabloUI/button.h"
#include "DiabloUI/dialogs.h"
#include "controls/controller.h"
#ifdef __SWITCH__
// for virtual keyboard on Switch
@ -187,9 +188,6 @@ void selhero_CatToName(char *in_buf, char *out_buf, int cnt)
bool UiFocusNavigation(SDL_Event *event)
{
if (event->type == SDL_QUIT)
exit(0);
switch (GetMenuAction(*event)) {
case MenuAction::SELECT:
UiFocusNavigationSelect();
@ -302,6 +300,19 @@ bool UiFocusNavigation(SDL_Event *event)
return false;
}
void UiHandleEvents(SDL_Event *event)
{
if (event->type == SDL_QUIT)
exit(0);
#ifndef USE_SDL1
if (event->type == SDL_JOYDEVICEADDED || event->type == SDL_JOYDEVICEREMOVED) {
InitController();
return;
}
#endif
}
void UiFocusNavigationSelect()
{
UiPlaySelectSound();
@ -544,6 +555,7 @@ void UiPollAndRender()
SDL_Event event;
while (SDL_PollEvent(&event)) {
UiFocusNavigation(&event);
UiHandleEvents(&event);
}
UiRenderItems(gUiItems, gUiItemCnt);
DrawMouse();

1
SourceX/DiabloUI/diabloui.h

@ -41,6 +41,7 @@ extern void (*gfnSoundFunction)(char *file);
bool IsInsideRect(const SDL_Event &event, const SDL_Rect &rect);
void UiFadeIn();
bool UiFocusNavigation(SDL_Event *event);
void UiHandleEvents(SDL_Event *event);
bool UiItemMouseEvents(SDL_Event *event, UiItem *items, std::size_t size);
int GetCenterOffset(int w, int bw = 0);
void LoadPalInMem(const PALETTEENTRY *pPal);

3
SourceX/DiabloUI/dialogs.cpp

@ -262,8 +262,6 @@ void DialogLoop(UiItem *items, std::size_t num_items, UiItem *render_behind, std
case SDL_MOUSEBUTTONUP:
UiItemMouseEvents(&event, items, num_items);
break;
case SDL_QUIT:
exit(0);
default:
switch (GetMenuAction(event)) {
case MenuAction::BACK:
@ -275,6 +273,7 @@ void DialogLoop(UiItem *items, std::size_t num_items, UiItem *render_behind, std
}
break;
}
UiHandleEvents(&event);
}
if (render_behind_size == 0) {

3
SourceX/DiabloUI/progress.cpp

@ -108,9 +108,8 @@ BOOL UiProgressDialog(HWND window, char *msg, int enable, int (*fnfunc)(), int r
case SDL_MOUSEBUTTONDOWN:
endMenu = true;
break;
case SDL_QUIT:
exit(0);
}
UiHandleEvents(&event);
}
}
BlackPalette();

3
SourceX/DiabloUI/title.cpp

@ -50,9 +50,8 @@ void UiTitleDialog()
case SDL_MOUSEBUTTONDOWN:
endMenu = true;
break;
case SDL_QUIT:
exit(0);
}
UiHandleEvents(&event);
}
}
BlackPalette();

12
SourceX/controls/devices/joystick.cpp

@ -260,8 +260,17 @@ int CurrentJoystickIndex()
void InitJoystick()
{
if (SDL_NumJoysticks() == 0)
#if HAS_KBCTRL == 1
sgbControllerActive = true;
#endif
if (SDL_NumJoysticks() == 0) {
current_joystick_index = -1;
#if HAS_KBCTRL == 0
sgbControllerActive = false;
#endif
return;
}
// Get the first available controller.
for (int i = 0; i < SDL_NumJoysticks(); ++i) {
@ -276,6 +285,7 @@ void InitJoystick()
continue;
}
current_joystick_index = i;
sgbControllerActive = true;
break;
}
}

9
SourceX/controls/menu_controls.cpp

@ -7,7 +7,9 @@ namespace dvl {
MenuAction GetMenuAction(const SDL_Event &event)
{
const ControllerButtonEvent ctrl_event = ToControllerButtonEvent(event);
sgbControllerActive = true;
if (ctrl_event.button != ControllerButton::NONE)
sgbControllerActive = true;
if (!ctrl_event.up) {
switch (ctrl_event.button) {
case ControllerButton::IGNORE:
@ -36,10 +38,11 @@ MenuAction GetMenuAction(const SDL_Event &event)
break;
}
}
if (ctrl_event.button == ControllerButton::NONE && (event.type < SDL_JOYAXISMOTION || event.type >= 0x700))
sgbControllerActive = false;
#if HAS_KBCTRL == 0
if (event.type >= SDL_KEYDOWN && event.type < SDL_JOYAXISMOTION)
sgbControllerActive = false;
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_UP:

3
SourceX/miniwin/misc.cpp

@ -128,9 +128,8 @@ bool SpawnWindow(LPCSTR lpWindowName, int nWidth, int nHeight)
#ifdef USE_SDL1
SDL_EnableUNICODE(1);
#endif
InitController();
#endif
int upscale = 1;
DvlIntSetting("upscale", &upscale);

10
SourceX/miniwin/misc_msg.cpp

@ -10,6 +10,7 @@
#include "controls/plrctrls.h"
#include "controls/touch.h"
#include "miniwin/ddraw.h"
#include "controls/controller.h"
#ifdef __SWITCH__
#include "platform/switch/docking.h"
@ -480,6 +481,15 @@ WINBOOL PeekMessageA(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilter
}
switch (e.type) {
#ifndef USE_SDL1
case SDL_CONTROLLERDEVICEADDED:
case SDL_CONTROLLERDEVICEREMOVED:
break;
case SDL_JOYDEVICEADDED:
case SDL_JOYDEVICEREMOVED:
InitController();
break;
#endif
case SDL_QUIT:
lpMsg->message = DVL_WM_QUIT;
break;

Loading…
Cancel
Save