2014-12-03 08:39:06 +00:00
|
|
|
/* Copyright (c) 2013-2014 Jeffrey Pfau
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2014-11-07 11:11:44 +00:00
|
|
|
#include "KeyEditor.h"
|
|
|
|
|
2015-01-08 08:24:43 +00:00
|
|
|
#include "GamepadAxisEvent.h"
|
2015-01-04 12:23:20 +00:00
|
|
|
#include "GamepadButtonEvent.h"
|
|
|
|
|
2014-11-07 11:11:44 +00:00
|
|
|
#include <QKeyEvent>
|
|
|
|
|
|
|
|
using namespace QGBA;
|
|
|
|
|
|
|
|
KeyEditor::KeyEditor(QWidget* parent)
|
|
|
|
: QLineEdit(parent)
|
2015-01-08 08:24:43 +00:00
|
|
|
, m_direction(GamepadAxisEvent::NEUTRAL)
|
2015-07-24 07:01:10 +00:00
|
|
|
, m_key(-1)
|
|
|
|
, m_axis(-1)
|
2015-04-23 03:18:54 +00:00
|
|
|
, m_button(false)
|
2014-11-07 11:11:44 +00:00
|
|
|
{
|
|
|
|
setAlignment(Qt::AlignCenter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyEditor::setValue(int key) {
|
2015-07-24 07:01:10 +00:00
|
|
|
m_key = key;
|
2014-12-15 05:11:22 +00:00
|
|
|
if (m_button) {
|
2015-07-24 07:01:10 +00:00
|
|
|
updateButtonText();
|
2014-12-03 08:07:56 +00:00
|
|
|
} else {
|
|
|
|
setText(QKeySequence(key).toString(QKeySequence::NativeText));
|
|
|
|
}
|
2014-11-07 11:11:44 +00:00
|
|
|
emit valueChanged(key);
|
|
|
|
}
|
|
|
|
|
2014-12-15 05:11:22 +00:00
|
|
|
void KeyEditor::setValueKey(int key) {
|
|
|
|
m_button = false;
|
|
|
|
setValue(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyEditor::setValueButton(int button) {
|
|
|
|
m_button = true;
|
|
|
|
setValue(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyEditor::setValueAxis(int axis, int32_t value) {
|
|
|
|
m_button = true;
|
2015-07-24 07:01:10 +00:00
|
|
|
m_axis = axis;
|
2015-01-08 08:24:43 +00:00
|
|
|
m_direction = value < 0 ? GamepadAxisEvent::NEGATIVE : GamepadAxisEvent::POSITIVE;
|
2015-07-24 07:01:10 +00:00
|
|
|
updateButtonText();
|
2014-12-15 05:11:22 +00:00
|
|
|
emit axisChanged(axis, m_direction);
|
|
|
|
}
|
|
|
|
|
2014-11-07 11:11:44 +00:00
|
|
|
QSize KeyEditor::sizeHint() const {
|
|
|
|
QSize hint = QLineEdit::sizeHint();
|
|
|
|
hint.setWidth(40);
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyEditor::keyPressEvent(QKeyEvent* event) {
|
2014-12-15 05:11:22 +00:00
|
|
|
if (!m_button) {
|
2014-12-03 08:07:56 +00:00
|
|
|
setValue(event->key());
|
|
|
|
}
|
2014-11-07 11:11:44 +00:00
|
|
|
event->accept();
|
|
|
|
}
|
2015-01-04 12:23:20 +00:00
|
|
|
|
|
|
|
bool KeyEditor::event(QEvent* event) {
|
|
|
|
if (!m_button) {
|
|
|
|
return QWidget::event(event);
|
|
|
|
}
|
|
|
|
if (event->type() == GamepadButtonEvent::Down()) {
|
|
|
|
setValueButton(static_cast<GamepadButtonEvent*>(event)->value());
|
|
|
|
event->accept();
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-08 08:24:43 +00:00
|
|
|
if (event->type() == GamepadAxisEvent::Type()) {
|
|
|
|
GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
|
|
|
|
if (gae->isNew()) {
|
|
|
|
setValueAxis(gae->axis(), gae->direction());
|
|
|
|
}
|
|
|
|
event->accept();
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-04 12:23:20 +00:00
|
|
|
return QWidget::event(event);
|
|
|
|
}
|
2015-07-24 07:01:10 +00:00
|
|
|
|
|
|
|
void KeyEditor::updateButtonText() {
|
|
|
|
QStringList text;
|
|
|
|
if (m_key >= 0) {
|
|
|
|
text.append(QString::number(m_key));
|
|
|
|
}
|
|
|
|
if (m_direction != GamepadAxisEvent::NEUTRAL) {
|
|
|
|
text.append((m_direction == GamepadAxisEvent::NEGATIVE ? "-" : "+") + QString::number(m_axis));
|
|
|
|
}
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
setText(tr("---"));
|
|
|
|
} else {
|
|
|
|
setText(text.join("/"));
|
|
|
|
}
|
|
|
|
}
|