]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/controllertab.cpp
Controller tab now shows correctly redefined keys.
[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 L. Hammons <jlhamm@acm.org>
9 //
10 // WHO  WHEN        WHAT
11 // ---  ----------  ------------------------------------------------------------
12 // JLH  06/23/2011  Created this file
13
14 #include "controllertab.h"
15
16 #include "joystick.h"
17 #include "keygrabber.h"
18 #include "settings.h"
19
20 // These tables are used to convert Qt keycodes into human readable form. Note that
21 // a lot of these are just filler.
22 char ControllerTab::keyName1[96][16] = {
23         "Space",
24         "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
25         "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?",
26         "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
27         "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
28         "[", "]", "\\", "^", "_", "`",
29         "$61", "$62", "$63", "$64", "$65", "$66", "$67", "$68", "$69", "$6A", "$6B", "$6C", "$6D", 
30         "$6E", "$6F", "$70", "$71", "$72", "$73", "$74", "$75", "$76", "$77", "$78", "$79", "$7A", 
31         "{", "|", "}", "~"
32 };
33
34 char ControllerTab::keyName2[64][16] = {
35         "Esc", "Tab", "BTab", "BS", "Ret", "Ent", "Ins", "Del", "Pau", "Prt", "SRq", "Clr",
36         "$C", "$D", "$E", "$F", "Hm", "End", "Lf", "Up", "Rt", "Dn", "PgU", "PgD", "$18",
37         "$19", "$1A", "$1B", "$1C", "$1D", "$1E", "$1F", "Shf", "Ctl", "Mta", "Alt",
38         "Cap", "Num", "ScL", "$27", "$28", "$29", "$2A", "$2B", "$2C", "$2D", "$2E", "$2F",
39         "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13",
40         "F14", "F15", "F16"
41 };
42
43 ControllerTab::ControllerTab(QWidget * parent/*= 0*/): QWidget(parent)
44 {
45         QLabel * img = new QLabel;
46 //      img->setPixmap(QPixmap(":/res/controller.png"));
47
48 /*
49         QImage cartImg(":/res/cart-blank.png");
50         QPainter painter(&cartImg);
51         painter.drawPixmap(23, 87, QPixmap(":/res/label-blank.png"));
52         painter.end();
53         cartSmall = cartImg.scaled(488/4, 395/4, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
54 */
55         QImage controller(":/res/controller.png");
56         QPainter painter(&controller);
57 //      painter.draw();
58 //      painter->drawText(QRect(option.rect.x()+10, option.rect.y()+36, 196/2, 70/2),
59 //              Qt::TextWordWrap | Qt::AlignHCenter, nameToDraw);
60
61         // Bump up the size of the default font...
62         QFont font = painter.font();
63         font.setPixelSize(14);
64         font.setBold(true);
65         painter.setFont(font);
66         painter.setPen(QColor(48, 255, 255, 255));      // This is R,G,B,A
67
68         // This is hard-coded crap. It's crap-tastic!
69 /*
70 Bitmap Locations:
71
72 Up: 87,64
73 Down: 87,94
74 Left: 73,78
75 Right: 105,77
76 C: 209,104
77 B: 225,80
78 A: 242,60
79 Pause: 141,109
80 Option: 165,107
81 1: 125,153
82 2: 160,153
83 3: 196,153
84 4: 125,177
85 5: 
86 6: 
87 7: 125,199
88 8: 
89 9: 
90 *: 125,223
91 0: 
92 #: 
93 enum { BUTTON_FIRST = 0, BUTTON_U = 0,
94 BUTTON_D = 1,
95 BUTTON_L = 2,
96 BUTTON_R = 3,
97
98 BUTTON_s = 4,
99 BUTTON_7 = 5,
100 BUTTON_4 = 6,
101 BUTTON_1 = 7,
102 BUTTON_0 = 8,
103 BUTTON_8 = 9,
104 BUTTON_5 = 10,
105 BUTTON_2 = 11,
106 BUTTON_d = 12,
107 BUTTON_9 = 13,
108 BUTTON_6 = 14,
109 BUTTON_3 = 15,
110
111 BUTTON_A = 16,
112 BUTTON_B = 17,
113 BUTTON_C = 18,
114 BUTTON_OPTION = 19,
115 BUTTON_PAUSE = 20, BUTTON_LAST = 20 };
116 */
117         int buttonPos[21][2] = { { 87, 64 }, { 87, 94 }, { 73, 78 }, { 105, 77 },
118                 { 125, 223 }, { 125, 199 }, { 125, 177 }, { 125, 153 },
119                 { 160, 223 }, { 160, 199 }, { 160, 177 }, { 160, 153 },
120                 { 196, 223 }, { 196, 199 }, { 196, 177 }, { 196, 153 },
121                 { 242, 60 }, { 225, 80 }, { 209, 104 }, { 165, 107 }, { 141, 109 }
122         };
123
124         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
125 //              painter.drawText(QPoint(buttonPos[i][0] - 5, buttonPos[i][1] + 5), QString(buttonName[i]));
126         {
127 // Need to convert this back to p1Keys, but will require some restructuring...
128 //              if (p1Keys[i] < 0x80)
129                 if (vjs.p1KeyBindings[i] < 0x80)
130 //                      painter.drawText(QPoint(buttonPos[i][0] - 5, buttonPos[i][1] + 5), QString(keyName1[p1Keys[i] - 0x20]));
131 //                      painter.drawText(QPoint(buttonPos[i][0] - 5, buttonPos[i][1] + 5), QString(keyName1[vjs.p1KeyBindings[i] - 0x20]));
132                         DrawBorderedText(painter, buttonPos[i][0] - 5, buttonPos[i][1] + 5, QString(keyName1[vjs.p1KeyBindings[i] - 0x20]));
133                 else if ((vjs.p1KeyBindings[i] & 0xFFFFFF00) == 0x01000000)
134                 {
135 //                      painter.drawText(QPoint(buttonPos[i][0] - 5, buttonPos[i][1] + 5), QString(keyName2[vjs.p1KeyBindings[i] & 0x3F]));
136                         DrawBorderedText(painter, buttonPos[i][0] - 5, buttonPos[i][1] + 5, QString(keyName2[vjs.p1KeyBindings[i] & 0x3F]));
137                 }
138                 else
139 //                      painter.drawText(QPoint(buttonPos[i][0] - 5, buttonPos[i][1] + 5), QString("???"));
140                         DrawBorderedText(painter, buttonPos[i][0] - 5, buttonPos[i][1] + 5, QString("???"));
141         }
142
143         painter.end();
144         img->setPixmap(QPixmap::fromImage(controller));
145
146         redefineAll = new QPushButton(tr("Redefine All Keys"));
147
148         QVBoxLayout * layout = new QVBoxLayout;
149         layout->addWidget(img);
150         layout->addWidget(redefineAll);
151         setLayout(layout);
152
153         connect(redefineAll, SIGNAL(clicked()), this, SLOT(DefineAllKeys()));
154 }
155
156 ControllerTab::~ControllerTab()
157 {
158 }
159
160 void ControllerTab::DefineAllKeys(void)
161 {
162         char jagButtonName[21][10] = { "Up", "Down", "Left", "Right",
163                 "*", "7", "4", "1", "0", "8", "5", "2", "#", "9", "6", "3",
164                 "A", "B", "C", "Option", "Pause" };
165         int orderToDefine[21] = { 0, 1, 2, 3, 18, 17, 16, 20, 19, 7, 11, 15, 6, 10, 14, 5, 9, 13, 8, 4, 12 };
166         KeyGrabber keyGrab(this);
167
168         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
169         {
170                 keyGrab.SetText(jagButtonName[orderToDefine[i]]);
171                 keyGrab.exec();
172                 int key = keyGrab.key;
173
174                 if (key == Qt::Key_Escape)
175                         break;
176
177                 // Otherwise, populate the appropriate spot in the settings...
178                 p1Keys[orderToDefine[i]] = key;
179         }
180 }
181
182 void ControllerTab::DrawBorderedText(QPainter & painter, int x, int y, QString text)
183 {
184         painter.setPen(QColor(0, 0, 0, 255));           // This is R,G,B,A
185
186         for(int i=-1; i<=1; i++)
187         {
188                 for(int j=-1; j<=1; j++)
189                 {
190                         painter.drawText(QPoint(x + i, y + j), text);
191                 }
192         }
193
194         painter.setPen(QColor(48, 255, 255, 255));      // This is R,G,B,A
195         painter.drawText(QPoint(x, y), text);
196 }