浏览代码

Make it possible to use InputComponent::registerHostCommand with a lambda instead of a Slot

Tobias Hieta 9 年之前
父节点
当前提交
468a5e43f8
共有 3 个文件被更改,包括 42 次插入11 次删除
  1. 27 9
      src/input/InputComponent.cpp
  2. 11 2
      src/input/InputComponent.h
  3. 4 0
      src/system/SystemComponent.cpp

+ 27 - 9
src/input/InputComponent.cpp

@@ -117,12 +117,22 @@ void InputComponent::handleAction(const QString& action, bool autoRepeat)
       ReceiverSlot* recvSlot = m_hostCommands.value(hostCommand);
       if (recvSlot)
       {
-        QLOG_DEBUG() << "Invoking slot" << qPrintable(recvSlot->m_slot.data());
-        QGenericArgument arg0 = QGenericArgument();
-        if (recvSlot->m_hasArguments)
-          arg0 = Q_ARG(const QString&, hostArguments);
-        QMetaObject::invokeMethod(recvSlot->m_receiver, recvSlot->m_slot.data(),
-                                  Qt::AutoConnection, arg0);
+        if (recvSlot->m_function)
+        {
+          QLOG_DEBUG() << "Invoking anonymous function";
+          recvSlot->m_function();
+        }
+        else
+        {
+          QLOG_DEBUG() << "Invoking slot" << qPrintable(recvSlot->m_slot.data());
+          QGenericArgument arg0 = QGenericArgument();
+
+          if (recvSlot->m_hasArguments)
+            arg0 = Q_ARG(const QString&, hostArguments);
+
+          QMetaObject::invokeMethod(recvSlot->m_receiver, recvSlot->m_slot.data(),
+                                    Qt::AutoConnection, arg0);
+        }
       }
     }
     else
@@ -145,6 +155,8 @@ void InputComponent::remapInput(const QString &source, const QString &keycode, b
 {
   QLOG_DEBUG() << "Input received: source:" << source << "keycode:" << keycode << "pressed:" << (pressDown ? "down" : "release");
 
+  emit receivedInput();
+
   if (!pressDown)
   {
     m_autoRepeatTimer->stop();
@@ -164,9 +176,6 @@ void InputComponent::remapInput(const QString &source, const QString &keycode, b
     return;
   }
 
-  // hide mouse if it's visible.
-  SystemComponent::Get().setCursorVisibility(false);
-
   auto actions = m_mappings->mapToAction(source, keycode);
   if (actions.isEmpty())
   {
@@ -225,3 +234,12 @@ void InputComponent::registerHostCommand(const QString& command, QObject* receiv
     QLOG_ERROR() << "Slot for host command missing, or has incorrect signature!";
   }
 }
+
+/////////////////////////////////////////////////////////////////////////////////////////
+void InputComponent::registerHostCommand(const QString& command, std::function<void(void)> function)
+{
+  auto recvSlot = new ReceiverSlot;
+  recvSlot->m_function = function;
+  QLOG_DEBUG() << "Adding host command:" << qPrintable(command) << "mapped to anonymous function";
+  m_hostCommands.insert(command, recvSlot);
+}

+ 11 - 2
src/input/InputComponent.h

@@ -1,12 +1,15 @@
 #ifndef INPUTADAPTER_H
 #define INPUTADAPTER_H
 
+#include "ComponentManager.h"
+#include "InputMapping.h"
+
 #include <QThread>
 #include <QVariantMap>
 #include <QTimer>
 #include <QTime>
-#include "ComponentManager.h"
-#include "InputMapping.h"
+
+#include <functional>
 
 class InputBase : public QObject
 {
@@ -70,6 +73,7 @@ signals:
 
 struct ReceiverSlot
 {
+  std::function<void(void)> m_function;
   QObject* m_receiver;
   QByteArray m_slot;
   bool m_hasArguments;
@@ -86,8 +90,13 @@ public:
   bool componentInitialize() override;
 
   void registerHostCommand(const QString& command, QObject* receiver, const char* slot);
+  void registerHostCommand(const QString& command, std::function<void(void)> function);
 
 signals:
+  // Always emitted when any input arrives
+  void receivedInput();
+
+  // Emitted when we have managed to map input to a action
   void receivedAction(const QString& action);
 
 private Q_SLOTS:

+ 4 - 0
src/system/SystemComponent.cpp

@@ -71,6 +71,10 @@ SystemComponent::SystemComponent(QObject* parent) : ComponentBase(parent), m_pla
 bool SystemComponent::componentInitialize()
 {
   QDir().mkpath(Paths::dataDir("scripts"));
+
+  // Hide mouse pointer on any keyboard input
+  connect(&InputComponent::Get(), &InputComponent::receivedInput, [=]() { setCursorVisibility(false); });
+
   return true;
 }