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"
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 ControllerWidget::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 ControllerWidget::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 char ControllerWidget::hatName[4][16] = { "Up", "Rt", "Dn", "Lf" };
47 char ControllerWidget::axisName[2][8] = { "+", "-" };
49 // This is hard-coded crap. It's crap-tastic!
50 // These are the positions to draw the button names at, ordered by the BUTTON_* sequence
51 // found in joystick.h.
52 int ControllerWidget::buttonPos[21][2] = { { 74, 32 }, { 71, 67 }, { 53, 49 }, { 93, 49 },
53 { 110, 200 }, { 110, 175 }, { 110, 151 }, { 110, 126 },
54 { 148, 200 }, { 148, 175 }, { 148, 151 }, { 148, 126 },
55 { 186, 200 }, { 186, 175 }, { 186, 151 }, { 186, 126 },
56 { 234, 31 }, { 216, 51 }, { 199, 71 }, { 164-11, 101-30 }, { 141-11, 108+13-30 }
60 ControllerWidget::ControllerWidget(QWidget * parent/*= 0*/): QWidget(parent),
61 controllerPic(":/res/controller.png"), widgetSize(controllerPic.size()),
62 keyToHighlight(-1), mouseDown(false)
64 // Seems we have to pad this stuff, otherwise it clips on the right side
65 widgetSize += QSize(4, 4);
66 // We want to know when the mouse is moving over our widget...
67 setMouseTracking(true);
69 //setFixedSize(widgetSize);
73 ControllerWidget::~ControllerWidget()
78 QSize ControllerWidget::sizeHint(void) const
84 QSizePolicy ControllerWidget::sizePolicy(void) const
86 return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
90 void ControllerWidget::paintEvent(QPaintEvent * /*event*/)
92 QPainter painter(this);
93 painter.setRenderHint(QPainter::Antialiasing);
94 painter.drawImage(QPoint(0, 0), controllerPic);
96 // Bump up the size of the default font...
97 QFont font = painter.font();
98 font.setPixelSize(15);
100 painter.setFont(font);
101 // painter.setPen(QColor(48, 255, 255, 255)); // This is R,G,B,A
102 painter.setPen(QColor(0, 0, 0, 255)); // This is R,G,B,A
103 painter.setBrush(QBrush(QColor(48, 255, 255, 255)));
105 // First, draw black oversize line, then dot, then colored line
106 QPen blackPen(QColor(0, 0, 0, 255));
107 blackPen.setWidth(4);
108 QPen colorPen(QColor(48, 255, 255, 255));
109 colorPen.setWidth(2);
110 QLine line(QPoint(141-11, 100-30), QPoint(141-11, 108+5-30));
112 painter.setPen(blackPen);
113 painter.drawLine(line);
114 blackPen.setWidth(1);
115 painter.setPen(blackPen);
116 painter.drawEllipse(QPoint(141-11, 100-30), 4, 4);
117 painter.setPen(colorPen);
118 painter.drawLine(line);
120 for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
122 if (keyToHighlight == i)
124 painter.setPen(QColor(255, 48, 255, 255)); // This is R,G,B,A
125 font.setPixelSize(mouseDown ? 15 : 18);
126 painter.setFont(font);
130 painter.setPen(QColor(48, 255, 255, 255)); // This is R,G,B,A
131 font.setPixelSize(15);
132 painter.setFont(font);
136 DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
137 QString(keyName1[keys[i] - 0x20]));
138 else if ((keys[i] & 0xFFFFFF00) == 0x01000000)
140 DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
141 QString(keyName2[keys[i] & 0x3F]));
144 else if (keys[i] & JOY_BUTTON)
146 DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
147 QString("JB%1").arg(keys[i] & JOY_BUTTON_MASK));
149 else if (keys[i] & JOY_HAT)
151 DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
152 QString("j%1").arg(hatName[keys[i] & JOY_BUTTON_MASK]));
154 else if (keys[i] & JOY_AXIS)
156 DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
157 QString("JA%1%2").arg((keys[i] & JOY_AXISNUM_MASK) >> 1).arg(axisName[keys[i] & JOY_AXISDIR_MASK]));
161 DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1], QString("???"));
166 void ControllerWidget::mousePressEvent(QMouseEvent * /*event*/)
173 void ControllerWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
176 // Spawning the keygrabber causes leaveEvent() to be called, so we need to save this
177 int keyToHighlightSave = keyToHighlight;
179 KeyGrabber keyGrab(this);
180 keyGrab.SetKeyText(keyToHighlightSave);
182 int key = keyGrab.key;
184 if (key != Qt::Key_Escape)
186 keys[keyToHighlightSave] = key;
187 emit(KeyDefined(keyToHighlightSave, key));
190 keyToHighlight = keyToHighlightSave;
195 void ControllerWidget::mouseMoveEvent(QMouseEvent * event)
200 // Save the current closest item
201 int keyToHighlightOld = keyToHighlight;
202 // Set up closest distance (this should be large enough)
203 double closest = 1e9;
205 for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
207 // We loop through the button text positions, to see which one is closest.
208 double distX = (double)(event->x() - buttonPos[i][0]);
209 double distY = (double)(event->y() - buttonPos[i][1]);
210 double currentDistance = sqrt((distX * distX) + (distY * distY));
212 if (currentDistance < closest)
214 closest = currentDistance;
219 if (keyToHighlightOld != keyToHighlight)
224 void ControllerWidget::leaveEvent(QEvent * /*event*/)
231 void ControllerWidget::DrawBorderedText(QPainter & painter, int x, int y, QString text)
233 // Text is drawn centered at (x, y) as well, using a bounding rect for the purpose.
234 QRect rect(0, 0, 60, 30);
235 QPen oldPen = painter.pen();
236 painter.setPen(QColor(0, 0, 0, 255)); // This is R,G,B,A
238 for(int i=-1; i<=1; i++)
240 for(int j=-1; j<=1; j++)
242 rect.moveCenter(QPoint(x + i, y + j));
243 painter.drawText(rect, Qt::AlignCenter, text);
247 painter.setPen(oldPen);
248 rect.moveCenter(QPoint(x, y));
249 painter.drawText(rect, Qt::AlignCenter, text);