h & cpp split
This commit is contained in:
parent
4610e71f42
commit
ed2a1326fc
39
doric/context.cpp
Normal file
39
doric/context.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
#include "constant.h"
|
||||
#include "context.h"
|
||||
#include "driver/native_driver.h"
|
||||
|
||||
Context::Context(int contextId, QString *source) {
|
||||
this->driver = NativeDriver::getInstance();
|
||||
|
||||
this->contextId = contextId;
|
||||
this->source = source;
|
||||
}
|
||||
|
||||
void Context::show() {
|
||||
QString *method = new QString(Constant::DORIC_ENTITY_SHOW);
|
||||
QVector<QString*> *arguments = new QVector<QString*>();
|
||||
|
||||
driver->invokeContextEntityMethod(contextId, method, nullptr);
|
||||
|
||||
delete arguments;
|
||||
delete method;
|
||||
}
|
||||
|
||||
void Context::init(double width, double height) {
|
||||
QJsonObject *jsonObject = new QJsonObject();
|
||||
jsonObject->insert("width", width);
|
||||
jsonObject->insert("height", height);
|
||||
|
||||
QString *method = new QString(Constant::DORIC_ENTITY_INIT);
|
||||
QVariant *variant = new QVariant();
|
||||
variant->setValue(*jsonObject);
|
||||
|
||||
driver->invokeContextEntityMethod(contextId, method, variant, nullptr);
|
||||
|
||||
delete variant;
|
||||
delete method;
|
||||
delete jsonObject;
|
||||
}
|
@ -2,12 +2,8 @@
|
||||
#define CONTEXT_H
|
||||
|
||||
#include <QString>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
#include "constant.h"
|
||||
#include "driver/driver.h"
|
||||
#include "driver/native_driver.h"
|
||||
|
||||
class Context
|
||||
{
|
||||
@ -17,38 +13,13 @@ private:
|
||||
QString *source;
|
||||
|
||||
public:
|
||||
Driver *driver = NativeDriver::getInstance();
|
||||
Driver *driver;
|
||||
|
||||
Context(int contextId, QString *source) {
|
||||
this->contextId = contextId;
|
||||
this->source = source;
|
||||
}
|
||||
Context(int contextId, QString *source);
|
||||
|
||||
void show() {
|
||||
QString *method = new QString(Constant::DORIC_ENTITY_SHOW);
|
||||
QVector<QString*> *arguments = new QVector<QString*>();
|
||||
void show();
|
||||
|
||||
driver->invokeContextEntityMethod(contextId, method, nullptr);
|
||||
|
||||
delete arguments;
|
||||
delete method;
|
||||
}
|
||||
|
||||
void init(double width, double height) {
|
||||
QJsonObject *jsonObject = new QJsonObject();
|
||||
jsonObject->insert("width", width);
|
||||
jsonObject->insert("height", height);
|
||||
|
||||
QString *method = new QString(Constant::DORIC_ENTITY_INIT);
|
||||
QVariant *variant = new QVariant();
|
||||
variant->setValue(*jsonObject);
|
||||
|
||||
driver->invokeContextEntityMethod(contextId, method, variant, nullptr);
|
||||
|
||||
delete variant;
|
||||
delete method;
|
||||
delete jsonObject;
|
||||
}
|
||||
void init(double width, double height);
|
||||
};
|
||||
|
||||
#endif // CONTEXT_H
|
||||
|
@ -15,9 +15,15 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
SOURCES += \
|
||||
constant.cpp \
|
||||
context.cpp \
|
||||
driver/native_driver.cpp \
|
||||
engine/js_engine.cpp \
|
||||
main.cpp \
|
||||
native/native_bridge.cpp
|
||||
native/native_bridge.cpp \
|
||||
native/native_empty.cpp \
|
||||
native/native_log.cpp \
|
||||
native/native_timer.cpp \
|
||||
registry.cpp
|
||||
|
||||
RESOURCES += qml.qrc
|
||||
|
||||
|
@ -3,6 +3,10 @@
|
||||
#include "native_driver.h"
|
||||
#include "utility/utility.h"
|
||||
|
||||
NativeDriver::NativeDriver() {
|
||||
qDebug() << "NativeDriver constructor";
|
||||
}
|
||||
|
||||
NativeDriver::~NativeDriver() {
|
||||
qDebug() << "NativeDriver destructor";
|
||||
}
|
||||
|
@ -11,9 +11,7 @@ class NativeDriver : public Driver {
|
||||
|
||||
private:
|
||||
static NativeDriver *local_instance;
|
||||
NativeDriver() {
|
||||
qDebug() << "NativeDriver constructor";
|
||||
}
|
||||
NativeDriver();
|
||||
|
||||
~NativeDriver() override;
|
||||
|
||||
|
80
doric/engine/js_engine.cpp
Normal file
80
doric/engine/js_engine.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include <QFile>
|
||||
#include <QResource>
|
||||
|
||||
#include "constant.h"
|
||||
#include "js_engine.h"
|
||||
|
||||
JSEngine::JSEngine() {
|
||||
initJSEngine();
|
||||
injectGlobal();
|
||||
initDoricRuntime();
|
||||
}
|
||||
|
||||
void JSEngine::prepareContext(int contextId, QString *script) {
|
||||
QString contextIdString = QString::number(contextId);
|
||||
QString source = QString(Constant::TEMPLATE_CONTEXT_CREATE)
|
||||
.replace("%s1", *script)
|
||||
.replace("%s2", contextIdString)
|
||||
.replace("%s3", contextIdString)
|
||||
.replace("%s4", contextIdString);
|
||||
QJSValue result = engine->evaluate(source, "context://" + contextIdString);
|
||||
qDebug() << "context://" + contextIdString + " result: " + result.toString();
|
||||
}
|
||||
|
||||
void JSEngine::destroyContext(int contextId) {
|
||||
QString contextIdString = QString::number(contextId);
|
||||
QString source = QString(Constant::TEMPLATE_CONTEXT_DESTROY)
|
||||
.replace("%s", contextIdString);
|
||||
QJSValue result = engine->evaluate(source, "_context://" + contextIdString);
|
||||
qDebug() << "context://" + contextIdString + " result: " + result.toString();
|
||||
}
|
||||
|
||||
void JSEngine::initJSEngine() {
|
||||
engine->installExtensions(QJSEngine::AllExtensions);
|
||||
}
|
||||
|
||||
void JSEngine::injectGlobal() {
|
||||
QJSValue log = engine->newQObject(nativeLog);
|
||||
engine->globalObject().setProperty(Constant::INJECT_LOG, log.property("function"));
|
||||
|
||||
QJSValue timer = engine->newQObject(nativeTimer);
|
||||
engine->globalObject().setProperty(Constant::INJECT_TIMER_SET, timer.property("setTimer"));
|
||||
engine->globalObject().setProperty(Constant::INJECT_TIMER_CLEAR, timer.property("clearTimer"));
|
||||
|
||||
QJSValue empty = engine->newQObject(nativeEmpty);
|
||||
engine->globalObject().setProperty(Constant::INJECT_EMPTY, empty.property("function"));
|
||||
|
||||
QJSValue bridge = engine->newQObject(nativeBridge);
|
||||
engine->globalObject().setProperty(Constant::INJECT_BRIDGE, bridge.property("function"));
|
||||
}
|
||||
|
||||
void JSEngine::initDoricRuntime() {
|
||||
{
|
||||
QResource resource(":/doric/doric-sandbox.js");
|
||||
QFile *file = new QFile(resource.fileName());
|
||||
file->open(QFile::ReadOnly | QFile::Text);
|
||||
QTextStream in(file);
|
||||
QString script = in.readAll();
|
||||
file->close();
|
||||
delete file;
|
||||
|
||||
QJSValue result = engine->evaluate(script, "doric-sandbox.js");
|
||||
qDebug() << "doric-sandbox.js result: " + result.toString();
|
||||
}
|
||||
|
||||
{
|
||||
QResource resource(":/doric/doric-lib.js");
|
||||
QFile *file = new QFile(resource.fileName());
|
||||
file->open(QFile::ReadOnly | QFile::Text);
|
||||
QTextStream in(file);
|
||||
QString script = in.readAll();
|
||||
file->close();
|
||||
delete file;
|
||||
|
||||
QString lib = QString(Constant::TEMPLATE_MODULE)
|
||||
.replace("%s1", "doric")
|
||||
.replace("%s2", script);
|
||||
QJSValue result = engine->evaluate(lib, "doric-lib.js");
|
||||
qDebug() << "doric-lib.js result: " + result.toString();
|
||||
}
|
||||
}
|
@ -1,47 +1,23 @@
|
||||
#ifndef JS_ENGINE_H
|
||||
#define JS_ENGINE_H
|
||||
|
||||
#include <QFile>
|
||||
#include <QJSEngine>
|
||||
#include <QObject>
|
||||
#include <QResource>
|
||||
|
||||
#include "constant.h"
|
||||
#include "native/native_bridge.h"
|
||||
#include "native/native_empty.h"
|
||||
#include "native/native_log.h"
|
||||
#include "native/native_timer.h"
|
||||
|
||||
class JSEngine : public QObject {
|
||||
Q_OBJECT
|
||||
class JSEngine {
|
||||
|
||||
public:
|
||||
QJSEngine *engine = new QJSEngine();
|
||||
|
||||
JSEngine(QObject *parent = nullptr) : QObject(parent) {
|
||||
initJSEngine();
|
||||
injectGlobal();
|
||||
initDoricRuntime();
|
||||
}
|
||||
JSEngine();
|
||||
|
||||
void prepareContext(int contextId, QString *script) {
|
||||
QString contextIdString = QString::number(contextId);
|
||||
QString source = QString(Constant::TEMPLATE_CONTEXT_CREATE)
|
||||
.replace("%s1", *script)
|
||||
.replace("%s2", contextIdString)
|
||||
.replace("%s3", contextIdString)
|
||||
.replace("%s4", contextIdString);
|
||||
QJSValue result = engine->evaluate(source, "context://" + contextIdString);
|
||||
qDebug() << "context://" + contextIdString + " result: " + result.toString();
|
||||
}
|
||||
void prepareContext(int contextId, QString *script);
|
||||
|
||||
void destroyContext(int contextId) {
|
||||
QString contextIdString = QString::number(contextId);
|
||||
QString source = QString(Constant::TEMPLATE_CONTEXT_DESTROY)
|
||||
.replace("%s", contextIdString);
|
||||
QJSValue result = engine->evaluate(source, "_context://" + contextIdString);
|
||||
qDebug() << "context://" + contextIdString + " result: " + result.toString();
|
||||
}
|
||||
void destroyContext(int contextId);
|
||||
|
||||
private:
|
||||
NativeLog *nativeLog = new NativeLog();
|
||||
@ -49,55 +25,11 @@ private:
|
||||
NativeEmpty *nativeEmpty = new NativeEmpty();
|
||||
NativeBridge *nativeBridge = new NativeBridge();
|
||||
|
||||
void initJSEngine() {
|
||||
engine->installExtensions(QJSEngine::AllExtensions);
|
||||
}
|
||||
void initJSEngine();
|
||||
|
||||
void injectGlobal() {
|
||||
QJSValue log = engine->newQObject(nativeLog);
|
||||
engine->globalObject().setProperty(Constant::INJECT_LOG, log.property("function"));
|
||||
void injectGlobal();
|
||||
|
||||
QJSValue timer = engine->newQObject(nativeTimer);
|
||||
engine->globalObject().setProperty(Constant::INJECT_TIMER_SET, timer.property("setTimer"));
|
||||
engine->globalObject().setProperty(Constant::INJECT_TIMER_CLEAR, timer.property("clearTimer"));
|
||||
|
||||
QJSValue empty = engine->newQObject(nativeEmpty);
|
||||
engine->globalObject().setProperty(Constant::INJECT_EMPTY, empty.property("function"));
|
||||
|
||||
QJSValue bridge = engine->newQObject(nativeBridge);
|
||||
engine->globalObject().setProperty(Constant::INJECT_BRIDGE, bridge.property("function"));
|
||||
}
|
||||
|
||||
void initDoricRuntime() {
|
||||
{
|
||||
QResource resource(":/doric/doric-sandbox.js");
|
||||
QFile *file = new QFile(resource.fileName());
|
||||
file->open(QFile::ReadOnly | QFile::Text);
|
||||
QTextStream in(file);
|
||||
QString script = in.readAll();
|
||||
file->close();
|
||||
delete file;
|
||||
|
||||
QJSValue result = engine->evaluate(script, "doric-sandbox.js");
|
||||
qDebug() << "doric-sandbox.js result: " + result.toString();
|
||||
}
|
||||
|
||||
{
|
||||
QResource resource(":/doric/doric-lib.js");
|
||||
QFile *file = new QFile(resource.fileName());
|
||||
file->open(QFile::ReadOnly | QFile::Text);
|
||||
QTextStream in(file);
|
||||
QString script = in.readAll();
|
||||
file->close();
|
||||
delete file;
|
||||
|
||||
QString lib = QString(Constant::TEMPLATE_MODULE)
|
||||
.replace("%s1", "doric")
|
||||
.replace("%s2", script);
|
||||
QJSValue result = engine->evaluate(lib, "doric-lib.js");
|
||||
qDebug() << "doric-lib.js result: " + result.toString();
|
||||
}
|
||||
}
|
||||
void initDoricRuntime();
|
||||
};
|
||||
|
||||
#endif // JS_ENGINE_H
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include <QFile>
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QResource>
|
||||
#include <QTextStream>
|
||||
|
||||
#include "context_manager.h"
|
||||
|
@ -1,7 +1,6 @@
|
||||
#ifndef NATIVE_BRIDGE_H
|
||||
#define NATIVE_BRIDGE_H
|
||||
|
||||
#include <QDebug>
|
||||
#include <QJSValue>
|
||||
#include <QObject>
|
||||
|
||||
|
7
doric/native/native_empty.cpp
Normal file
7
doric/native/native_empty.cpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "native_empty.h"
|
||||
|
||||
void NativeEmpty::function() {
|
||||
qDebug() << "nativeEmpty";
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
#define NATIVE_EMPTY_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QDebug>
|
||||
|
||||
class NativeEmpty : public QObject {
|
||||
Q_OBJECT
|
||||
@ -10,9 +9,7 @@ class NativeEmpty : public QObject {
|
||||
public:
|
||||
NativeEmpty(QObject *parent = nullptr) : QObject(parent) {}
|
||||
|
||||
Q_INVOKABLE void function() {
|
||||
qDebug() << "nativeEmpty";
|
||||
}
|
||||
Q_INVOKABLE void function();
|
||||
};
|
||||
|
||||
#endif // NATIVE_EMPTY_H
|
||||
|
13
doric/native/native_log.cpp
Normal file
13
doric/native/native_log.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "native_log.h"
|
||||
|
||||
void NativeLog::function(QString level, QString content) {
|
||||
if (level == 'w') {
|
||||
qWarning() << content;
|
||||
} else if (level == 'd') {
|
||||
qDebug() << content;
|
||||
} else if (level == 'e') {
|
||||
qCritical() << content;
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
#define NATIVELOG_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QDebug>
|
||||
|
||||
class NativeLog : public QObject {
|
||||
Q_OBJECT
|
||||
@ -10,15 +9,7 @@ class NativeLog : public QObject {
|
||||
public:
|
||||
NativeLog(QObject *parent = nullptr) : QObject(parent) {}
|
||||
|
||||
Q_INVOKABLE void function(QString level, QString content) {
|
||||
if (level == 'w') {
|
||||
qWarning() << content;
|
||||
} else if (level == 'd') {
|
||||
qDebug() << content;
|
||||
} else if (level == 'e') {
|
||||
qCritical() << content;
|
||||
}
|
||||
}
|
||||
Q_INVOKABLE void function(QString level, QString content);
|
||||
};
|
||||
|
||||
#endif // NATIVELOG_H
|
||||
|
30
doric/native/native_timer.cpp
Normal file
30
doric/native/native_timer.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include <QTimer>
|
||||
|
||||
#include "native_timer.h"
|
||||
|
||||
void NativeTimer::setTimer(long timerId, int time, bool repeat) {
|
||||
QTimer *timer = new QTimer(this);
|
||||
timer->setSingleShot(!repeat);
|
||||
connect(timer, &QTimer::timeout, this, [=] () {
|
||||
if (deletedTimerIds->contains(timerId)) {
|
||||
deletedTimerIds->remove(timerId);
|
||||
delete timer;
|
||||
} else {
|
||||
engine->evaluate(
|
||||
Constant::GLOBAL_DORIC + "." +
|
||||
Constant::DORIC_TIMER_CALLBACK + "(" +
|
||||
QString::number(timerId) + ")"
|
||||
);
|
||||
|
||||
if (!repeat) {
|
||||
deletedTimerIds->remove(timerId);
|
||||
delete timer;
|
||||
}
|
||||
}
|
||||
});
|
||||
timer->start(time);
|
||||
}
|
||||
|
||||
void NativeTimer::clearTimer(long timerId) {
|
||||
deletedTimerIds->insert(timerId);
|
||||
}
|
@ -4,7 +4,6 @@
|
||||
#include <QJSEngine>
|
||||
#include <QObject>
|
||||
#include <QSet>
|
||||
#include <QTimer>
|
||||
|
||||
#include "constant.h"
|
||||
|
||||
@ -20,32 +19,9 @@ public:
|
||||
this->engine = engine;
|
||||
}
|
||||
|
||||
Q_INVOKABLE void setTimer(long timerId, int time, bool repeat) {
|
||||
QTimer *timer = new QTimer(this);
|
||||
timer->setSingleShot(!repeat);
|
||||
connect(timer, &QTimer::timeout, this, [=] () {
|
||||
if (deletedTimerIds->contains(timerId)) {
|
||||
deletedTimerIds->remove(timerId);
|
||||
delete timer;
|
||||
} else {
|
||||
engine->evaluate(
|
||||
Constant::GLOBAL_DORIC + "." +
|
||||
Constant::DORIC_TIMER_CALLBACK + "(" +
|
||||
QString::number(timerId) + ")"
|
||||
);
|
||||
Q_INVOKABLE void setTimer(long timerId, int time, bool repeat);
|
||||
|
||||
if (!repeat) {
|
||||
deletedTimerIds->remove(timerId);
|
||||
delete timer;
|
||||
}
|
||||
}
|
||||
});
|
||||
timer->start(time);
|
||||
}
|
||||
|
||||
Q_INVOKABLE void clearTimer(long timerId) {
|
||||
deletedTimerIds->insert(timerId);
|
||||
}
|
||||
Q_INVOKABLE void clearTimer(long timerId);
|
||||
|
||||
};
|
||||
#endif // NATIVE_TIMER_H
|
||||
|
11
doric/registry.cpp
Normal file
11
doric/registry.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "registry.h"
|
||||
|
||||
Registry::Registry() {
|
||||
registerNativePlugin(typeid(ShaderPlugin).name());
|
||||
}
|
||||
|
||||
void Registry::registerNativePlugin(QString name) {
|
||||
qDebug() << name;
|
||||
}
|
@ -12,13 +12,9 @@ private:
|
||||
QMap<QString, QString> pluginInfoMap;
|
||||
|
||||
public:
|
||||
Registry() {
|
||||
registerNativePlugin(typeid(ShaderPlugin).name());
|
||||
}
|
||||
Registry();
|
||||
|
||||
void registerNativePlugin(QString name) {
|
||||
qDebug() << name;
|
||||
}
|
||||
void registerNativePlugin(QString name);
|
||||
};
|
||||
|
||||
#endif // REGISTRY_H
|
||||
|
Reference in New Issue
Block a user