From 46807a9feea303144884f2865ac37f55c54fdb5a Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 1 Jan 2014 10:48:33 +0100 Subject: joystick: don't be a phonebook --- .../ftnoir_tracker_joystick.cpp | 22 +--- ftnoir_tracker_joystick/ftnoir_tracker_joystick.h | 49 +++++--- .../ftnoir_tracker_joystick_dialog.cpp | 134 ++++++--------------- .../ftnoir_tracker_joystick_dll.cpp | 29 +---- 4 files changed, 71 insertions(+), 163 deletions(-) (limited to 'ftnoir_tracker_joystick') diff --git a/ftnoir_tracker_joystick/ftnoir_tracker_joystick.cpp b/ftnoir_tracker_joystick/ftnoir_tracker_joystick.cpp index 5be6b3db..28fc034a 100644 --- a/ftnoir_tracker_joystick/ftnoir_tracker_joystick.cpp +++ b/ftnoir_tracker_joystick/ftnoir_tracker_joystick.cpp @@ -10,24 +10,24 @@ static BOOL CALLBACK EnumJoysticksCallback2( const DIDEVICEINSTANCE* pdidInstanc self->def = *pdidInstance; - return self->iter++ == self->joyid ? DIENUM_STOP : DIENUM_CONTINUE; + return self->iter++ == self->s.joyid ? DIENUM_STOP : DIENUM_CONTINUE; } FTNoIR_Tracker::FTNoIR_Tracker() : g_pDI(nullptr), g_pJoystick(nullptr), - joyid(-1), iter(-1), mtx(QMutex::Recursive) { for (int i = 0; i < 6; i++) - axes[i] = min_[i] = max_[i] = 0; + *s.axes[i] = min_[i] = max_[i] = 0; GUID bar = {0}; preferred = bar; } void FTNoIR_Tracker::reload() { + s.b->reload(); QMutexLocker foo(&mtx); if (g_pJoystick) { @@ -100,7 +100,6 @@ void FTNoIR_Tracker::StartTracker(QFrame* frame) QMutexLocker foo(&mtx); this->frame = frame; iter = 0; - loadSettings(); auto hr = CoInitialize( nullptr ); DI_ENUM_CONTEXT enumContext = {0}; @@ -218,7 +217,7 @@ void FTNoIR_Tracker::GetHeadPoseData(double *data) for (int i = 0; i < 6; i++) { - auto idx = axes[i] - 1; + auto idx = *s.axes[i] - 1; if (idx < 0 || idx > 7) { data[i] = 0; @@ -234,19 +233,6 @@ void FTNoIR_Tracker::GetHeadPoseData(double *data) } } -void FTNoIR_Tracker::loadSettings() { - - QMutexLocker foo(&mtx); - QSettings settings("opentrack"); // Registry settings (in HK_USER) - QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString(); - QSettings iniFile( currentFile, QSettings::IniFormat ); // Application settings (in INI-file) - iniFile.beginGroup ( "tracker-joy" ); - joyid = iniFile.value("joyid", -1).toInt(); - for (int i = 0; i < 6; i++) - axes[i] = iniFile.value(QString("axis-%1").arg(i), 0).toInt() - 1; - iniFile.endGroup (); -} - extern "C" FTNOIR_TRACKER_BASE_EXPORT ITracker* CALLING_CONVENTION GetConstructor() { return new FTNoIR_Tracker; diff --git a/ftnoir_tracker_joystick/ftnoir_tracker_joystick.h b/ftnoir_tracker_joystick/ftnoir_tracker_joystick.h index 67291e6f..162cbe48 100644 --- a/ftnoir_tracker_joystick/ftnoir_tracker_joystick.h +++ b/ftnoir_tracker_joystick/ftnoir_tracker_joystick.h @@ -24,10 +24,12 @@ #include #include #include -//#include #include #include +#include "facetracknoir/options.h" +using namespace options; + struct DI_ENUM_CONTEXT { GUID preferred_instance; @@ -35,29 +37,48 @@ struct DI_ENUM_CONTEXT LPDIRECTINPUT8 g_pDI; }; +struct settings { + pbundle b; + value axis_0; + value axis_1; + value axis_2; + value axis_3; + value axis_4; + value axis_5; + value joyid; + value* axes[6]; + settings() : + b(bundle("tracker-joystick")), + axis_0(b, "axis-0", 0), + axis_1(b, "axis-1", 0), + axis_2(b, "axis-2", 0), + axis_3(b, "axis-3", 0), + axis_4(b, "axis-4", 0), + axis_5(b, "axis-5", 0), + joyid(b, "joy-id", 0), + axes{&axis_0, &axis_1, &axis_2, &axis_3, &axis_4, &axis_5} + {} +}; + class FTNoIR_Tracker : public ITracker { public: FTNoIR_Tracker(); ~FTNoIR_Tracker(); - void StartTracker(QFrame *frame); void GetHeadPoseData(double *data); - void loadSettings(); + void reload(); LPDIRECTINPUT8 g_pDI; LPDIRECTINPUTDEVICE8 g_pJoystick; - int axes[6]; int min_[8], max_[8]; GUID preferred; - int joyid; QMutex mtx; QFrame* frame; DIDEVICEINSTANCE def; - void reload(); int iter; // XXX bad style + settings s; }; -// Widget that has controls for FTNoIR protocol client-settings. class TrackerControls: public QWidget, public ITrackerDialog { Q_OBJECT @@ -71,32 +92,20 @@ public: } QList guids; Ui::UIJoystickControls ui; - void loadSettings(); - void save(); - bool settingsDirty; FTNoIR_Tracker* tracker; - + settings s; private slots: void doOK(); void doCancel(); - void settingChanged() { settingsDirty = true; } - void settingChanged(int) { settingsDirty = true; } }; -//******************************************************************************************************* -// FaceTrackNoIR Tracker DLL. Functions used to get general info on the Tracker -//******************************************************************************************************* class FTNoIR_TrackerDll : public Metadata { public: - FTNoIR_TrackerDll(); - ~FTNoIR_TrackerDll(); - void getFullName(QString *strToBeFilled); void getShortName(QString *strToBeFilled); void getDescription(QString *strToBeFilled); void getIcon(QIcon *icon); - private: QString trackerFullName; // Trackers' name and description QString trackerShortName; diff --git a/ftnoir_tracker_joystick/ftnoir_tracker_joystick_dialog.cpp b/ftnoir_tracker_joystick/ftnoir_tracker_joystick_dialog.cpp index 42ca8689..812ad454 100644 --- a/ftnoir_tracker_joystick/ftnoir_tracker_joystick_dialog.cpp +++ b/ftnoir_tracker_joystick/ftnoir_tracker_joystick_dialog.cpp @@ -11,22 +11,21 @@ static BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance return DIENUM_CONTINUE; } -TrackerControls::TrackerControls() : - QWidget(), tracker(nullptr), settingsDirty(false) +TrackerControls::TrackerControls() : tracker(nullptr) { - ui.setupUi( this ); + ui.setupUi( this ); - // Connect Qt signals to member-functions + // Connect Qt signals to member-functions connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(doOK())); connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(doCancel())); - connect(ui.joylist, SIGNAL(currentIndexChanged(int)), this, SLOT(settingChanged(int))); - connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(settingChanged(int))); - connect(ui.comboBox_2, SIGNAL(currentIndexChanged(int)), this, SLOT(settingChanged(int))); - connect(ui.comboBox_3, SIGNAL(currentIndexChanged(int)), this, SLOT(settingChanged(int))); - connect(ui.comboBox_4, SIGNAL(currentIndexChanged(int)), this, SLOT(settingChanged(int))); - connect(ui.comboBox_5, SIGNAL(currentIndexChanged(int)), this, SLOT(settingChanged(int))); - connect(ui.comboBox_6, SIGNAL(currentIndexChanged(int)), this, SLOT(settingChanged(int))); + tie_setting(s.joyid, ui.joylist); + tie_setting(s.axis_0, ui.comboBox); + tie_setting(s.axis_1, ui.comboBox_2); + tie_setting(s.axis_2, ui.comboBox_3); + tie_setting(s.axis_3, ui.comboBox_4); + tie_setting(s.axis_4, ui.comboBox_5); + tie_setting(s.axis_5, ui.comboBox_6); { auto hr = CoInitialize( nullptr ); @@ -46,107 +45,42 @@ fin: if (g_pDI) g_pDI->Release(); } - - loadSettings(); } void TrackerControls::doOK() { - save(); - this->close(); + s.b->save(); + if (tracker) + tracker->reload(); + this->close(); } void TrackerControls::doCancel() { - if (settingsDirty) { - int ret = QMessageBox::question ( this, "Settings have changed", "Do you want to save the settings?", QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Discard ); + if (s.b->modifiedp()) { + int ret = QMessageBox::question ( this, "Settings have changed", "Do you want to save the settings?", QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel ); switch (ret) { - case QMessageBox::Save: - save(); - this->close(); - break; - case QMessageBox::Discard: - this->close(); - break; - case QMessageBox::Cancel: - // Cancel was clicked - break; - default: - // should never be reached - break; - } - } - else { - this->close(); - } -} - -void TrackerControls::loadSettings() { - - QSettings settings("opentrack"); // Registry settings (in HK_USER) - - QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString(); - QSettings iniFile( currentFile, QSettings::IniFormat ); // Application settings (in INI-file) - - QComboBox* boxen[] = { - ui.comboBox_4, - ui.comboBox_5, - ui.comboBox_6, - ui.comboBox, - ui.comboBox_2, - ui.comboBox_3, - }; - - iniFile.beginGroup ( "tracker-joy" ); - for (int i = 0; i < 6; i++) - { - boxen[i]->setCurrentIndex(iniFile.value(QString("axis-%1").arg(i), 0).toInt()); - } - ui.joylist->setCurrentIndex(iniFile.value("joyid", -1).toInt()); - iniFile.endGroup (); - - settingsDirty = false; -} - -void TrackerControls::save() { - QSettings settings("opentrack"); // Registry settings (in HK_USER) - - QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString(); - { - QSettings iniFile( currentFile, QSettings::IniFormat ); // Application settings (in INI-file) - - QComboBox* boxen[] = { - ui.comboBox_4, - ui.comboBox_5, - ui.comboBox_6, - ui.comboBox, - ui.comboBox_2, - ui.comboBox_3, - }; - - iniFile.beginGroup ( "tracker-joy" ); - for (int i = 0; i < 6; i++) - { - iniFile.setValue(QString("axis-%1").arg(i), boxen[i]->currentIndex()); + case QMessageBox::Save: + s.b->save(); + if (tracker) + tracker->reload(); + this->close(); + break; + case QMessageBox::Discard: + s.b->reload(); + this->close(); + break; + case QMessageBox::Cancel: + // Cancel was clicked + break; + default: + // should never be reached + break; } - iniFile.setValue("joyid", ui.joylist->currentIndex()); - iniFile.endGroup (); } - - if(tracker) - { - tracker->reload(); + else { + this->close(); } - - settingsDirty = false; } -//////////////////////////////////////////////////////////////////////////////// -// Factory function that creates instances if the Tracker-settings dialog object. - -// Export both decorated and undecorated names. -// GetTrackerDialog - Undecorated name, which can be easily used with GetProcAddress -// Win32 API function. -// _GetTrackerDialog@0 - Common name decoration for __stdcall functions in C language. -//#pragma comment(linker, "/export:GetTrackerDialog=_GetTrackerDialog@0") extern "C" FTNOIR_TRACKER_BASE_EXPORT ITrackerDialog* CALLING_CONVENTION GetDialog( ) { diff --git a/ftnoir_tracker_joystick/ftnoir_tracker_joystick_dll.cpp b/ftnoir_tracker_joystick/ftnoir_tracker_joystick_dll.cpp index c5ee4e5f..325d24a4 100644 --- a/ftnoir_tracker_joystick/ftnoir_tracker_joystick_dll.cpp +++ b/ftnoir_tracker_joystick/ftnoir_tracker_joystick_dll.cpp @@ -2,31 +2,19 @@ #include #include "facetracknoir/global-settings.h" -FTNoIR_TrackerDll::FTNoIR_TrackerDll() { - //populate the description strings - trackerFullName = "Joystick"; - trackerShortName = "Joystick"; - trackerDescription = "joystick"; -} - -FTNoIR_TrackerDll::~FTNoIR_TrackerDll() -{ - -} - void FTNoIR_TrackerDll::getFullName(QString *strToBeFilled) { - *strToBeFilled = trackerFullName; + *strToBeFilled = "Joystick"; } void FTNoIR_TrackerDll::getShortName(QString *strToBeFilled) { - *strToBeFilled = trackerShortName; + *strToBeFilled = "Joystick"; } void FTNoIR_TrackerDll::getDescription(QString *strToBeFilled) { - *strToBeFilled = trackerDescription; + *strToBeFilled = "Joystick"; } void FTNoIR_TrackerDll::getIcon(QIcon *icon) @@ -34,16 +22,7 @@ void FTNoIR_TrackerDll::getIcon(QIcon *icon) *icon = QIcon(":/images/facetracknoir.png"); } -//////////////////////////////////////////////////////////////////////////////// -// Factory function that creates instances if the Tracker object. - -// Export both decorated and undecorated names. -// GetTrackerDll - Undecorated name, which can be easily used with GetProcAddress -// Win32 API function. -// _GetTrackerDll@0 - Common name decoration for __stdcall functions in C language. -//#pragma comment(linker, "/export:GetTrackerDll=_GetTrackerDll@0") - extern "C" FTNOIR_TRACKER_BASE_EXPORT Metadata* CALLING_CONVENTION GetMetadata() { - return new FTNoIR_TrackerDll; + return new FTNoIR_TrackerDll; } -- cgit v1.2.3