2 // controllertab.cpp: "Controller" tab on the config dialog
4 // Part of the Virtual Jaguar Project
5 // (C) 2011 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- ------------------------------------------------------------
12 // JLH 06/23/2011 Created this file
13 // JLH 07/20/2011 Fixed a bunch of stuff
16 #include "controllertab.h"
19 #include "keygrabber.h"
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] = {
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",
36 char ControllerTab::keyName2[64][16] = {
37 "Esc", "Tab", "BTab", "BS", "Ret", "Ent", "Ins", "Del", "Pause", "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",
45 ControllerTab::ControllerTab(QWidget * parent/*= 0*/): QWidget(parent)
47 controllerPic = new QLabel;
48 QImage controller(":/res/controller.png");
49 controllerPic->setPixmap(QPixmap::fromImage(controller));
51 redefineAll = new QPushButton(tr("Redefine All Keys"));
53 QVBoxLayout * layout = new QVBoxLayout;
54 layout->addWidget(controllerPic);
55 layout->addWidget(redefineAll);
58 connect(redefineAll, SIGNAL(clicked()), this, SLOT(DefineAllKeys()));
61 ControllerTab::~ControllerTab()
65 void ControllerTab::DefineAllKeys(void)
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);
73 for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
75 keyGrab.SetText(jagButtonName[orderToDefine[i]]);
77 int key = keyGrab.key;
79 if (key == Qt::Key_Escape)
82 // Otherwise, populate the appropriate spot in the settings & update screen...
83 p1Keys[orderToDefine[i]] = key;
88 void ControllerTab::UpdateLabel(void)
90 QImage controller(":/res/controller.png");
91 QPainter painter(&controller);
92 painter.setRenderHint(QPainter::Antialiasing);
94 // Bump up the size of the default font...
95 QFont font = painter.font();
96 font.setPixelSize(15);
98 painter.setFont(font);
99 // painter.setPen(QColor(48, 255, 255, 255)); // This is R,G,B,A
100 painter.setPen(QColor(0, 0, 0, 255)); // This is R,G,B,A
101 painter.setBrush(QBrush(QColor(48, 255, 255, 255)));
103 // This is hard-coded crap. It's crap-tastic!
104 int buttonPos[21][2] = { { 87-1, 64-5 }, { 87-1, 94 }, { 73-5, 78-2 }, { 105+3, 77-1 },
105 { 125, 223 }, { 125, 200 }, { 125, 177 }, { 125, 153 },
106 { 160, 223 }, { 160, 200 }, { 160, 177 }, { 160, 153 },
107 { 196, 223 }, { 196, 200 }, { 196, 177 }, { 196, 153 },
108 { 242, 60 }, { 225-1, 80 }, { 209-2, 104 }, { 162+2, 108-7}, { 141, 108+13 }
111 // First, draw black oversize line, then dot, then colored line
112 QPen blackPen(QColor(0, 0, 0, 255));
113 blackPen.setWidth(4);
114 QPen colorPen(QColor(48, 255, 255, 255));
115 colorPen.setWidth(2);
116 QLine line(QPoint(141, 100), QPoint(141, 108+5));
118 painter.setPen(blackPen);
119 painter.drawLine(line);//QPoint(141, 100), QPoint(141, 108+5));
120 blackPen.setWidth(1);
121 painter.setPen(blackPen);
122 painter.drawEllipse(QPoint(141, 100), 4, 4);
123 painter.setPen(colorPen);
124 painter.drawLine(line);//QPoint(141, 100), QPoint(141, 108+5));
126 for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
128 if (p1Keys[i] < 0x80)
129 DrawBorderedText(painter, buttonPos[i][0] /*- 5*/, buttonPos[i][1] /*+ 5*/,
130 QString(keyName1[p1Keys[i] - 0x20]));
131 else if ((p1Keys[i] & 0xFFFFFF00) == 0x01000000)
133 DrawBorderedText(painter, buttonPos[i][0] /*- 5*/, buttonPos[i][1] /*+ 5*/,
134 QString(keyName2[p1Keys[i] & 0x3F]));
137 DrawBorderedText(painter, buttonPos[i][0] - 5, buttonPos[i][1] + 5, QString("???"));
141 controllerPic->setPixmap(QPixmap::fromImage(controller));
144 void ControllerTab::DrawBorderedText(QPainter & painter, int x, int y, QString text)
146 // Text is drawn centered at (x, y) as well, using a bounding rect for the purpose.
147 QRect rect(0, 0, 60, 30);
148 painter.setPen(QColor(0, 0, 0, 255)); // This is R,G,B,A
150 for(int i=-1; i<=1; i++)
152 for(int j=-1; j<=1; j++)
154 // painter.drawText(QPoint(x + i, y + j), text);
155 rect.moveCenter(QPoint(x + i, y + j));
156 painter.drawText(rect, Qt::AlignCenter, text);
160 painter.setPen(QColor(48, 255, 255, 255)); // This is R,G,B,A
161 // painter.drawText(QPoint(x, y), text);
162 rect.moveCenter(QPoint(x, y));
163 painter.drawText(rect, Qt::AlignCenter, text);