Skip to content

Autohide touch controls #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/pc/controller/controller_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "controller_api.h"
#if defined(__ANDROID__)
int curInputSource=touchScreen;
#else
int curInputSource=keyboard;
#endif


void set_current_input(int in){
curInputSource=in;
}

int get_current_input(){
return curInputSource;
}
12 changes: 12 additions & 0 deletions src/pc/controller/controller_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@ struct ControllerAPI {
void (*read)(OSContPad *pad);
};

enum Input{
keyboard,
emscriptenKeyboard,
sdlGameController,
touchScreen,
wup,
xinput
};

extern void set_current_input(int in);
extern int get_current_input();

#endif
58 changes: 47 additions & 11 deletions src/pc/controller/controller_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
static bool init_ok;
static SDL_GameController *sdl_cntrl;

static int16_t leftx;
static int16_t lefty;
static int16_t rightx;
static int16_t righty;
static int16_t ltrig;
static int16_t rtrig;

static void controller_sdl_init(void) {
if (SDL_Init(SDL_INIT_GAMECONTROLLER) != 0) {
fprintf(stderr, "SDL init error: %s\n", SDL_GetError());
Expand Down Expand Up @@ -49,20 +56,49 @@ static void controller_sdl_read(OSContPad *pad) {
return;
}
}
bool inputChanged = false;

if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_START)){
pad->button |= START_BUTTON;
inputChanged = true;
}
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_LEFTSHOULDER)){
pad->button |= Z_TRIG;
inputChanged = true;
}
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)){
pad->button |= R_TRIG;
inputChanged = true;
}
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_A)){
pad->button |= A_BUTTON;
inputChanged = true;
}
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_X)){
pad->button |= B_BUTTON;
inputChanged = true;
}

int16_t previousLeftX = leftx;
int16_t previousLeftY = lefty;
int16_t previousRightX = rightx;
int16_t previousRightY = righty;
int16_t previousLTrig = ltrig;
int16_t previousRTrig = rtrig;

leftx = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTX);
lefty = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTY);
rightx = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_RIGHTX);
righty = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_RIGHTY);

if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_START)) pad->button |= START_BUTTON;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) pad->button |= Z_TRIG;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) pad->button |= R_TRIG;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_A)) pad->button |= A_BUTTON;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_X)) pad->button |= B_BUTTON;
ltrig = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
rtrig = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_TRIGGERRIGHT);

int16_t leftx = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTX);
int16_t lefty = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTY);
int16_t rightx = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_RIGHTX);
int16_t righty = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_RIGHTY);
if(leftx!=previousLeftX||lefty!=previousLeftY||rightx!=previousRightX||righty!=previousRightY||ltrig!=previousLTrig||rtrig!=previousRTrig)
inputChanged = true;

int16_t ltrig = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
int16_t rtrig = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
if(inputChanged)
set_current_input(sdlGameController);

#ifdef TARGET_WEB
// Firefox has a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1606562
Expand Down
5 changes: 5 additions & 0 deletions src/pc/controller/controller_touchscreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ static int ControlElementsLength = sizeof(ControlElementsDefault)/sizeof(struct
((pos.y + size / 2 > CORRECT_TOUCH_Y(event->y)) && (pos.y - size / 2 < CORRECT_TOUCH_Y(event->y))))

void touch_down(struct TouchEvent* event) {
set_current_input(touchScreen);

struct Position pos;
for(int i = 0; i < ControlElementsLength; i++) {
if (ControlElements[i].touchID == 0) {
Expand Down Expand Up @@ -182,6 +184,9 @@ static void DrawSprite(s32 x, s32 y, int scaling) {
}

void render_touch_controls(void) {
if(get_current_input()!=touchScreen)
return;

Mtx *mtx;

mtx = alloc_display_list(sizeof(*mtx));
Expand Down