qmlFrontend/hwengine.cpp
author unc0rr
Mon, 13 Oct 2014 23:46:49 +0400
branchqmlfrontend
changeset 10440 b74a7bbe224e
parent 10436 084e046f6bd5
child 10442 c58db813240b
permissions -rw-r--r--
- Implement getTeamsList (not tested) - Some small fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     1
#include <QLibrary>
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     2
#include <QtQml>
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
     3
#include <QDebug>
10420
unc0rr
parents: 10418
diff changeset
     4
#include <QPainter>
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
     5
#include <QUuid>
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     6
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     7
#include "hwengine.h"
10420
unc0rr
parents: 10418
diff changeset
     8
#include "previewimageprovider.h"
10436
084e046f6bd5 flib provides theme icons, qmlFrontend shows them
unc0rr
parents: 10434
diff changeset
     9
#include "themeiconprovider.h"
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    10
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    11
extern "C" {
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    12
    RunEngine_t *flibRunEngine;
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    13
    registerGUIMessagesCallback_t *flibRegisterGUIMessagesCallback;
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    14
    setSeed_t *flibSetSeed;
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    15
    getSeed_t *flibGetSeed;
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    16
    getPreview_t *flibGetPreview;
10432
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    17
    runQuickGame_t *flibRunQuickGame;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    18
    flibInit_t *flibInit;
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    19
    flibFree_t *flibFree;
10434
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    20
    getThemesList_t *flibGetThemesList;
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    21
    freeThemesList_t *flibFreeThemesList;
10436
084e046f6bd5 flib provides theme icons, qmlFrontend shows them
unc0rr
parents: 10434
diff changeset
    22
    getThemeIcon_t *flibGetThemeIcon;
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    23
}
10420
unc0rr
parents: 10418
diff changeset
    24
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    25
Q_DECLARE_METATYPE(MessageType);
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    26
10420
unc0rr
parents: 10418
diff changeset
    27
HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) :
unc0rr
parents: 10418
diff changeset
    28
    QObject(parent),
