more touch work

This commit is contained in:
MelonSpeedruns 2022-05-21 10:23:04 -04:00
commit 381fd652fd
10 changed files with 143 additions and 48 deletions

View file

@ -2,6 +2,7 @@
#include "GlobalCtx2.h"
#include "stox.h"
#include <memory>
#include <SDL2/SDL_events.h>
namespace Ship {
Controller::Controller(int32_t dwControllerNumber) : dwControllerNumber(dwControllerNumber) {
@ -16,6 +17,29 @@ namespace Ship {
void Controller::Read(OSContPad* pad) {
ReadFromSource();
// Mouse Inputs
int x, y;
Uint32 buttons;
SDL_PumpEvents();
buttons = SDL_GetMouseState(&x, &y);
wTouchX = x;
wTouchY = y;
if ((buttons & SDL_BUTTON_LMASK) != 0) {
wLeftClick = 1;
}
else {
wLeftClick = 0;
}
if ((buttons & SDL_BUTTON_RMASK) != 0) {
wRightClick = 1;
}
else {
wRightClick = 0;
}
pad->button |= dwPressedButtons & 0xFFFF;
if (pad->stick_x == 0) {
@ -47,8 +71,8 @@ namespace Ship {
pad->touch_x = wTouchX;
pad->touch_y = wTouchY;
pad->touch = wTouching;
pad->left_click = wLeftClick;
pad->right_click = wRightClick;
}
void Controller::SetButtonMapping(const std::string& szButtonName, int32_t dwScancode) {

View file

@ -38,7 +38,9 @@ namespace Ship {
int8_t wStickY;
float wGyroX;
float wGyroY;
bool wTouching;
bool wLeftClick;
bool wRightClick;
float wTouchX;
float wTouchY;

View file

@ -114,10 +114,11 @@ typedef struct {
/* 0x04 */ u8 err_no;
/* 0x05 */ f32 gyro_x;
/* 0x09 */ f32 gyro_y;
/* 0x0A */ u8 touch;
/* 0x0B */ f32 touch_x;
/* 0x0F */ f32 touch_y;
} OSContPad; // size = 0x13
/* 0x0A */ u8 left_click;
/* 0x0B */ u8 right_click;
/* 0x0C */ f32 touch_x;
/* 0x10 */ f32 touch_y;
} OSContPad; // size = 0x14
typedef struct {
/* 0x00 */ u8 rumble;

View file

@ -227,21 +227,6 @@ namespace Ship {
wGyroX *= gyro_sensitivity;
wGyroY *= gyro_sensitivity;
int x, y;
Uint32 buttons;
SDL_PumpEvents();
buttons = SDL_GetMouseState(&x, &y);
wTouchX = x;
wTouchY = y;
if ((buttons & SDL_BUTTON_LMASK) != 0) {
wTouching = 1;
}
else {
wTouching = 0;
}
}
for (int32_t i = SDL_CONTROLLER_BUTTON_A; i < SDL_CONTROLLER_BUTTON_MAX; i++) {

View file

@ -120,10 +120,11 @@ typedef struct {
/* 0x04 */ uint8_t err_no;
/* 0x05 */ float gyro_x;
/* 0x09 */ float gyro_y;
/* 0x0A */ uint8_t touch;
/* 0x0B */ float touch_x;
/* 0x0F */ float touch_y;
} OSContPad; // size = 0x13
/* 0x0A */ uint8_t left_click;
/* 0x0B */ uint8_t right_click;
/* 0x0C */ float touch_x;
/* 0x10 */ float touch_y;
} OSContPad; // size = 0x14
typedef struct {
/* 0x00 */ uint8_t rumble;

View file

@ -247,7 +247,8 @@ void PadMgr_ProcessInputs(PadMgr* padMgr) {
input->cur.button = 0;
input->cur.stick_x = 0;
input->cur.stick_y = 0;
input->cur.touch = 0;
input->cur.left_click = 0;
input->cur.right_click = 0;
input->cur.err_no = padnow1->err_no;
if (padMgr->ctrlrIsConnected[i]) {
padMgr->ctrlrIsConnected[i] = false;
@ -272,9 +273,13 @@ void PadMgr_ProcessInputs(PadMgr* padMgr) {
input->press.stick_y += (s8)(input->cur.stick_y - input->prev.stick_y);
u8 buttonDiff2;
buttonDiff2 = input->prev.touch != input->cur.touch;
input->press.touch = (u8)(input->cur.touch && buttonDiff2);
input->rel.touch = (u8)(!input->cur.touch && buttonDiff2);
buttonDiff2 = input->prev.left_click != input->cur.left_click;
input->press.left_click = (u8)(input->cur.left_click && buttonDiff2);
input->rel.left_click = (u8)(!input->cur.left_click && buttonDiff2);
buttonDiff2 = input->prev.right_click != input->cur.right_click;
input->press.right_click = (u8)(input->cur.right_click && buttonDiff2);
input->rel.right_click = (u8)(!input->cur.right_click && buttonDiff2);
}
controllerCallback.rumble = CVar_GetS32("gRumbleEnabled", 0) && (padMgr->rumbleEnable[0] > 0);

View file

@ -341,7 +341,7 @@ void EnMag_Update(Actor* thisx, GlobalContext* globalCtx) {
if (this->globalState < MAG_STATE_DISPLAY) {
if (CHECK_BTN_ALL(globalCtx->state.input[0].press.button, BTN_START) ||
CHECK_BTN_ALL(globalCtx->state.input[0].press.button, BTN_A) ||
CHECK_BTN_ALL(globalCtx->state.input[0].press.button, BTN_B) || globalCtx->state.input[0].press.touch) {
CHECK_BTN_ALL(globalCtx->state.input[0].press.button, BTN_B) || globalCtx->state.input[0].press.left_click) {
Audio_PlaySoundGeneral(NA_SE_SY_PIECE_OF_HEART, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8);
@ -368,7 +368,7 @@ void EnMag_Update(Actor* thisx, GlobalContext* globalCtx) {
if (CHECK_BTN_ALL(globalCtx->state.input[0].press.button, BTN_START) ||
CHECK_BTN_ALL(globalCtx->state.input[0].press.button, BTN_A) ||
CHECK_BTN_ALL(globalCtx->state.input[0].press.button, BTN_B) ||
globalCtx->state.input[0].press.touch) {
globalCtx->state.input[0].press.left_click) {
if (globalCtx->sceneLoadFlag != 20) {
Audio_SetCutsceneFlag(0);

View file

@ -214,6 +214,11 @@ void FileChoose_DrawOptions(GameState* thisx);
void FileChoose_DrawNameEntry(GameState* thisx);
void FileChoose_DrawCharacter(GraphicsContext* gfxCtx, void* texture, s16 vtx);
void HandleMouseInput(Input* input);
u8 HandleMouseCursor(FileChooseContext* this, Input* input, int minx, int miny, int maxx, int maxy);
Vec2f HandleMouseCursorSplit(FileChooseContext* this, Input* input, int minx, int miny, int maxx, int maxy, int countx,
int county);
extern s16 D_808123F0[];
#endif

View file

@ -164,7 +164,7 @@ void FileChoose_FinishFadeIn(GameState* thisx) {
}
void HandleMouseInput(Input* input) {
if (input->press.touch) {
if (input->press.left_click) {
input->press.button = BTN_A;
}
}
@ -175,8 +175,8 @@ u8 HandleMouseCursor(FileChooseContext* this, Input* input, int minx, int miny,
float newX = ((240.0f * ratio) - (240.0f * ogratio)) / 2.0f;
float pos_x = ((input->cur.touch_x / OTRGetCurrentWidth()) * (240.0f * ratio) - newX);
float pos_y = (input->cur.touch_y / OTRGetCurrentHeight()) * 240.0f;
uint32_t pos_x = ((input->cur.touch_x / OTRGetCurrentWidth()) * (240.0f * ratio) - newX);
uint32_t pos_y = (input->cur.touch_y / OTRGetCurrentHeight()) * 240.0f;
if (pos_x >= minx && pos_x <= minx + maxx) {
if (pos_y >= miny && pos_y <= miny + maxy) {
@ -187,6 +187,46 @@ u8 HandleMouseCursor(FileChooseContext* this, Input* input, int minx, int miny,
return 0;
}
Vec2f HandleMouseCursorSplit(FileChooseContext* this, Input* input, int minx, int miny, int maxx, int maxy, int countx, int county) {
float ogratio = 320.0f / 240.0f;
float ratio = (float)OTRGetCurrentWidth() / (float)OTRGetCurrentHeight();
float newX = ((240.0f * ratio) - (240.0f * ogratio)) / 2.0f;
uint32_t pos_x = ((input->cur.touch_x / OTRGetCurrentWidth()) * (240.0f * ratio) - newX);
uint32_t pos_y = (input->cur.touch_y / OTRGetCurrentHeight()) * 240.0f;
Vec2f pos;
if (pos_x < minx || pos_x > minx + maxx || pos_y < miny || pos_y > miny + maxy) {
pos.x = -1;
pos.y = -1;
return pos;
}
int pointerx = (pos_x - minx) / 16.3f;
int pointery = (pos_y - miny) / 16.0f;
if (pointerx <= 0) {
pointerx = 0;
}
if (pointery <= 0) {
pointery = 0;
}
if (pointerx >= countx - 1) {
pointerx = countx - 1;
}
if (pointery >= county - 1) {
pointery = county - 1;
}
pos.x = pointerx;
pos.y = pointery;
return pos;
}
/**
* Update the cursor and wait for the player to select a button to change menus accordingly.
* If an empty file is selected, enter the name entry config mode.
@ -202,23 +242,21 @@ void FileChoose_UpdateMainMenu(GameState* thisx) {
Input* input = &this->state.input[0];
bool dpad = CVar_GetS32("gDpadPauseName", 0);
if (HandleMouseCursor(this, input, 57, 74, 66, 17)) {
if (HandleMouseCursor(this, input, 57, 74, 66, 17) == 1) {
if (this->buttonIndex != FS_BTN_MAIN_FILE_1) {
this->buttonIndex = FS_BTN_MAIN_FILE_1;
Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8);
}
HandleMouseInput(input);
}
else if (HandleMouseCursor(this, input, 57, 91, 66, 17)) {
} else if (HandleMouseCursor(this, input, 57, 91, 66, 17) == 1) {
if (this->buttonIndex != FS_BTN_MAIN_FILE_2) {
this->buttonIndex = FS_BTN_MAIN_FILE_2;
Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8);
}
HandleMouseInput(input);
}
else if (HandleMouseCursor(this, input, 57, 107, 66, 17)) {
} else if (HandleMouseCursor(this, input, 57, 107, 66, 17) == 1) {
if (this->buttonIndex != FS_BTN_MAIN_FILE_3) {
this->buttonIndex = FS_BTN_MAIN_FILE_3;
Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8);
@ -1367,6 +1405,22 @@ void FileChoose_ConfirmFile(GameState* thisx) {
Input* input = &this->state.input[0];
bool dpad = CVar_GetS32("gDpadPauseName", 0);
if (HandleMouseCursor(this, input, 57, 156, 66, 17) == 1) {
if (this->confirmButtonIndex != FS_BTN_CONFIRM_YES) {
this->confirmButtonIndex = FS_BTN_CONFIRM_YES;
Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8);
}
HandleMouseInput(input);
} else if (HandleMouseCursor(this, input, 57, 173, 66, 17) == 1) {
if (this->confirmButtonIndex != FS_BTN_CONFIRM_QUIT) {
this->confirmButtonIndex = FS_BTN_CONFIRM_QUIT;
Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8);
}
HandleMouseInput(input);
}
if (CHECK_BTN_ALL(input->press.button, BTN_START) || (CHECK_BTN_ALL(input->press.button, BTN_A))) {
if (this->confirmButtonIndex == FS_BTN_CONFIRM_YES) {
func_800AA000(300.0f, 180, 20, 100);
@ -1377,7 +1431,7 @@ void FileChoose_ConfirmFile(GameState* thisx) {
Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CLOSE, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8);
this->selectMode++;
}
} else if (CHECK_BTN_ALL(input->press.button, BTN_B)) {
} else if (CHECK_BTN_ALL(input->press.button, BTN_B) || input->press.right_click) {
Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CLOSE, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8);
this->selectMode++;
} else if ((ABS(this->stickRelY) >= 30) || (dpad && CHECK_BTN_ANY(input->press.button, BTN_DDOWN | BTN_DUP))) {

View file

@ -347,19 +347,36 @@ void FileChoose_DrawNameEntry(GameState* thisx) {
PRIMITIVE, 0);
gDPSetPrimColor(POLY_OPA_DISP++, 0, 0, 255, 255, 255, 255);
if (HandleMouseCursor(this, input, 193, 169, 28, 17)) {
if (HandleMouseCursor(this, input, 193, 169, 28, 17) == 1) {
if (this->kbdButton != FS_KBD_BTN_BACKSPACE) {
this->kbdButton = FS_KBD_BTN_BACKSPACE;
this->charIndex = 0;
this->kbdX = 3;
this->kbdY = 5;
Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8);
}
HandleMouseInput(input);
}
if (HandleMouseCursor(this, input, 226, 169, 44, 17)) {
if (this->kbdY != 5 && this->kbdX != 4) {
this->kbdY = 5;
if (HandleMouseCursor(this, input, 226, 169, 44, 17) == 1) {
if (this->kbdButton != FS_KBD_BTN_END) {
this->charIndex = 0;
this->kbdX = 4;
this->kbdY = 5;
Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8);
}
HandleMouseInput(input);
}
Vec2f namePos = HandleMouseCursorSplit(this, input, 60, 79, 211, 79, 13, 5);
if (namePos.x != -1 && namePos.y != -1) {
int index = namePos.x + (namePos.y * 13);
if (this->charIndex != index) {
this->kbdButton = 99;
this->kbdX = 0;
this->kbdY = 0;
this->charIndex = index;
Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8);
}
@ -372,7 +389,7 @@ void FileChoose_DrawNameEntry(GameState* thisx) {
// place cursor on END button
this->kbdY = 5;
this->kbdX = 4;
} else if (CHECK_BTN_ALL(input->press.button, BTN_B)) {
} else if (CHECK_BTN_ALL(input->press.button, BTN_B) || input->press.right_click) {
if ((this->newFileNameCharCount == 7) && (this->fileNames[this->buttonIndex][7] != 0x3E)) {
for (i = this->newFileNameCharCount; i < 7; i++) {
this->fileNames[this->buttonIndex][i] = this->fileNames[this->buttonIndex][i + 1];
@ -416,7 +433,8 @@ void FileChoose_DrawNameEntry(GameState* thisx) {
this->newFileNameCharCount = 7;
}
}
} else if (CHECK_BTN_ALL(input->press.button, BTN_A) && (this->charPage != this->kbdButton)) {
} else if (CHECK_BTN_ALL(input->press.button, BTN_A) &&
(this->charPage != this->kbdButton)) {
if (this->kbdButton == FS_KBD_BTN_BACKSPACE) {
if ((this->newFileNameCharCount == 7) && (this->fileNames[this->buttonIndex][7] != 0x3E)) {
for (i = this->newFileNameCharCount; i < 7; i++) {