VR-Engage  2.2
Loading...
Searching...
No Matches
qmlGamepad.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies, Inc.
3** All rights reserved.
4*******************************************************************************/
5//! \file qmlGamepad.h
6//! \brief QML-based virtual gamepad UI component for custom input device example
7//!
8//! DtQmlGamepad implements a touch screen interface mimicking a gamepad, demonstrating
9//! how to integrate QML-based UI with VR-Engage's input system. This serves as the
10//! user-facing UI component for the custom input device example, showing the complete
11//! pattern for bridging QML events into VR-Engage's C++ input processing pipeline.
12
13#pragma once
14
15#include "export.h"
16
18
19#include <vrvUtil/signalslib.h>
20#include "vreUtil/delegate.h"
21
22#include <QObject>
23
24#include <unordered_set>
25
26class QQuickItem;
27
28namespace makVre
29{
30//! \brief QML-based virtual gamepad UI that bridges QML events to C++ input callbacks
31//!
32//! DtQmlGamepad loads a QML user interface displaying virtual gamepad controls (buttons,
33//! joysticks) and translates Qt Quick touch events into a generic event format that can
34//! be consumed by VR-Engage's input system.
35//!
36//! Key Patterns Demonstrated:
37//! - QML-to-C++ event bridging using Q_INVOKABLE methods
38//! - Context property registration for QML access to C++ objects
39//! - QML page lifecycle management (load, show/hide, unload)
40//! - Application state-driven UI visibility control
41//! - Generic event callback system using DtDelegate
42//!
43//! This class acts as the "view" layer in a custom input device implementation,
44//! handling presentation and user interaction while delegating input processing
45//! to DtQmlGamepadInputDevice.
46//!
47//! Integration with DtQmlGamepadInputDevice:
48//! DtQmlGamepadInputDevice registers callbacks with this class to receive input
49//! events, then transforms them into DtInputData structures for VR-Engage's
50//! input mapping system.
51//!
52class INPUTDEVICE_DLL DtQmlGampad : public QObject
53{
54 Q_OBJECT;
55
56public:
58 virtual ~DtQmlGampad();
59
60 //! \brief Register a callback to receive virtual gamepad input events
61 //!
62 //! PATTERN: Callback Registration for Custom Input Devices
63 //! Custom input devices should provide a callback mechanism allowing consumers
64 //! to receive events without tight coupling. This pattern uses DtDelegate to
65 //! store member function pointers with type-safe invocation.
66 //!
67 //! The callback signature is: void(std::string eventType, int id, double value)
68 //! - eventType: "axis", "button", "hat", etc.
69 //! - id: Control identifier (e.g., axis 0, button 1)
70 //! - value: Event value (axis deflection, button state)
71 //!
72 //! REUSABLE: This callback pattern can be adapted for any event-driven device
73 //! interface where multiple consumers need to receive events.
74 //!
75 //! \tparam T Type of the object receiving callbacks
76 //! \tparam Method Member function pointer type
77 //! \param object Pointer to the object instance that will receive callbacks
78 //! \param method Member function to invoke when events occur
79 //!
80 template <typename T, typename Method>
81 void addEventCallback(T* object, Method method)
82 {
83 myEventCallbacks[object] = EventCallback(object, method);
84 }
85
86 //! \brief Remove a previously registered event callback
87 //!
88 //! Should be called during cleanup (typically in the input device's shutdown()
89 //! method) to prevent callbacks to destroyed objects.
90 //!
91 //! \tparam T Type of the object that registered the callback
92 //! \tparam Method Member function pointer type (unused but kept for symmetry)
93 //! \param object Pointer to the object whose callback should be removed
94 //! \param method Member function (unused in current implementation)
95 //!
96 template <typename T, typename Method>
97 void removeEventCallback(T* object, Method method)
98 {
99 myEventCallbacks.erase(object);
100 }
101
102 //! \brief QML-invokable method called when virtual gamepad generates input
103 //!
104 //! PATTERN: QML-to-C++ Event Bridge
105 //! This method is marked Q_INVOKABLE and registered as a QML context property
106 //! (named "DtQmlGamepad" in QML), allowing the QML UI to call directly into
107 //! C++ when user interacts with on-screen controls:
108 //!
109 //! // In QML:
110 //! onPressed: DtQmlGamepad.qmlEvent("button", buttonId, 1.0)
111 //!
112 //! The Q_INVOKABLE macro makes this method accessible from QML script code.
113 //! Qt's meta-object system handles type conversions between QML types (QString,
114 //! qreal) and C++ types automatically.
115 //!
116 //! REUSABLE: This pattern applies to any QML-based custom UI that needs to
117 //! communicate events to VR-Engage components.
118 //!
119 //! \param type Input event type string ("axis", "button", "hat", etc.)
120 //! \param id Control identifier within that type (e.g., button 0, axis 1)
121 //! \param value Event value (button pressed=1.0/released=0.0, axis deflection)
122 //!
123 virtual Q_INVOKABLE void qmlEvent(QString type, int id, qreal value);
124
125 //! \brief Show the virtual gamepad UI
126 //!
127 //! Typically called when entering engaged/gameplay state. The QML page becomes
128 //! visible and interactive.
129 //!
130 virtual void show();
131
132 //! \brief Hide the virtual gamepad UI
133 //!
134 //! Typically called when leaving engaged state (returning to menus, disconnecting).
135 //! The QML page is hidden but remains loaded for quick re-display.
136 //!
137 virtual void hide();
138
139protected:
140 //! \brief Message handler to automatically show/hide gamepad based on app state
141 //!
142 //! PATTERN: UI Visibility Driven by Application State
143 //! VR-Engage applications progress through states: Disconnected → Connecting →
144 //! RoleSelection → Engaged. Custom UI should typically only be visible during
145 //! the ENGAGED_STATE when the player is actively controlling an entity.
146 //!
147 //! This handler listens for state change messages and automatically shows/hides
148 //! the gamepad appropriately. This is preferable to manually tracking state
149 //! in multiple places.
150 //!
151 //! \param msg State change message from DtPlayerStationStateManager
152 //! \return HANDLED to indicate message was processed
153 //!
155
156 //! \brief Check if QML gamepad UI is currently visible
157 //! \return true if visible, false if hidden or not loaded
158 virtual bool isVisible();
159
160 //! \brief Set QML gamepad UI visibility state
161 //! \param visible true to show, false to hide
162 virtual void setVisibility(bool visible);
163
164protected:
165 //! Callback delegate type for input events
167
168 QQuickItem* myRoot; //!< Root QML item of the gamepad UI
169 std::string myTargetWindow; //!< Window identifier for QML placement
170 std::string myTargetFile; //!< Path to QML file defining UI
171
172 //! Registered event callbacks keyed by object pointer
173 //! When input events occur, all registered callbacks are invoked
174 std::map<void*, EventCallback> myEventCallbacks;
175};
176
177} // namespace makVre
Modern delegate class that can bind and invoke callables with any number of parameters.
Definition delegate.h:31
DtDelegate< void, std::string, int, double > EventCallback
Callback delegate type for input events.
Definition qmlGamepad.h:166
virtual void hide()
Hide the virtual gamepad UI.
virtual makVre::DtVreMessageResult handleAppStateMessage(makVre::DtVreMessage *msg)
Message handler to automatically show/hide gamepad based on app state.
virtual Q_INVOKABLE void qmlEvent(QString type, int id, qreal value)
QML-invokable method called when virtual gamepad generates input.
virtual ~DtQmlGampad()
virtual void show()
Show the virtual gamepad UI.
std::string myTargetFile
Path to QML file defining UI.
Definition qmlGamepad.h:170
virtual bool isVisible()
Check if QML gamepad UI is currently visible.
QQuickItem * myRoot
Root QML item of the gamepad UI.
Definition qmlGamepad.h:168
void addEventCallback(T *object, Method method)
Register a callback to receive virtual gamepad input events.
Definition qmlGamepad.h:81
std::map< void *, EventCallback > myEventCallbacks
Registered event callbacks keyed by object pointer When input events occur, all registered callbacks ...
Definition qmlGamepad.h:174
void removeEventCallback(T *object, Method method)
Remove a previously registered event callback.
Definition qmlGamepad.h:97
std::string myTargetWindow
Window identifier for QML placement.
Definition qmlGamepad.h:169
virtual void setVisibility(bool visible)
Set QML gamepad UI visibility state.
Abstract base class for all VREngage messages.
Definition vreMessage.h:50
#define INPUTDEVICE_DLL
Definition export.h:15
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
DtVreMessageResult
Enumeration of possible message handling results.
Definition vreMessage.h:33
Defines the base class for all VREngage messages.