123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227 |
- /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
- //
- // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
- // Copyright (c) 2011-2017, SDLPAL development team.
- // All rights reserved.
- //
- // This file is part of SDLPAL.
- //
- // SDLPAL is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- #include "main.h"
- #include <math.h>
- volatile PALINPUTSTATE g_InputState;
- #if PAL_HAS_JOYSTICKS
- static SDL_Joystick *g_pJoy = NULL;
- #endif
- #if !SDL_VERSION_ATLEAST(2,0,0)
- # define SDLK_KP_1 SDLK_KP1
- # define SDLK_KP_2 SDLK_KP2
- # define SDLK_KP_3 SDLK_KP3
- # define SDLK_KP_4 SDLK_KP4
- # define SDLK_KP_5 SDLK_KP5
- # define SDLK_KP_6 SDLK_KP6
- # define SDLK_KP_7 SDLK_KP7
- # define SDLK_KP_8 SDLK_KP8
- # define SDLK_KP_9 SDLK_KP9
- # define SDLK_KP_0 SDLK_KP0
- # define SDL_JoystickNameForIndex SDL_JoystickName
- # define SDL_GetKeyboardState SDL_GetKeyState
- # define SDL_GetScancodeFromKey(x) (x)
- #endif
- BOOL g_fUseJoystick = TRUE;
- static void _default_init_filter() {}
- static int _default_input_event_filter(const SDL_Event *event, volatile PALINPUTSTATE *state) { return 0; }
- static void _default_input_shutdown_filter() {}
- static void (*input_init_filter)() = _default_init_filter;
- static int (*input_event_filter)(const SDL_Event *, volatile PALINPUTSTATE *) = _default_input_event_filter;
- static void (*input_shutdown_filter)() = _default_input_shutdown_filter;
- static const int g_KeyMap[][2] = {
- { SDLK_UP, kKeyUp },
- { SDLK_KP_8, kKeyUp },
- { SDLK_DOWN, kKeyDown },
- { SDLK_KP_2, kKeyDown },
- { SDLK_LEFT, kKeyLeft },
- { SDLK_KP_4, kKeyLeft },
- { SDLK_RIGHT, kKeyRight },
- { SDLK_KP_6, kKeyRight },
- { SDLK_ESCAPE, kKeyMenu },
- { SDLK_INSERT, kKeyMenu },
- { SDLK_LALT, kKeyMenu },
- { SDLK_RALT, kKeyMenu },
- { SDLK_KP_0, kKeyMenu },
- { SDLK_RETURN, kKeySearch },
- { SDLK_SPACE, kKeySearch },
- { SDLK_KP_ENTER, kKeySearch },
- { SDLK_LCTRL, kKeySearch },
- { SDLK_PAGEUP, kKeyPgUp },
- { SDLK_KP_9, kKeyPgUp },
- { SDLK_PAGEDOWN, kKeyPgDn },
- { SDLK_KP_3, kKeyPgDn },
- { SDLK_HOME, kKeyHome },
- { SDLK_KP_7, kKeyHome },
- { SDLK_END, kKeyEnd },
- { SDLK_KP_1, kKeyEnd },
- { SDLK_r, kKeyRepeat },
- { SDLK_a, kKeyAuto },
- { SDLK_d, kKeyDefend },
- { SDLK_e, kKeyUseItem },
- { SDLK_w, kKeyThrowItem },
- { SDLK_q, kKeyFlee },
- { SDLK_f, kKeyForce },
- { SDLK_s, kKeyStatus }
- };
- static VOID
- PAL_KeyDown(
- INT key,
- BOOL fRepeat
- )
- /*++
- Purpose:
- Called when user pressed a key.
- Parameters:
- [IN] key - keycode of the pressed key.
- Return value:
- None.
- --*/
- {
- switch (key)
- {
- case kKeyUp:
- if (g_InputState.dir != kDirNorth && !fRepeat)
- {
- g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
- g_InputState.dir = kDirNorth;
- }
- g_InputState.dwKeyPress |= kKeyUp;
- break;
- case kKeyDown:
- if (g_InputState.dir != kDirSouth && !fRepeat)
- {
- g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
- g_InputState.dir = kDirSouth;
- }
- g_InputState.dwKeyPress |= kKeyDown;
- break;
- case kKeyLeft:
- if (g_InputState.dir != kDirWest && !fRepeat)
- {
- g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
- g_InputState.dir = kDirWest;
- }
- g_InputState.dwKeyPress |= kKeyLeft;
- break;
- case kKeyRight:
- if (g_InputState.dir != kDirEast && !fRepeat)
- {
- g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
- g_InputState.dir = kDirEast;
- }
- g_InputState.dwKeyPress |= kKeyRight;
- break;
- default:
- g_InputState.dwKeyPress |= key;
- break;
- }
- }
- static VOID
- PAL_KeyUp(
- INT key
- )
- /*++
- Purpose:
- Called when user released a key.
- Parameters:
- [IN] key - keycode of the released key.
- Return value:
- None.
- --*/
- {
- switch (key)
- {
- case kKeyUp:
- if (g_InputState.dir == kDirNorth)
- {
- g_InputState.dir = g_InputState.prevdir;
- }
- g_InputState.prevdir = kDirUnknown;
- break;
- case kKeyDown:
- if (g_InputState.dir == kDirSouth)
- {
- g_InputState.dir = g_InputState.prevdir;
- }
- g_InputState.prevdir = kDirUnknown;
- break;
- case kKeyLeft:
- if (g_InputState.dir == kDirWest)
- {
- g_InputState.dir = g_InputState.prevdir;
- }
- g_InputState.prevdir = kDirUnknown;
- break;
- case kKeyRight:
- if (g_InputState.dir == kDirEast)
- {
- g_InputState.dir = g_InputState.prevdir;
- }
- g_InputState.prevdir = kDirUnknown;
- break;
- default:
- break;
- }
- }
- static VOID
- PAL_UpdateKeyboardState(
- VOID
- )
- /*++
- Purpose:
- Poll & update keyboard state.
- Parameters:
- None.
- Return value:
- None.
- --*/
- {
- static DWORD rgdwKeyLastTime[sizeof(g_KeyMap) / sizeof(g_KeyMap[0])] = {0};
- LPCBYTE keyState = (LPCBYTE)SDL_GetKeyboardState(NULL);
- int i;
- DWORD dwCurrentTime = SDL_GetTicks();
- for (i = 0; i < sizeof(g_KeyMap) / sizeof(g_KeyMap[0]); i++)
- {
- if (keyState[SDL_GetScancodeFromKey(g_KeyMap[i][0])])
- {
- if (dwCurrentTime > rgdwKeyLastTime[i])
- {
- PAL_KeyDown(g_KeyMap[i][1], (rgdwKeyLastTime[i] != 0));
- rgdwKeyLastTime[i] = dwCurrentTime + (rgdwKeyLastTime[i] == 0 ? 200 : 75);
- }
- }
- else
- {
- if (rgdwKeyLastTime[i] != 0)
- {
- PAL_KeyUp(g_KeyMap[i][1]);
- rgdwKeyLastTime[i] = 0;
- }
- }
- }
- }
- static VOID
- PAL_KeyboardEventFilter(
- const SDL_Event *lpEvent
- )
- /*++
- Purpose:
- Handle keyboard events.
- Parameters:
- [IN] lpEvent - pointer to the event.
- Return value:
- None.
- --*/
- {
- if (lpEvent->type == SDL_KEYDOWN)
- {
- //
- // Pressed a key
- //
- if (lpEvent->key.keysym.mod & KMOD_ALT)
- {
- if (lpEvent->key.keysym.sym == SDLK_RETURN)
- {
- //
- // Pressed Alt+Enter (toggle fullscreen)...
- //
- VIDEO_ToggleFullscreen();
- return;
- }
- else if (lpEvent->key.keysym.sym == SDLK_F4)
- {
- //
- // Pressed Alt+F4 (Exit program)...
- //
- PAL_Shutdown(0);
- }
- }
- else if (lpEvent->key.keysym.sym == SDLK_p)
- {
- VIDEO_SaveScreenshot();
- }
- }
- }
- static VOID
- PAL_MouseEventFilter(
- const SDL_Event *lpEvent
- )
- /*++
- Purpose:
- Handle mouse events.
- Parameters:
- [IN] lpEvent - pointer to the event.
- Return value:
- None.
- --*/
- {
- #if PAL_HAS_MOUSE
- static short hitTest = 0; // Double click detect;
- const SDL_VideoInfo *vi;
- double screenWidth, gridWidth;
- double screenHeight, gridHeight;
- double mx, my;
- double thumbx;
- double thumby;
- INT gridIndex;
- BOOL isLeftMouseDBClick = FALSE;
- BOOL isLeftMouseClick = FALSE;
- BOOL isRightMouseClick = FALSE;
- static INT lastReleaseButtonTime, lastPressButtonTime, betweenTime;
- static INT lastPressx = 0;
- static INT lastPressy = 0;
- static INT lastReleasex = 0;
- static INT lastReleasey = 0;
- if (lpEvent->type!= SDL_MOUSEBUTTONDOWN && lpEvent->type != SDL_MOUSEBUTTONUP)
- return;
- vi = SDL_GetVideoInfo();
- screenWidth = vi->current_w;
- screenHeight = vi->current_h;
- gridWidth = screenWidth / 3;
- gridHeight = screenHeight / 3;
- mx = lpEvent->button.x;
- my = lpEvent->button.y;
- thumbx = ceil(mx / gridWidth);
- thumby = floor(my / gridHeight);
- gridIndex = thumbx + thumby * 3 - 1;
-
- switch (lpEvent->type)
- {
- case SDL_MOUSEBUTTONDOWN:
- lastPressButtonTime = SDL_GetTicks();
- lastPressx = lpEvent->button.x;
- lastPressy = lpEvent->button.y;
- switch (gridIndex)
- {
- case 2:
- g_InputState.prevdir = g_InputState.dir;
- g_InputState.dir = kDirNorth;
- break;
- case 6:
- g_InputState.prevdir = g_InputState.dir;
- g_InputState.dir = kDirSouth;
- break;
- case 0:
- g_InputState.prevdir = g_InputState.dir;
- g_InputState.dir = kDirWest;
- break;
- case 8:
- g_InputState.prevdir = g_InputState.dir;
- g_InputState.dir = kDirEast;
- break;
- case 1:
- //g_InputState.prevdir = g_InputState.dir;
- //g_InputState.dir = kDirNorth;
- g_InputState.dwKeyPress |= kKeyUp;
- break;
- case 7:
- //g_InputState.prevdir = g_InputState.dir;
- //g_InputState.dir = kDirSouth;
- g_InputState.dwKeyPress |= kKeyDown;
- break;
- case 3:
- //g_InputState.prevdir = g_InputState.dir;
- //g_InputState.dir = kDirWest;
- g_InputState.dwKeyPress |= kKeyLeft;
- break;
- case 5:
- //g_InputState.prevdir = g_InputState.dir;
- //g_InputState.dir = kDirEast;
- g_InputState.dwKeyPress |= kKeyRight;
- break;
- }
- break;
- case SDL_MOUSEBUTTONUP:
- lastReleaseButtonTime = SDL_GetTicks();
- lastReleasex = lpEvent->button.x;
- lastReleasey = lpEvent->button.y;
- hitTest ++;
- if (abs(lastPressx - lastReleasex) < 25 &&
- abs(lastPressy - lastReleasey) < 25)
- {
- betweenTime = lastReleaseButtonTime - lastPressButtonTime;
- if (betweenTime >500)
- {
- isRightMouseClick = TRUE;
- }
- else if (betweenTime >=0)
- {
- if((betweenTime < 100) && (hitTest >= 2))
- {
- isLeftMouseClick = TRUE;
- hitTest = 0;
- }
- else
- {
- isLeftMouseClick = TRUE;
- if(betweenTime > 100)
- {
- hitTest = 0;
- }
-
- }
- }
- }
- switch (gridIndex)
- {
- case 2:
- if( isLeftMouseDBClick )
- {
- AUDIO_IncreaseVolume();
- break;
- }
- case 6:
- case 0:
- if( isLeftMouseDBClick )
- {
- AUDIO_DecreaseVolume();
- break;
- }
- case 7:
- if (isRightMouseClick) //repeat attack
- {
- g_InputState.dwKeyPress |= kKeyRepeat;
- break;
- }
- case 8:
- g_InputState.dir = kDirUnknown;
- g_InputState.prevdir = kDirUnknown;
- break;
- case 1:
- if( isRightMouseClick )
- {
- g_InputState.dwKeyPress |= kKeyForce;
- }
- break;
- case 3:
- if( isRightMouseClick )
- {
- g_InputState.dwKeyPress |= kKeyAuto;
- }
- break;
- case 5:
- if( isRightMouseClick )
- {
- g_InputState.dwKeyPress |= kKeyDefend;
- }
- break;
- case 4:
- if (isRightMouseClick) // menu
- {
- g_InputState.dwKeyPress |= kKeyMenu;
- }
- else if (isLeftMouseClick) // search
- {
- g_InputState.dwKeyPress |= kKeySearch;
- }
-
- break;
- }
- break;
- }
- #endif
- }
- static VOID
- PAL_JoystickEventFilter(
- const SDL_Event *lpEvent
- )
- /*++
- Purpose:
- Handle joystick events.
- Parameters:
- [IN] lpEvent - pointer to the event.
- Return value:
- None.
- --*/
- {
- #if PAL_HAS_JOYSTICKS
- switch (lpEvent->type)
- {
- case SDL_JOYAXISMOTION:
- //
- // Moved an axis on joystick
- //
- switch (lpEvent->jaxis.axis)
- {
- case 0:
- //
- // X axis
- //
- if (lpEvent->jaxis.value > 3200)
- {
- g_InputState.axisX = 1;
- }
- else if (lpEvent->jaxis.value < -3200)
- {
- g_InputState.axisX = -1;
- }
- else
- {
- g_InputState.axisX = 0;
- }
- break;
- case 1:
- //
- // Y axis
- //
- if (lpEvent->jaxis.value > 3200)
- {
- g_InputState.axisY = 1;
- }
- else if (lpEvent->jaxis.value < -3200)
- {
- g_InputState.axisY = -1;
- }
- else
- {
- g_InputState.axisY = 0;
- }
- break;
- }
- break;
- case SDL_JOYHATMOTION:
- //
- // Pressed the joystick hat button
- //
- switch (lpEvent->jhat.value)
- {
- case SDL_HAT_LEFT:
- case SDL_HAT_LEFTUP:
- g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
- g_InputState.dir = kDirWest;
- g_InputState.dwKeyPress = kKeyLeft;
- break;
- case SDL_HAT_RIGHT:
- case SDL_HAT_RIGHTDOWN:
- g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
- g_InputState.dir = kDirEast;
- g_InputState.dwKeyPress = kKeyRight;
- break;
- case SDL_HAT_UP:
- case SDL_HAT_RIGHTUP:
- g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
- g_InputState.dir = kDirNorth;
- g_InputState.dwKeyPress = kKeyUp;
- break;
- case SDL_HAT_DOWN:
- case SDL_HAT_LEFTDOWN:
- g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
- g_InputState.dir = kDirSouth;
- g_InputState.dwKeyPress = kKeyDown;
- break;
- case SDL_HAT_CENTERED:
- g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
- g_InputState.dir = kDirUnknown;
- g_InputState.dwKeyPress = kKeyNone;
- break;
- }
- break;
- case SDL_JOYBUTTONDOWN:
- //
- // Pressed the joystick button
- //
- switch (lpEvent->jbutton.button & 1)
- {
- case 0:
- g_InputState.dwKeyPress |= kKeyMenu;
- break;
- case 1:
- g_InputState.dwKeyPress |= kKeySearch;
- break;
- }
- break;
- }
- #endif
- }
- #if PAL_HAS_JOYSTICKS
- static VOID
- PAL_UpdateJoyStickState(
- VOID
- )
- /*++
- Purpose:
-
- Poll & update joystick state.
-
- Parameters:
-
- None.
-
- Return value:
-
- None.
-
- --*/
- {
- if( g_InputState.axisX == 1 && g_InputState.axisY >= 0 )
- {
- g_InputState.prevdir = g_InputState.dir;
- g_InputState.dir = kDirEast;
- g_InputState.dwKeyPress |= kKeyRight;
- }
- else if( g_InputState.axisX == -1 && g_InputState.axisY <= 0 )
- {
- g_InputState.prevdir = g_InputState.dir;
- g_InputState.dir = kDirWest;
- g_InputState.dwKeyPress |= kKeyLeft;
- }
- else if( g_InputState.axisY == 1 && g_InputState.axisX <= 0 )
- {
- g_InputState.prevdir = g_InputState.dir;
- g_InputState.dir = kDirSouth;
- g_InputState.dwKeyPress |= kKeyDown;
- }
- else if( g_InputState.axisY == -1 && g_InputState.axisX >= 0 )
- {
- g_InputState.prevdir = g_InputState.dir;
- g_InputState.dir = kDirNorth;
- g_InputState.dwKeyPress |= kKeyUp;
- }
- else
- {
- g_InputState.prevdir = g_InputState.dir;
- g_InputState.dir = kDirUnknown;
- if(!input_event_filter)
- g_InputState.dwKeyPress = kKeyNone;
- }
- }
- #endif
- #if PAL_HAS_TOUCH
- #define TOUCH_NONE 0
- #define TOUCH_UP 1
- #define TOUCH_DOWN 2
- #define TOUCH_LEFT 3
- #define TOUCH_RIGHT 4
- #define TOUCH_BUTTON1 5
- #define TOUCH_BUTTON2 6
- #define TOUCH_BUTTON3 7
- #define TOUCH_BUTTON4 8
- static float gfTouchXMin = 0.0f;
- static float gfTouchXMax = 1.0f;
- static float gfTouchYMin = 0.0f;
- static float gfTouchYMax = 1.0f;
- static SDL_TouchID gFinger1 = -1, gFinger2 = -1;
- static DWORD g_dwFinger1Time = 0, g_dwFinger2Time = 0;
- static int g_iPrevTouch1 = TOUCH_NONE;
- static int g_iPrevTouch2 = TOUCH_NONE;
- VOID
- PAL_SetTouchBounds(
- DWORD dwScreenWidth,
- DWORD dwScreenHeight,
- SDL_Rect renderRect
- )
- {
- gfTouchXMin = (float)renderRect.x / dwScreenWidth;
- gfTouchXMax = (float)(renderRect.x + renderRect.w) / dwScreenWidth;
- gfTouchYMin = (float)renderRect.y / dwScreenHeight;
- gfTouchYMax = (float)(renderRect.y + renderRect.h) / dwScreenHeight;
- }
- static int
- PAL_GetTouchArea(
- float X,
- float Y
- )
- {
- if (X < gfTouchXMin || X > gfTouchXMax || Y < 0.5f || Y > gfTouchYMax)
- {
- //
- // Upper area or cropped area
- //
- return TOUCH_NONE;
- }
- else
- {
- X = (X - gfTouchXMin) / (gfTouchXMax - gfTouchXMin);
- Y = (Y - gfTouchYMin) / (gfTouchYMax - gfTouchYMin);
- }
- if (X < 1.0f / 3)
- {
- if (Y - 0.5f < (1.0f / 6 - fabsf(X - 1.0f / 3 / 2)) * (0.5f / (1.0f / 3)))
- {
- return TOUCH_UP;
- }
- else if (Y - 0.75f > fabsf(X - 1.0f / 3 / 2) * (0.5f / (1.0f / 3)))
- {
- return TOUCH_DOWN;
- }
- else if (X < 1.0f / 3 / 2 && fabsf(Y - 0.75f) < 0.25f - X * (0.5f / (1.0f / 3)))
- {
- return TOUCH_LEFT;
- }
- else
- {
- return TOUCH_RIGHT;
- }
- }
- else if (X > 1.0f - 1.0f / 3)
- {
- if (X < 1.0f - (1.0f / 3 / 2))
- {
- if (Y < 0.75f)
- {
- return TOUCH_BUTTON1;
- }
- else
- {
- return TOUCH_BUTTON3;
- }
- }
- else
- {
- if (Y < 0.75f)
- {
- return TOUCH_BUTTON2;
- }
- else
- {
- return TOUCH_BUTTON4;
- }
- }
- }
- else
- {
- return TOUCH_NONE;
- }
- }
- static VOID
- PAL_SetTouchAction(
- int area
- )
- {
- switch (area)
- {
- case TOUCH_UP:
- g_InputState.dir = kDirNorth;
- g_InputState.dwKeyPress |= kKeyUp;
- break;
- case TOUCH_DOWN:
- g_InputState.dir = kDirSouth;
- g_InputState.dwKeyPress |= kKeyDown;
- break;
- case TOUCH_LEFT:
- g_InputState.dir = kDirWest;
- g_InputState.dwKeyPress |= kKeyLeft;
- break;
- case TOUCH_RIGHT:
- g_InputState.dir = kDirEast;
- g_InputState.dwKeyPress |= kKeyRight;
- break;
- case TOUCH_BUTTON1:
- g_InputState.dwKeyPress |= kKeyForce;
- break;
- case TOUCH_BUTTON2:
- g_InputState.dwKeyPress |= kKeyMenu;
- break;
- case TOUCH_BUTTON3:
- if (gpGlobals->fInBattle)
- {
- g_InputState.dwKeyPress |= kKeyRepeat;
- }
- else
- {
- g_InputState.dwKeyPress |= kKeyUseItem;
- }
- break;
- case TOUCH_BUTTON4:
- g_InputState.dwKeyPress |= kKeySearch;
- break;
- }
- }
- static VOID
- PAL_UnsetTouchAction(
- int area
- )
- {
- switch (area)
- {
- case TOUCH_UP:
- case TOUCH_DOWN:
- case TOUCH_LEFT:
- case TOUCH_RIGHT:
- g_InputState.dir = kDirUnknown;
- break;
- }
- }
- static VOID
- PAL_TouchRepeatCheck(
- VOID
- )
- {
- if (gFinger1 != -1 && SDL_GetTicks() > g_dwFinger1Time)
- {
- PAL_UnsetTouchAction(g_iPrevTouch1);
- PAL_SetTouchAction(g_iPrevTouch1);
- g_dwFinger1Time = SDL_GetTicks() + 120;
- }
- if (gFinger2 != -1 && SDL_GetTicks() > g_dwFinger2Time)
- {
- PAL_UnsetTouchAction(g_iPrevTouch2);
- PAL_SetTouchAction(g_iPrevTouch2);
- g_dwFinger2Time = SDL_GetTicks() + 120;
- }
- }
- #endif
- static VOID
- PAL_TouchEventFilter(
- const SDL_Event *lpEvent
- )
- /*++
- Purpose:
- Handle touch events.
- Parameters:
- [IN] lpEvent - pointer to the event.
- Return value:
- None.
- --*/
- {
- #if PAL_HAS_TOUCH
- switch (lpEvent->type)
- {
- case SDL_FINGERDOWN:
- if (gFinger1 == -1)
- {
- int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
- gFinger1 = lpEvent->tfinger.fingerId;
- g_iPrevTouch1 = area;
- PAL_SetTouchAction(area);
- g_dwFinger1Time = SDL_GetTicks() + 500;
- }
- else if (gFinger2 == -1)
- {
- int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
- gFinger2 = lpEvent->tfinger.fingerId;
- g_iPrevTouch2 = area;
- PAL_SetTouchAction(area);
- g_dwFinger2Time = SDL_GetTicks() + 500;
- }
- break;
- case SDL_FINGERUP:
- if (lpEvent->tfinger.fingerId == gFinger1)
- {
- PAL_UnsetTouchAction(g_iPrevTouch1);
- gFinger1 = -1;
- g_iPrevTouch1 = TOUCH_NONE;
- }
- else if (lpEvent->tfinger.fingerId == gFinger2)
- {
- PAL_UnsetTouchAction(g_iPrevTouch2);
- gFinger2 = -1;
- g_iPrevTouch2 = TOUCH_NONE;
- }
- break;
- case SDL_FINGERMOTION:
- if (lpEvent->tfinger.fingerId == gFinger1)
- {
- int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
- if (g_iPrevTouch1 != area && area != TOUCH_NONE)
- {
- PAL_UnsetTouchAction(g_iPrevTouch1);
- g_iPrevTouch1 = area;
- PAL_SetTouchAction(area);
- g_dwFinger1Time = SDL_GetTicks() + 500;
- }
- }
- else if (lpEvent->tfinger.fingerId == gFinger2)
- {
- int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
- if (g_iPrevTouch2 != area && area != TOUCH_NONE)
- {
- PAL_UnsetTouchAction(g_iPrevTouch2);
- g_iPrevTouch2 = area;
- PAL_SetTouchAction(area);
- g_dwFinger2Time = SDL_GetTicks() + 500;
- }
- }
- break;
- }
- #endif
- }
- static int SDLCALL
- PAL_EventFilter(
- const SDL_Event *lpEvent
- )
- /*++
- Purpose:
- SDL event filter function. A filter to process all events.
- Parameters:
- [IN] lpEvent - pointer to the event.
- Return value:
- 1 = the event will be added to the internal queue.
- 0 = the event will be dropped from the queue.
- --*/
- {
- switch (lpEvent->type)
- {
- #if SDL_VERSION_ATLEAST(2,0,0)
- case SDL_WINDOWEVENT:
- if (lpEvent->window.event == SDL_WINDOWEVENT_RESIZED)
- {
- //
- // resized the window
- //
- VIDEO_Resize(lpEvent->window.data1, lpEvent->window.data2);
- }
- break;
- case SDL_APP_WILLENTERBACKGROUND:
- g_bRenderPaused = TRUE;
- break;
- case SDL_APP_DIDENTERFOREGROUND:
- g_bRenderPaused = FALSE;
- VIDEO_UpdateScreen(NULL);
- break;
- #else
- case SDL_VIDEORESIZE:
- //
- // resized the window
- //
- VIDEO_Resize(lpEvent->resize.w, lpEvent->resize.h);
- break;
- #endif
- case SDL_QUIT:
- //
- // clicked on the close button of the window. Quit immediately.
- //
- PAL_Shutdown(0);
- }
- PAL_KeyboardEventFilter(lpEvent);
- PAL_MouseEventFilter(lpEvent);
- PAL_JoystickEventFilter(lpEvent);
- PAL_TouchEventFilter(lpEvent);
- //
- // All events are handled here; don't put anything to the internal queue
- //
- return 0;
- }
- VOID
- PAL_ClearKeyState(
- VOID
- )
- /*++
- Purpose:
- Clear the record of pressed keys.
- Parameters:
- None.
- Return value:
- None.
- --*/
- {
- g_InputState.dwKeyPress = 0;
- }
- VOID
- PAL_InitInput(
- VOID
- )
- /*++
- Purpose:
- Initialize the input subsystem.
- Parameters:
- None.
- Return value:
- None.
- --*/
- {
- memset((void *)&g_InputState, 0, sizeof(g_InputState));
- g_InputState.dir = kDirUnknown;
- g_InputState.prevdir = kDirUnknown;
- //
- // Check for joystick
- //
- #if PAL_HAS_JOYSTICKS
- if (SDL_NumJoysticks() > 0 && g_fUseJoystick)
- {
- int i;
- for (i = 0; i < SDL_NumJoysticks(); i++)
- {
- if (PAL_IS_VALID_JOYSTICK(SDL_JoystickNameForIndex(i)))
- {
- g_pJoy = SDL_JoystickOpen(i);
- break;
- }
- }
- if (g_pJoy != NULL)
- {
- SDL_JoystickEventState(SDL_ENABLE);
- }
- }
- #endif
- input_init_filter();
- }
- VOID
- PAL_ShutdownInput(
- VOID
- )
- /*++
- Purpose:
- Shutdown the input subsystem.
- Parameters:
- None.
- Return value:
- None.
- --*/
- {
- #if PAL_HAS_JOYSTICKS
- if (g_pJoy != NULL)
- {
- SDL_JoystickClose(g_pJoy);
- g_pJoy = NULL;
- }
- #endif
- input_shutdown_filter();
- }
- static int
- PAL_PollEvent(
- SDL_Event *event
- )
- /*++
- Purpose:
- Poll and process one event.
- Parameters:
- [OUT] event - Events polled from SDL.
- Return value:
- Return value of PAL_PollEvent.
- --*/
- {
- SDL_Event evt;
- int ret = SDL_PollEvent(&evt);
- if (ret != 0 && !input_event_filter(&evt, &g_InputState))
- {
- PAL_EventFilter(&evt);
- }
- if (event != NULL)
- {
- *event = evt;
- }
- return ret;
- }
- VOID
- PAL_ProcessEvent(
- VOID
- )
- /*++
- Purpose:
- Process all events.
- Parameters:
- None.
- Return value:
- None.
- --*/
- {
- while (PAL_PollEvent(NULL));
- PAL_UpdateKeyboardState();
- #if PAL_HAS_JOYSTICKS
- PAL_UpdateJoyStickState();
- #endif
- #if PAL_HAS_TOUCH
- PAL_TouchRepeatCheck();
- #endif
- }
- VOID
- PAL_RegisterInputFilter(
- void (*init_filter)(),
- int (*event_filter)(const SDL_Event *, volatile PALINPUTSTATE *),
- void (*shutdown_filter)()
- )
- /*++
- Purpose:
- Register caller-defined input event filter.
- Parameters:
- [IN] init_filter - Filter that will be called inside PAL_InitInput
- [IN] event_filter - Filter that will be called inside PAL_PollEvent,
- return non-zero value from this filter disables
- further internal event processing.
- [IN] shutdown_filter - Filter that will be called inside PAL_ShutdownInput
- Passing NULL to either parameter means the caller does not provide such filter.
- Return value:
- None.
- --*/
- {
- if (init_filter)
- input_init_filter = init_filter;
- if (event_filter)
- input_event_filter = event_filter;
- if (shutdown_filter)
- input_shutdown_filter = shutdown_filter;
- }
|