Skip to content

Commit

Permalink
Merge pull request #5 from franklin654/User_Preferences
Browse files Browse the repository at this point in the history
Added partial support for key remapping. Thanks @franklin654.
  • Loading branch information
kitswas authored Dec 16, 2023
2 parents c54d64a + b78da90 commit 860a475
Show file tree
Hide file tree
Showing 30 changed files with 1,159 additions and 49 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ set(PROJECT_SOURCES
src/networking/executor.cpp
src/networking/executor.hpp
res/icons.qrc
src/preferences.h
src/preferences.cpp
src/preferences.ui
src/settings.h
src/settings.cpp
src/settings_key_variables.h
src/settings_key_variables.cpp
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
Expand Down
2 changes: 1 addition & 1 deletion VGP_Data_Exchange
12 changes: 12 additions & 0 deletions res/icons.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,17 @@
<file>logos/GamepadIcon.svg</file>
<file>logos/GamepadIcon_transparent.png</file>
<file>logos/SquareIcon.png</file>
<file>icons/icons8-down-30.png</file>
<file>icons/icons8-left-30.png</file>
<file>icons/icons8-right-30.png</file>
<file>icons/icons8-up-30.png</file>
<file>icons/icons8-xbox-a-30.png</file>
<file>icons/icons8-xbox-b-30.png</file>
<file>icons/icons8-xbox-lt-30.png</file>
<file>icons/icons8-xbox-rt-30.png</file>
<file>icons/icons8-xbox-x-30.png</file>
<file>icons/icons8-xbox-y-30.png</file>
<file>icons/icons8-xbox-menu-30.png</file>
<file>icons/icons8-xbox-windows-30.png</file>
</qresource>
</RCC>
Binary file added res/icons/icons8-down-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/icons/icons8-left-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/icons/icons8-right-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/icons/icons8-up-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/icons/icons8-xbox-a-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/icons/icons8-xbox-b-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/icons/icons8-xbox-lt-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/icons/icons8-xbox-menu-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/icons/icons8-xbox-rt-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/icons/icons8-xbox-windows-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/icons/icons8-xbox-x-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/icons/icons8-xbox-y-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#include "mainwindow.hpp"
#include "networking/server.hpp"

#include "settings.h"
#include "ui_mainwindow.h"
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
load_settings_file(this);
load_mouse_setting();
load_key_maps();
this->p = new Preferences(this);
ui->setupUi(this);
ui->settingsButton->connect(ui->settingsButton, &QPushButton::pressed, this, [=] { this->p->show(); });
}

MainWindow::~MainWindow()
Expand Down
2 changes: 2 additions & 0 deletions src/mainwindow.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "preferences.h"
#include <QMainWindow>

QT_BEGIN_NAMESPACE
Expand All @@ -22,4 +23,5 @@ class MainWindow : public QMainWindow

private:
Ui::MainWindow *ui;
Preferences *p = nullptr;
};
9 changes: 8 additions & 1 deletion src/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="settingsButton">
<property name="text">
<string>Preferences</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
Expand All @@ -37,7 +44,7 @@
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
<height>21</height>
</rect>
</property>
</widget>
Expand Down
31 changes: 24 additions & 7 deletions src/networking/executor.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "executor.hpp"
#include "../settings_key_variables.h"

#include <QApplication>

#define THRESHOLD 0.5
#define MOUSE_SENSITIVITY 1000

