2 // controllerwidget.cpp: A widget for changing "Controller" configuration
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 Hammons <jlhamm@acm.org>
11 // --- ---------- ------------------------------------------------------------
12 // JLH 07/20/2011 Created this file
15 #include "controllerwidget.h"
18 #include "keygrabber.h"
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 ControllerWidget::keyName1[96][16] = {
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",
34 char ControllerWidget::keyName2[64][16] = {
35 "Esc", "Tab", "BTab", "BS", "Ret", "Ent", "Ins", "Del", "Pause", "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",
43 // This is hard-coded crap. It's crap-tastic!
44 // These are the positions to draw the button names at, ordered by the BUTTON_* sequence
45 // found in joystick.h.
46 int ControllerWidget::buttonPos[21][2] = { { 86, 59 }, { 86, 94 }, { 68, 76 }, { 108, 76 },
47 { 125, 223 }, { 125, 200 }, { 125, 177 }, { 125, 153 },
48 { 160, 223 }, { 160, 200 }, { 160, 177 }, { 160, 153 },
49 { 196, 223 }, { 196, 200 }, { 196, 177 }, { 196, 153 },
50 { 242, 60 }, { 224, 80 }, { 207, 104 }, { 164, 101 }, { 141, 108+13 }
53 ControllerWidget::ControllerWidget(QWidget * parent/*= 0*/): QWidget(parent),
54 controllerPic(":/res/controller.png"), widgetSize(controllerPic.size()),
55 keyToHighlight(-1), mouseDown(false)
57 // Seems we have to pad this stuff, otherwise it clips on the right side
58 widgetSize += QSize(4, 4);
59 // We want to know when the mouse is moving over our widget...
60 setMouseTracking(true);
63 ControllerWidget::~ControllerWidget()
67 QSize ControllerWidget::sizeHint(void) const
72 QSizePolicy ControllerWidget::sizePolicy(void) const
74 return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
77 void ControllerWidget::paintEvent(QPaintEvent * /*event*/)
79 QPainter painter(this);
80 painter.setRenderHint(QPainter::Antialiasing);
81 painter.drawImage(QPoint(0, 0), controllerPic);
83 // Bump up the size of the default font...
84 QFont font = painter.font();
85 font.setPixelSize(15);
87 painter.setFont(font);
88 // painter.setPen(QColor(48, 255, 255, 255)); // This is R,G,B,A
89 painter.setPen(QColor(0, 0, 0, 255)); // This is R,G,B,A
90 painter.setBrush(QBrush(QColor(48, 255, 255, 255)));
92 // First, draw black oversize line, then dot, then colored line
93 QPen blackPen(QColor(0, 0, 0, 255));
95 QPen colorPen(QColor(48, 255, 255, 255));
97 QLine line(QPoint(141, 100), QPoint(141, 108+5));
99 painter.setPen(blackPen);
100 painter.drawLine(line);
101 blackPen.setWidth(1);
102 painter.setPen(blackPen);
103 painter.drawEllipse(QPoint(141, 100), 4, 4);
104 painter.setPen(colorPen);
105 painter.drawLine(line);
107 for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
109 if (keyToHighlight == i)
111 painter.setPen(QColor(255, 48, 255, 255)); // This is R,G,B,A
112 font.setPixelSize(mouseDown ? 15 : 18);
113 painter.setFont(font);
117 painter.setPen(QColor(48, 255, 255, 255)); // This is R,G,B,A
118 font.setPixelSize(15);
119 painter.setFont(font);
123 DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
124 QString(keyName1[keys[i] - 0x20]));
125 else if ((keys[i] & 0xFFFFFF00) == 0x01000000)
127 DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
128 QString(keyName2[keys[i] & 0x3F]));
131 DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1], QString("???"));
135 void ControllerWidget::mousePressEvent(QMouseEvent * /*event*/)
141 void ControllerWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
144 // Spawning the keygrabber causes leaveEvent() to be called, so we need to save this
145 int keyToHighlightSave = keyToHighlight;
147 KeyGrabber keyGrab(this);
148 keyGrab.SetKeyText(keyToHighlightSave);
150 int key = keyGrab.key;
152 if (key != Qt::Key_Escape)
153 keys[keyToHighlightSave] = key;
155 keyToHighlight = keyToHighlightSave;
159 void ControllerWidget::mouseMoveEvent(QMouseEvent * event)
164 // Save the current closest item
165 int keyToHighlightOld = keyToHighlight;
166 // Set up closest distance (this should be large enough)
167 double closest = 1e9;
169 for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
171 // We loop through the button text positions, to see which one is closest.
172 double distX = (double)(event->x() - buttonPos[i][0]);
173 double distY = (double)(event->y() - buttonPos[i][1]);
174 double currentDistance = sqrt((distX * distX) + (distY * distY));
176 if (currentDistance < closest)
178 closest = currentDistance;
183 if (keyToHighlightOld != keyToHighlight)
187 void ControllerWidget::leaveEvent(QEvent * /*event*/)
193 void ControllerWidget::DrawBorderedText(QPainter & painter, int x, int y, QString text)
195 // Text is drawn centered at (x, y) as well, using a bounding rect for the purpose.
196 QRect rect(0, 0, 60, 30);
197 QPen oldPen = painter.pen();
198 painter.setPen(QColor(0, 0, 0, 255)); // This is R,G,B,A
200 for(int i=-1; i<=1; i++)
202 for(int j=-1; j<=1; j++)
204 rect.moveCenter(QPoint(x + i, y + j));
205 painter.drawText(rect, Qt::AlignCenter, text);
209 painter.setPen(oldPen);
210 rect.moveCenter(QPoint(x, y));
211 painter.drawText(rect, Qt::AlignCenter, text);