]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/controllertab.cpp
Minor changes to make the key definition dialog a bit nicer.
[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 // JLH  07/20/2011  Fixed a bunch of stuff
14 //
15
16 #include "controllertab.h"
17
18 #include "joystick.h"
19 #include "keygrabber.h"
20 #include "settings.h"
21
22 // These tables are used to convert Qt keycodes into human readable form. Note that
23 // a lot of these are just filler.
24 char ControllerTab::keyName1[96][16] = {
25         "Space",
26         "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
27         "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?",
28         "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
29         "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
30         "[", "]", "\\", "^", "_", "`",
31         "$61", "$62", "$63", "$64", "$65", "$66", "$67", "$68", "$69", "$6A", "$6B", "$6C", "$6D", 
32         "$6E", "$6F", "$70", "$71", "$72", "$73", "$74", "$75", "$76", "$77", "$78", "$79", "$7A", 
33         "{", "|", "}", "~"
34 };
35
36 char ControllerTab::keyName2[64][16] = {
37         "Esc", "Tab", "BTab", "BS", "Ret", "Ent", "Ins", "Del", "Pau", "Prt", "SRq", "Clr",
38         "$C", "$D", "$E", "$F", "Hm", "End", "Lf", "Up", "Rt", "Dn", "PgU", "PgD", "$18",
39         "$19", "$1A", "$1B", "$1C", "$1D", "$1E", "$1F", "Shf", "Ctl", "Mta", "Alt",
40         "Cap", "Num", "ScL", "$27", "$28", "$29", "$2A", "$2B", "$2C", "$2D", "$2E", "$2F",
41         "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13",
42         "F14", "F15", "F16"
43 };
44
45 ControllerTab::ControllerTab(QWidget * parent/*= 0*/): QWidget(parent)
46 {
47         controllerPic = new QLabel;
48         QImage controller(":/res/controller.png");
49         controllerPic->setPixmap(QPixmap::fromImage(controller));
50
51         redefineAll = new QPushButton(tr("Redefine All Keys"));
52
53         QVBoxLayout * layout = new QVBoxLayout;
54         layout->addWidget(controllerPic);
55         layout->addWidget(redefineAll);
56         setLayout(layout);
57
58         connect(redefineAll, SIGNAL(clicked()), this, SLOT(DefineAllKeys()));
59 }
60
61 ControllerTab::~ControllerTab()
62 {
63 }
64
65 void ControllerTab::DefineAllKeys(void)
66 {
67         char jagButtonName[21][10] = { "Up", "Down", "Left", "Right",
68                 "*", "7", "4", "1", "0", "8", "5", "2", "#", "9", "6", "3",
69                 "A", "B", "C", "Option", "Pause" };
70         int orderToDefine[21] = { 0, 1, 2, 3, 18, 17, 16, 20, 19, 7, 11, 15, 6, 10, 14, 5, 9, 13, 8, 4, 12 };
71         KeyGrabber keyGrab(this);
72
73         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
74         {
75                 keyGrab.SetText(jagButtonName[orderToDefine[i]]);
76                 keyGrab.exec();
77                 int key = keyGrab.key;
78
79                 if (key == Qt::Key_Escape)
80                         break;
81
82                 // Otherwise, populate the appropriate spot in the settings & update screen...
83                 p1Keys[orderToDefine[i]] = key;
84                 UpdateLabel();
85         }
86 }
87
88 void ControllerTab::UpdateLabel(void)
89 {
90         QImage controller(":/res/controller.png");
91         QPainter painter(&controller);
92
93         // Bump up the size of the default font...
94         QFont font = painter.font();
95         font.setPixelSize(15);
96         font.setBold(true);
97         painter.setFont(font);
98         painter.setPen(QColor(48, 255, 255, 255));      // This is R,G,B,A
99
100         // This is hard-coded crap. It's crap-tastic!
101         int buttonPos[21][2] = { { 87, 64 }, { 87, 94 }, { 73, 78 }, { 105, 77 },
102                 { 125, 223 }, { 125, 199 }, { 125, 178 }, { 125, 153 },
103                 { 160, 223 }, { 160, 199 }, { 160, 178 }, { 160, 153 },
104                 { 196, 223 }, { 196, 199 }, { 196, 178 }, { 196, 153 },
105                 { 242, 60 }, { 225, 80 }, { 209, 104 }, { 165, 108 }, { 141, 108 }
106         };
107
108         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
109         {
110                 if (p1Keys[i] < 0x80)
111                         DrawBorderedText(painter, buttonPos[i][0] - 5, buttonPos[i][1] + 5,
112                                 QString(keyName1[p1Keys[i] - 0x20]));
113                 else if ((p1Keys[i] & 0xFFFFFF00) == 0x01000000)
114                 {
115                         DrawBorderedText(painter, buttonPos[i][0] - 5, buttonPos[i][1] + 5,
116                                 QString(keyName2[p1Keys[i] & 0x3F]));
117                 }
118                 else
119                         DrawBorderedText(painter, buttonPos[i][0] - 5, buttonPos[i][1] + 5, QString("???"));
120         }
121
122         painter.end();
123         controllerPic->setPixmap(QPixmap::fromImage(controller));
124 }
125
126 void ControllerTab::DrawBorderedText(QPainter & painter, int x, int y, QString text)
127 {
128         painter.setPen(QColor(0, 0, 0, 255));           // This is R,G,B,A
129
130         for(int i=-1; i<=1; i++)
131         {
132                 for(int j=-1; j<=1; j++)
133                 {
134                         painter.drawText(QPoint(x + i, y + j), text);
135                 }
136         }
137
138         painter.setPen(QColor(48, 255, 255, 255));      // This is R,G,B,A
139         painter.drawText(QPoint(x, y), text);
140 }