+
+QSize ControllerWidget::sizeHint(void) const
+{
+ return widgetSize;
+}
+
+
+QSizePolicy ControllerWidget::sizePolicy(void) const
+{
+ return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+}
+
+
+void ControllerWidget::paintEvent(QPaintEvent * /*event*/)
+{
+ QPainter painter(this);
+ painter.setRenderHint(QPainter::Antialiasing);
+ painter.drawImage(QPoint(0, 0), controllerPic);
+
+ // Bump up the size of the default font...
+ QFont font = painter.font();
+ font.setPixelSize(15);
+ font.setBold(true);
+ painter.setFont(font);
+// painter.setPen(QColor(48, 255, 255, 255)); // This is R,G,B,A
+ painter.setPen(QColor(0, 0, 0, 255)); // This is R,G,B,A
+ painter.setBrush(QBrush(QColor(48, 255, 255, 255)));
+
+ // First, draw black oversize line, then dot, then colored line
+ QPen blackPen(QColor(0, 0, 0, 255));
+ blackPen.setWidth(4);
+ QPen colorPen(QColor(48, 255, 255, 255));
+ colorPen.setWidth(2);
+ QLine line(QPoint(141-11, 100-30), QPoint(141-11, 108+5-30));
+
+ painter.setPen(blackPen);
+ painter.drawLine(line);
+ blackPen.setWidth(1);
+ painter.setPen(blackPen);
+ painter.drawEllipse(QPoint(141-11, 100-30), 4, 4);
+ painter.setPen(colorPen);
+ painter.drawLine(line);
+
+//#define DEBUG_CWPAINT
+#ifdef DEBUG_CWPAINT
+printf("------------------------------\n");
+#endif
+ for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
+ {
+ if (keyToHighlight == i)
+ {
+ painter.setPen(QColor(255, 48, 255, 255)); // This is R,G,B,A
+ font.setPixelSize(mouseDown ? 15 : 18);
+ painter.setFont(font);
+ }
+ else
+ {
+ painter.setPen(QColor(48, 255, 255, 255)); // This is R,G,B,A
+ font.setPixelSize(15);
+ painter.setFont(font);
+ }
+
+#ifdef DEBUG_CWPAINT
+printf("key %02i: ", i);
+#endif
+
+ if (keys[i] < 0x80)
+#ifdef DEBUG_CWPAINT
+{
+printf("Drawing a key < 0x80 [keys[i]=%X, keyname=%s]...\n", keys[i], keyName1[keys[i] - 0x20]);
+#endif
+ DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
+ QString(keyName1[keys[i] - 0x20]));
+#ifdef DEBUG_CWPAINT
+}
+#endif
+ else if ((keys[i] & 0xFFFFFF00) == 0x01000000)
+ {
+#ifdef DEBUG_CWPAINT
+printf("Drawing a key with bit 48 set...\n");
+#endif
+ DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
+ QString(keyName2[keys[i] & 0x3F]));
+ }
+#if 1
+ else if (keys[i] & JOY_BUTTON)
+ {
+#ifdef DEBUG_CWPAINT
+printf("Drawing a joystick button...\n");
+#endif
+ DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
+ QString("JB%1").arg(keys[i] & JOY_BUTTON_MASK));
+ }
+ else if (keys[i] & JOY_HAT)
+ {
+#ifdef DEBUG_CWPAINT
+printf("Drawing a joystick hat...\n");
+#endif
+ DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
+ QString("j%1").arg(hatName[keys[i] & JOY_BUTTON_MASK]));
+ }
+ else if (keys[i] & JOY_AXIS)
+ {
+#ifdef DEBUG_CWPAINT
+printf("Drawing a joystick axis...\n");
+#endif
+ DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1],
+ QString("JA%1%2").arg((keys[i] & JOY_AXISNUM_MASK) >> 1).arg(axisName[keys[i] & JOY_AXISDIR_MASK]));
+ }
+#endif
+ else
+#ifdef DEBUG_CWPAINT
+{
+printf("Drawing ???...\n");
+#endif
+ DrawBorderedText(painter, buttonPos[i][0], buttonPos[i][1], QString("???"));
+#ifdef DEBUG_CWPAINT
+}
+#endif
+ }
+}
+
+
+void ControllerWidget::mousePressEvent(QMouseEvent * /*event*/)
+{
+ mouseDown = true;
+ update();
+}
+
+
+void ControllerWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
+{
+ mouseDown = false;
+ // Spawning the keygrabber causes leaveEvent() to be called, so we need to
+ // save this
+ int keyToHighlightSave = keyToHighlight;
+
+ KeyGrabber keyGrab(this);
+ keyGrab.SetKeyText(keyToHighlightSave);
+ keyGrab.exec();
+ int key = keyGrab.key;
+
+ if (key != Qt::Key_Escape)
+ {
+ keys[keyToHighlightSave] = key;
+ emit(KeyDefined(keyToHighlightSave, key));
+ }
+
+ keyToHighlight = keyToHighlightSave;
+ update();
+}
+
+
+void ControllerWidget::mouseMoveEvent(QMouseEvent * event)
+{
+ if (mouseDown)
+ return;
+
+ // Save the current closest item
+ int keyToHighlightOld = keyToHighlight;
+ // Set up closest distance (this should be large enough)
+ double closest = 1e9;
+
+ for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
+ {
+ // We loop through the button text positions, to see which one is closest.
+ double distX = (double)(event->x() - buttonPos[i][0]);
+ double distY = (double)(event->y() - buttonPos[i][1]);
+ double currentDistance = sqrt((distX * distX) + (distY * distY));
+
+ if (currentDistance < closest)
+ {
+ closest = currentDistance;
+ keyToHighlight = i;
+ }
+ }
+
+ if (keyToHighlightOld != keyToHighlight)
+ update();
+}
+
+
+void ControllerWidget::leaveEvent(QEvent * /*event*/)
+{
+ keyToHighlight = -1;
+ update();
+}
+
+
+void ControllerWidget::DrawBorderedText(QPainter & painter, int x, int y, QString text)
+{
+ // Text is drawn centered at (x, y) as well, using a bounding rect for the purpose.
+ QRect rect(0, 0, 60, 30);
+ QPen oldPen = painter.pen();
+ painter.setPen(QColor(0, 0, 0, 255)); // This is R,G,B,A
+
+ for(int i=-1; i<=1; i++)
+ {
+ for(int j=-1; j<=1; j++)
+ {
+ rect.moveCenter(QPoint(x + i, y + j));
+ painter.drawText(rect, Qt::AlignCenter, text);
+ }
+ }
+
+ painter.setPen(oldPen);
+ rect.moveCenter(QPoint(x, y));
+ painter.drawText(rect, Qt::AlignCenter, text);
+}
+