Skip to content
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

Implementing the use of the touchscreen as camera movement #1896

Closed
Closed
5 changes: 5 additions & 0 deletions src/DSi_SPI_TSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ void DSi_TSC::SetTouchCoords(u16 x, u16 y)
}
}

void DSi_TSC::MoveTouchCoords(u16 x, u16 y)
{

}

void DSi_TSC::MicInputFrame(s16* data, int samples)
{
if (TSCMode == 0x00) return TSC::MicInputFrame(data, samples);
Expand Down
1 change: 1 addition & 0 deletions src/DSi_SPI_TSC.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class DSi_TSC : public TSC
void SetMode(u8 mode);

void SetTouchCoords(u16 x, u16 y) override;
void MoveTouchCoords(u16 x, u16 y) override;
void MicInputFrame(s16* data, int samples) override;

void Write(u8 val) override;
Expand Down
21 changes: 21 additions & 0 deletions src/NDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,12 @@ void NDS::TouchScreen(u16 x, u16 y)
SPI.GetTSC()->SetTouchCoords(x, y);
}

void NDS::MoveOnTouchScreen(u16 x, u16 y)
{
SPI.GetTSC()->MoveTouchCoords(x, y);
KeyInput &= ~(1 << (16+6));
}

void NDS::ReleaseScreen()
{
SPI.GetTSC()->SetTouchCoords(0x000, 0xFFF);
Expand Down Expand Up @@ -1169,6 +1175,21 @@ void NDS::SetKeyMask(u32 mask)
CheckKeyIRQ(1, oldkey, KeyInput);
}

void NDS::SetTouchKeyMask(u32 mask)
{
u32 right = (~mask) & 0x1;
u32 left = (~(mask >> 1)) & 0x1;
u32 up = (~(mask >> 2)) & 0x1;
u32 down = (~(mask >> 3)) & 0x1;

if (right | left | up | down) {
MoveOnTouchScreen((right - left) + 1, (down - up) + 1);
}
else {
ReleaseScreen();
}
}

bool NDS::IsLidClosed()
{
if (KeyInput & (1<<23)) return true;
Expand Down
2 changes: 2 additions & 0 deletions src/NDS.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,11 @@ class NDS
bool IsRunning() const noexcept { return Running; }

void TouchScreen(u16 x, u16 y);
void MoveOnTouchScreen(u16 x, u16 y);
void ReleaseScreen();

void SetKeyMask(u32 mask);
void SetTouchKeyMask(u32 mask);

bool IsLidClosed();
void SetLidClosed(bool closed);
Expand Down
24 changes: 24 additions & 0 deletions src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,30 @@ void TSC::SetTouchCoords(u16 x, u16 y)
NDS.KeyInput &= ~(1 << (16+6));
}

void TSC::MoveTouchCoords(u16 x, u16 y) // 0 -> negative, 1 -> neutral, 2 -> positive
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think you could make this an enum instead of a series of magic numbers? I could easily see some poor bastard trying to pass mouse coordinates in here, wondering why nothing works.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Any preferable place where I should place the enum?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SPI.h would be my first instinct.

Copy link
Author

@vitor251093 vitor251093 Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum added, but now I'm wondering if replacing x and y enums with left, top, right and down booleans wouldn't make more sense lol

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think @JesseTG ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, I didn't think you were asking me the first time.

No, keep the two-argument version; the "cursor" can move along both axes, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct

{
u16 sensitivityX = 4;
u16 sensitivityY = 8;

if (x == 0) {
TouchX -= sensitivityX << 4;
}
if (x == 2) {
TouchX += sensitivityX << 4;
}
if (y == 0) {
TouchY -= sensitivityY << 4;
}
if (y == 2) {
TouchY += sensitivityY << 4;
}

if (TouchY > (255 << 4) || TouchX > (191 << 4)) {
TouchX = 128 << 4; // aprox middle of 255
TouchY = 95 << 4; // aprox middle of 191
}
}

