[Emulator] Handle left click

This commit is contained in:
wheremyfoodat 2023-06-13 15:32:31 +03:00
parent f9f34d564f
commit 5200c10b27
4 changed files with 90 additions and 47 deletions

View file

@ -49,10 +49,12 @@ class HIDService {
u32 oldButtons; // The previous pad state
s16 circlePadX, circlePadY; // Circlepad state
s16 touchScreenX, touchScreenY; // Touchscreen state
bool accelerometerEnabled;
bool eventsInitialized;
bool gyroEnabled;
bool touchScreenPressed;
std::array<std::optional<Handle>, 5> events;
@ -116,4 +118,14 @@ public:
std::memset(ptr, 0, 0x2b0);
}
}
void setTouchScreenPress(u16 x, u16 y) {
touchScreenX = x;
touchScreenY = y;
touchScreenPressed = true;
}
void releaseTouchScreen() {
touchScreenPressed = false;
}
};

View file

@ -85,9 +85,12 @@ public:
void signalDSPEvents() { dsp.signalEvents(); }
// Input function wrappers
void pressKey(u32 key) { hid.pressKey(key); }
void releaseKey(u32 key) { hid.releaseKey(key); }
void setCirclepadX(u16 x) { hid.setCirclepadX(x); }
void setCirclepadY(u16 y) { hid.setCirclepadY(y); }
void updateInputs(u64 currentTimestamp) { hid.updateInputs(currentTimestamp); }
void setTouchScreenPress(u16 x, u16 y) { hid.setTouchScreenPress(x, y); }
void releaseTouchScreen() { hid.releaseTouchScreen(); }
};

View file

@ -25,6 +25,7 @@ void HIDService::reset() {
accelerometerEnabled = false;
eventsInitialized = false;
gyroEnabled = false;
touchScreenPressed = false;
// Deinitialize HID events
for (auto& e : events) {
@ -36,6 +37,7 @@ void HIDService::reset() {
// Reset button states
newButtons = oldButtons = 0;
circlePadX = circlePadY = 0;
touchScreenX = touchScreenY = 0;
}
void HIDService::handleSyncRequest(u32 messagePointer) {

View file

@ -38,60 +38,86 @@ void Emulator::run() {
namespace Keys = HID::Keys;
switch (event.type) {
case SDL_QUIT:
printf("Bye :(\n");
running = false;
return;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_l: srv.pressKey(Keys::A); break;
case SDLK_k: srv.pressKey(Keys::B); break;
case SDLK_o: srv.pressKey(Keys::X); break;
case SDLK_i: srv.pressKey(Keys::Y); break;
case SDL_QUIT:
printf("Bye :(\n");
running = false;
return;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_l: srv.pressKey(Keys::A); break;
case SDLK_k: srv.pressKey(Keys::B); break;
case SDLK_o: srv.pressKey(Keys::X); break;
case SDLK_i: srv.pressKey(Keys::Y); break;
case SDLK_q: srv.pressKey(Keys::L); break;
case SDLK_p: srv.pressKey(Keys::R); break;
case SDLK_q: srv.pressKey(Keys::L); break;
case SDLK_p: srv.pressKey(Keys::R); break;
case SDLK_RIGHT: srv.pressKey(Keys::Right); break;
case SDLK_LEFT: srv.pressKey(Keys::Left); break;
case SDLK_UP: srv.pressKey(Keys::Up); break;
case SDLK_DOWN: srv.pressKey(Keys::Down); break;
case SDLK_RIGHT: srv.pressKey(Keys::Right); break;
case SDLK_LEFT: srv.pressKey(Keys::Left); break;
case SDLK_UP: srv.pressKey(Keys::Up); break;
case SDLK_DOWN: srv.pressKey(Keys::Down); break;
case SDLK_w: srv.setCirclepadY(0x9C); break;
case SDLK_a: srv.setCirclepadX(-0x9C); break;
case SDLK_s: srv.setCirclepadY(-0x9C); break;
case SDLK_d: srv.setCirclepadX(0x9C); break;
case SDLK_w: srv.setCirclepadY(0x9C); break;
case SDLK_a: srv.setCirclepadX(-0x9C); break;
case SDLK_s: srv.setCirclepadY(-0x9C); break;
case SDLK_d: srv.setCirclepadX(0x9C); break;
case SDLK_RETURN: srv.pressKey(Keys::Start); break;
case SDLK_BACKSPACE: srv.pressKey(Keys::Select); break;
case SDLK_RETURN: srv.pressKey(Keys::Start); break;
case SDLK_BACKSPACE: srv.pressKey(Keys::Select); break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_l: srv.releaseKey(Keys::A); break;
case SDLK_k: srv.releaseKey(Keys::B); break;
case SDLK_o: srv.releaseKey(Keys::X); break;
case SDLK_i: srv.releaseKey(Keys::Y); break;
case SDLK_q: srv.releaseKey(Keys::L); break;
case SDLK_p: srv.releaseKey(Keys::R); break;
case SDLK_RIGHT: srv.releaseKey(Keys::Right); break;
case SDLK_LEFT: srv.releaseKey(Keys::Left); break;
case SDLK_UP: srv.releaseKey(Keys::Up); break;
case SDLK_DOWN: srv.releaseKey(Keys::Down); break;
// Err this is probably not ideal
case SDLK_w: srv.setCirclepadY(0); break;
case SDLK_a: srv.setCirclepadX(0); break;
case SDLK_s: srv.setCirclepadY(0); break;
case SDLK_d: srv.setCirclepadX(0); break;
case SDLK_RETURN: srv.releaseKey(Keys::Start); break;
case SDLK_BACKSPACE: srv.releaseKey(Keys::Select); break;
}
break;
case SDL_MOUSEBUTTONDOWN: {
if (event.button.button == SDL_BUTTON_LEFT) {
const s32 x = event.button.x;
const s32 y = event.button.y;
// Check if touch falls in the touch screen area
if (y >= 240 && y <= 480 && x >= 40 && x < 40 + 320) {
// Convert to 3DS coordinates
u16 x_converted = static_cast<u16>(x) - 40;
u16 y_converted = static_cast<u16>(y) - 240;
srv.setTouchScreenPress(x_converted, y_converted);
}
else {
srv.releaseTouchScreen();
}
}
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_l: srv.releaseKey(Keys::A); break;
case SDLK_k: srv.releaseKey(Keys::B); break;
case SDLK_o: srv.releaseKey(Keys::X); break;
case SDLK_i: srv.releaseKey(Keys::Y); break;
case SDLK_q: srv.releaseKey(Keys::L); break;
case SDLK_p: srv.releaseKey(Keys::R); break;
case SDLK_RIGHT: srv.releaseKey(Keys::Right); break;
case SDLK_LEFT: srv.releaseKey(Keys::Left); break;
case SDLK_UP: srv.releaseKey(Keys::Up); break;
case SDLK_DOWN: srv.releaseKey(Keys::Down); break;
// Err this is probably not ideal
case SDLK_w: srv.setCirclepadY(0); break;
case SDLK_a: srv.setCirclepadX(0); break;
case SDLK_s: srv.setCirclepadY(0); break;
case SDLK_d: srv.setCirclepadX(0); break;
case SDLK_RETURN: srv.releaseKey(Keys::Start); break;
case SDLK_BACKSPACE: srv.releaseKey(Keys::Select); break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT) {
srv.releaseTouchScreen();
}
break;
}
break;
}
}
// Update inputs in the HID module