mirror of
https://thingvellir.net/git/overte
synced 2025-03-27 23:52:03 +01:00
Add WindowScriptingInterface and LocationScriptingInterface
This commit is contained in:
parent
3ab61f231b
commit
b9f296ddb3
4 changed files with 223 additions and 0 deletions
49
interface/src/scripting/LocationScriptingInterface.cpp
Normal file
49
interface/src/scripting/LocationScriptingInterface.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// LocationScriptingInterface.cpp
|
||||
// interface/src/scripting
|
||||
//
|
||||
// Created by Ryan Huffman on 4/29/14.
|
||||
// Copyright 2014 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "NodeList.h"
|
||||
|
||||
#include "LocationScriptingInterface.h"
|
||||
|
||||
LocationScriptingInterface* LocationScriptingInterface::getInstance() {
|
||||
static LocationScriptingInterface sharedInstance;
|
||||
return &sharedInstance;
|
||||
}
|
||||
|
||||
QString LocationScriptingInterface::getHref() {
|
||||
return getProtocol() + "//" + getHostname() + getPathname();
|
||||
}
|
||||
|
||||
QString LocationScriptingInterface::getPathname() {
|
||||
const glm::vec3& position = Application::getInstance()->getAvatar()->getPosition();
|
||||
QString path;
|
||||
path.sprintf("/%.4f,%.4f,%.4f", position.x, position.y, position.z);
|
||||
return path;
|
||||
}
|
||||
|
||||
QString LocationScriptingInterface::getHostname() {
|
||||
return NodeList::getInstance()->getDomainHandler().getHostname();
|
||||
}
|
||||
|
||||
void LocationScriptingInterface::assign(const QString& url) {
|
||||
QMetaObject::invokeMethod(Menu::getInstance(), "goToURL", Q_ARG(const QString&, url));
|
||||
}
|
||||
|
||||
QScriptValue LocationScriptingInterface::locationGetter(QScriptContext* context, QScriptEngine* engine) {
|
||||
return engine->newQObject(getInstance());
|
||||
}
|
||||
|
||||
QScriptValue LocationScriptingInterface::locationSetter(QScriptContext* context, QScriptEngine* engine) {
|
||||
LocationScriptingInterface::getInstance()->assign(context->argument(0).toString());
|
||||
return QScriptValue::UndefinedValue;
|
||||
}
|
46
interface/src/scripting/LocationScriptingInterface.h
Normal file
46
interface/src/scripting/LocationScriptingInterface.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
//
|
||||
// LocationScriptingInterface.h
|
||||
// interface/src/scripting
|
||||
//
|
||||
// Created by Ryan Huffman on 4/29/14.
|
||||
// Copyright 2014 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#ifndef hifi_LocationScriptingInterface_h
|
||||
#define hifi_LocationScriptingInterface_h
|
||||
|
||||
#include <QObject>
|
||||
#include <QScriptContext>
|
||||
#include <QScriptEngine>
|
||||
#include <QScriptValue>
|
||||
#include <QString>
|
||||
|
||||
#include "Application.h"
|
||||
|
||||
class LocationScriptingInterface : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString href READ getHref)
|
||||
Q_PROPERTY(QString protocol READ getProtocol)
|
||||
Q_PROPERTY(QString hostname READ getHostname)
|
||||
Q_PROPERTY(QString pathname READ getPathname)
|
||||
LocationScriptingInterface() { };
|
||||
public:
|
||||
static LocationScriptingInterface* getInstance();
|
||||
|
||||
QString getHref();
|
||||
QString getProtocol() { return CUSTOM_URL_SCHEME; };
|
||||
QString getPathname();
|
||||
QString getHostname();
|
||||
|
||||
static QScriptValue locationGetter(QScriptContext* context, QScriptEngine* engine);
|
||||
static QScriptValue locationSetter(QScriptContext* context, QScriptEngine* engine);
|
||||
|
||||
public slots:
|
||||
void assign(const QString& url);
|
||||
|
||||
};
|
||||
|
||||
#endif // hifi_LocationScriptingInterface_h
|
85
interface/src/scripting/WindowScriptingInterface.cpp
Normal file
85
interface/src/scripting/WindowScriptingInterface.cpp
Normal file
|
@ -0,0 +1,85 @@
|
|||
//
|
||||
// WindowScriptingInterface.cpp
|
||||
// interface/src/scripting
|
||||
//
|
||||
// Created by Ryan Huffman on 4/29/14.
|
||||
// Copyright 2014 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#include <QMessageBox>
|
||||
#include <QInputDialog>
|
||||
|
||||
#include "Application.h"
|
||||
#include "Menu.h"
|
||||
|
||||
#include "WindowScriptingInterface.h"
|
||||
|
||||
WindowScriptingInterface* WindowScriptingInterface::getInstance() {
|
||||
static WindowScriptingInterface sharedInstance;
|
||||
return &sharedInstance;
|
||||
}
|
||||
|
||||
QScriptValue WindowScriptingInterface::alert(const QString& message) {
|
||||
QScriptValue retVal;
|
||||
QMetaObject::invokeMethod(this, "showAlert", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QScriptValue, retVal), Q_ARG(const QString&, message));
|
||||
return retVal;
|
||||
}
|
||||
|
||||
QScriptValue WindowScriptingInterface::confirm(const QString& message) {
|
||||
bool retVal;
|
||||
QMetaObject::invokeMethod(this, "showConfirm", Qt::BlockingQueuedConnection,
|
||||
Q_RETURN_ARG(bool, retVal), Q_ARG(const QString&, message));
|
||||
return QScriptValue(retVal);
|
||||
}
|
||||
|
||||
QScriptValue WindowScriptingInterface::prompt(const QString& message, const QString& defaultText) {
|
||||
QScriptValue retVal;
|
||||
QMetaObject::invokeMethod(this, "showPrompt", Qt::BlockingQueuedConnection,
|
||||
Q_RETURN_ARG(QScriptValue, retVal),
|
||||
Q_ARG(const QString&, message), Q_ARG(const QString&, defaultText));
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/// Display an alert box
|
||||
/// \param const QString& message message to display
|
||||
/// \return QScriptValue::UndefinedValue
|
||||
QScriptValue WindowScriptingInterface::showAlert(const QString& message) {
|
||||
QMessageBox::warning(Application::getInstance()->getWindow(), "", message);
|
||||
return QScriptValue::UndefinedValue;
|
||||
}
|
||||
|
||||
/// Display a confirmation box with the options 'Yes' and 'No'
|
||||
/// \param const QString& message message to display
|
||||
/// \return QScriptValue `true` if 'Yes' was clicked, `false` otherwise
|
||||
QScriptValue WindowScriptingInterface::showConfirm(const QString& message) {
|
||||
QMessageBox::StandardButton response = QMessageBox::question(Application::getInstance()->getWindow(), "", message);
|
||||
return QScriptValue(response == QMessageBox::Yes);
|
||||
}
|
||||
|
||||
/// Display a prompt with a text box
|
||||
/// \param const QString& message message to display
|
||||
/// \param const QString& defaultText default text in the text box
|
||||
/// \return QScriptValue string text value in text box if the dialog was accepted, `null` otherwise.
|
||||
QScriptValue WindowScriptingInterface::showPrompt(const QString& message, const QString& defaultText) {
|
||||
QInputDialog promptDialog(Application::getInstance()->getWindow());
|
||||
promptDialog.setWindowTitle("");
|
||||
promptDialog.setLabelText(message);
|
||||
promptDialog.setTextValue(defaultText);
|
||||
|
||||
if (promptDialog.exec() == QDialog::Accepted) {
|
||||
return QScriptValue(promptDialog.textValue());
|
||||
}
|
||||
|
||||
return QScriptValue::NullValue;
|
||||
}
|
||||
|
||||
int WindowScriptingInterface::getInnerWidth() {
|
||||
return Application::getInstance()->getWindow()->geometry().width();
|
||||
}
|
||||
|
||||
int WindowScriptingInterface::getInnerHeight() {
|
||||
return Application::getInstance()->getWindow()->geometry().height();
|
||||
}
|
43
interface/src/scripting/WindowScriptingInterface.h
Normal file
43
interface/src/scripting/WindowScriptingInterface.h
Normal file
|
@ -0,0 +1,43 @@
|
|||
//
|
||||
// WindowScriptingInterface.cpp
|
||||
// interface/src/scripting
|
||||
//
|
||||
// Created by Ryan Huffman on 4/29/14.
|
||||
// Copyright 2014 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#ifndef hifi_WindowScriptingInterface_h
|
||||
#define hifi_WindowScriptingInterface_h
|
||||
|
||||
#include <QDebug>
|
||||
#include <QMutex>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
#include "LocationScriptingInterface.h"
|
||||
|
||||
class WindowScriptingInterface : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(int innerWidth READ getInnerWidth)
|
||||
Q_PROPERTY(int innerHeight READ getInnerHeight)
|
||||
WindowScriptingInterface() { };
|
||||
public:
|
||||
static WindowScriptingInterface* getInstance();
|
||||
int getInnerWidth();
|
||||
int getInnerHeight();
|
||||
|
||||
public slots:
|
||||
QScriptValue alert(const QString& message);
|
||||
QScriptValue confirm(const QString& message);
|
||||
QScriptValue prompt(const QString& message, const QString& defaultText="");
|
||||
|
||||
private slots:
|
||||
QScriptValue showAlert(const QString& message);
|
||||
QScriptValue showConfirm(const QString& message);
|
||||
QScriptValue showPrompt(const QString& message, const QString& defaultText);
|
||||
};
|
||||
|
||||
#endif // hifi_WindowScriptingInterface_h
|
Loading…
Reference in a new issue