| 1 | // |
| 2 | // keygrabber.cpp - Widget to grab a key and dismiss itself |
| 3 | // |
| 4 | // by James Hammons |
| 5 | // (C) 2011 Underground Software |
| 6 | // |
| 7 | // JLH = James Hammons <jlhamm@acm.org> |
| 8 | // |
| 9 | // Who When What |
| 10 | // --- ---------- ------------------------------------------------------------ |
| 11 | // JLH 07/18/2011 Created this file |
| 12 | // |
| 13 | |
| 14 | #include "keygrabber.h" |
| 15 | #include "gamepad.h" |
| 16 | |
| 17 | |
| 18 | // Class variables |
| 19 | // These need to be preserved between calls to this class, otherwise bad stuff |
| 20 | // (like controllers not working correctly) can happen. |
| 21 | /*static*/ bool KeyGrabber::buttonDown = false; |
| 22 | /*static*/ int KeyGrabber::button = -1; |
| 23 | |
| 24 | |
| 25 | KeyGrabber::KeyGrabber(QWidget * parent/*= 0*/): QDialog(parent), |
| 26 | label(new QLabel), timer(new QTimer)//, buttonDown(false) |
| 27 | { |
| 28 | // label = new QLabel(this); |
| 29 | QVBoxLayout * mainLayout = new QVBoxLayout; |
| 30 | mainLayout->addWidget(label); |
| 31 | setLayout(mainLayout); |
| 32 | setWindowTitle(tr("Grab")); |
| 33 | connect(timer, SIGNAL(timeout()), this, SLOT(CheckGamepad())); |
| 34 | timer->setInterval(100); |
| 35 | timer->start(); |
| 36 | |
| 37 | // Will this make Mac OSX work??? |
| 38 | setFocusPolicy(Qt::StrongFocus); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | KeyGrabber::~KeyGrabber() |
| 43 | { |
| 44 | timer->stop(); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | void KeyGrabber::SetKeyText(int keyNum) |
| 49 | { |
| 50 | char jagButtonName[21][10] = { "Up", "Down", "Left", "Right", |
| 51 | "*", "7", "4", "1", "0", "8", "5", "2", "#", "9", "6", "3", |
| 52 | "A", "B", "C", "Option", "Pause" }; |
| 53 | |
| 54 | QString text = QString(tr("Press key for \"%1\"<br>(ESC to cancel)")) |
| 55 | .arg(QString(jagButtonName[keyNum])); |
| 56 | label->setText(text); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void KeyGrabber::keyPressEvent(QKeyEvent * e) |
| 61 | { |
| 62 | key = e->key(); |
| 63 | |
| 64 | // Since this is problematic, we don't allow this key... |
| 65 | if (key != Qt::Key_Alt) |
| 66 | accept(); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | void KeyGrabber::CheckGamepad(void) |
| 71 | { |
| 72 | // How do we determine which joystick it is, if more than one? As it turns |
| 73 | // out, we don't really have to care. It's up to the user to play nice with |
| 74 | // the interface because while we can enforce a 'first user to press a |
| 75 | // button wins' type of thing, it doesn't really buy you anything that you |
| 76 | // couldn't get by having the users involved behave like nice people. :-P |
| 77 | Gamepad::Update(); |
| 78 | |
| 79 | if (!buttonDown) |
| 80 | { |
| 81 | button = Gamepad::CheckButtonPressed(); |
| 82 | |
| 83 | if (button == -1) |
| 84 | return; |
| 85 | |
| 86 | // Do it so that it sets the button on button down, not release :-P |
| 87 | key = button; |
| 88 | accept(); |
| 89 | buttonDown = true; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | if (Gamepad::CheckButtonPressed() == button) |
| 94 | return; |
| 95 | |
| 96 | // key = button; |
| 97 | // accept(); |
| 98 | buttonDown = false; |
| 99 | } |
| 100 | } |
| 101 | |