author | unc0rr |
Tue, 18 Nov 2014 23:39:30 +0300 | |
branch | qmlfrontend |
changeset 10515 | 7705784902e1 |
parent 10456 | 6fd99bb73524 |
child 10517 | 844bd43db47a |
permissions | -rw-r--r-- |
10402 | 1 |
#include <QLibrary> |
2 |
#include <QtQml> |
|
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
3 |
#include <QDebug> |
10420 | 4 |
#include <QPainter> |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
5 |
#include <QUuid> |
10402 | 6 |
|
7 |
#include "hwengine.h" |
|
10420 | 8 |
#include "previewimageprovider.h" |
10436 | 9 |
#include "themeiconprovider.h" |
10402 | 10 |
|
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 | 14 |
setSeed_t *flibSetSeed; |
15 |
getSeed_t *flibGetSeed; |
|
10456 | 16 |
setTheme_t *flibSetTheme; |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
17 |
getPreview_t *flibGetPreview; |
10432 | 18 |
runQuickGame_t *flibRunQuickGame; |
10448 | 19 |
runLocalGame_t *flibRunLocalGame; |
10416 | 20 |
flibInit_t *flibInit; |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
21 |
flibFree_t *flibFree; |
10450 | 22 |
resetGameConfig_t * flibResetGameConfig; |
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
23 |
getThemesList_t *flibGetThemesList; |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
24 |
freeThemesList_t *flibFreeThemesList; |
10436 | 25 |
getThemeIcon_t *flibGetThemeIcon; |
10442 | 26 |
getTeamsList_t *flibGetTeamsList; |
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
27 |
tryAddTeam_t * flibTryAddTeam; |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
28 |
tryRemoveTeam_t * flibTryRemoveTeam; |
10452
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
29 |
changeTeamColor_t * flibChangeTeamColor; |
10402 | 30 |
} |
10420 | 31 |
|
10430 | 32 |
Q_DECLARE_METATYPE(MessageType); |
33 |
||
10420 | 34 |
HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) : |
35 |
QObject(parent), |
|
36 |
m_engine(engine) |
|
10402 | 37 |
{ |
10430 | 38 |
qRegisterMetaType<MessageType>("MessageType"); |
39 |
||
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
40 |
QLibrary hwlib("./libhwengine.so"); |
10402 | 41 |
|
42 |
if(!hwlib.load()) |
|
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
43 |
qWarning() << "Engine library not found" << hwlib.errorString(); |
10402 | 44 |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
45 |
flibRunEngine = (RunEngine_t*) hwlib.resolve("RunEngine"); |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
46 |
flibRegisterGUIMessagesCallback = (registerGUIMessagesCallback_t*) hwlib.resolve("registerGUIMessagesCallback"); |
10430 | 47 |
flibSetSeed = (setSeed_t*) hwlib.resolve("setSeed"); |
48 |
flibGetSeed = (getSeed_t*) hwlib.resolve("getSeed"); |
|
10456 | 49 |
flibSetTheme = (setTheme_t*) hwlib.resolve("setTheme"); |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
50 |
flibGetPreview = (getPreview_t*) hwlib.resolve("getPreview"); |
10432 | 51 |
flibRunQuickGame = (runQuickGame_t*) hwlib.resolve("runQuickGame"); |
10448 | 52 |
flibRunLocalGame = (runLocalGame_t*) hwlib.resolve("runLocalGame"); |
10416 | 53 |
flibInit = (flibInit_t*) hwlib.resolve("flibInit"); |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
54 |
flibFree = (flibFree_t*) hwlib.resolve("flibFree"); |
10416 | 55 |
|
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
56 |
flibGetThemesList = (getThemesList_t*) hwlib.resolve("getThemesList"); |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
57 |
flibFreeThemesList = (freeThemesList_t*) hwlib.resolve("freeThemesList"); |
10436 | 58 |
flibGetThemeIcon = (getThemeIcon_t*) hwlib.resolve("getThemeIcon"); |
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
59 |
|
10450 | 60 |
flibResetGameConfig = (resetGameConfig_t*) hwlib.resolve("resetGameConfig"); |
10442 | 61 |
flibGetTeamsList = (getTeamsList_t*) hwlib.resolve("getTeamsList"); |
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
62 |
flibTryAddTeam = (tryAddTeam_t*) hwlib.resolve("tryAddTeam"); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
63 |
flibTryRemoveTeam = (tryRemoveTeam_t*) hwlib.resolve("tryRemoveTeam"); |
10452
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
64 |
flibChangeTeamColor = (changeTeamColor_t*) hwlib.resolve("changeTeamColor"); |
10442 | 65 |
|
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
66 |
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
|
67 |
flibRegisterGUIMessagesCallback(this, &guiMessagesCallback); |
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
68 |
|
10436 | 69 |
ThemeIconProvider * themeIcon = (ThemeIconProvider *)m_engine->imageProvider(QLatin1String("theme")); |
70 |
themeIcon->setFileContentsFunction(flibGetThemeIcon); |
|
71 |
||
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
72 |
fillModels(); |
10402 | 73 |
} |
74 |
||
75 |
HWEngine::~HWEngine() |
|
76 |
{ |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
77 |
flibFree(); |
10402 | 78 |
} |
79 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
80 |
void HWEngine::getPreview() |
10402 | 81 |
{ |
10430 | 82 |
flibSetSeed(QUuid::createUuid().toString().toLatin1()); |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
83 |
flibGetPreview(); |
10402 | 84 |
} |
85 |
||
10432 | 86 |
void HWEngine::runQuickGame() |
87 |
{ |
|
88 |
flibSetSeed(QUuid::createUuid().toString().toLatin1()); |
|
89 |
flibRunQuickGame(); |
|
90 |
} |
|
91 |
||
10448 | 92 |
void HWEngine::runLocalGame() |
93 |
{ |
|
94 |
flibRunLocalGame(); |
|
95 |
} |
|
96 |
||
97 |
||
10402 | 98 |
static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) |
99 |
{ |
|
100 |
Q_UNUSED(scriptEngine) |
|
101 |
||
10420 | 102 |
HWEngine *hwengine = new HWEngine(engine); |
10402 | 103 |
return hwengine; |
104 |
} |
|
105 |
||
106 |
void HWEngine::exposeToQML() |
|
107 |
{ |
|
108 |
qDebug("HWEngine::exposeToQML"); |
|
109 |
qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider); |
|
110 |
} |
|
10416 | 111 |
|
112 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
113 |
void HWEngine::guiMessagesCallback(void *context, MessageType mt, const char * msg, uint32_t len) |
10420 | 114 |
{ |
115 |
HWEngine * obj = (HWEngine *)context; |
|
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
116 |
QByteArray b = QByteArray(msg, len); |
10420 | 117 |
|
118 |
qDebug() << "FLIPC in" << b.size() << b; |
|
119 |
||
10430 | 120 |
QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(MessageType, mt), Q_ARG(QByteArray, b)); |
10416 | 121 |
} |
122 |
||
10430 | 123 |
void HWEngine::engineMessageHandler(MessageType mt, const QByteArray &msg) |
10416 | 124 |
{ |
10430 | 125 |
switch(mt) |
10426 | 126 |
{ |
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
127 |
case MSG_PREVIEW: { |
10426 | 128 |
PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview")); |
129 |
preview->setPixmap(msg); |
|
130 |
emit previewImageChanged(); |
|
10430 | 131 |
break; |
10426 | 132 |
} |
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
133 |
case MSG_ADDPLAYINGTEAM: { |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
134 |
QStringList l = QString::fromUtf8(msg).split('\n'); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
135 |
emit playingTeamAdded(l[1], l[0].toInt(), true); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
136 |
break; |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
137 |
} |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
138 |
case MSG_REMOVEPLAYINGTEAM: { |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
139 |
emit playingTeamRemoved(msg); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
140 |
break; |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
141 |
} |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
142 |
case MSG_ADDTEAM: { |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
143 |
emit localTeamAdded(msg, 0); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
144 |
break; |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
145 |
} |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
146 |
case MSG_REMOVETEAM: { |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
147 |
emit localTeamRemoved(msg); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
148 |
break; |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
149 |
} |
10452
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
150 |
case MSG_TEAMCOLOR: { |
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
151 |
QStringList l = QString::fromUtf8(msg).split('\n'); |
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
152 |
emit teamColorChanged(l[0], QColor::fromRgba(l[1].toInt()).name()); |
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
153 |
break; |
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
154 |
} |
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
155 |
} |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
156 |
} |
10416 | 157 |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
158 |
QString HWEngine::currentSeed() |
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
159 |
{ |
10430 | 160 |
return QString::fromLatin1(flibGetSeed()); |
10416 | 161 |
} |
10434
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
162 |
|
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
163 |
void HWEngine::fillModels() |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
164 |
{ |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
165 |
QStringList resultModel; |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
166 |
|
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
167 |
char ** themes = flibGetThemesList(); |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
168 |
for (char **i = themes; *i != NULL; i++) { |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
169 |
QString theme = QString::fromUtf8(*i); |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
170 |
|
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
171 |
resultModel << theme; |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
172 |
} |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
173 |
flibFreeThemesList(themes); |
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
174 |
|
1614b13ad35e
Themes model, also add some files I forgot to add previously
unc0rr
parents:
10432
diff
changeset
|
175 |
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
|
176 |
} |
10442 | 177 |
|
178 |
void HWEngine::getTeamsList() |
|
179 |
{ |
|
180 |
char ** teams = flibGetTeamsList(); |
|
181 |
for (char **i = teams; *i != NULL; i++) { |
|
182 |
QString team = QString::fromUtf8(*i); |
|
183 |
||
184 |
emit localTeamAdded(team, 0); |
|
185 |
} |
|
186 |
} |
|
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
187 |
|
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
188 |
void HWEngine::tryAddTeam(const QString &teamName) |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
189 |
{ |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
190 |
flibTryAddTeam(teamName.toUtf8().constData()); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
191 |
} |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
192 |
|
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
193 |
void HWEngine::tryRemoveTeam(const QString &teamName) |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
194 |
{ |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
195 |
flibTryRemoveTeam(teamName.toUtf8().constData()); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10442
diff
changeset
|
196 |
} |
10450 | 197 |
|
198 |
void HWEngine::resetGameConfig() |
|
199 |
{ |
|
200 |
flibResetGameConfig(); |
|
201 |
} |
|
10452
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
202 |
|
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
203 |
void HWEngine::changeTeamColor(const QString &teamName, int dir) |
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
204 |
{ |
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
205 |
flibChangeTeamColor(teamName.toUtf8().constData(), dir); |
03519fd9f98d
Show team color in teams list widget, also allow to change it on mouse click
unc0rr
parents:
10450
diff
changeset
|
206 |
} |
10456 | 207 |
|
208 |
void HWEngine::setTheme(const QString &theme) |
|
209 |
{ |
|
210 |
flibSetTheme(theme.toUtf8().constData()); |
|
211 |
} |