unc0rr
parents: 10418
diff changeset
    29
    m_engine(engine)
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    30
{
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    31
    qRegisterMetaType<MessageType>("MessageType");
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    32
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
    33
    QLibrary hwlib("./libhwengine.so");
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    34
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    35
    if(!hwlib.load())
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
    36
        qWarning() << "Engine library not found" << hwlib.errorString();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    37
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    38
    flibRunEngine = (RunEngine_t*) hwlib.resolve("RunEngine");
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    39
    flibRegisterGUIMessagesCallback = (registerGUIMessagesCallback_t*) hwlib.resolve("registerGUIMessagesCallback");
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    40
    flibSetSeed = (setSeed_t*) hwlib.resolve("setSeed");
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    41
    flibGetSeed = (getSeed_t*) hwlib.resolve("getSeed");
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    42
    flibGetPreview = (getPreview_t*) hwlib.resolve("getPreview");
10432
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    43
    flibRunQuickGame = (runQuickGame_t*) hwlib.resolve("runQuickGame");
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    44
    flibInit = (flibInit_t*) hwlib.resolve("flibInit");
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    45
    flibFree = (flibFree_t*) hwlib.resolve("flibFree");
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    46
10434
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    47
    flibGetThemesList = (getThemesList_t*) hwlib.resolve("getThemesList");
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    48
    flibFreeThemesList = (freeThemesList_t*) hwlib.resolve("freeThemesList");
10436
084e046f6bd5 flib provides theme icons, qmlFrontend shows them
unc0rr
parents: 10434
diff changeset
    49
    flibGetThemeIcon = (getThemeIcon_t*) hwlib.resolve("getThemeIcon");
10434
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    50
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    51
    flibInit("/usr/home/unC0Rr/Sources/Hedgewars/Hedgewars-GC/share/hedgewars/Data", "/usr/home/unC0Rr/.hedgewars");
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    52
    flibRegisterGUIMessagesCallback(this, &guiMessagesCallback);
10434
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    53
10436
084e046f6bd5 flib provides theme icons, qmlFrontend shows them
unc0rr
parents: 10434
diff changeset
    54
    ThemeIconProvider * themeIcon = (ThemeIconProvider *)m_engine->imageProvider(QLatin1String("theme"));
084e046f6bd5 flib provides theme icons, qmlFrontend shows them
unc0rr
parents: 10434
diff changeset
    55
    themeIcon->setFileContentsFunction(flibGetThemeIcon);
084e046f6bd5 flib provides theme icons, qmlFrontend shows them
unc0rr
parents: 10434
diff changeset
    56
10434
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
    57
    fillModels();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    58
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    59
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    60
HWEngine::~HWEngine()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    61
{
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    62
    flibFree();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    63
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    64
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    65
void HWEngine::getPreview()
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    66
{
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    67
    flibSetSeed(QUuid::createUuid().toString().toLatin1());
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    68
    flibGetPreview();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    69
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    70
10432
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    71
void HWEngine::runQuickGame()
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    72
{
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    73
    flibSetSeed(QUuid::createUuid().toString().toLatin1());
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    74
    flibRunQuickGame();
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    75
}
b0abef0ee78c Quick Game PoC
unc0rr
parents: 10430
diff changeset
    76
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    77
static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    78
{
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    79
    Q_UNUSED(scriptEngine)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    80
10420
unc0rr
parents: 10418
diff changeset
    81
    HWEngine *hwengine = new HWEngine(engine);
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    82
    return hwengine;
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    83
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    84
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    85
void HWEngine::exposeToQML()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    86
{
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    87
    qDebug("HWEngine::exposeToQML");
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    88
    qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider);
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    89
}
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    90
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    91
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    92
void HWEngine::guiMessagesCallback(void *context, MessageType mt, const char * msg, uint32_t len)
10420
unc0rr
parents: 10418
diff changeset
    93
{
unc0rr
parents: 10418
diff changeset
    94
    HWEngine * obj = (HWEngine *)context;
unc0rr
parents: 10418
diff changeset
    95
    QByteArray b = QByteArray::fromRawData(msg, len);
unc0rr
parents: 10418
diff changeset
    96
unc0rr
parents: 10418
diff changeset
    97
    qDebug() << "FLIPC in" << b.size() << b;
unc0rr
parents: 10418
diff changeset
    98
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
    99
    QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(MessageType, mt), Q_ARG(QByteArray, b));
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
   100
}
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
   101
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
   102
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray &msg)
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
   103
{
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
   104
    switch(mt)
10426
727a154cf784 Some refactoring
unc0rr
parents: 10424
diff changeset
   105
    {
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
   106
    case MSG_PREVIEW:
10426
727a154cf784 Some refactoring
unc0rr
parents: 10424
diff changeset
   107
        PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview"));
727a154cf784 Some refactoring
unc0rr
parents: 10424
diff changeset
   108
        preview->setPixmap(msg);
727a154cf784 Some refactoring
unc0rr
parents: 10424
diff changeset
   109
        emit previewImageChanged();
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
   110
        break;
10426
727a154cf784 Some refactoring
unc0rr
parents: 10424
diff changeset
   111
    }
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
   112
}
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
   113
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
   114
QString HWEngine::currentSeed()
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
   115
{
10430
899a30018ede Getter and setter for seed
unc0rr
parents: 10428
diff changeset
   116
    return QString::fromLatin1(flibGetSeed());
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
   117
}
10434
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   118
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   119
void HWEngine::fillModels()
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   120
{
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   121
    QStringList resultModel;
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   122
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   123
    char ** themes = flibGetThemesList();
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   124
    for (char **i = themes; *i != NULL; i++) {
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   125
        QString theme = QString::fromUtf8(*i);
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   126
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   127
        resultModel << theme;
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   128
    }
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   129
    flibFreeThemesList(themes);
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   130
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   131
    m_engine->rootContext()->setContextProperty("themesModel", QVariant::fromValue(resultModel));
1614b13ad35e Themes model, also add some files I forgot to add previously
unc0rr
parents: 10432
diff changeset
   132
}