controller_config_util.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QPushButton>
  5. #include <QStyle>
  6. #include <QGridLayout>
  7. #include <QKeyEvent>
  8. #include <QHBoxLayout>
  9. #include <QLabel>
  10. #include "controller_config_util.h"
  11. /* TODO(bunnei): ImplementMe
  12. GStickConfig::GStickConfig(common::Config::Control leftid, common::Config::Control rightid, common::Config::Control upid, common::Config::Control downid, QObject* change_receiver, QWidget* parent) : QWidget(parent)
  13. {
  14. left = new GKeyConfigButton(leftid, style()->standardIcon(QStyle::SP_ArrowLeft), QString(), change_receiver, this);
  15. right = new GKeyConfigButton(rightid, style()->standardIcon(QStyle::SP_ArrowRight), QString(), change_receiver, this);
  16. up = new GKeyConfigButton(upid, style()->standardIcon(QStyle::SP_ArrowUp), QString(), change_receiver, this);
  17. down = new GKeyConfigButton(downid, style()->standardIcon(QStyle::SP_ArrowDown), QString(), change_receiver, this);
  18. clear = new QPushButton(tr("Clear"), this);
  19. QGridLayout* layout = new QGridLayout(this);
  20. layout->addWidget(left, 1, 0);
  21. layout->addWidget(right, 1, 2);
  22. layout->addWidget(up, 0, 1);
  23. layout->addWidget(down, 2, 1);
  24. layout->addWidget(clear, 1, 1);
  25. setLayout(layout);
  26. }
  27. GKeyConfigButton::GKeyConfigButton(common::Config::Control id, const QIcon& icon, const QString& text, QObject* change_receiver, QWidget* parent) : QPushButton(icon, text, parent), id(id), inputGrabbed(false)
  28. {
  29. connect(this, SIGNAL(clicked()), this, SLOT(OnClicked()));
  30. connect(this, SIGNAL(KeyAssigned(common::Config::Control, int, const QString&)), change_receiver, SLOT(OnKeyConfigChanged(common::Config::Control, int, const QString&)));
  31. connect(change_receiver, SIGNAL(ActivePortChanged(const common::Config::ControllerPort&)), this, SLOT(OnActivePortChanged(const common::Config::ControllerPort&)));
  32. }
  33. GKeyConfigButton::GKeyConfigButton(common::Config::Control id, const QString& text, QObject* change_receiver, QWidget* parent) : QPushButton(text, parent), id(id), inputGrabbed(false)
  34. {
  35. connect(this, SIGNAL(clicked()), this, SLOT(OnClicked()));
  36. connect(this, SIGNAL(KeyAssigned(common::Config::Control, int, const QString&)), change_receiver, SLOT(OnKeyConfigChanged(common::Config::Control, int, const QString&)));
  37. connect(change_receiver, SIGNAL(ActivePortChanged(const common::Config::ControllerPort&)), this, SLOT(OnActivePortChanged(const common::Config::ControllerPort&)));
  38. }
  39. void GKeyConfigButton::OnActivePortChanged(const common::Config::ControllerPort& config)
  40. {
  41. // TODO: Doesn't use joypad struct if that's the input source...
  42. QString text = QKeySequence(config.keys.key_code[id]).toString(); // has a nicer format
  43. if (config.keys.key_code[id] == Qt::Key_Shift) text = tr("Shift");
  44. else if (config.keys.key_code[id] == Qt::Key_Control) text = tr("Control");
  45. else if (config.keys.key_code[id] == Qt::Key_Alt) text = tr("Alt");
  46. else if (config.keys.key_code[id] == Qt::Key_Meta) text = tr("Meta");
  47. setText(text);
  48. }
  49. void GKeyConfigButton::OnClicked()
  50. {
  51. grabKeyboard();
  52. grabMouse();
  53. inputGrabbed = true;
  54. old_text = text();
  55. setText(tr("Input..."));
  56. }
  57. void GKeyConfigButton::keyPressEvent(QKeyEvent* event)
  58. {
  59. if (inputGrabbed)
  60. {
  61. releaseKeyboard();
  62. releaseMouse();
  63. setText(QString());
  64. // TODO: Doesn't capture "return" key
  65. // TODO: This doesn't quite work well, yet... find a better way
  66. QString text = QKeySequence(event->key()).toString(); // has a nicer format than event->text()
  67. int key = event->key();
  68. if (event->modifiers() == Qt::ShiftModifier) { text = tr("Shift"); key = Qt::Key_Shift; }
  69. else if (event->modifiers() == Qt::ControlModifier) { text = tr("Ctrl"); key = Qt::Key_Control; }
  70. else if (event->modifiers() == Qt::AltModifier) { text = tr("Alt"); key = Qt::Key_Alt; }
  71. else if (event->modifiers() == Qt::MetaModifier) { text = tr("Meta"); key = Qt::Key_Meta; }
  72. setText(old_text);
  73. emit KeyAssigned(id, key, text);
  74. inputGrabbed = false;
  75. // TODO: Keys like "return" cause another keyPressEvent to be generated after this one...
  76. }
  77. QPushButton::keyPressEvent(event); // TODO: Necessary?
  78. }
  79. void GKeyConfigButton::mousePressEvent(QMouseEvent* event)
  80. {
  81. // Abort key assignment
  82. if (inputGrabbed)
  83. {
  84. releaseKeyboard();
  85. releaseMouse();
  86. setText(old_text);
  87. inputGrabbed = false;
  88. }
  89. QAbstractButton::mousePressEvent(event);
  90. }
  91. GButtonConfigGroup::GButtonConfigGroup(const QString& name, common::Config::Control id, QObject* change_receiver, QWidget* parent) : QWidget(parent), id(id)
  92. {
  93. QHBoxLayout* layout = new QHBoxLayout(this);
  94. QPushButton* clear_button = new QPushButton(tr("Clear"));
  95. layout->addWidget(new QLabel(name, this));
  96. layout->addWidget(config_button = new GKeyConfigButton(id, QString(), change_receiver, this));
  97. layout->addWidget(clear_button);
  98. // TODO: connect config_button, clear_button
  99. setLayout(layout);
  100. }
  101. */