summaryrefslogtreecommitdiffhomepage
path: root/ftnoir_tracker_joystick/ftnoir_tracker_joystick_dialog.cpp
blob: 812ad4540c44c60356a1e6b813c33cb1e33f8da3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "ftnoir_tracker_joystick.h"
#include "facetracknoir/global-settings.h"

static BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance, VOID* pContext )
{
    auto self = ( TrackerControls* )pContext;

    self->guids.push_back(pdidInstance->guidInstance);
    self->ui.joylist->addItem(QString(pdidInstance->tszInstanceName));

    return DIENUM_CONTINUE;
}

TrackerControls::TrackerControls() : tracker(nullptr)
{
    ui.setupUi( this );

    // Connect Qt signals to member-functions
    connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(doOK()));
    connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(doCancel()));

    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 );
        LPDIRECTINPUT8 g_pDI = nullptr;

        if( FAILED( hr = DirectInput8Create( GetModuleHandle( NULL ), DIRECTINPUT_VERSION,
                                             IID_IDirectInput8, ( VOID** )&g_pDI, NULL ) ) )
            goto fin;

        if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL,
                                             EnumJoysticksCallback,
                                             this,
                                             DIEDFL_ATTACHEDONLY )))
            goto fin;

fin:
        if (g_pDI)
            g_pDI->Release();
    }
}

void TrackerControls::doOK() {
    s.b->save();
    if (tracker)
        tracker->reload();
    this->close();
}

void TrackerControls::doCancel() {
    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:
            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;
        }
    }
    else {
        this->close();
    }
}

extern "C" FTNOIR_TRACKER_BASE_EXPORT ITrackerDialog* CALLING_CONVENTION GetDialog( )
{
    return new TrackerControls;
}