]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/controllerwidget.cpp
25fb8b1b8206bef1baa4f6b2059379b567644803
[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 "keygrabber.h"
19
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] = {
23         "Space",
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", 
31         "{", "|", "}", "~"
32 };
33
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",
40         "F14", "F15", "F16"
41 };
42
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] = { { 74, 32 }, { 71, 67 }, { 53, 49 }, { 93, 49 },
47         { 110, 200 }, { 110, 175 }, { 110, 151 }, { 110, 126 },
48         { 148, 200 }, { 148, 175 }, { 148, 151 }, { 148, 126 },
49         { 186, 200 }, { 186, 175 }, { 186, 151 }, { 186, 126 },
50         { 234, 31 }, { 216, 51 }, { 199, 71 }, { 164-11, 101-30 }, { 141-11, 108+13-30 }
51 };
52
53 ControllerWidget::ControllerWidget(QWidget * parent/*= 0*/): QWidget(parent),
54         controllerPic(":/res/controller.png"), widgetSize(controllerPic.size()),
55         keyToHighlight(-1), mouseDown(false)
56 {
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);
61 }
62
63 ControllerWidget::~ControllerWidget()
64 {
65 }
66
67 QSize ControllerWidget::sizeHint(void) const
68 {
69         return widgetSize;
70 }
71
72 QSizePolicy ControllerWidget::sizePolicy(void) const
73 {
74         return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
75 }
76
77 void ControllerWidget::paintEvent(QPaintEvent * /*event*/)
78 {
79         QPainter painter(this);
80         painter.setRenderHint(QPainter::Antialiasing);
81         painter.drawImage(QPoint(0, 0), controllerPic);
82
83         // Bump up the size of the default font...
84         QFont font = painter.font();
85         font.setPixelSize(15);
86         font.setBold(true);
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)));
91
92         // First, draw black oversize line, then dot, then colored line
93         QPen blackPen(QColor(0, 0, 0, 255));
94         blackPen.setWidth(4);
95         QPen colorPen(QColor(48, 255, 255, 255));
96         colorPen.setWidth(2);
97         QLine line(QPoint(141-11, 100-30), QPoint(141-11, 108+5-30));
98
99         painter.setPen(blackPen);
100         painter.drawLine(line);
101         blackPen.setWidth(1);
102         painter.setPen(blackPen);
103         painter.drawEllipse(QPoint(141-11, 100-30), 4, 4);
104         painter.setPen(colorPen);
105         painter.drawLine(line);
106
107         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
108         {
109                 if (keyToHighlight == i)
110                 {
111                         painter.setPen(QColor(255, 48, 255, 255));      // This is R,G,B,A
112                         font.setPixelSize(mouseDown ? 15 : 18);
113                         painter.setFont(font);
114                 }
115                 else
116                 {
117                         painter.setPen(QColor(48, 255, 255, 255));      // This is R,G,B,A
118                         font.setPixelSize(15);
119                         painter.setFont(font);
120                 }
121
122                 if (keys[i] < 0x80)
123                         DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
124                                 QString(keyName1[keys[i] - 0x20]));
125                 else if ((keys[i] & 0xFFFFFF00) == 0x01000000)
126                 {
127                         DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
128                                 QString(keyName2[keys[i] & 0x3F]));
129                 }
130                 else
131                         DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1], QString("???"));
132         }
133 }
134
135 void ControllerWidget::mousePressEvent(QMouseEvent * /*event*/)
136 {
137         mouseDown = true;
138         update();
139 }
140
141 void ControllerWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
142 {
143         mouseDown = false;
144         // Spawning the keygrabber causes leaveEvent() to be called, so we need to save this
145         int keyToHighlightSave = keyToHighlight;
146
147         KeyGrabber keyGrab(this);
148         keyGrab.SetKeyText(keyToHighlightSave);
149         keyGrab.exec();
150         int key = keyGrab.key;
151
152         if (key != Qt::Key_Escape)
153                 keys[keyToHighlightSave] = key;
154
155         keyToHighlight = keyToHighlightSave;
156         update();
157 }
158
159 void ControllerWidget::mouseMoveEvent(QMouseEvent * event)
160 {
161         if (mouseDown)
162                 return;
163
164         // Save the current closest item
165         int keyToHighlightOld = keyToHighlight;
166         // Set up closest distance (this should be large enough)
167         double closest = 1e9;
168
169         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
170         {
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));
175
176                 if (currentDistance < closest)
177                 {
178                         closest = currentDistance;
179                         keyToHighlight = i;
180                 }
181         }
182
183         if (keyToHighlightOld != keyToHighlight)
184                 update();
185 }
186
187 void ControllerWidget::leaveEvent(QEvent * /*event*/)
188 {
189         keyToHighlight = -1;
190         update();
191 }
192
193 void ControllerWidget::DrawBorderedText(QPainter & painter, int x, int y, QString text)
194 {
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
199
200         for(int i=-1; i<=1; i++)
201         {
202                 for(int j=-1; j<=1; j++)
203                 {
204                         rect.moveCenter(QPoint(x + i, y + j));
205                         painter.drawText(rect, Qt::AlignCenter, text);
206                 }
207         }
208
209         painter.setPen(oldPen);
210         rect.moveCenter(QPoint(x, y));
211         painter.drawText(rect, Qt::AlignCenter, text);
212 }