Skip to content

Commit

Permalink
Merge pull request #607 from rampa3/feature/mouse_look
Browse files Browse the repository at this point in the history
Add mouse look script - continuation
  • Loading branch information
ksuprynowicz authored Sep 10, 2023
2 parents 79a58da + f8aec05 commit 9d6f08b
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 6 deletions.
2 changes: 1 addition & 1 deletion interface/resources/qml/hifi/tablet/ControllerSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ Item {
anchors.fill: stackView
id: controllerPrefereneces
objectName: "TabletControllerPreferences"
showCategories: ["VR Movement", "Game Controller", "Sixense Controllers", "Perception Neuron", "Leap Motion", "Open Sound Control (OSC)"]
showCategories: ["Desktop Movement", "VR Movement", "Game Controller", "Sixense Controllers", "Perception Neuron", "Leap Motion", "Open Sound Control (OSC)"]
categoryProperties: {
"VR Movement" : {
"User real-world height (meters)" : { "anchors.right" : "undefined" },
Expand Down
20 changes: 19 additions & 1 deletion interface/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,22 @@ bool setupEssentials(int& argc, char** argv, const QCommandLineParser& parser, b
DependencyManager::set<SceneScriptingInterface>();
#if !defined(DISABLE_QML)
DependencyManager::set<OffscreenUi>();
{
auto window = DependencyManager::get<OffscreenUi>()->getWindow();
auto desktopScriptingInterface = DependencyManager::get<DesktopScriptingInterface>();
QObject::connect(window, &QQuickWindow::focusObjectChanged, [desktopScriptingInterface](QObject *object) {
if (object) {
if (object->objectName() == QString("desktop")) {
emit desktopScriptingInterface->uiFocusChanged(false);
return;
}
// Signal with empty object name happens in addition to regular named ones and is not necessary here
if (!object->objectName().isEmpty()) {
emit desktopScriptingInterface->uiFocusChanged(true);
}
}
});
}
#endif
DependencyManager::set<Midi>();
DependencyManager::set<PathUtils>();
Expand Down Expand Up @@ -1489,7 +1505,6 @@ Application::Application(
qCDebug(interfaceapp, "Initialized Display");

if (_displayPlugin && !_displayPlugin->isHmd()) {
_preferredCursor.set(Cursor::Manager::getIconName(Cursor::Icon::SYSTEM));
showCursor(Cursor::Manager::lookupIcon(_preferredCursor.get()));
}
// An audio device changed signal received before the display plugins are set up will cause a crash,
Expand Down Expand Up @@ -6311,6 +6326,9 @@ void Application::update(float deltaTime) {
if (QCursor::pos() != point) {
_mouseCaptureTarget = point;
_ignoreMouseMove = true;
if (_captureMouse) {
_keyboardMouseDevice->updateMousePositionForCapture(QCursor::pos(), _mouseCaptureTarget);
}
QCursor::setPos(point);
}
}
Expand Down
4 changes: 3 additions & 1 deletion interface/src/scripting/DesktopScriptingInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ static const QVariantMap RELATIVE_POSITION_ANCHOR {
};

DesktopScriptingInterface::DesktopScriptingInterface(QObject* parent, bool restricted)
: QObject(parent), _restricted(restricted) { }
: QObject(parent), _restricted(restricted) {
connect(this, &DesktopScriptingInterface::uiFocusChanged, [this] (bool isActive) { _isOverlayWindowFocused = isActive; });
}

int DesktopScriptingInterface::getWidth() {
QSize size = qApp->getWindow()->windowHandle()->screen()->virtualSize();
Expand Down
17 changes: 17 additions & 0 deletions interface/src/scripting/DesktopScriptingInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@ class DesktopScriptingInterface : public QObject, public Dependency {
int getWidth();
int getHeight();

/*@jsdoc
* Checks whether the keyboard focus belongs to overlay UI window.
* @function Desktop.isOverlayWindowFocused
* @returns {boolean} <code>true</code> if the keyboard focus is on overlay UI window, <code>false</code> if not.
*/
Q_INVOKABLE bool isOverlayWindowFocused() { return _isOverlayWindowFocused; };

signals:

/*@jsdoc
* Triggered when keyboard focus changes to another overlay UI window.
* @param {boolean} isActive - <code>true</code> if the keyboard focus is on overlay UI window, <code>false</code> if not.
* @function Desktop.uiFocusChanged
* @returns {Signal}
*/
void uiFocusChanged(bool isActive);

private:
static int flagAlwaysOnTop() { return AlwaysOnTop; }
Expand All @@ -115,6 +131,7 @@ class DesktopScriptingInterface : public QObject, public Dependency {
static QVariantMap getRelativePositionAnchor();
Q_INVOKABLE static QVariantMap getPresentationMode();
const bool _restricted;
std::atomic<bool> _isOverlayWindowFocused { false };
};


Expand Down
2 changes: 2 additions & 0 deletions interface/src/ui/LoginDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "Application.h"
#include "scripting/HMDScriptingInterface.h"
#include "Constants.h"
#include "scripting/DesktopScriptingInterface.h"

HIFI_QML_DEF(LoginDialog)

Expand Down Expand Up @@ -67,6 +68,7 @@ void LoginDialog::showWithSelection() {
auto hmd = DependencyManager::get<HMDScriptingInterface>();

if (!qApp->isHMDMode()) {
emit DependencyManager::get<DesktopScriptingInterface>()->uiFocusChanged(true);
if (qApp->getLoginDialogPoppedUp()) {
LoginDialog::show();
return;
Expand Down
7 changes: 7 additions & 0 deletions interface/src/ui/PreferencesDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ void setupPreferences() {
preferences->addPreference(preference);
}

static const QString DESKTOP_MOVEMENT{ "Desktop Movement" };
{
auto getter = []()->bool { return qApp->getCamera().getMouseLook(); };
auto setter = [](bool value) { qApp->getCamera().setMouseLook(value); };
auto preference = new CheckPreference(DESKTOP_MOVEMENT, "Mouse look (toggle with M key)", getter, setter);
preferences->addPreference(preference);
}
static const QString VR_MOVEMENT{ "VR Movement" };
{
auto getter = [myAvatar]()->bool { return myAvatar->getAllowTeleporting(); };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,18 @@ void KeyboardMouseDevice::eraseMouseClicked() {
_inputDevice->_buttonPressedMap.erase(_inputDevice->makeInput(Qt::RightButton, true).getChannel());
}

void KeyboardMouseDevice::updateMousePositionForCapture(QPoint globalPos, QPointF captureTarget) {
if (!isNaN(captureTarget.x())) {
QPointF change = globalPos - captureTarget;
_accumulatedMove += QPoint(change.x(), change.y());
}
}

void KeyboardMouseDevice::mouseMoveEvent(QMouseEvent* event, bool capture, QPointF captureTarget) {
QPoint currentPos = event->pos();

if (!capture) {
_accumulatedMove += currentPos - _lastCursor;
} else if (!isNaN(captureTarget.x())) {
QPointF change = event->globalPos() - captureTarget;
_accumulatedMove += QPoint(change.x(), change.y());
}

// FIXME - this has the characteristic that it will show large jumps when you move the cursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class KeyboardMouseDevice : public InputPlugin {
void wheelEvent(QWheelEvent* event);
bool isWheelByTouchPad(QWheelEvent* event);

// This gets called from Application::update just before resetting cursor position when mouse capture is enabled
void updateMousePositionForCapture(QPoint globalPos, QPointF captureTarget);

static void enableTouch(bool enableTouch) { _enableTouch = enableTouch; }

static const char* NAME;
Expand Down
7 changes: 7 additions & 0 deletions libraries/shared/src/shared/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ QString Camera::getModeString() const {
return modeToString(_mode);
}

void Camera::setMouseLook(bool mouseLook) { Setting::Handle<bool>{"MouseLookAllowed", false }.set(mouseLook);
if (!mouseLook) {
setCaptureMouse(false);
}
emit mouseLookChanged(mouseLook);
}

void Camera::lookAt(const glm::vec3& lookAt) {
glm::vec3 up = IDENTITY_UP;
glm::mat4 lookAtMatrix = glm::lookAt(_position, lookAt, up);
Expand Down
32 changes: 32 additions & 0 deletions libraries/shared/src/shared/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "../GLMHelpers.h"
#include "../RegisteredMetaTypes.h"
#include "../ViewFrustum.h"
#include "../SettingHandle.h"

enum CameraMode
{
Expand Down Expand Up @@ -130,6 +131,23 @@ public slots:
*/
void setCaptureMouse(bool captureMouse) { _captureMouse = captureMouse; emit captureMouseChanged(captureMouse); }

/*@jsdoc
* Gets the current mouse look setting state.
* @function Camera.getMouseLook
* @returns {boolean} <code>true</code> if the mouse look setting is enabled (mouse look can be toggled with M key in this
* mode), <code>false</code> if the mouse look setting is disabled.
*/
bool getMouseLook() const { return Setting::Handle<bool>{"MouseLookAllowed", false }.get(); }

/*@jsdoc
* Sets the mouse look setting state. When <code>true</code>, the mouse look setting is enabled (mouse look can be toggled
* with M key in this mode). When <code>false</code>, the mouse behaves normally.
* @function Camera.setMouseLook
* @param {boolean} mouseLook - <code>true</code> to enable mouse look setting, <code>false</code> to disable mouse look
* setting.
*/
void setMouseLook(bool mouseLook);

/*@jsdoc
* Gets the current camera sensitivity.
* @function Camera.getSensitivity
Expand Down Expand Up @@ -229,6 +247,20 @@ public slots:
*/
void captureMouseChanged(bool newCaptureMouse);

/*@jsdoc
* Triggered when mouse look setting changes.
* @function Camera.mouseLookChanged
* @param {boolean} mouseLookChanged - The new mouse look state.
* @returns {Signal}
* @example <caption>Report mouse look state changes.</caption>
* function onMouseLookChanged(newMouseLook) {
* print("The mouse look has changed to " + newMouseLook);
* }
*
* Camera.mouseLookChanged.connect(onMouseLookChanged);
*/
void mouseLookChanged(bool newMouseLook);

private:
void recompose();
void decompose();
Expand Down
1 change: 1 addition & 0 deletions scripts/system/controllers/controllerScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var CONTOLLER_SCRIPTS = [
"grab.js",
"toggleAdvancedMovementForHandControllers.js",
"handTouch.js",
"mouseLook.js",
"controllerModules/nearParentGrabOverlay.js",
"controllerModules/stylusInput.js",
"controllerModules/equipEntity.js",
Expand Down
Loading

0 comments on commit 9d6f08b

Please sign in to comment.