vgp_data_exchange_gamepad_reading parse_gamepad_state(const char *data, size_t len)
{
Expand Down Expand Up @@ -34,11 +34,28 @@ bool inject_gamepad_state(vgp_data_exchange_gamepad_reading reading)
{
if (reading.buttons_down & button)
{
keyDown(key);
if (key.is_mouse_key)
{ // checking if the input key is a mouse key.
if (key.vk == VK_LBUTTON)
{ // if it's a left mouse click execute a left click
leftClick();
}
else if (key.vk == VK_RBUTTON)
{ // if it's a right mouse click execute a right mouse
rightClick();
}

else
{ // else execute a middle mouse click.
middleClick();
}
}
else
keyDown(key.vk);
}
else if (reading.buttons_up & button)
{
keyUp(key);
keyUp(key.vk);
}
}

Expand Down Expand Up @@ -72,10 +89,10 @@ bool inject_gamepad_state(vgp_data_exchange_gamepad_reading reading)

// Use the right thumbstick to move the mouse
// if (abs(reading.right_thumbstick_x) > THRESHOLD || abs(reading.right_thumbstick_y) > THRESHOLD)
int offsetX = reading.right_thumbstick_x * MOUSE_SENSITIVITY;
int offsetY = reading.right_thumbstick_y * MOUSE_SENSITIVITY;
int scaleX = abs(offsetX) < (THRESHOLD * MOUSE_SENSITIVITY) ? 0 : 1;
int scaleY = abs(offsetY) < (THRESHOLD * MOUSE_SENSITIVITY) ? 0 : 1;
int offsetX = reading.right_thumbstick_x * mouse_sensivity;
int offsetY = reading.right_thumbstick_y * mouse_sensivity;
int scaleX = abs(offsetX) < (THRESHOLD * mouse_sensivity) ? 0 : 1;
int scaleY = abs(offsetY) < (THRESHOLD * mouse_sensivity) ? 0 : 1;

qDebug() << "Moving mouse by" << offsetX << ", " << offsetY;
for (int count = 1; count <= std::max(abs(offsetX), abs(offsetY)); ++count)
Expand Down
37 changes: 0 additions & 37 deletions src/networking/executor.hpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,7 @@
#pragma once

#include "../../VGP_Data_Exchange/C/Colfer.h"
#include "../../VGP_Data_Exchange/C/GameButtons.h"
#include "../simulation/simulate.hpp"

#include <map>

vgp_data_exchange_gamepad_reading parse_gamepad_state(const char *data, size_t len);
bool inject_gamepad_state(vgp_data_exchange_gamepad_reading reading);

// Map of gamepad buttons to keyboard keys
const std::map<GamepadButtons, WORD> GAMEPAD_BUTTONS = {{GamepadButtons::GamepadButtons_Menu, VK_MENU},
{GamepadButtons::GamepadButtons_View, VK_TAB},
{GamepadButtons::GamepadButtons_A, VK_RETURN},
{GamepadButtons::GamepadButtons_B, VK_ESCAPE},
{GamepadButtons::GamepadButtons_X, VK_SHIFT},
{GamepadButtons::GamepadButtons_Y, VK_CONTROL},
{GamepadButtons::GamepadButtons_DPadUp, VK_UP},
{GamepadButtons::GamepadButtons_DPadDown, VK_DOWN},
{GamepadButtons::GamepadButtons_DPadLeft, VK_LEFT},
{GamepadButtons::GamepadButtons_DPadRight, VK_RIGHT},
{GamepadButtons::GamepadButtons_LeftShoulder, 'E'},
{GamepadButtons::GamepadButtons_RightShoulder, VK_SPACE},
{GamepadButtons::GamepadButtons_LeftThumbstick, VK_PRIOR},
{GamepadButtons::GamepadButtons_RightThumbstick, VK_NEXT}};

enum Thumbstick
{
LeftThumbstickUp,
LeftThumbstickDown,
LeftThumbstickLeft,
LeftThumbstickRight,
RightThumbstickUp,
RightThumbstickDown,
RightThumbstickLeft,
RightThumbstickRight
};

const std::map<Thumbstick, WORD> THUMBSTICK_KEYS = {
{Thumbstick::LeftThumbstickUp, 'W'}, {Thumbstick::LeftThumbstickDown, 'S'},
{Thumbstick::LeftThumbstickLeft, 'A'}, {Thumbstick::LeftThumbstickRight, 'D'},
{Thumbstick::RightThumbstickUp, VK_UP}, {Thumbstick::RightThumbstickDown, VK_DOWN},
{Thumbstick::RightThumbstickLeft, VK_LEFT}, {Thumbstick::RightThumbstickRight, VK_RIGHT}};
3 changes: 2 additions & 1 deletion src/networking/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "../../VGP_Data_Exchange/C/Colfer.h"
#include "../../third-party-libs/QR-Code-generator/cpp/qrcodegen.hpp"
#include "../settings_key_variables.h"

#include <QByteArray>
#include <QDataStream>
Expand Down Expand Up @@ -73,7 +74,7 @@ Server::~Server()
void Server::initServer()
{
tcpServer->setListenBacklogSize(0);
if (!tcpServer->listen(QHostAddress::AnyIPv4, 7878))
if (!tcpServer->listen(QHostAddress::AnyIPv4, port))
{
QMessageBox::critical(this, tr("VGamepad Server"),
tr("Unable to start the server: %1.").arg(tcpServer->errorString()));
Expand Down
Loading

0 comments on commit 860a475

Please sign in to comment.