void TSC::MicInputFrame(s16* data, int samples)
{
if (!data)
Expand Down
1 change: 1 addition & 0 deletions src/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class TSC : public SPIDevice
virtual void DoSavestate(Savestate* file) override;

virtual void SetTouchCoords(u16 x, u16 y);
virtual void MoveTouchCoords(u16 x, u16 y);
virtual void MicInputFrame(s16* data, int samples);

virtual void Write(u8 val) override;
Expand Down
13 changes: 13 additions & 0 deletions src/frontend/qt_sdl/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ int JoyMapping[12];
int HKKeyMapping[HK_MAX];
int HKJoyMapping[HK_MAX];

int TouchKeyMapping[4];
int TouchJoyMapping[4];

int JoystickID;

int WindowWidth;
Expand Down Expand Up @@ -224,6 +227,16 @@ ConfigEntry ConfigFile[] =
{"HKJoy_VolumeUp", 0, &HKJoyMapping[HK_VolumeUp], -1, true},
{"HKJoy_VolumeDown", 0, &HKJoyMapping[HK_VolumeDown], -1, true},

{"Key_TouchRight", 0, &TouchKeyMapping[0], -1, true},
{"Key_TouchLeft", 0, &TouchKeyMapping[1], -1, true},
{"Key_TouchUp", 0, &TouchKeyMapping[2], -1, true},
{"Key_TouchDown", 0, &TouchKeyMapping[3], -1, true},

{"Joy_TouchRight", 0, &TouchJoyMapping[0], -1, true},
{"Joy_TouchLeft", 0, &TouchJoyMapping[1], -1, true},
{"Joy_TouchUp", 0, &TouchJoyMapping[2], -1, true},
{"Joy_TouchDown", 0, &TouchJoyMapping[3], -1, true},

{"JoystickID", 0, &JoystickID, 0, true},

{"WindowWidth", 0, &WindowWidth, 256, true},
Expand Down
3 changes: 3 additions & 0 deletions src/frontend/qt_sdl/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ extern int JoyMapping[12];
extern int HKKeyMapping[HK_MAX];
extern int HKJoyMapping[HK_MAX];

extern int TouchKeyMapping[4];
extern int TouchJoyMapping[4];

extern int JoystickID;

extern int WindowWidth;
Expand Down
19 changes: 18 additions & 1 deletion src/frontend/qt_sdl/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ int JoystickID;
SDL_Joystick* Joystick = nullptr;

u32 KeyInputMask, JoyInputMask;
u32 KeyTouchInputMask, JoyTouchInputMask;
u32 KeyHotkeyMask, JoyHotkeyMask;
u32 HotkeyMask, LastHotkeyMask;
u32 HotkeyPress, HotkeyRelease;

u32 InputMask;
u32 InputMask, TouchInputMask;


void Init()
{
KeyInputMask = 0xFFF;
JoyInputMask = 0xFFF;
KeyTouchInputMask = 0xFFF;
JoyTouchInputMask = 0xFFF;
InputMask = 0xFFF;
TouchInputMask = 0xFFF;

KeyHotkeyMask = 0;
JoyHotkeyMask = 0;
Expand Down Expand Up @@ -107,6 +111,10 @@ void KeyPress(QKeyEvent* event)
if (keyKP == Config::KeyMapping[i])
KeyInputMask &= ~(1<<i);

for (int i = 0; i < 4; i++)
if (keyKP == Config::TouchKeyMapping[i])
KeyTouchInputMask &= ~(1<<i);

for (int i = 0; i < HK_MAX; i++)
if (keyHK == Config::HKKeyMapping[i])
KeyHotkeyMask |= (1<<i);
Expand All @@ -123,6 +131,10 @@ void KeyRelease(QKeyEvent* event)
if (keyKP == Config::KeyMapping[i])
KeyInputMask |= (1<<i);

for (int i = 0; i < 4; i++)
if (keyKP == Config::TouchKeyMapping[i])
KeyTouchInputMask |= (1<<i);

for (int i = 0; i < HK_MAX; i++)
if (keyHK == Config::HKKeyMapping[i])
KeyHotkeyMask &= ~(1<<i);
Expand Down Expand Up @@ -204,11 +216,16 @@ void Process()
}

JoyInputMask = 0xFFF;
JoyTouchInputMask = 0xFFF;
for (int i = 0; i < 12; i++)
if (JoystickButtonDown(Config::JoyMapping[i]))
JoyInputMask &= ~(1<<i);
for (int i = 0; i < 4; i++)
if (JoystickButtonDown(Config::TouchJoyMapping[i]))
JoyTouchInputMask &= ~(1<<i);

InputMask = KeyInputMask & JoyInputMask;
TouchInputMask = KeyTouchInputMask & JoyTouchInputMask;

JoyHotkeyMask = 0;
for (int i = 0; i < HK_MAX; i++)
Expand Down
1 change: 1 addition & 0 deletions src/frontend/qt_sdl/Input.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern int JoystickID;
extern SDL_Joystick* Joystick;

extern u32 InputMask;
extern u32 TouchInputMask;

void Init();

Expand Down
27 changes: 27 additions & 0 deletions src/frontend/qt_sdl/InputConfig/InputConfigDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <QLabel>
#include <QKeyEvent>
#include <QDebug>
#include <QVBoxLayout>

#include <SDL2/SDL.h>

Expand All @@ -38,6 +39,8 @@ InputConfigDialog* InputConfigDialog::currentDlg = nullptr;
const int dskeyorder[12] = {0, 1, 10, 11, 5, 4, 6, 7, 9, 8, 2, 3};
const char* dskeylabels[12] = {"A", "B", "X", "Y", "Left", "Right", "Up", "Down", "L", "R", "Select", "Start"};

const int dstouchkeyorder[12] = {1, 0, 2, 3};

InputConfigDialog::InputConfigDialog(QWidget* parent) : QDialog(parent), ui(new Ui::InputConfigDialog)
{
ui->setupUi(this);
Expand Down Expand Up @@ -65,6 +68,12 @@ InputConfigDialog::InputConfigDialog(QWidget* parent) : QDialog(parent), ui(new
i++;
}

for (int i = 0; i < touchscreen_num; i++)
{
touchScreenKeyMap[i] = Config::TouchKeyMapping[dstouchkeyorder[i]];
touchScreenJoyMap[i] = Config::TouchJoyMapping[dstouchkeyorder[i]];
}

populatePage(ui->tabAddons, hk_addons_labels, addonsKeyMap, addonsJoyMap);
populatePage(ui->tabHotkeysGeneral, hk_general_labels, hkGeneralKeyMap, hkGeneralJoyMap);

Expand All @@ -85,6 +94,7 @@ InputConfigDialog::InputConfigDialog(QWidget* parent) : QDialog(parent), ui(new
}

setupKeypadPage();
setupTouchScreenPage();

int inst = Platform::InstanceID();
if (inst > 0)
Expand Down Expand Up @@ -121,6 +131,17 @@ void InputConfigDialog::setupKeypadPage()
}
}

