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-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)
|
2014-12-15 05:11:22 +00:00
|
|
|
, m_direction(InputController::NEUTRAL)
|
2014-11-07 11:11:44 +00:00
|
|
|
{
|
|
|
|
setAlignment(Qt::AlignCenter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyEditor::setValue(int key) {
|
2014-12-15 05:11:22 +00:00
|
|
|
if (m_button) {
|
2014-12-03 08:07:56 +00:00
|
|
|
setText(QString::number(key));
|
|
|
|
} else {
|
|
|
|
setText(QKeySequence(key).toString(QKeySequence::NativeText));
|
|
|
|
}
|
2014-11-07 11:11:44 +00:00
|
|
|
m_key = key;
|
|
|
|
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;
|
2015-01-05 06:35:41 +00:00
|
|
|
m_direction = InputController::NEUTRAL;
|
2014-12-15 05:11:22 +00:00
|
|
|
setValue(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyEditor::setValueAxis(int axis, int32_t value) {
|
|
|
|
m_button = true;
|
|
|
|
m_key = axis;
|
|
|
|
m_direction = value < 0 ? InputController::NEGATIVE : InputController::POSITIVE;
|
|
|
|
setText((value < 0 ? "-" : "+") + QString::number(axis));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return QWidget::event(event);
|
|
|
|
}
|