1// Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "mousedevice_p.h"
5
6#include <Qt3DInput/qmousedevice.h>
7#include <Qt3DCore/qentity.h>
8#include <Qt3DCore/qnode.h>
9
10#include <Qt3DInput/private/inputmanagers_p.h>
11#include <Qt3DInput/private/inputhandler_p.h>
12#include <Qt3DInput/private/qmousedevice_p.h>
13
14QT_BEGIN_NAMESPACE
15
16namespace Qt3DInput {
17namespace Input {
18
19MouseDevice::MouseDevice()
20 : QAbstractPhysicalDeviceBackendNode(ReadOnly)
21 , m_inputHandler(nullptr)
22 , m_wasPressed(false)
23 , m_sensitivity(0.1f)
24 , m_updateAxesContinuously(false)
25{
26}
27
28MouseDevice::~MouseDevice()
29{
30}
31
32void MouseDevice::setInputHandler(InputHandler *handler)
33{
34 m_inputHandler = handler;
35}
36
37InputHandler *MouseDevice::inputHandler() const
38{
39 return m_inputHandler;
40}
41
42float MouseDevice::axisValue(int axisIdentifier) const
43{
44 switch (axisIdentifier) {
45 case QMouseDevice::X:
46 return m_mouseState.xAxis;
47 case QMouseDevice::Y:
48 return m_mouseState.yAxis;
49 case QMouseDevice::WheelX:
50 return m_mouseState.wXAxis;
51 case QMouseDevice::WheelY:
52 return m_mouseState.wYAxis;
53 default:
54 break;
55 }
56 return 0.0f;
57}
58
59bool MouseDevice::isButtonPressed(int buttonIdentifier) const
60{
61 switch (buttonIdentifier) {
62 case QMouseEvent::LeftButton:
63 return m_mouseState.leftPressed;
64 case QMouseEvent::MiddleButton:
65 return m_mouseState.centerPressed;
66 case QMouseEvent::RightButton:
67 return m_mouseState.rightPressed;
68 default:
69 break;
70 }
71 return false;
72}
73
74MouseDevice::MouseState MouseDevice::mouseState() const
75{
76 return m_mouseState;
77}
78
79QPointF MouseDevice::previousPos() const
80{
81 return m_previousPos;
82}
83
84bool MouseDevice::wasPressed() const
85{
86 return m_wasPressed;
87}
88
89float MouseDevice::sensitivity() const
90{
91 return m_sensitivity;
92}
93
94bool MouseDevice::updateAxesContinuously() const
95{
96 return m_updateAxesContinuously;
97}
98
99#if QT_CONFIG(wheelevent)
100void MouseDevice::updateWheelEvent(QT_PREPEND_NAMESPACE(QWheelEvent) *event)
101{
102 m_mouseState.wXAxis += m_sensitivity * event->angleDelta().x();
103 m_mouseState.wYAxis += m_sensitivity * event->angleDelta().y();
104}
105#endif
106
107void MouseDevice::updateMouseEvent(QT_PREPEND_NAMESPACE(QMouseEvent) *event)
108{
109 m_mouseState.leftPressed = event->buttons() & (Qt::LeftButton);
110 m_mouseState.centerPressed = event->buttons() & (Qt::MiddleButton);
111 m_mouseState.rightPressed = event->buttons() & (Qt::RightButton);
112 const bool pressed = m_mouseState.leftPressed || m_mouseState.centerPressed || m_mouseState.rightPressed;
113 if (m_updateAxesContinuously || (m_wasPressed && pressed)) {
114 m_mouseState.xAxis += m_sensitivity * float(event->globalPosition().x() - m_previousPos.x());
115 m_mouseState.yAxis += m_sensitivity * float(m_previousPos.y() - event->globalPosition().y());
116 }
117 m_wasPressed = pressed;
118 m_previousPos = event->globalPosition();
119}
120
121void MouseDevice::resetMouseAxisState()
122{
123 // Reset axis values before we accumulate new values for this frame
124 m_mouseState.xAxis = 0.0f;
125 m_mouseState.yAxis = 0.0f;
126#if QT_CONFIG(wheelevent)
127 m_mouseState.wXAxis = 0.0f;
128 m_mouseState.wYAxis = 0.0f;
129#endif
130}
131
132void MouseDevice::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime)
133{
134 QAbstractPhysicalDeviceBackendNode::syncFromFrontEnd(frontEnd, firstTime);
135 const Qt3DInput::QMouseDevice *node = qobject_cast<const Qt3DInput::QMouseDevice *>(object: frontEnd);
136 if (!node)
137 return;
138
139 m_sensitivity = node->sensitivity();
140 m_updateAxesContinuously = node->updateAxesContinuously();
141}
142
143MouseDeviceFunctor::MouseDeviceFunctor(QInputAspect *inputAspect, InputHandler *handler)
144 : m_inputAspect(inputAspect)
145 , m_handler(handler)
146{
147}
148
149Qt3DCore::QBackendNode *MouseDeviceFunctor::create(Qt3DCore::QNodeId id) const
150{
151 MouseDevice *controller = m_handler->mouseDeviceManager()->getOrCreateResource(id);
152 controller->setInputAspect(m_inputAspect);
153 controller->setInputHandler(m_handler);
154 m_handler->appendMouseDevice(device: m_handler->mouseDeviceManager()->lookupHandle(id));
155 return controller;
156}
157
158Qt3DCore::QBackendNode *MouseDeviceFunctor::get(Qt3DCore::QNodeId id) const
159{
160 return m_handler->mouseDeviceManager()->lookupResource(id);
161}
162
163void MouseDeviceFunctor::destroy(Qt3DCore::QNodeId id) const
164{
165 m_handler->removeMouseDevice(device: m_handler->mouseDeviceManager()->lookupHandle(id));
166 m_handler->mouseDeviceManager()->releaseResource(id);
167}
168
169} // namespace Input
170} // namespace Qt3DInput
171
172QT_END_NAMESPACE
173

source code of qt3d/src/input/backend/mousedevice.cpp