void InputConfigDialog::setupTouchScreenPage()
{
QVBoxLayout* main_layout = new QVBoxLayout();

QWidget* touch_widget = new QWidget();
populatePage(touch_widget, ds_touch_key_labels, touchScreenKeyMap, touchScreenJoyMap);
main_layout->addWidget(touch_widget);

ui->tabTouchScreen->setLayout(main_layout);
}

void InputConfigDialog::populatePage(QWidget* page,
const std::initializer_list<const char*>& labels,
int* keymap, int* joymap)
Expand Down Expand Up @@ -196,6 +217,12 @@ void InputConfigDialog::on_InputConfigDialog_accepted()
i++;
}

for (int i = 0; i < touchscreen_num; i++)
{
Config::TouchKeyMapping[dstouchkeyorder[i]] = touchScreenKeyMap[i];
Config::TouchJoyMapping[dstouchkeyorder[i]] = touchScreenJoyMap[i];
}

Config::JoystickID = Input::JoystickID;
Config::Save();

Expand Down
10 changes: 10 additions & 0 deletions src/frontend/qt_sdl/InputConfig/InputConfigDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "Config.h"

static constexpr int keypad_num = 12;
static constexpr int touchscreen_num = 4;

static constexpr std::initializer_list<int> hk_addons =
{
Expand Down Expand Up @@ -77,6 +78,13 @@ static constexpr std::initializer_list<const char*> hk_general_labels =

static_assert(hk_general.size() == hk_general_labels.size());

static constexpr std::initializer_list<const char*> ds_touch_key_labels =
{
"Left",
"Right",
"Up",
"Down"
};

namespace Ui { class InputConfigDialog; }
class InputConfigDialog;
Expand Down Expand Up @@ -120,12 +128,14 @@ private slots:
const std::initializer_list<const char*>& labels,
int* keymap, int* joymap);
void setupKeypadPage();
void setupTouchScreenPage();

Ui::InputConfigDialog* ui;

int keypadKeyMap[12], keypadJoyMap[12];
int addonsKeyMap[hk_addons.size()], addonsJoyMap[hk_addons.size()];
int hkGeneralKeyMap[hk_general.size()], hkGeneralJoyMap[hk_general.size()];
int touchScreenKeyMap[4], touchScreenJoyMap[4];
};


Expand Down
5 changes: 5 additions & 0 deletions src/frontend/qt_sdl/InputConfig/InputConfigDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,11 @@
<string>General hotkeys</string>
</attribute>
</widget>
<widget class="QWidget" name="tabTouchScreen">
<attribute name="title">
<string>Touch Screen</string>
</attribute>
</widget>
</widget>
</item>
<item row="7" column="0" colspan="2">
Expand Down
1 change: 1 addition & 0 deletions src/frontend/qt_sdl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ void EmuThread::run()

// process input and hotkeys
NDS->SetKeyMask(Input::InputMask);
NDS->SetTouchKeyMask(Input::TouchInputMask);

if (Input::HotkeyPressed(HK_Lid))
{
Expand Down