diff --git a/InputRedirectionClient-Qt.pro b/InputRedirectionClient-Qt.pro index 884b5f5..4295a1a 100644 --- a/InputRedirectionClient-Qt.pro +++ b/InputRedirectionClient-Qt.pro @@ -1,28 +1,45 @@ -#------------------------------------------------- -# -# Project created by QtCreator 2017-05-15T08:44:03 -# -#------------------------------------------------- - -QT += core gui network gamepad - -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets - -TARGET = InputRedirectionClient-Qt -TEMPLATE = app - -# The following define makes your compiler emit warnings if you use -# any feature of Qt which as been marked as deprecated (the exact warnings -# depend on your compiler). Please consult the documentation of the -# deprecated API in order to know how to port your code away from it. -DEFINES += QT_DEPRECATED_WARNINGS - -# You can also make your code fail to compile if you use deprecated APIs. -# In order to do so, uncomment the following line. -# You can also select to disable deprecated APIs only up to a certain version of Qt. -#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 - - -SOURCES += main.cpp - -#HEADERS += widget.h +#------------------------------------------------- +# +# Project created by QtCreator 2017-05-15T08:44:03 +# +#------------------------------------------------- + +QT += core gui network gamepad + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = InputRedirectionClient-Qt +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + + +SOURCES += main.cpp \ + global.cpp \ + gpmanager.cpp \ + touchscreen.cpp \ + configwindow.cpp \ + settings.cpp \ + shortcut.cpp + +#HEADERS += widget.h + +HEADERS += \ + global.h \ + gpmanager.h \ + mainwidget.h \ + touchscreen.h \ + configwindow.h \ + gpconfigurator.h \ + settings.h \ + tsshortcut.h \ + shortcut.h diff --git a/configwindow.cpp b/configwindow.cpp new file mode 100644 index 0000000..c3ba923 --- /dev/null +++ b/configwindow.cpp @@ -0,0 +1,344 @@ +#include "configwindow.h" +#include "gpmanager.h" + +ConfigWindow::ConfigWindow(QWidget *parent, TouchScreen *ts) : QDialog(parent) +{ + this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint); + this->setWindowTitle(tr("InputRedirectionClient-Qt - Button Config")); + + comboBoxA = populateItems(variantToButton(settings.value("ButtonA", QGamepadManager::ButtonA))); + comboBoxB = populateItems(variantToButton(settings.value("ButtonB", QGamepadManager::ButtonB))); + comboBoxX = populateItems(variantToButton(settings.value("ButtonX", QGamepadManager::ButtonX))); + comboBoxY = populateItems(variantToButton(settings.value("ButtonY", QGamepadManager::ButtonY))); + comboBoxUp = populateItems(variantToButton(settings.value("ButtonUp", QGamepadManager::ButtonUp))); + comboBoxDown = populateItems(variantToButton(settings.value("ButtonDown", QGamepadManager::ButtonDown))); + comboBoxLeft = populateItems(variantToButton(settings.value("ButtonLeft", QGamepadManager::ButtonLeft))); + comboBoxRight = populateItems(variantToButton(settings.value("ButtonRight", QGamepadManager::ButtonRight))); + comboBoxL = populateItems(variantToButton(settings.value("ButtonL", QGamepadManager::ButtonL1))); + comboBoxR = populateItems(variantToButton(settings.value("ButtonR", QGamepadManager::ButtonR1))); + comboBoxSelect = populateItems(variantToButton(settings.value("ButtonSelect", QGamepadManager::ButtonSelect))); + comboBoxStart = populateItems(variantToButton(settings.value("ButtonStart", QGamepadManager::ButtonStart))); + comboBoxZL = populateItems(variantToButton(settings.value("ButtonZL", QGamepadManager::ButtonL2))); + comboBoxZR = populateItems(variantToButton(settings.value("ButtonZR", QGamepadManager::ButtonR2))); + comboBoxHome = populateItems(variantToButton(settings.value("ButtonHome", QGamepadManager::ButtonInvalid))); + comboBoxPower = populateItems(variantToButton(settings.value("ButtonPower", QGamepadManager::ButtonInvalid))); + comboBoxPowerLong = populateItems(variantToButton(settings.value("ButtonPowerLong", QGamepadManager::ButtonInvalid))); + txtCppVal = new QLineEdit(); + txtStickVal = new QLineEdit(); + validator = new QIntValidator(); + + txtCppVal->setValidator(validator); + txtStickVal->setValidator(validator); + + txtCppVal->setText(tr("%1").arg(CPP_BOUND)); + txtStickVal->setText(tr("%1").arg(CPAD_BOUND)); + + txtCppVal->setClearButtonEnabled(true); + txtStickVal->setClearButtonEnabled(true); + + invertYCheckbox = new QCheckBox(this); + invertYCppCheckbox = new QCheckBox(this); + swapSticksCheckbox = new QCheckBox(this); + mhCameraCheckbox = new QCheckBox(this); + rsSmashCheckbox = new QCheckBox(this); + rsFaceButtonsCheckbox = new QCheckBox(); + disableCStickCheckbox = new QCheckBox(); + + saveButton = new QPushButton(tr("&SAVE"), this); + + layout = new QGridLayout(this); + + layout->addWidget(new QLabel("Y Button"), 0, 0); + layout->addWidget(comboBoxY, 0, 1); + layout->addWidget(new QLabel("X Button"), 0, 2); + layout->addWidget(comboBoxX, 0, 3); + layout->addWidget(new QLabel("B Button"), 1, 0); + layout->addWidget(comboBoxB, 1, 1); + layout->addWidget(new QLabel("A Button"), 1, 2); + layout->addWidget(comboBoxA, 1, 3); + + layout->addWidget(new QLabel("DPad-Down"), 2, 0); + layout->addWidget(comboBoxDown, 2, 1); + layout->addWidget(new QLabel("DPad-Up"), 2, 2); + layout->addWidget(comboBoxUp, 2, 3); + layout->addWidget(new QLabel("DPad-Left"), 3, 0); + layout->addWidget(comboBoxLeft, 3, 1); + layout->addWidget(new QLabel("DPad-Right"), 3, 2); + layout->addWidget(comboBoxRight, 3, 3); + + layout->addWidget(new QLabel("L Button"), 4, 0); + layout->addWidget(comboBoxL, 4, 1); + layout->addWidget(new QLabel("R Button"), 4, 2); + layout->addWidget(comboBoxR, 4, 3); + layout->addWidget(new QLabel("ZL Button"), 5, 0); + layout->addWidget(comboBoxZL, 5, 1); + layout->addWidget(new QLabel("ZR Button"), 5, 2); + layout->addWidget(comboBoxZR, 5, 3); + + layout->addWidget(new QLabel("Select"), 6, 0); + layout->addWidget(comboBoxSelect, 6, 1); + layout->addWidget(new QLabel("Start"), 6, 2); + layout->addWidget(comboBoxStart, 6, 3); + + layout->addWidget(new QLabel("Power Button"), 7, 0); + layout->addWidget(comboBoxPower, 7, 1); + layout->addWidget(new QLabel("Power-Long"), 7, 2); + layout->addWidget(comboBoxPowerLong, 7, 3); + layout->addWidget(new QLabel("Home Button"), 8, 0, 1, 2); + layout->addWidget(comboBoxHome, 8, 1, 1, 2); + layout->addWidget(new QLabel("Stick Range:"), 9, 0); + layout->addWidget(txtStickVal, 9, 1, 2, 2); + layout->addWidget(new QLabel("CPP Range:"), 11, 0); + layout->addWidget(txtCppVal, 11, 1, 2, 2); + + layout->addWidget(new QLabel("Invert Y axis"), 17, 0); + layout->addWidget(invertYCheckbox, 17, 1); + layout->addWidget(new QLabel("Invert CPP Y"), 17, 2); + layout->addWidget(invertYCppCheckbox, 17, 3); + layout->addWidget(new QLabel("Swap CPads"), 18, 2); + layout->addWidget(swapSticksCheckbox, 18, 3); + layout->addWidget(new QLabel("Disable C"), 18, 0); + layout->addWidget(disableCStickCheckbox, 18, 1); + + layout->addWidget(new QLabel("RS as DPad"), 19, 0); + layout->addWidget(mhCameraCheckbox, 19, 1); + layout->addWidget(new QLabel("RS as Smash"), 20, 0); + layout->addWidget(rsSmashCheckbox, 20, 1); + layout->addWidget(new QLabel("RS as ABXY"), 19, 2); + layout->addWidget(rsFaceButtonsCheckbox, 19, 3); + + layout->addWidget(saveButton, 21, 1, 1, 2); + + connect(invertYCheckbox, &QCheckBox::stateChanged, this, + [](int state) + { + switch(state) + { + case Qt::Unchecked: + yAxisMultiplier = 1; + settings.setValue("invertY", false); + break; + case Qt::Checked: + yAxisMultiplier = -1; + settings.setValue("invertY", true); + break; + default: break; + } + }); + + connect(invertYCppCheckbox, &QCheckBox::stateChanged, this, + [](int state) + { + switch(state) + { + case Qt::Unchecked: + yAxisMultiplierCpp = 1; + settings.setValue("invertCPPY", false); + break; + case Qt::Checked: + yAxisMultiplierCpp = -1; + settings.setValue("invertCPPY", true); + break; + default: break; + } + }); + + + connect(swapSticksCheckbox, &QCheckBox::stateChanged, this, + [](int state) + { + switch(state) + { + case Qt::Unchecked: + btnSettings.setShouldSwapStick(false); + settings.setValue("swapSticks", false); + break; + case Qt::Checked: + btnSettings.setShouldSwapStick(true); + settings.setValue("swapSticks", true); + break; + default: break; + } + + }); + + connect(rsSmashCheckbox, &QCheckBox::stateChanged, this, + [](int state) + { + switch(state) + { + case Qt::Unchecked: + btnSettings.setRightStickSmash(false); + settings.setValue("rightStickSmash", false); + break; + case Qt::Checked: + btnSettings.setRightStickSmash(true); + settings.setValue("rightStickSmash", true); + break; + default: break; + } + + }); + + connect(mhCameraCheckbox, &QCheckBox::stateChanged, this, + [](int state) + { + switch(state) + { + case Qt::Unchecked: + btnSettings.setMonsterHunterCamera(false); + settings.setValue("monsterHunterCamera", false); + break; + case Qt::Checked: + btnSettings.setMonsterHunterCamera(true); + settings.setValue("monsterHunterCamera", true); + break; + default: break; + } + }); + connect(disableCStickCheckbox, &QCheckBox::stateChanged, this, + [](int state) + { + switch(state) + { + case Qt::Unchecked: + btnSettings.setCStickDisabled(false); + settings.setValue("cStickDisable", false); + break; + case Qt::Checked: + btnSettings.setCStickDisabled(true); + settings.setValue("cStickDisable", true); + break; + default: break; + } + }); + connect(rsFaceButtonsCheckbox, &QCheckBox::stateChanged, this, + [](int state) + { + switch(state) + { + case Qt::Unchecked: + btnSettings.setRightStickFaceButtons(false); + settings.setValue("rightStickABXY", false); + break; + case Qt::Checked: + btnSettings.setRightStickFaceButtons(true); + settings.setValue("rightStickABXY", true); + break; + default: break; + } + }); + + connect(saveButton, &QPushButton::pressed, this, + [this, ts](void) + { + QGamepadManager::GamepadButton a = variantToButton(currentData(comboBoxA)); + hidButtonsAB[0] = a; + settings.setValue("ButtonA", a); + QGamepadManager::GamepadButton b = variantToButton(currentData(comboBoxB)); + hidButtonsAB[1] = b; + settings.setValue("ButtonB", b); + + QGamepadManager::GamepadButton select = variantToButton(currentData(comboBoxSelect)); + hidButtonsMiddle[0] = select; + settings.setValue("ButtonSelect", select); + QGamepadManager::GamepadButton start = variantToButton(currentData(comboBoxStart)); + hidButtonsMiddle[1] = start; + settings.setValue("ButtonStart", start); + QGamepadManager::GamepadButton right = variantToButton(currentData(comboBoxRight)); + hidButtonsMiddle[2] = right; + settings.setValue("ButtonRight", right); + QGamepadManager::GamepadButton left = variantToButton(currentData(comboBoxLeft)); + hidButtonsMiddle[3] = left; + settings.setValue("ButtonLeft", left); + QGamepadManager::GamepadButton up = variantToButton(currentData(comboBoxUp)); + hidButtonsMiddle[4] = up; + settings.setValue("ButtonUp", up); + QGamepadManager::GamepadButton down = variantToButton(currentData(comboBoxDown)); + hidButtonsMiddle[5] = down; + settings.setValue("ButtonDown", down); + QGamepadManager::GamepadButton r = variantToButton(currentData(comboBoxR)); + hidButtonsMiddle[6] = r; + settings.setValue("ButtonR", r); + QGamepadManager::GamepadButton l = variantToButton(currentData(comboBoxL)); + hidButtonsMiddle[7] = l; + settings.setValue("ButtonL", l); + + QGamepadManager::GamepadButton x = variantToButton(currentData(comboBoxX)); + hidButtonsXY[0] = x; + settings.setValue("ButtonX", x); + QGamepadManager::GamepadButton y = variantToButton(currentData(comboBoxY)); + hidButtonsXY[1] = y; + settings.setValue("ButtonY", y); + + QGamepadManager::GamepadButton zr = variantToButton(currentData(comboBoxZR)); + irButtons[0] = zr; + settings.setValue("ButtonZR", zr); + QGamepadManager::GamepadButton zl = variantToButton(currentData(comboBoxZL)); + irButtons[1] = zl; + settings.setValue("ButtonZL", zl); + + QGamepadManager::GamepadButton power = variantToButton(currentData(comboBoxPower)); + powerButton = power; + settings.setValue("ButtonPower", power); + QGamepadManager::GamepadButton powerLong = variantToButton(currentData(comboBoxPowerLong)); + powerLongButton = powerLong; + settings.setValue("ButtonPowerLong", powerLong); + QGamepadManager::GamepadButton home = variantToButton(currentData(comboBoxHome)); + homeButton = home; + settings.setValue("ButtonHome", home); + + CPP_BOUND = txtCppVal->text().toInt(); + CPAD_BOUND = txtStickVal->text().toInt(); + settings.setValue("StickBound", CPAD_BOUND); + settings.setValue("CppBound", CPP_BOUND); + + ts->updatePixmap(); + + }); + + invertYCheckbox->setChecked(settings.value("invertY", false).toBool()); + invertYCppCheckbox->setChecked(settings.value("invertCPPY", false).toBool()); + swapSticksCheckbox->setChecked(settings.value("swapSticks", false).toBool()); + mhCameraCheckbox->setChecked(settings.value("monsterHunterCamera", false).toBool()); + rsSmashCheckbox->setChecked(settings.value("rightStickSmash", false).toBool()); + disableCStickCheckbox->setChecked(settings.value("cStickDisable", false).toBool()); + rsFaceButtonsCheckbox->setChecked(settings.value("rightStickABXY", false).toBool()); +} + +QComboBox* ConfigWindow::populateItems(QGamepadManager::GamepadButton button) +{ + QComboBox *comboBox = new QComboBox(); + comboBox->addItem("A", QGamepadManager::ButtonA); + comboBox->addItem("B", QGamepadManager::ButtonB); + comboBox->addItem("X", QGamepadManager::ButtonX); + comboBox->addItem("Y", QGamepadManager::ButtonY); + comboBox->addItem("Up", QGamepadManager::ButtonUp); + comboBox->addItem("Down", QGamepadManager::ButtonDown); + comboBox->addItem("Right", QGamepadManager::ButtonRight); + comboBox->addItem("Left", QGamepadManager::ButtonLeft); + comboBox->addItem("LB", QGamepadManager::ButtonL1); + comboBox->addItem("RB", QGamepadManager::ButtonR1); + comboBox->addItem("LT", QGamepadManager::ButtonL2); + comboBox->addItem("RT", QGamepadManager::ButtonR2); + comboBox->addItem("Start", QGamepadManager::ButtonStart); + comboBox->addItem("Back", QGamepadManager::ButtonSelect); + comboBox->addItem("L3", QGamepadManager::ButtonL3); + comboBox->addItem("R3", QGamepadManager::ButtonR3); + comboBox->addItem("Guide", QGamepadManager::ButtonGuide); + comboBox->addItem("None", QGamepadManager::ButtonInvalid); + + int index = comboBox->findData(button); + comboBox->setCurrentIndex(index); + + return comboBox; +} + +QVariant ConfigWindow::currentData(QComboBox *comboBox) +{ + QVariant variant; + + variant = comboBox->itemData(comboBox->currentIndex()); + + return variant; +} diff --git a/configwindow.h b/configwindow.h new file mode 100644 index 0000000..24915b7 --- /dev/null +++ b/configwindow.h @@ -0,0 +1,36 @@ +#ifndef CONFIGWINDOW_H +#define CONFIGWINDOW_H + +#include "global.h" +#include "touchscreen.h" +#include + +class ConfigWindow : public QDialog +{ +private: + QGridLayout *layout; + QComboBox *comboBoxA, *comboBoxB, *comboBoxX, + *comboBoxY, *comboBoxL, *comboBoxR, + *comboBoxUp, *comboBoxDown, *comboBoxLeft, + *comboBoxRight, *comboBoxStart, *comboBoxSelect, + *comboBoxZL, *comboBoxZR, *comboBoxHome, + *comboBoxPower, *comboBoxPowerLong; + + QPushButton *saveButton; + + QCheckBox *invertYCheckbox, *invertYCppCheckbox, *swapSticksCheckbox, + *mhCameraCheckbox, *rsSmashCheckbox, + *disableCStickCheckbox, *rsFaceButtonsCheckbox; + + QComboBox* populateItems(QGamepadManager::GamepadButton button); + + QLineEdit *txtStickVal, *txtCppVal; + QValidator *validator; + + QVariant currentData(QComboBox *comboBox); + +public: + ConfigWindow(QWidget *parent = nullptr, TouchScreen *ts = nullptr); +}; + +#endif // CONFIGWINDOW_H diff --git a/global.cpp b/global.cpp new file mode 100644 index 0000000..13580b3 --- /dev/null +++ b/global.cpp @@ -0,0 +1,197 @@ +#include "global.h" +#include +#include "gpmanager.h" + +QSettings settings("TuxSH", "InputRedirectionClient-Qt"); + +Worker worker; +Settings btnSettings; +double tsRatio; + +std::vector listShortcuts; + +QGamepadManager::GamepadButtons buttons = 0; +u32 interfaceButtons = 0; +int yAxisMultiplier = 1, yAxisMultiplierCpp = 1; +bool shouldSwapStick = false; +int CPAD_BOUND = (settings.contains("StickBound") ? settings.value("StickBound").toInt() : 1488); +int CPP_BOUND = (settings.contains("CppBound") ? settings.value("CppBound").toInt() : 127); + +GamepadConfigurator *gpConfigurator; + +QString ipAddress; + +bool touchScreenPressed; +QSize touchScreenSize = QSize(TOUCH_SCREEN_WIDTH, TOUCH_SCREEN_HEIGHT); +QPoint touchScreenPosition; + +QGamepadManager::GamepadButton homeButton = variantToButton(settings.value("ButtonHome", QGamepadManager::ButtonInvalid)); +QGamepadManager::GamepadButton powerButton = variantToButton(settings.value("ButtonPower", QGamepadManager::ButtonInvalid)); +QGamepadManager::GamepadButton powerLongButton = variantToButton(settings.value("ButtonPowerLong", QGamepadManager::ButtonInvalid)); + +QGamepadManager::GamepadButton touchButton1 = variantToButton(settings.value("ButtonT1", QGamepadManager::ButtonInvalid)); +QGamepadManager::GamepadButton touchButton2 = variantToButton(settings.value("ButtonT2", QGamepadManager::ButtonInvalid)); +QGamepadManager::GamepadButton touchButton3 = variantToButton(settings.value("ButtonT3", QGamepadManager::ButtonInvalid)); +QGamepadManager::GamepadButton touchButton4 = variantToButton(settings.value("ButtonT4", QGamepadManager::ButtonInvalid)); + +TouchButton tbOne={.x=settings.value("touchButton1X").toInt(), + .y=settings.value("touchButton1Y").toInt()}; + +TouchButton tbTwo={.x=settings.value("touchButton2X").toInt(), + .y=settings.value("touchButton2Y").toInt()}; + +TouchButton tbThree={.x=settings.value("touchButton3X").toInt(), + .y=settings.value("touchButton3Y").toInt()}; + +TouchButton tbFour={.x=settings.value("touchButton4X").toInt(), + .y=settings.value("touchButton4Y").toInt()}; + +QGamepadManager::GamepadButton hidButtonsAB[2]={ +variantToButton(settings.value("ButtonA", QGamepadManager::ButtonA)), +variantToButton(settings.value("ButtonB", QGamepadManager::ButtonB))}; + +QGamepadManager::GamepadButton hidButtonsMiddle[8] ={ +variantToButton(settings.value("ButtonSelect", QGamepadManager::ButtonSelect)), +variantToButton(settings.value("ButtonStart", QGamepadManager::ButtonStart)), +variantToButton(settings.value("ButtonRight", QGamepadManager::ButtonRight)), +variantToButton(settings.value("ButtonLeft", QGamepadManager::ButtonLeft)), +variantToButton(settings.value("ButtonUp", QGamepadManager::ButtonUp)), +variantToButton(settings.value("ButtonDown", QGamepadManager::ButtonDown)), +variantToButton(settings.value("ButtonR", QGamepadManager::ButtonR1)), +variantToButton(settings.value("ButtonL", QGamepadManager::ButtonL1))}; + +QGamepadManager::GamepadButton hidButtonsXY[2] = { + variantToButton(settings.value("ButtonX", QGamepadManager::ButtonX)), + variantToButton(settings.value("ButtonY", QGamepadManager::ButtonY))}; + +QGamepadManager::GamepadButton irButtons[2] = { + variantToButton(settings.value("ButtonZR", QGamepadManager::ButtonR2)), + variantToButton(settings.value("ButtonZL", QGamepadManager::ButtonL2))}; + + +void Worker::setLeftAxis(double x, double y) +{ + leftAxis.x = x; + leftAxis.y = y; +} + +void Worker::setRightAxis(double x, double y) +{ + rightAxis.x = x; + rightAxis.y = y; +} + +void Worker::setPreviousLAxis(double x, double y) +{ + previousLeftAxis.x = x; + previousLeftAxis.y = y; +} + +MyAxis Worker::getLeftAxis() +{ + return leftAxis; +} + +MyAxis Worker::getRightAxis() +{ + return rightAxis; +} + +MyAxis Worker::getPreviousLAxis() +{ + return previousLeftAxis; +} + +void Worker::sendFrame(void) +{ + u32 hidPad = 0xfff; + for(u32 i = 0; i < 2; i++) + { + if(buttons & (1 << hidButtonsAB[i])) + hidPad &= ~(1 << i); + } + + for(u32 i = 2; i < 10; i++) + { + if(buttons & (1 << hidButtonsMiddle[i-2])) + hidPad &= ~(1 << i); + } + + for(u32 i = 10; i < 12; i++) + { + if(buttons & (1 << hidButtonsXY[i-10])) + hidPad &= ~(1 << i); + } + + u32 irButtonsState = 0; + for(u32 i = 0; i < 2; i++) + { + if(buttons & (1 << irButtons[i])) + irButtonsState |= 1 << (i + 1); + } + + u32 touchScreenState = 0x2000000; + u32 circlePadState = 0x7ff7ff; + u32 cppState = 0x80800081; + + if(touchScreenPressed) + { + + u32 x = (u32)(0xfff * std::min(std::max(0, touchScreenPosition.x()), + TOUCH_SCREEN_WIDTH*touchScreenPosition.x())) / touchScreenSize.width(); + u32 y = (u32)(0xfff * std::min(std::max(0, touchScreenPosition.y()), + TOUCH_SCREEN_HEIGHT*touchScreenPosition.y())) / touchScreenSize.height(); + + touchScreenState = (1 << 24) | (y << 12) | x; + } + + if(leftAxis.x != 0.0 || leftAxis.y != 0.0) + { + u32 x = (u32)(leftAxis.x * CPAD_BOUND + 0x800); + u32 y = (u32)(leftAxis.y * CPAD_BOUND + 0x800); + x = x >= 0xfff ? (leftAxis.x < 0.0 ? 0x000 : 0xfff) : x; + y = y >= 0xfff ? (leftAxis.y < 0.0 ? 0x000 : 0xfff) : y; + + circlePadState = (y << 12) | x; + } + + if(rightAxis.x != 0.0 || rightAxis.y != 0.0 || irButtonsState != 0) + { + // We have to rotate the c-stick position 45°. Thanks, Nintendo. + u32 x = (u32)(M_SQRT1_2 * (rightAxis.x + rightAxis.y) * CPP_BOUND + 0x80); + u32 y = (u32)(M_SQRT1_2 * (rightAxis.y - rightAxis.x) * CPP_BOUND + 0x80); + x = x >= 0xff ? (rightAxis.x < 0.0 ? 0x00 : 0xff) : x; + y = y >= 0xff ? (rightAxis.y < 0.0 ? 0x00 : 0xff) : y; + + cppState = (y << 24) | (x << 16) | (irButtonsState << 8) | 0x81; + } + + QByteArray ba(20, 0); + qToLittleEndian(hidPad, (uchar *)ba.data()); + qToLittleEndian(touchScreenState, (uchar *)ba.data() + 4); + qToLittleEndian(circlePadState, (uchar *)ba.data() + 8); + qToLittleEndian(cppState, (uchar *)ba.data() + 12); + qToLittleEndian(interfaceButtons, (uchar *)ba.data() + 16); + QUdpSocket().writeDatagram(ba, QHostAddress(ipAddress), 4950); +} + +QGamepadManager::GamepadButton variantToButton(QVariant variant) +{ + QGamepadManager::GamepadButton button; + + button = static_cast(variant.toInt()); + + return button; +} + +int appScreenTo3dsX(int posX) +{ + qDebug() << "PosX: " << posX; + return TOUCH_SCREEN_WIDTH*((touchScreenSize.height()*posX)/TOUCH_SCREEN_HEIGHT)/touchScreenSize.width(); +} + +int appScreenTo3dsY(int posY) +{ + qDebug() << "PosX: " << posY; + return TOUCH_SCREEN_HEIGHT*((touchScreenSize.width()*posY)/TOUCH_SCREEN_WIDTH)/touchScreenSize.height(); +} diff --git a/global.h b/global.h new file mode 100644 index 0000000..887d8d7 --- /dev/null +++ b/global.h @@ -0,0 +1,133 @@ +#ifndef GLOBAL_H +#define GLOBAL_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "gpconfigurator.h" +#include "settings.h" +#include "shortcut.h" + +#define TOUCH_SCREEN_WIDTH 320 +#define TOUCH_SCREEN_HEIGHT 240 + +extern int CPAD_BOUND; +extern int CPP_BOUND; + +extern Settings btnSettings; + +typedef uint32_t u32; +typedef uint16_t u16; +typedef uint8_t u8; + +int appScreenTo3dsX(int); +int appScreenTo3dsY(int); + +struct TouchButton +{ + int x, y; +}; + + +extern std::vector listShortcuts; +QGamepadManager::GamepadButton variantToButton(QVariant variant); +ShortCut variantToShortCut(QVariant variant); + +extern int id, fid; + +extern QSettings settings; + +extern QGamepadManager::GamepadButtons buttons; +extern u32 interfaceButtons; + +extern int yAxisMultiplier, yAxisMultiplierCpp; + +extern QString ipAddress; +extern bool timerEnabled; + +extern GamepadConfigurator *gpConfigurator; + +extern bool touchScreenPressed; +extern QSize touchScreenSize; +extern QPoint touchScreenPosition; +extern double tsRatio; + +extern QGamepadManager::GamepadButton homeButton; +extern QGamepadManager::GamepadButton powerButton; +extern QGamepadManager::GamepadButton powerLongButton; + +extern QGamepadManager::GamepadButton touchButton1; +extern QGamepadManager::GamepadButton touchButton2; +extern QGamepadManager::GamepadButton touchButton3; +extern QGamepadManager::GamepadButton touchButton4; + +extern TouchButton tbOne, tbTwo, tbThree, tbFour; + +extern QGamepadManager::GamepadButton hidButtonsAB[2]; +extern QGamepadManager::GamepadButton hidButtonsMiddle[8]; +extern QGamepadManager::GamepadButton hidButtonsXY[2]; +extern QGamepadManager::GamepadButton irButtons[2]; + + +struct MyAxis +{ + double x, y; +}; + +class Worker : public QObject { + Q_OBJECT + public: + + MyAxis getLeftAxis(); + MyAxis getRightAxis(); + MyAxis getPreviousLAxis(); + + void setLeftAxis(double x, double y); + void setRightAxis(double x, double y); + void setPreviousLAxis(double x, double y); + void closeThread() { emit finished(); } + + explicit Worker(QObject *parent = 0) : QObject(parent) + { + previousLeftAxis.x = leftAxis.x; + previousLeftAxis.y = leftAxis.y; + } + ~Worker() + { + + } + + public slots: + void sendFrame(); + + signals: + void finished(); + void error(QString err); + + private: + MyAxis leftAxis; + MyAxis rightAxis; + MyAxis previousLeftAxis; + +}; + +extern Worker worker; + +#endif // GLOBAL_H diff --git a/gpconfigurator.h b/gpconfigurator.h new file mode 100644 index 0000000..c28f8c1 --- /dev/null +++ b/gpconfigurator.h @@ -0,0 +1,218 @@ +#ifndef GPCONFIGURATOR_H +#define GPCONFIGURATOR_H + +#include "global.h" + +struct GamepadConfigurator : public QWidget { + +private: + QLabel* lblCurButton; + QFormLayout *formLayout; + QVBoxLayout* layout; + int devId; + int count; + +public: + QPushButton* skipButton, *resetConfigButton; + + GamepadConfigurator(QWidget *parent = 0) : QWidget(parent) + { + this->setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); + this->setWindowTitle(tr("Gamepad Configurator")); + + + layout = new QVBoxLayout(this); + formLayout = new QFormLayout(this); + lblCurButton = new QLabel(this); + skipButton = new QPushButton(tr("&Skip"), this); + resetConfigButton = new QPushButton(tr("&Reset Configuration"), this); + + count = 0; + + resetConfigButton->setEnabled(false); + skipButton->setEnabled(false); + + formLayout->addRow(lblCurButton); + lblCurButton->setText(QString("Press any button on the controller to continue...")); + layout->addLayout(formLayout); + layout->addWidget(skipButton); + layout->addWidget(resetConfigButton); + + setMinimumSize(300, 75); + + } + + void setCurDeviceId(int id) + { + devId = id; + } + + int getCurDeviceId() + { + return devId; + } + + int getCount() + { + return count; + } + + void next() + { + + getInput(devId, showDirections(QGamepadManager::ButtonInvalid)); + repaint(); + } + + QGamepadManager::GamepadButton showDirections(QGamepadManager::GamepadButton btn) + { + QGamepadManager::GamepadButton btnToMap = QGamepadManager::ButtonInvalid; + btnToMap = btn; + + + switch(count) + { + case 0: + lblCurButton->setText("Press Any Button To Continue..."); + btnToMap = QGamepadManager::ButtonA; + + return btnToMap; + break; + + case 1: + resetConfigButton->setEnabled(true); + skipButton->setEnabled(true); + + lblCurButton->setText("BUTTON A"); + btnToMap = QGamepadManager::ButtonB; + break; + + case 2: + lblCurButton->setText("BUTTON B"); + btnToMap = QGamepadManager::ButtonX; + break; + + case 3: + lblCurButton->setText("BUTTON X"); + btnToMap = QGamepadManager::ButtonY; + break; + + case 4: + lblCurButton->setText("BUTTON Y"); + btnToMap = QGamepadManager::ButtonR1; + break; + + case 5: + lblCurButton->setText("R1"); + btnToMap = QGamepadManager::ButtonR2; + break; + + case 6: + lblCurButton->setText("R2"); + btnToMap = QGamepadManager::ButtonR3; + break; + + case 7: + lblCurButton->setText("R3"); + btnToMap = QGamepadManager::ButtonL1; + break; + + case 8: + lblCurButton->setText("L1"); + btnToMap = QGamepadManager::ButtonL2; + break; + + case 9: + lblCurButton->setText("L2"); + btnToMap = QGamepadManager::ButtonL3; + break; + + case 10: + lblCurButton->setText("L3"); + btnToMap = QGamepadManager::ButtonUp; + break; + + case 11: + lblCurButton->setText("D Up"); + btnToMap = QGamepadManager::ButtonDown; + break; + + case 12: + lblCurButton->setText("D Down"); + btnToMap = QGamepadManager::ButtonLeft; + break; + + case 13: + lblCurButton->setText("D Left"); + btnToMap = QGamepadManager::ButtonRight; + break; + + case 14: + lblCurButton->setText("D Right"); + btnToMap = QGamepadManager::ButtonSelect; + break; + + case 15: + lblCurButton->setText("SELECT"); + btnToMap = QGamepadManager::ButtonStart; + break; + + case 16: + lblCurButton->setText("Start"); + btnToMap = QGamepadManager::ButtonGuide; + break; + + + case 17: + lblCurButton->setText("Guide Button"); + btnToMap = QGamepadManager::ButtonGuide; + break; + + case 18: + lblCurButton->setText("Done!!"); + break; + + default: + resetConfigButton->setEnabled(false); + skipButton->setEnabled(false); + + this->close(); + count = 0; + break; + } + + this->repaint(); + + return btnToMap; + } + + void getInput(int deviceId, QGamepadManager::GamepadButton btn) + { + QGamepadManager::GamepadButton btnToMap = QGamepadManager::ButtonInvalid; + btnToMap = showDirections(btn); + + devId = deviceId; + + while(!QGamepadManager::instance()->configureButton(devId, btnToMap)); + + count++; + + btnToMap = showDirections(btn); + } + + void showGui() + { + if(!this->isVisible()) + { + this->setVisible(true); + showDirections(QGamepadManager::ButtonInvalid); + } + } + + virtual ~GamepadConfigurator(void) + { + + } +}; + +#endif // GPCONFIGURATOR_H diff --git a/gpmanager.cpp b/gpmanager.cpp new file mode 100644 index 0000000..195a803 --- /dev/null +++ b/gpmanager.cpp @@ -0,0 +1,215 @@ +#include "gpmanager.h" + +GamepadMonitor::GamepadMonitor(QObject *parent) : QObject(parent) +{ + connect(QGamepadManager::instance(), &QGamepadManager::gamepadButtonPressEvent, this, + [this](int deviceId, QGamepadManager::GamepadButton button, double value) + { + (void)deviceId; + (void)value; + buttons |= QGamepadManager::GamepadButtons(1 << button); + + if (button == homeButton) + { + interfaceButtons |= 1; + } + if (button == powerButton) + { + interfaceButtons |= 2; + } + if (button == powerLongButton) + { + interfaceButtons |= 4; + } + for (auto it = listShortcuts.begin(); it != listShortcuts.end(); ++it) { + int index = std::distance(listShortcuts.begin(), it); + ShortCut curShort = listShortcuts.at(index); + + if(curShort.button == button) + { + touchScreenPressed = true; + touchScreenPosition = curShort.pos*tsRatio; + } + } + + }); + + connect(QGamepadManager::instance(), &QGamepadManager::gamepadButtonReleaseEvent, this, + [this](int deviceId, QGamepadManager::GamepadButton button) + { + (void)deviceId; + buttons &= QGamepadManager::GamepadButtons(~(1 << button)); + + if (button == homeButton) + { + interfaceButtons &= ~1; + } + if (button == powerButton) + { + interfaceButtons &= ~2; + } + if (button == powerLongButton) + { + interfaceButtons &= ~4; + } + + + for (auto it = listShortcuts.begin(); it != listShortcuts.end(); ++it) { + int index = std::distance(listShortcuts.begin(), it); + ShortCut curShort = listShortcuts.at(index); + + if(curShort.button == button) + { + touchScreenPressed = false; + return; + } + } + + }); + + connect(QGamepadManager::instance(), &QGamepadManager::gamepadAxisEvent, this, + [this](int deviceId, QGamepadManager::GamepadAxis axis, double value) + { + (void)deviceId; + (void)value; + QGamepadManager::GamepadAxis axLeftX = QGamepadManager::AxisLeftX, + axLeftY= QGamepadManager::AxisLeftY, + axRightX= QGamepadManager::AxisRightX, + axRightY= QGamepadManager::AxisRightY; + + if(btnSettings.isShouldSwapStick()) + { + axLeftX = QGamepadManager::AxisRightX; + axLeftY = QGamepadManager::AxisRightY; + + axRightX = QGamepadManager::AxisLeftX; + axRightY = QGamepadManager::AxisLeftY; + } + + if(axis==axLeftX) + { + worker.setLeftAxis(value, worker.getLeftAxis().y); + worker.setPreviousLAxis(worker.getLeftAxis().x, worker.getPreviousLAxis().y); + } + else + if(axis==axLeftY) + { + worker.setLeftAxis(worker.getLeftAxis().x, yAxisMultiplier * -value); // for some reason qt inverts this + worker.setPreviousLAxis(worker.getPreviousLAxis().x, worker.getLeftAxis().y); + } + else + if(axis==axRightX) + { + if (!btnSettings.isCStickDisabled()) worker.setRightAxis(value, worker.getRightAxis().y); + + if (btnSettings.isMonsterHunterCamera()) + { + if (value > -1.2 && value < -0.5) // RS tilted left + { + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsMiddle[3]); // press Left + } else if (value > 0.5 && value < 1.2) // RS tilted right + { + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsMiddle[2]); // press Right + } else { // RS neutral, release buttons + buttons &= QGamepadManager::GamepadButtons(~(1 << hidButtonsMiddle[3])); // Release Left + buttons &= QGamepadManager::GamepadButtons(~(1 << hidButtonsMiddle[2])); // release Right + } + } + if (btnSettings.isRightStickFaceButtons()) + { + if (value > -1.2 && value < -0.5) // RS tilted left + { + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsXY[1]); // press Y + } else if (value > 0.5 && value < 1.2) // RS tilted right + { + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsAB[0]); // press A + } else { // RS neutral, release buttons + buttons &= QGamepadManager::GamepadButtons(~(1 << hidButtonsXY[1])); // Release Y + buttons &= QGamepadManager::GamepadButtons(~(1 << hidButtonsAB[0])); // release A + } + } + if (btnSettings.isRightStickSmash()) + { + if (value > -1.2 && value < -0.5) // RS tilted left + { + btnSettings.setSmashingH(true); + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsAB[0]); // press A + worker.setLeftAxis(-1.2, worker.getLeftAxis().y); + } else if (value > 0.5 && value < 1.2) // RS tilted right + { + btnSettings.setSmashingH(true); + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsAB[0]); // press A + worker.setLeftAxis(1.2, worker.getLeftAxis().y); + } else { // RS neutral, release buttons + if (btnSettings.isSmashingH()) + { + if (!btnSettings.isSmashingV()) + buttons &= QGamepadManager::GamepadButtons(~(1 << hidButtonsAB[0])); // Release A + worker.setLeftAxis(worker.getPreviousLAxis().x, worker.getRightAxis().y); + btnSettings.setSmashingH(false); + } + } + } + } + else + if(axis==axRightY) + { + worker.setRightAxis(worker.getRightAxis().x, yAxisMultiplierCpp * -value); + + if (btnSettings.isMonsterHunterCamera()) + { + if (worker.getRightAxis().y > -1.2 && worker.getRightAxis().y < -0.5) // RS tilted down + { + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsMiddle[5]); // press Down + } else if (worker.getRightAxis().y > 0.5 && worker.getRightAxis().y < 1.2) // RS tilted up + { + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsMiddle[4]); // press Up + } else { // RS neutral, release buttons + buttons &= QGamepadManager::GamepadButtons(~(1 << hidButtonsMiddle[5])); // release Down + buttons &= QGamepadManager::GamepadButtons(~(1 << hidButtonsMiddle[4])); // Release Up + } + } + if (btnSettings.isRightStickFaceButtons()) + { + if (worker.getRightAxis().y > -1.2 && worker.getRightAxis().y < -0.5) // RS tilted down + { + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsAB[1]); // press B + } else if (worker.getRightAxis().y > 0.5 && worker.getRightAxis().y < 1.2) // RS tilted up + { + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsXY[0]); // press X + } else { // RS neutral, release buttons + buttons &= QGamepadManager::GamepadButtons(~(1 << hidButtonsAB[1])); // release B + buttons &= QGamepadManager::GamepadButtons(~(1 << hidButtonsXY[0])); // Release X + } + } + if (btnSettings.isRightStickSmash()) + { + if (worker.getRightAxis().y > -1.2 && worker.getRightAxis().y < -0.5) // RS tilted down + { + btnSettings.setSmashingV(true); + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsAB[0]); // press A + worker.setLeftAxis(worker.getLeftAxis().x, -1.2); + } else if (worker.getRightAxis().y > 0.5 && worker.getRightAxis().y < 1.2) // RS tilted up + { + btnSettings.setSmashingV(true); + buttons |= QGamepadManager::GamepadButtons(1 << hidButtonsAB[0]); // press A + worker.setLeftAxis(worker.getLeftAxis().x, 1.2); + } else { // RS neutral, release button A + if (btnSettings.isSmashingV()) + { + if (!btnSettings.isSmashingH()) + buttons &= QGamepadManager::GamepadButtons(~(1 << hidButtonsAB[0])); // Release A + worker.setLeftAxis(worker.getLeftAxis().x, worker.getPreviousLAxis().y); + btnSettings.setSmashingV(false); + } + } + } + if (btnSettings.isCStickDisabled()) + { + worker.setRightAxis(0.0, 0.0); + } + } + }); +} + + diff --git a/gpmanager.h b/gpmanager.h new file mode 100644 index 0000000..56d3c3b --- /dev/null +++ b/gpmanager.h @@ -0,0 +1,13 @@ +#ifndef GPMANAGER_H +#define GPMANAGER_H + +#include +#include + +#include "global.h" + +struct GamepadMonitor : public QObject { + GamepadMonitor(QObject *parent); +}; + +#endif // GPMANAGER_H diff --git a/main.cpp b/main.cpp index ba3089d..0fa4a7f 100644 --- a/main.cpp +++ b/main.cpp @@ -2,465 +2,31 @@ #define _USE_MATH_DEFINES #endif -#include -#include -#include -#include -#include -#include -#include +#include "mainwidget.h" +#include "gpmanager.h" #include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#define CPAD_BOUND 0x5d0 -#define CPP_BOUND 0x7f - -#define TOUCH_SCREEN_WIDTH 320 -#define TOUCH_SCREEN_HEIGHT 240 - -typedef uint32_t u32; -typedef uint16_t u16; -typedef uint8_t u8; - -double lx = 0.0, ly = 0.0; -double rx = 0.0, ry = 0.0; -QGamepadManager::GamepadButtons buttons = 0; -u32 interfaceButtons = 0; -QString ipAddress; -int yAxisMultiplier = 1; -bool abInverse = false; -bool xyInverse = false; - -bool touchScreenPressed; -QPoint touchScreenPosition; - -QSettings settings("TuxSH", "InputRedirectionClient-Qt"); - -void sendFrame(void) -{ - static const QGamepadManager::GamepadButton hidButtonsAB[] = { - QGamepadManager::ButtonA, - QGamepadManager::ButtonB, - }; - - static const QGamepadManager::GamepadButton hidButtonsMiddle[] = { - QGamepadManager::ButtonSelect, - QGamepadManager::ButtonStart, - QGamepadManager::ButtonRight, - QGamepadManager::ButtonLeft, - QGamepadManager::ButtonUp, - QGamepadManager::ButtonDown, - QGamepadManager::ButtonR1, - QGamepadManager::ButtonL1, - }; - - static const QGamepadManager::GamepadButton hidButtonsXY[] = { - QGamepadManager::ButtonX, - QGamepadManager::ButtonY, - }; - - static const QGamepadManager::GamepadButton irButtons[] = { - QGamepadManager::ButtonR2, - QGamepadManager::ButtonL2, - }; - - static const QGamepadManager::GamepadButton speButtons[] = { - QGamepadManager::ButtonL3, - QGamepadManager::ButtonR3, - QGamepadManager::ButtonGuide, - }; - - u32 hidPad = 0xfff; - if(!abInverse) - { - for(u32 i = 0; i < 2; i++) - { - if(buttons & (1 << hidButtonsAB[i])) - hidPad &= ~(1 << i); - } - } - else - { - for(u32 i = 0; i < 2; i++) - { - if(buttons & (1 << hidButtonsAB[1-i])) - hidPad &= ~(1 << i); - } - } - - for(u32 i = 2; i < 10; i++) - { - if(buttons & (1 << hidButtonsMiddle[i-2])) - hidPad &= ~(1 << i); - } - - if(!xyInverse) - { - for(u32 i = 10; i < 12; i++) - { - if(buttons & (1 << hidButtonsXY[i-10])) - hidPad &= ~(1 << i); - } - } - else - { - for(u32 i = 10; i < 12; i++) - { - if(buttons & (1 << hidButtonsXY[1-(i-10)])) - hidPad &= ~(1 << i); - } - } - - u32 irButtonsState = 0; - for(u32 i = 0; i < 2; i++) - { - if(buttons & (1 << irButtons[i])) - irButtonsState |= 1 << (i + 1); - } - - u32 specialButtonsState = 0; - for(u32 i = 0; i < 3; i++) - { - - if(buttons & (1 << speButtons[i])) - specialButtonsState |= 1 << i; - } - specialButtonsState |= interfaceButtons; - - u32 touchScreenState = 0x2000000; - u32 circlePadState = 0x7ff7ff; - u32 cppState = 0x80800081; - - if(lx != 0.0 || ly != 0.0) - { - u32 x = (u32)(lx * CPAD_BOUND + 0x800); - u32 y = (u32)(ly * CPAD_BOUND + 0x800); - x = x >= 0xfff ? (lx < 0.0 ? 0x000 : 0xfff) : x; - y = y >= 0xfff ? (ly < 0.0 ? 0x000 : 0xfff) : y; - - circlePadState = (y << 12) | x; - } - - if(rx != 0.0 || ry != 0.0 || irButtonsState != 0) - { - // We have to rotate the c-stick position 45°. Thanks, Nintendo. - u32 x = (u32)(M_SQRT1_2 * (rx + ry) * CPP_BOUND + 0x80); - u32 y = (u32)(M_SQRT1_2 * (ry - rx) * CPP_BOUND + 0x80); - x = x >= 0xff ? (rx < 0.0 ? 0x00 : 0xff) : x; - y = y >= 0xff ? (ry < 0.0 ? 0x00 : 0xff) : y; - - cppState = (y << 24) | (x << 16) | (irButtonsState << 8) | 0x81; - } - - if(touchScreenPressed) - { - u32 x = (u32)(0xfff * std::min(std::max(0, touchScreenPosition.x()), TOUCH_SCREEN_WIDTH)) / TOUCH_SCREEN_WIDTH; - u32 y = (u32)(0xfff * std::min(std::max(0, touchScreenPosition.y()), TOUCH_SCREEN_HEIGHT)) / TOUCH_SCREEN_HEIGHT; - touchScreenState = (1 << 24) | (y << 12) | x; - } - - QByteArray ba(20, 0); - qToLittleEndian(hidPad, (uchar *)ba.data()); - qToLittleEndian(touchScreenState, (uchar *)ba.data() + 4); - qToLittleEndian(circlePadState, (uchar *)ba.data() + 8); - qToLittleEndian(cppState, (uchar *)ba.data() + 12); - qToLittleEndian(specialButtonsState, (uchar *)ba.data() + 16); - QUdpSocket().writeDatagram(ba, QHostAddress(ipAddress), 4950); -} - -struct GamepadMonitor : public QObject { - - GamepadMonitor(QObject *parent = nullptr) : QObject(parent) - { - connect(QGamepadManager::instance(), &QGamepadManager::gamepadButtonPressEvent, this, - [](int deviceId, QGamepadManager::GamepadButton button, double value) - { - (void)deviceId; - (void)value; - buttons |= QGamepadManager::GamepadButtons(1 << button); - sendFrame(); - }); - - connect(QGamepadManager::instance(), &QGamepadManager::gamepadButtonReleaseEvent, this, - [](int deviceId, QGamepadManager::GamepadButton button) - { - (void)deviceId; - buttons &= QGamepadManager::GamepadButtons(~(1 << button)); - sendFrame(); - }); - connect(QGamepadManager::instance(), &QGamepadManager::gamepadAxisEvent, this, - [](int deviceId, QGamepadManager::GamepadAxis axis, double value) - { - (void)deviceId; - (void)value; - switch(axis) - { - case QGamepadManager::AxisLeftX: - lx = value; - break; - case QGamepadManager::AxisLeftY: - ly = yAxisMultiplier * -value; // for some reason qt inverts this - break; - - case QGamepadManager::AxisRightX: - rx = value; - break; - case QGamepadManager::AxisRightY: - ry = yAxisMultiplier * -value; // for some reason qt inverts this - break; - default: break; - } - sendFrame(); - }); - } -}; - -struct TouchScreen : public QDialog { - TouchScreen(QWidget *parent = nullptr) : QDialog(parent) - { - this->setFixedSize(TOUCH_SCREEN_WIDTH, TOUCH_SCREEN_HEIGHT); - this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint); - this->setWindowTitle(tr("InputRedirectionClient-Qt - Touch screen")); - } - - void mousePressEvent(QMouseEvent *ev) - { - if(ev->button() == Qt::LeftButton) - { - touchScreenPressed = true; - touchScreenPosition = ev->pos(); - sendFrame(); - } - } - - void mouseMoveEvent(QMouseEvent *ev) - { - if(touchScreenPressed && (ev->buttons() & Qt::LeftButton)) - { - touchScreenPosition = ev->pos(); - sendFrame(); - } - } - - void mouseReleaseEvent(QMouseEvent *ev) - { - if(ev->button() == Qt::LeftButton) - { - touchScreenPressed = false; - sendFrame(); - } - } - - void closeEvent(QCloseEvent *ev) - { - touchScreenPressed = false; - sendFrame(); - ev->accept(); - } -}; - -struct FrameTimer : public QTimer { - FrameTimer(QObject *parent = nullptr) : QTimer(parent) - { - connect(this, &QTimer::timeout, this, - [](void) - { - sendFrame(); - }); - } -}; - - -class Widget : public QWidget -{ -private: - QVBoxLayout *layout; - QFormLayout *formLayout; - QLineEdit *addrLineEdit; - QCheckBox *invertYCheckbox, *invertABCheckbox, *invertXYCheckbox; - QPushButton *homeButton, *powerButton, *longPowerButton; - TouchScreen *touchScreen; -public: - Widget(QWidget *parent = nullptr) : QWidget(parent) - { - layout = new QVBoxLayout(this); - - addrLineEdit = new QLineEdit(this); - addrLineEdit->setClearButtonEnabled(true); - - invertYCheckbox = new QCheckBox(this); - invertABCheckbox = new QCheckBox(this); - invertXYCheckbox = new QCheckBox(this); - formLayout = new QFormLayout; - - formLayout->addRow(tr("IP &address"), addrLineEdit); - formLayout->addRow(tr("&Invert Y axis"), invertYCheckbox); - formLayout->addRow(tr("Invert A<->&B"), invertABCheckbox); - formLayout->addRow(tr("Invert X<->&Y"), invertXYCheckbox); - - homeButton = new QPushButton(tr("&HOME"), this); - powerButton = new QPushButton(tr("&POWER"), this); - longPowerButton = new QPushButton(tr("POWER (&long)"), this); - - layout->addLayout(formLayout); - layout->addWidget(homeButton); - layout->addWidget(powerButton); - layout->addWidget(longPowerButton); - - connect(addrLineEdit, &QLineEdit::textChanged, this, - [](const QString &text) - { - ipAddress = text; - settings.setValue("ipAddress", text); - }); - - connect(invertYCheckbox, &QCheckBox::stateChanged, this, - [](int state) - { - switch(state) - { - case Qt::Unchecked: - yAxisMultiplier = 1; - settings.setValue("invertY", false); - break; - case Qt::Checked: - yAxisMultiplier = -1; - settings.setValue("invertY", true); - break; - default: break; - } - }); - - connect(invertABCheckbox, &QCheckBox::stateChanged, this, - [](int state) - { - switch(state) - { - case Qt::Unchecked: - abInverse = false; - settings.setValue("invertAB", false); - break; - case Qt::Checked: - abInverse = true; - settings.setValue("invertAB", true); - break; - default: break; - } - }); - - connect(invertXYCheckbox, &QCheckBox::stateChanged, this, - [](int state) - { - switch(state) - { - case Qt::Unchecked: - xyInverse = false; - settings.setValue("invertXY", false); - break; - case Qt::Checked: - xyInverse = true; - settings.setValue("invertXY", true); - break; - default: break; - } - }); - - connect(homeButton, &QPushButton::pressed, this, - [](void) - { - interfaceButtons |= 1; - sendFrame(); - }); - - connect(homeButton, &QPushButton::released, this, - [](void) - { - interfaceButtons &= ~1; - sendFrame(); - }); - - connect(powerButton, &QPushButton::pressed, this, - [](void) - { - interfaceButtons |= 2; - sendFrame(); - }); - - connect(powerButton, &QPushButton::released, this, - [](void) - { - interfaceButtons &= ~2; - sendFrame(); - }); - - connect(longPowerButton, &QPushButton::pressed, this, - [](void) - { - interfaceButtons |= 4; - sendFrame(); - }); - - connect(longPowerButton, &QPushButton::released, this, - [](void) - { - interfaceButtons &= ~4; - sendFrame(); - }); - - touchScreen = new TouchScreen(nullptr); - this->setWindowTitle(tr("InputRedirectionClient-Qt")); - - addrLineEdit->setText(settings.value("ipAddress", "").toString()); - invertYCheckbox->setChecked(settings.value("invertY", false).toBool()); - invertABCheckbox->setChecked(settings.value("invertAB", false).toBool()); - invertXYCheckbox->setChecked(settings.value("invertXY", false).toBool()); - } - - void show(void) - { - QWidget::show(); - touchScreen->show(); - } - - void closeEvent(QCloseEvent *ev) - { - touchScreen->close(); - ev->accept(); - } - - virtual ~Widget(void) - { - lx = ly = rx = ry = 0.0; - buttons = 0; - interfaceButtons = 0; - touchScreenPressed = false; - sendFrame(); - delete touchScreen; - } - -}; - +#include int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; GamepadMonitor m(&w); - FrameTimer t(&w); - TouchScreen ts; - t.start(50); - w.show(); - return a.exec(); + w.show(); + QThread* thread = new QThread(); + QTimer timer; + timer.setInterval(20); + + timer.moveToThread(thread); + worker.moveToThread(thread); + + QObject::connect(thread, SIGNAL (started()), &timer, SLOT (start())); + QObject::connect(&worker, SIGNAL (finished()), thread, SLOT (quit())); + QObject::connect(thread, SIGNAL (finished()), thread, SLOT (deleteLater())); + QObject::connect(&timer, SIGNAL(timeout()), &worker, SLOT(sendFrame())); + thread->start(); + a.exec(); + worker.closeThread(); + return 0; } diff --git a/mainwidget.h b/mainwidget.h new file mode 100644 index 0000000..2a3dafe --- /dev/null +++ b/mainwidget.h @@ -0,0 +1,267 @@ +#ifndef MAINWIDGET_H +#define MAINWIDGET_H + +#include "global.h" + +#include +#include + +#include "touchscreen.h" +#include "configwindow.h" +#include "gpconfigurator.h" + + +class Widget : public QWidget +{ + +private slots: + void ShowContextMenu(const QPoint &pos); +private: + QVBoxLayout *layout; + QFormLayout *formLayout; + QPushButton *homeButton, *powerButton, *longPowerButton, *settingsConfigButton, + *clearImageButton, *configGamepadButton; + QLineEdit *addrLineEdit; + QSlider *touchOpacitySlider; + ConfigWindow *settingsConfig; + +public: + TouchScreen *touchScreen; + + Widget(QWidget *parent = nullptr) : QWidget(parent) + { + layout = new QVBoxLayout(this); + + addrLineEdit = new QLineEdit(this); + addrLineEdit->setClearButtonEnabled(true); + + formLayout = new QFormLayout(); + formLayout->addRow(tr("IP &address"), addrLineEdit); + + touchOpacitySlider = new QSlider(Qt::Horizontal); + touchOpacitySlider->setRange(0, 10); + touchOpacitySlider->setValue(10); + touchOpacitySlider->setTickInterval(1); + formLayout->addRow(tr("TS &Opacity"), touchOpacitySlider); + + homeButton = new QPushButton(tr("&Home"), this); + powerButton = new QPushButton(tr("&Power"), this); + longPowerButton = new QPushButton(tr("Power (&long)"), this); + settingsConfigButton = new QPushButton(tr("&Settings"), this); + clearImageButton = new QPushButton(tr("&Clear Image"), this); + configGamepadButton = new QPushButton(tr("&Configure Custom Gamepad")); + + setContextMenuPolicy(Qt::CustomContextMenu); + + // Disable/hide the configurator button if running windows since it's not supported + if (QSysInfo::productType() == "windows" || + QSysInfo::productType() == "osx") + { + configGamepadButton->setEnabled(false); + configGamepadButton->setVisible(false); + } + + layout->addLayout(formLayout); + layout->addWidget(homeButton); + layout->addWidget(powerButton); + layout->addWidget(longPowerButton); + layout->addWidget(configGamepadButton); + layout->addWidget(settingsConfigButton); + layout->addWidget(clearImageButton); + + gpConfigurator = new GamepadConfigurator(); + + connect(addrLineEdit, &QLineEdit::textChanged, this, + [](const QString &text) + { + ipAddress = text; + settings.setValue("ipAddress", text); + }); + + connect(configGamepadButton, &QPushButton::released, this, + [](void) + { + gpConfigurator->showGui(); + }); + + connect(homeButton, &QPushButton::pressed, this, + [](void) + { + interfaceButtons |= 1; + }); + + connect(homeButton, &QPushButton::released, this, + [](void) + { + interfaceButtons &= ~1; + }); + + connect(powerButton, &QPushButton::pressed, this, + [](void) + { + interfaceButtons |= 2; + }); + + connect(QGamepadManager::instance(), &QGamepadManager::gamepadButtonReleaseEvent, this, + [](int deviceId, QGamepadManager::GamepadButton button) + { + (void)deviceId; + + gpConfigurator->setCurDeviceId(deviceId); + + if(gpConfigurator->isVisible()) + { + gpConfigurator->getInput(deviceId, button); + return; + } + + buttons &= QGamepadManager::GamepadButtons(~(1 << button)); + }); + + connect(gpConfigurator->skipButton, &QPushButton::released, this, + [](void) + { + gpConfigurator->next(); + }); + + + connect(gpConfigurator->resetConfigButton, &QPushButton::released, this, + [](void) + { + QMessageBox *msgBox = new QMessageBox(0); + QGamepadManager::instance()->resetConfiguration(gpConfigurator->getCurDeviceId()); + + msgBox->setText("Reset"); + msgBox->setInformativeText("Please restart the program for changes to take affect."); + msgBox->show(); + + }); + + connect(powerButton, &QPushButton::released, this, + [](void) + { + interfaceButtons &= ~2; + }); + + connect(longPowerButton, &QPushButton::pressed, this, + [](void) + { + interfaceButtons |= 4; + }); + + connect(longPowerButton, &QPushButton::released, this, + [](void) + { + interfaceButtons &= ~4; + }); + + connect(settingsConfigButton, &QPushButton::released, this, + [this](void) + { + if (!settingsConfig->isVisible()) + { + settingsConfig->move(this->x() - settingsConfig->width() - 5,this->y()); + settingsConfig->show(); + } else settingsConfig->hide(); + }); + + connect(clearImageButton, &QPushButton::released, this, + [this](void) + { + touchScreen->clearImage(); + }); + + connect(touchOpacitySlider, &QSlider::valueChanged, this, + [this](int value) + { + touchScreen->setWindowOpacity(value / 10.0); + touchScreen->update(); + }); + + touchScreen = new TouchScreen(nullptr); + settingsConfig = new ConfigWindow(nullptr, touchScreen); + this->setWindowTitle(tr("InputRedirectionClient-Qt")); + + addrLineEdit->setText(settings.value("ipAddress", "").toString()); + } + + void show(void) + { + QWidget::show(); + touchScreen->move(this->x() + this->width() + 5,this->y()); + touchScreen->show(); + settingsConfig->hide(); + } + + //When closing, save shortcuts + void closeEvent(QCloseEvent *ev) + { + //Save shortcuts + unsigned int i = 0; + for (i=0; iclose(); + settingsConfig->close(); + + touchScreen->setTouchScreenPressed(false); + delete touchScreen; + ev->accept(); + } + + //Move touchscreen window with main window if moved + void moveEvent(QMoveEvent *event) + { + touchScreen->move(touchScreen->pos() + (event->pos() - event->oldPos())); + } + + //When main window is opened, load shortcut settings + void showEvent(QShowEvent* event) + { + qRegisterMetaType("ShortCut"); + qRegisterMetaTypeStreamOperators("ShortCut"); + + QString valName = "tsShortcut0"; + for(int i = 0; settings.contains(valName); i++) + { + valName = tr("tsShortcut%1").arg(i); + QVariant variant = settings.value(valName); + + ShortCut curShort = variant.value(); + if(variant.isValid() && curShort.name != "") + { + if(curShort.name != "") + { + listShortcuts.push_back(curShort); + } + + } + else + { + settings.remove(tr("tsShortcut%1").arg(i)); + } + + } + } + + virtual ~Widget(void) + { + worker.setLeftAxis(0.0, 0.0); + worker.setRightAxis(0.0, 0.0); + + buttons = 0; + interfaceButtons = 0; + + delete settingsConfig; + } +}; + +#endif // MAINWIDGET_H diff --git a/settings.cpp b/settings.cpp new file mode 100644 index 0000000..f7b77d3 --- /dev/null +++ b/settings.cpp @@ -0,0 +1,72 @@ +#include "settings.h" + +bool Settings::isShouldSwapStick() +{ + return shouldSwapStick; +} + +bool Settings::isMonsterHunterCamera() +{ + return monsterHunterCamera; +} + +bool Settings::isRightStickSmash() +{ + return rightStickSmash; +} + +bool Settings::isSmashingV() +{ + return smashingV; +} + +bool Settings::isSmashingH() +{ + return smashingH; +} + +bool Settings::isRightStickFaceButtons() +{ + return rightStickFaceButtons; +} + +bool Settings::isCStickDisabled() +{ + return cStickDisabled; +} + +/***SETTERS***/ +void Settings::setShouldSwapStick(bool b) +{ + shouldSwapStick = b; +} + +void Settings::setMonsterHunterCamera(bool b) +{ + monsterHunterCamera = b; +} + +void Settings::setRightStickSmash(bool b) +{ + rightStickSmash = b; +} + +void Settings::setSmashingV(bool b) +{ + smashingV = b; +} + +void Settings::setSmashingH(bool b) +{ + smashingH = b; +} + +void Settings::setRightStickFaceButtons(bool b) +{ + rightStickFaceButtons = b; +} + +void Settings::setCStickDisabled(bool b) +{ + cStickDisabled = b; +} diff --git a/settings.h b/settings.h new file mode 100644 index 0000000..4e4ee12 --- /dev/null +++ b/settings.h @@ -0,0 +1,39 @@ +#ifndef SETTINGS_H +#define SETTINGS_H + +class Settings +{ + private: + bool shouldSwapStick; + bool monsterHunterCamera; + bool rightStickSmash; + bool smashingV; + bool smashingH; + bool rightStickFaceButtons; + bool cStickDisabled; + + public: + Settings() + { + shouldSwapStick = false; + } + + bool isShouldSwapStick(); + bool isMonsterHunterCamera(); + bool isRightStickSmash(); + bool isSmashingV(); + bool isSmashingH(); + bool isRightStickFaceButtons(); + bool isCStickDisabled(); + + + void setShouldSwapStick(bool); + void setMonsterHunterCamera(bool); + void setRightStickSmash(bool); + void setSmashingV(bool); + void setSmashingH(bool); + void setRightStickFaceButtons(bool); + void setCStickDisabled(bool); +}; + +#endif // SETTINGS_H diff --git a/shortcut.cpp b/shortcut.cpp new file mode 100644 index 0000000..45201ef --- /dev/null +++ b/shortcut.cpp @@ -0,0 +1,179 @@ +#include "shortcut.h" +#include +#include + +qint8 getButtonId(QGamepadManager::GamepadButton btn) +{ + qint8 button = 0; + if(btn == QGamepadManager::ButtonInvalid) + { + button = 0; + } + if(btn == QGamepadManager::ButtonA) + { + button = 1; + } + + if(btn == QGamepadManager::ButtonB) + { + button = 2; + } + if(btn == QGamepadManager::ButtonX) + { + button = 3; + } + if(btn == QGamepadManager::ButtonY) + { + button = 4; + } + if(btn == QGamepadManager::ButtonL1) + { + button = 5; + } + if(btn == QGamepadManager::ButtonL2) + { + button = 6; + } + + if(btn == QGamepadManager::ButtonL3) + { + button = 7; + } + if(btn == QGamepadManager::ButtonR1) + { + button = 8; + } + if(btn == QGamepadManager::ButtonR2) + { + button = 9; + } + if(btn == QGamepadManager::ButtonR3) + { + button = 10; + } + if(btn == QGamepadManager::ButtonUp) + { + button = 11; + } + if(btn == QGamepadManager::ButtonDown) + { + button = 12; + } + if(btn == QGamepadManager::ButtonLeft) + { + button = 13; + } + if(btn == QGamepadManager::ButtonRight) + { + button = 14; + } + if(btn == QGamepadManager::ButtonSelect) + { + button = 15; + } + if(btn == QGamepadManager::ButtonStart) + { + button = 16; + } + if(btn == QGamepadManager::ButtonGuide) + { + button = 17; + } + + return button; +} + +QGamepadManager::GamepadButton getIdButton(int btn) +{ + QGamepadManager::GamepadButton button; + if(btn == 0) + { + button = QGamepadManager::ButtonInvalid; + } + if(btn == 1) + { + button = QGamepadManager::ButtonA; + } + + if(btn == 2) + { + button = QGamepadManager::ButtonB; + } + if(btn == 3) + { + button = QGamepadManager::ButtonX; + } + if(btn == 4) + { + button = QGamepadManager::ButtonY; + } + + if(btn == 5) + { + button = QGamepadManager::ButtonL1; + } + if(btn == 6) + { + button = QGamepadManager::ButtonL2; + } + + if(btn == 7) + { + button = QGamepadManager::ButtonL3; + } + if(btn == 8) + { + button = QGamepadManager::ButtonR1; + } + if(btn == 9) + { + button = QGamepadManager::ButtonR2; + } + if(btn == 10) + { + button = QGamepadManager::ButtonR3; + } + if(btn == 11) + { + button = QGamepadManager::ButtonUp; + } + if(btn == 12) + { + button = QGamepadManager::ButtonDown; + } + if(btn == 13) + { + button = QGamepadManager::ButtonLeft; + } + if(btn == 14) + { + button = QGamepadManager::ButtonRight; + } + if(btn == 15) + { + button = QGamepadManager::ButtonSelect; + } + if(btn == 16) + { + button = QGamepadManager::ButtonStart; + } + if(btn == 17) + { + button = QGamepadManager::ButtonGuide; + } + + return button; +} + +QDataStream &operator<<(QDataStream &out, const ShortCut &obj) +{ + out << obj.name << obj.pos << obj.color << getIdButton(obj.button); + return out; +} + +QDataStream &operator>>(QDataStream &in, ShortCut &obj) +{ + qint8 id = getButtonId(obj.button); + in >> obj.name >> obj.pos >> obj.color >> id; + return in; +} diff --git a/shortcut.h b/shortcut.h new file mode 100644 index 0000000..f177941 --- /dev/null +++ b/shortcut.h @@ -0,0 +1,22 @@ +#ifndef SHORTCUT_H +#define SHORTCUT_H +#include +#include +#include +#include +#include +struct ShortCut + +{ + QString name; + QGamepadManager::GamepadButton button; + QPoint pos; + QColor color; + +};Q_DECLARE_METATYPE(ShortCut) + +extern QDataStream &operator<<(QDataStream &out, const ShortCut &obj); +extern QDataStream &operator>>(QDataStream &in, ShortCut &obj); + + +#endif // SHORTCUT_H diff --git a/touchscreen.cpp b/touchscreen.cpp new file mode 100644 index 0000000..886f75b --- /dev/null +++ b/touchscreen.cpp @@ -0,0 +1,223 @@ +#include "touchscreen.h" +#include "global.h" +#include + +void TouchScreen::setTouchScreenPressed(bool b) +{ + touchScreenPressed = b; +} + +bool TouchScreen::isTouchScreenPressed() +{ + return touchScreenPressed; +} + +QSize TouchScreen::getTouchScreenSize() +{ + return touchScreenSize; +} + +QPoint TouchScreen::getTouchScreenPosition() +{ + return touchScreenPosition; +} + +TouchScreen::TouchScreen(QWidget *parent) : QWidget(parent) +{ + this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMinimizeButtonHint); + this->setWindowTitle(tr("InputRedirectionClient-Qt - Touch screen")); + this->setContextMenuPolicy(Qt::CustomContextMenu); + + connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), + this, SLOT(ShowContextMenu(const QPoint&))); + + bgLabel = new QLabel(this); + + updatePixmap(); + setMinimumWidth(TOUCH_SCREEN_WIDTH); + setMinimumHeight(TOUCH_SCREEN_HEIGHT); + + bgLabel->setScaledContents(true); + bgLabel->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored ); + + ellipNeedDraw = true; +} + +void TouchScreen::ShowContextMenu(const QPoint& pos) +{ + QMenu* myMenu = new QMenu(); + QString strOpenOverlay = "Open Overlay Image..."; + QString clearOverlayBtn = "Clear Overlay"; + QString strPtToBtn = "Set point to button..."; + QPoint globalPos = this->mapToGlobal(pos); + + myMenu->addAction(strOpenOverlay); + myMenu->addAction(clearOverlayBtn); + myMenu->addSeparator(); + myMenu->addAction(strPtToBtn); + + myMenu->popup(globalPos); + QAction* selectedItem = myMenu->exec(globalPos); + + QPoint newPos; + newPos.setX(pos.x()); + newPos.setY(pos.y()); + //If custom size, scale the point to orig 3ds touchscreen size + if(this->width() != TOUCH_SCREEN_WIDTH || + this->height() != TOUCH_SCREEN_HEIGHT) + { + newPos.setX((TOUCH_SCREEN_WIDTH*pos.x())/this->width()); + newPos.setY((TOUCH_SCREEN_HEIGHT*pos.y())/this->height()); + } + + //Update the shortcut gui position title text + tsShortcutGui.setCurrentPos(newPos); + + if(!selectedItem) + { + myMenu->close(); + return; + } + + if(selectedItem->text() == strOpenOverlay) + { + QString strPic = QFileDialog::getOpenFileName(this, + tr("Open Touchscreen Image (320x240)"), "MyDocuments", + tr("Image Files (*.jpg *.jpeg *.png *.bmp *.gif *.pbm *.pgm *.ppm *.xbm *.xpm)")); + + if(!strPic.isNull()) + { + settings.setValue("tsBackgroundImage", strPic); + updatePixmap(); + } + } + + if(selectedItem->text() == strPtToBtn ) + { + if(tsShortcutGui.isVisible()) + { + tsShortcutGui.updateTitleText(); + } + + tsShortcutGui.setVisible(true); + + } + + if(selectedItem->text() == clearOverlayBtn) + { + clearImage(); + } + + myMenu->deleteLater(); +} + +void TouchScreen::resizeEvent(QResizeEvent* e) +{ + QSize newWinSize = e->size(); + QSize curWinSize = e->oldSize(); + QSize propWinSize = e->size(); + + if(curWinSize.height() != newWinSize.height()) + { + propWinSize.setWidth((TOUCH_SCREEN_WIDTH*newWinSize.height())/TOUCH_SCREEN_HEIGHT); + propWinSize.setHeight(newWinSize.height()); + } + + if(curWinSize.width() != newWinSize.width()) + { + propWinSize.setWidth(newWinSize.width()); + propWinSize.setHeight((TOUCH_SCREEN_HEIGHT*newWinSize.width())/TOUCH_SCREEN_WIDTH); + } + + touchScreenSize = propWinSize; + this->resize(propWinSize); + bgLabel->setFixedHeight(this->height()); + bgLabel->setFixedWidth(this->width()); + + tsRatio = (double)this->width() / (double)TOUCH_SCREEN_WIDTH; +} + +void TouchScreen::mousePressEvent(QMouseEvent *ev) +{ + if(ev->button() == Qt::LeftButton) + { + touchScreenPressed = true; + touchScreenPosition = ev->pos(); + } +} + +void TouchScreen::mouseMoveEvent(QMouseEvent *ev) +{ + if(touchScreenPressed && (ev->buttons() & Qt::LeftButton)) + { + touchScreenPosition = ev->pos(); + } +} + +void TouchScreen::mouseReleaseEvent(QMouseEvent *ev) +{ + if(ev->button() == Qt::LeftButton) + { + touchScreenPressed = false; + } +} + +void TouchScreen::closeEvent(QCloseEvent *ev) +{ + touchScreenPressed = false; + ev->accept(); +} + +void TouchScreen::updatePixmap(void) +{ + QString strPic = settings.value("tsBackgroundImage", "").toString(); + QPixmap newPic(strPic); + + if (newPic.isNull()) + { + newPic = QPixmap(TOUCH_SCREEN_WIDTH, TOUCH_SCREEN_HEIGHT); + newPic.fill(Qt::transparent); + } +} + + +void TouchScreen::paintEvent(QPaintEvent* e) +{ + QString strPic = settings.value("tsBackgroundImage", "").toString(); + QPixmap newPic(strPic); + + if (newPic.isNull()) + { + newPic = QPixmap(TOUCH_SCREEN_WIDTH, TOUCH_SCREEN_HEIGHT); + newPic.fill(Qt::transparent); + } + + + QPainter painter; + painter.begin(&newPic); + painter.setBrush(QBrush(Qt::black)); + painter.setRenderHint(QPainter::Antialiasing, true); + + for (unsigned i=0; iheight()*curShort.pos.x())/TOUCH_SCREEN_HEIGHT)/this->width(), + TOUCH_SCREEN_HEIGHT*((this->width()*curShort.pos.y())/TOUCH_SCREEN_WIDTH)/this->height(), + (3*this->width())/TOUCH_SCREEN_WIDTH, + (3*this->height())/TOUCH_SCREEN_HEIGHT); + + } + + bgLabel->setPixmap(newPic); + bgLabel->show(); + e->accept(); +} + +void TouchScreen::clearImage(void) +{ + settings.setValue("tsBackgroundImage", ""); + updatePixmap(); +} + diff --git a/touchscreen.h b/touchscreen.h new file mode 100644 index 0000000..d896185 --- /dev/null +++ b/touchscreen.h @@ -0,0 +1,46 @@ +#include "global.h" +#include "gpmanager.h" +#include "tsshortcut.h" +#include + +#ifndef TOUCHSCREEN_H +#define TOUCHSCREEN_H + +#include "tsshortcut.h" + +struct TouchScreen : public QWidget { + Q_OBJECT +public slots: + void ShowContextMenu(const QPoint& pos); + +private: + QLabel *bgLabel; + TsShortcut tsShortcutGui; + +public: + TouchScreen(QWidget *parent = nullptr); + + bool ellipNeedDraw; + + void resizeEvent(QResizeEvent* e); + void mousePressEvent(QMouseEvent *ev); + void mouseMoveEvent(QMouseEvent *ev); + void mouseReleaseEvent(QMouseEvent *ev); + void closeEvent(QCloseEvent *ev); + + QPoint getTouchScreenPosition(); + QSize getTouchScreenSize(); + bool isTouchScreenPressed(); + void setTouchScreenPressed(bool b); + void updatePixmap(void); + void clearImage(void); + void paintEvent(QPaintEvent* e); + + ~TouchScreen(void) + { + qDebug() << "DECON"; + tsShortcutGui.close(); + } +}; + +#endif // TOUCHSCREEN_H diff --git a/tsshortcut.h b/tsshortcut.h new file mode 100644 index 0000000..ff56205 --- /dev/null +++ b/tsshortcut.h @@ -0,0 +1,211 @@ +#ifndef TSSHORTCUT_H +#define TSSHORTCUT_H +#include "global.h" +#include +#include +#include +#include +#include +#include + +class TsShortcut : public QWidget +{ +private: + QString wTitle; + QVBoxLayout *layout; + QFormLayout *formLayout; + QListWidget *lstWidget; + QLabel *lblDirections; + QPushButton *btnColorDialog, *btnCreateShort, + *btnDelShort, *btnHelp, *btnPressNow; + QLineEdit* txtShortName; + QPoint curPos; + QComboBox* cboxBtns; + QColor curColor; + + +public: + TsShortcut(QWidget *parent = nullptr) : QWidget(parent) + { + layout = new QVBoxLayout(this); + formLayout= new QFormLayout(this); + + lblDirections = new QLabel(this); + lstWidget = new QListWidget(this); + btnColorDialog = new QPushButton(this); + btnCreateShort = new QPushButton(this); + btnPressNow = new QPushButton(this); + btnDelShort = new QPushButton(this); + btnHelp = new QPushButton(this); + txtShortName = new QLineEdit(this); + cboxBtns = populateItems(); + + lblDirections->setText("Select a shortcut, then press a button to map it."); + btnColorDialog->setText("Choose &Color"); + btnPressNow->setText("Press Selected &Shortcut"); + btnCreateShort->setText("&Create"); + btnDelShort->setText("&Delete"); + btnHelp->setText("&Help"); + + layout->addWidget(lblDirections); + layout->addWidget(lstWidget); + layout->addWidget(txtShortName); + layout->addWidget(cboxBtns); + + layout->addWidget(btnColorDialog); + layout->addWidget(btnCreateShort); + layout->addSpacing(10); + layout->addWidget(btnPressNow); + layout->addWidget(btnDelShort); + layout->addWidget(btnHelp); + + this->setMinimumSize(300, 200); + this->setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); + this->setVisible(false); + + connect(btnColorDialog, &QPushButton::released, this, + [this](void) + { + QColor color = QColorDialog::getColor(Qt::yellow, this ); + if( color.isValid() ) + { + curColor = color; + QString qss = QString("background-color: %1").arg(color.name()); + btnColorDialog->setStyleSheet(qss); + + } + }); + + connect(btnCreateShort, &QPushButton::released, this, + [this](void) + { + ShortCut newShortCut; + QString newShortName = txtShortName->text(); + + if(newShortName != "") + { + newShortCut.name = newShortName; + + newShortCut.button =variantToButton(cboxBtns->currentData()); + newShortCut.pos = curPos; + newShortCut.color = curColor; + + if(!curColor.isValid()) + { + newShortCut.color = Qt::blue; + } + + lstWidget->addItem(newShortName); + listShortcuts.push_back(newShortCut); + txtShortName->clear(); + btnColorDialog->setStyleSheet(""); + curColor = nullptr; + } + else + { + QMessageBox *msgBox = new QMessageBox(0); + msgBox->setInformativeText(tr("Cannot create new shortcut without a name.")); + msgBox->show(); + } + }); + + connect(btnDelShort, &QPushButton::released, this, + [this](void) + { + if(lstWidget->selectedItems().size() != 0) + { + listShortcuts.erase(listShortcuts.begin()+(lstWidget->currentRow())); + qDeleteAll(lstWidget->selectedItems()); + } + }); + + connect(btnPressNow, &QPushButton::pressed, this, + [this](void) + { + if(lstWidget->selectedItems().size() != 0) + { + touchScreenPressed = true; + touchScreenPosition = listShortcuts[lstWidget->currentRow()].pos*tsRatio; + } + + }); + + connect(btnPressNow, &QPushButton::released, this, + [this](void) + { + touchScreenPressed = false; + }); + + + connect(btnHelp, &QPushButton::released, this, + [this](void) + { + QMessageBox *msgBox = new QMessageBox(0); + + msgBox->setText("Map Touchpad to Button"); + msgBox->setInformativeText(tr("1. Right-click touchpad in the position you want then open this menu.\n\ + 2. Type a name for your shortcut in the textbox.\n\ + 3. Choose a button on the gamepad to map this point to.\n\ + 4. Choose a color for your shortcut, (this will the circle's color\ + on the touchpad window\ + 5. Press create, then close this window")); + msgBox->show(); + + }); + + + } + + void setCurrentPos(QPoint pos) + { + curPos = pos; + } + + void updateTitleText() + { + wTitle = QString("Current X: %1 Y: %2").arg(QString::number(curPos.x())).arg(QString::number(curPos.y())); + this->setWindowTitle(wTitle); + } + + void showEvent(QShowEvent * event) + { + lstWidget->clear(); + for (unsigned int i=0; iaddItem(curName); + } + + updateTitleText(); + event->accept(); + } + + + QComboBox* populateItems() + { + QComboBox *comboBox = new QComboBox(); + comboBox->addItem("None", QGamepadManager::ButtonInvalid); + comboBox->addItem("A", QGamepadManager::ButtonA); + comboBox->addItem("B", QGamepadManager::ButtonB); + comboBox->addItem("X", QGamepadManager::ButtonX); + comboBox->addItem("Y", QGamepadManager::ButtonY); + comboBox->addItem("Up", QGamepadManager::ButtonUp); + comboBox->addItem("Down", QGamepadManager::ButtonDown); + comboBox->addItem("Right", QGamepadManager::ButtonRight); + comboBox->addItem("Left", QGamepadManager::ButtonLeft); + comboBox->addItem("LB", QGamepadManager::ButtonL1); + comboBox->addItem("RB", QGamepadManager::ButtonR1); + comboBox->addItem("LT", QGamepadManager::ButtonL2); + comboBox->addItem("RT", QGamepadManager::ButtonR2); + comboBox->addItem("Start", QGamepadManager::ButtonStart); + comboBox->addItem("Back", QGamepadManager::ButtonSelect); + comboBox->addItem("L3", QGamepadManager::ButtonL3); + comboBox->addItem("R3", QGamepadManager::ButtonR3); + comboBox->addItem("Guide", QGamepadManager::ButtonGuide); + + return comboBox; + } + +}; + +#endif // TSSHORTCUT_H