]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/controllerwidget.cpp
More gamepad work.
[virtualjaguar] / src / gui / controllerwidget.cpp
1 //
2 // controllerwidget.cpp: A widget for changing "Controller" configuration
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 Hammons <jlhamm@acm.org>
9 //
10 // WHO  WHEN        WHAT
11 // ---  ----------  ------------------------------------------------------------
12 // JLH  07/20/2011  Created this file
13 //
14
15 #include "controllerwidget.h"
16
17 #include "joystick.h"
18 #include "gamepad.h"
19 #include "keygrabber.h"
20
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 ControllerWidget::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 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",
42         "F14", "F15", "F16"
43 };
44
45 char ControllerWidget::hatName[4][16] = { "Up", "Rt", "Dn", "Lf" };
46
47 // This is hard-coded crap. It's crap-tastic!
48 // These are the positions to draw the button names at, ordered by the BUTTON_* sequence
49 // found in joystick.h.
50 int ControllerWidget::buttonPos[21][2] = { { 74, 32 }, { 71, 67 }, { 53, 49 }, { 93, 49 },
51         { 110, 200 }, { 110, 175 }, { 110, 151 }, { 110, 126 },
52         { 148, 200 }, { 148, 175 }, { 148, 151 }, { 148, 126 },
53         { 186, 200 }, { 186, 175 }, { 186, 151 }, { 186, 126 },
54         { 234, 31 }, { 216, 51 }, { 199, 71 }, { 164-11, 101-30 }, { 141-11, 108+13-30 }
55 };
56
57
58 ControllerWidget::ControllerWidget(QWidget * parent/*= 0*/): QWidget(parent),
59         controllerPic(":/res/controller.png"), widgetSize(controllerPic.size()),
60         keyToHighlight(-1), mouseDown(false)
61 {
62         // Seems we have to pad this stuff, otherwise it clips on the right side
63         widgetSize += QSize(4, 4);
64         // We want to know when the mouse is moving over our widget...
65         setMouseTracking(true);
66 }
67
68
69 ControllerWidget::~ControllerWidget()
70 {
71 }
72
73
74 QSize ControllerWidget::sizeHint(void) const
75 {
76         return widgetSize;
77 }
78
79
80 QSizePolicy ControllerWidget::sizePolicy(void) const
81 {
82         return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
83 }
84
85
86 void ControllerWidget::paintEvent(QPaintEvent * /*event*/)
87 {
88         QPainter painter(this);
89         painter.setRenderHint(QPainter::Antialiasing);
90         painter.drawImage(QPoint(0, 0), controllerPic);
91
92         // Bump up the size of the default font...
93         QFont font = painter.font();
94         font.setPixelSize(15);
95         font.setBold(true);
96         painter.setFont(font);
97 //      painter.setPen(QColor(48, 255, 255, 255));      // This is R,G,B,A
98         painter.setPen(QColor(0, 0, 0, 255));           // This is R,G,B,A
99         painter.setBrush(QBrush(QColor(48, 255, 255, 255)));
100
101         // First, draw black oversize line, then dot, then colored line
102         QPen blackPen(QColor(0, 0, 0, 255));
103         blackPen.setWidth(4);
104         QPen colorPen(QColor(48, 255, 255, 255));
105         colorPen.setWidth(2);
106         QLine line(QPoint(141-11, 100-30), QPoint(141-11, 108+5-30));
107
108         painter.setPen(blackPen);
109         painter.drawLine(line);
110         blackPen.setWidth(1);
111         painter.setPen(blackPen);
112         painter.drawEllipse(QPoint(141-11, 100-30), 4, 4);
113         painter.setPen(colorPen);
114         painter.drawLine(line);
115
116         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
117         {
118                 if (keyToHighlight == i)
119                 {
120                         painter.setPen(QColor(255, 48, 255, 255));      // This is R,G,B,A
121                         font.setPixelSize(mouseDown ? 15 : 18);
122                         painter.setFont(font);
123                 }
124                 else
125                 {
126                         painter.setPen(QColor(48, 255, 255, 255));      // This is R,G,B,A
127                         font.setPixelSize(15);
128                         painter.setFont(font);
129                 }
130
131                 if (keys[i] < 0x80)
132                         DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
133                                 QString(keyName1[keys[i] - 0x20]));
134                 else if ((keys[i] & 0xFFFFFF00) == 0x01000000)
135                 {
136                         DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
137                                 QString(keyName2[keys[i] & 0x3F]));
138                 }
139 #if 1
140                 else if (keys[i] & JOY_BUTTON)
141                 {
142                         DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
143                                 QString("JB%1").arg(keys[i] & JOY_BUTTON_MASK));
144                 }
145                 else if (keys[i] & JOY_HAT)
146                 {
147                         DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
148                                 QString("j%1").arg(hatName[keys[i] & JOY_BUTTON_MASK]));
149                 }
150 #endif
151                 else
152                         DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1], QString("???"));
153         }
154 }
155
156
157 void ControllerWidget::mousePressEvent(QMouseEvent * /*event*/)
158 {
159         mouseDown = true;
160         update();
161 }
162
163
164 void ControllerWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
165 {
166         mouseDown = false;
167         // Spawning the keygrabber causes leaveEvent() to be called, so we need to save this
168         int keyToHighlightSave = keyToHighlight;
169
170         KeyGrabber keyGrab(this);
171         keyGrab.SetKeyText(keyToHighlightSave);
172         keyGrab.exec();
173         int key = keyGrab.key;
174
175         if (key != Qt::Key_Escape)
176                 keys[keyToHighlightSave] = key;
177
178         keyToHighlight = keyToHighlightSave;
179         update();
180 }
181
182
183 void ControllerWidget::mouseMoveEvent(QMouseEvent * event)
184 {
185         if (mouseDown)
186                 return;
187
188         // Save the current closest item
189         int keyToHighlightOld = keyToHighlight;
190         // Set up closest distance (this should be large enough)
191         double closest = 1e9;
192
193         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
194         {
195                 // We loop through the button text positions, to see which one is closest.
196                 double distX = (double)(event->x() - buttonPos[i][0]);
197                 double distY = (double)(event->y() - buttonPos[i][1]);
198                 double currentDistance = sqrt((distX * distX) + (distY * distY));
199
200                 if (currentDistance < closest)
201                 {
202                         closest = currentDistance;
203                         keyToHighlight = i;
204                 }
205         }
206
207         if (keyToHighlightOld != keyToHighlight)
208                 update();
209 }
210
211
212 void ControllerWidget::leaveEvent(QEvent * /*event*/)
213 {
214         keyToHighlight = -1;
215         update();
216 }
217
218
219 void ControllerWidget::DrawBorderedText(QPainter & painter, int x, int y, QString text)
220 {
221         // Text is drawn centered at (x, y) as well, using a bounding rect for the purpose.
222         QRect rect(0, 0, 60, 30);
223         QPen oldPen = painter.pen();
224         painter.setPen(QColor(0, 0, 0, 255));           // This is R,G,B,A
225
226         for(int i=-1; i<=1; i++)
227         {
228                 for(int j=-1; j<=1; j++)
229                 {
230                         rect.moveCenter(QPoint(x + i, y + j));
231                         painter.drawText(rect, Qt::AlignCenter, text);
232                 }
233         }
234
235         painter.setPen(oldPen);
236         rect.moveCenter(QPoint(x, y));
237         painter.drawText(rect, Qt::AlignCenter, text);
238 }
239