]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/controllertab.cpp
Various fixes for GPU/DSP DIV instruction, fixes for joypad handling.
[virtualjaguar] / src / gui / controllertab.cpp
1 //
2 // controllertab.cpp: "Controller" tab on the config dialog
3 //
4 // Part of the Virtual Jaguar Project
5 // (C) 2011 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
7 //
8 // JLH = James Hammons <jlhamm@acm.org>
9 //
10 // WHO  WHEN        WHAT
11 // ---  ----------  ------------------------------------------------------------
12 // JLH  06/23/2011  Created this file
13 // JLH  07/20/2011  Fixed a bunch of stuff
14 //
15
16 #include "controllertab.h"
17
18 #include "controllerwidget.h"
19 #include "gamepad.h"
20 #include "joystick.h"
21 #include "keygrabber.h"
22
23
24 ControllerTab::ControllerTab(QWidget * parent/*= 0*/): QWidget(parent),
25         label(new QLabel(tr("Controller:"))),
26         profileList(new QComboBox(this)),
27         redefineAll(new QPushButton(tr("Define All Inputs"))),
28         controllerWidget(new ControllerWidget(this))
29 {
30         QVBoxLayout * layout = new QVBoxLayout;
31         QHBoxLayout * top = new QHBoxLayout;
32         layout->addLayout(top);
33         top->addWidget(label);
34         top->addWidget(profileList, 0, Qt::AlignLeft);
35         layout->addWidget(controllerWidget);
36         layout->addWidget(redefineAll, 0, Qt::AlignHCenter);
37         setLayout(layout);
38         // At least by doing this, it keeps the QComboBox from resizing itself too
39         // large and breaking the layout. :-P
40         setFixedWidth(sizeHint().width());
41
42         connect(redefineAll, SIGNAL(clicked()), this, SLOT(DefineAllKeys()));
43         connect(profileList, SIGNAL(currentIndexChanged(int)), this, SLOT(ChangeProfile(int)));
44
45         // Set up the profile combobox (Keyboard is the default, and always
46         // present)
47         profileList->addItem(tr("Keyboard"));
48
49         for(int i=0; i<Gamepad::numJoysticks; i++)
50                 profileList->addItem(Gamepad::GetJoystickName(i));
51 }
52
53
54 ControllerTab::~ControllerTab()
55 {
56 }
57
58
59 void ControllerTab::DefineAllKeys(void)
60 {
61 //      char jagButtonName[21][10] = { "Up", "Down", "Left", "Right",
62 //              "*", "7", "4", "1", "0", "8", "5", "2", "#", "9", "6", "3",
63 //              "A", "B", "C", "Option", "Pause" };
64         int orderToDefine[21] = { 0, 1, 2, 3, 18, 17, 16, 20, 19, 7, 11, 15, 6, 10, 14, 5, 9, 13, 8, 4, 12 };
65         KeyGrabber keyGrab(this);
66
67         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
68         {
69 //              keyGrab.SetText(jagButtonName[orderToDefine[i]]);
70                 keyGrab.SetKeyText(orderToDefine[i]);
71                 keyGrab.exec();
72                 int key = keyGrab.key;
73
74                 if (key == Qt::Key_Escape)
75                         break;
76
77                 // Otherwise, populate the appropriate spot in the settings & update screen...
78                 controllerWidget->keys[orderToDefine[i]] = key;
79                 controllerWidget->update();
80         }
81 }
82
83
84 void ControllerTab::ChangeProfile(int profile)
85 {
86 printf("You selected profile: %s\n", (profile == 0 ? "Keyboard" : Gamepad::GetJoystickName(profile - 1)));
87 }
88
89 #if 0
90 The profiles need the following:
91
92  - The name of the controller
93  - A unique human readable ID
94  - The key definitions for that controller (keyboard keys can be mixed in)
95
96 So there can be more than one profile for each unique controller; the
97 relationship is many-to-one. So basically, how it works it like this: SDL
98 reports all connected controllers. If there are none connected, the default
99 controller is the keyboard (which can have multiple profiles). The UI only
100 presents those profiles which are usuable with the controllers that are plugged
101 in, all else is ignored. The user can pick the profile for the controller and
102 configure the keys for it; the UI automagically saves everything.
103
104 How to handle the case of identical controllers being plugged in? How does the
105 UI know which is which? Each controller will have a mapping to a default
106 Jaguar controller (#1 or #2). Still doesn't prevent confusion though. Actually,
107 it can: The profile can have a field that maps it to a preferred Jaguar
108 controller, which can also be both (#1 AND #2--in this case we can set it to
109 zero which means no preference). If the UI detects two of the same controller
110 and each can be mapped to the same profile, it assigns them in order since it
111 doesn't matter, the profiles are identical.
112
113 The default profile is always available and is the keyboard (hey, we're PC
114 centric here). The default profile is usually #0.
115
116 Can there be more than one keyboard profile? Why not? You will need separate
117 ones for controller #1 and controller #2.
118
119 A profile might look like this:
120
121 Field 1: Nostomo N45 Analog
122 Field 2: Dad's #1
123 Field 3: Jaguar controller #1
124 Field 4: The button/stick mapping
125
126 Profile # would be implicit in the order that they are stored in the internal
127 data structure.
128
129 When a new controller is plugged in with no profiles attached, it defaults to
130 a set keyboard layout which the user can change. So every new controller will
131 always have at least one profile.
132 #endif
133