input system
This commit is contained in:
@@ -11,6 +11,7 @@ qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(nomad-shell
|
||||
src/main.cpp
|
||||
src/input/InputManager.cpp
|
||||
)
|
||||
|
||||
qt_add_qml_module(nomad-shell
|
||||
|
||||
+26
-4
@@ -26,16 +26,38 @@ QtObject {
|
||||
height: 540
|
||||
visible: true
|
||||
title: "nomad-bottom"
|
||||
property string lastInputText: "none"
|
||||
|
||||
Connections {
|
||||
target: inputManager
|
||||
|
||||
function onActionPressed(action) {
|
||||
bottomWindow.lastInputText = inputManager.actionName(action) + " pressed"
|
||||
}
|
||||
function onActionReleased(action) {
|
||||
bottomWindow.lastInputText = inputManager.actionName(action) + " released"
|
||||
}
|
||||
}
|
||||
|
||||
Rectangle {
|
||||
anchors.fill: parent
|
||||
color: "#181818"
|
||||
|
||||
Text {
|
||||
Column {
|
||||
anchors.centerIn: parent
|
||||
text: "nomad bottom screen"
|
||||
color: "white"
|
||||
font.pixelSize: 50
|
||||
spacing: 20
|
||||
|
||||
Text {
|
||||
text: "nomad bottom screen"
|
||||
color: "white"
|
||||
font.pixelSize: 50
|
||||
}
|
||||
|
||||
Text {
|
||||
text: "last input: " + bottomWindow.lastInputText
|
||||
color: "white"
|
||||
font.pixelSize: 30
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
#include "InputManager.h"
|
||||
|
||||
#include <QEvent>
|
||||
#include <QKeyEvent>
|
||||
|
||||
bool InputManager::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
// filter for keyboard presses and releases
|
||||
if (event->type() != QEvent::KeyPress &&
|
||||
event->type() != QEvent::KeyRelease) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// convert to QKeyEvent
|
||||
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
|
||||
|
||||
// translate to nomad Action
|
||||
Action action;
|
||||
|
||||
switch (keyEvent->key()) {
|
||||
case Qt::Key_Up:
|
||||
action = Action::Up;
|
||||
break;
|
||||
|
||||
case Qt::Key_Down:
|
||||
action = Action::Down;
|
||||
break;
|
||||
|
||||
case Qt::Key_Left:
|
||||
action = Action::Left;
|
||||
break;
|
||||
|
||||
case Qt::Key_Right:
|
||||
action = Action::Right;
|
||||
break;
|
||||
|
||||
|
||||
// Secondary directional controls. IJKL
|
||||
case Qt::Key_I:
|
||||
action = Action::Up2;
|
||||
break;
|
||||
|
||||
case Qt::Key_K:
|
||||
action = Action::Down2;
|
||||
break;
|
||||
|
||||
case Qt::Key_J:
|
||||
action = Action::Left2;
|
||||
break;
|
||||
|
||||
case Qt::Key_L:
|
||||
action = Action::Right2;
|
||||
break;
|
||||
|
||||
|
||||
// basic select and back buttons
|
||||
case Qt::Key_Return:
|
||||
case Qt::Key_Enter:
|
||||
action = Action::Select;
|
||||
break;
|
||||
|
||||
case Qt::Key_Escape:
|
||||
action = Action::Back;
|
||||
break;
|
||||
|
||||
// context buttons
|
||||
case Qt::Key_X:
|
||||
action = Action::Context1;
|
||||
break;
|
||||
|
||||
case Qt::Key_Y:
|
||||
action = Action::Context2;
|
||||
break;
|
||||
|
||||
case Qt::Key_Q:
|
||||
action = Action::ContextLeft1;
|
||||
break;
|
||||
|
||||
case Qt::Key_1:
|
||||
action = Action::ContextLeft2;
|
||||
break;
|
||||
|
||||
case Qt::Key_E:
|
||||
action = Action::ContextRight1;
|
||||
break;
|
||||
|
||||
case Qt::Key_3:
|
||||
action = Action::ContextRight2;
|
||||
break;
|
||||
|
||||
|
||||
// meta controls.
|
||||
case Qt::Key_Tab:
|
||||
action = Action::Start;
|
||||
break;
|
||||
|
||||
case Qt::Key_Space:
|
||||
action = Action::Options;
|
||||
break;
|
||||
|
||||
|
||||
// must not be a nomad input
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
// We've translated the physical key into a nomad Action.
|
||||
// Now distinguish between pressing it and releasing it.
|
||||
if (event->type() == QEvent::KeyPress) {
|
||||
emit actionPressed(action);
|
||||
} else {
|
||||
emit actionReleased(action);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QString InputManager::actionName(Action action) const
|
||||
{
|
||||
switch (action) {
|
||||
case Action::Up:
|
||||
return "Up";
|
||||
case Action::Down:
|
||||
return "Down";
|
||||
case Action::Left:
|
||||
return "Left";
|
||||
case Action::Right:
|
||||
return "Right";
|
||||
|
||||
case Action::Up2:
|
||||
return "Up2";
|
||||
case Action::Down2:
|
||||
return "Down2";
|
||||
case Action::Left2:
|
||||
return "Left2";
|
||||
case Action::Right2:
|
||||
return "Right2";
|
||||
|
||||
case Action::Select:
|
||||
return "Select";
|
||||
case Action::Back:
|
||||
return "Back";
|
||||
case Action::Context1:
|
||||
return "Context1";
|
||||
case Action::Context2:
|
||||
return "Context2";
|
||||
|
||||
case Action::ContextLeft1:
|
||||
return "ContextLeft1";
|
||||
case Action::ContextLeft2:
|
||||
return "ContextLeft2";
|
||||
case Action::ContextRight1:
|
||||
return "ContextRight1";
|
||||
case Action::ContextRight2:
|
||||
return "ContextRight2";
|
||||
|
||||
case Action::Start:
|
||||
return "Start";
|
||||
case Action::Options:
|
||||
return "Options";
|
||||
}
|
||||
|
||||
return "Unknown";
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
class QEvent;
|
||||
|
||||
class InputManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum class Action {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
|
||||
Up2,
|
||||
Down2,
|
||||
Left2,
|
||||
Right2,
|
||||
|
||||
Select,
|
||||
Back,
|
||||
Context1,
|
||||
Context2,
|
||||
|
||||
ContextLeft1,
|
||||
ContextLeft2,
|
||||
ContextRight1,
|
||||
ContextRight2,
|
||||
|
||||
Start,
|
||||
Options
|
||||
};
|
||||
Q_ENUM(Action)
|
||||
|
||||
Q_INVOKABLE QString actionName(Action action) const;
|
||||
|
||||
signals:
|
||||
void actionPressed(Action action);
|
||||
void actionReleased(Action action);
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *watched, QEvent *event) override;
|
||||
};
|
||||
@@ -1,11 +1,18 @@
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QQmlContext>
|
||||
#include "input/InputManager.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
InputManager inputManager;
|
||||
app.installEventFilter(&inputManager);
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
engine.rootContext()->setContextProperty("inputManager", &inputManager);
|
||||
|
||||
engine.loadFromModule("nomad", "Main");
|
||||
|
||||
if (engine.rootObjects().isEmpty())
|
||||
|
||||
Reference in New Issue
Block a user