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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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(SPITouchScreenMovement x, SPITouchScreenMovement y)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason that DSi_TSC's override of this method does nothing?

Copy link
Author

@vitor251093 vitor251093 Dec 3, 2023

Choose a reason for hiding this comment

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

Oh, sorry, I forgot to mention that.

I've only tested the touchscreen camera controls with the DS mode, but not with the DSi mode. Since the implementation of SetTouchCoords was different for each of them, I decided to include that override to avoid unexpected behaviours. At the time I had implemented that only for KH Days, so there was no use in implementing that function either.

I can make some tests with the DSi mode to implement that function, but that may take a little while.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think that would be for the best.

{

}

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(SPITouchScreenMovement x, SPITouchScreenMovement y) override;
void MicInputFrame(s16* data, int samples) override;

void Write(u8 val) override;
Expand Down
35 changes: 35 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(SPITouchScreenMovement x, SPITouchScreenMovement 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,35 @@ 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)) {
ReleaseScreen();
return;
}

SPITouchScreenMovement x = SPITouchScreenMovement_None;
SPITouchScreenMovement y = SPITouchScreenMovement_None;
if (left) {
x = SPITouchScreenMovement_Negative;
}
if (right) {
x = SPITouchScreenMovement_Positive;
}
if (down) {
y = SPITouchScreenMovement_Negative;
}
if (up) {
y = SPITouchScreenMovement_Positive;
}
MoveOnTouchScreen(x, y);
}

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(SPITouchScreenMovement x, SPITouchScreenMovement 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(SPITouchScreenMovement x, SPITouchScreenMovement y)
{
u16 sensitivityX = 4;
u16 sensitivityY = 8;

if (x == SPITouchScreenMovement_Negative) {
TouchX -= sensitivityX << 4;
}
if (x == SPITouchScreenMovement_Positive) {
TouchX += sensitivityX << 4;
}
if (y == SPITouchScreenMovement_Negative) {
TouchY -= sensitivityY << 4;
}
if (y == SPITouchScreenMovement_Positive) {
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
8 changes: 8 additions & 0 deletions src/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ enum
SPIDevice_MAX
};

enum SPITouchScreenMovement
{
SPITouchScreenMovement_Negative,
SPITouchScreenMovement_None,
SPITouchScreenMovement_Positive
};

u16 CRC16(const u8* data, u32 len, u32 start);

class SPIHost;
Expand Down Expand Up @@ -122,6 +129,7 @@ class TSC : public SPIDevice
virtual void DoSavestate(Savestate* file) override;

virtual void SetTouchCoords(u16 x, u16 y);
virtual void MoveTouchCoords(SPITouchScreenMovement x, SPITouchScreenMovement 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