/
ccfmain.cpp
199 lines (174 loc) · 6.09 KB
/
ccfmain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/****************************************************************************
** Close Combat Free is a free, easily moddable CC-like game.
** Copyright (C) 2011 Tomasz Siekierda
** Email: sierdzio@gmail.com, website: http://www.sierdzio.com
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program (in doc/ directory).
** If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
****************************************************************************/
#include <QGuiApplication>
#include <QQmlContext>
#include <QQmlEngine>
#include "ccfmain.h"
#include "qmlBase/ccfqmlbaserostermenu.h"
#include "qmlBase/ccfqmlbasescenario.h"
#include "qmlBase/ccfqmlbasemap.h"
#include "qmlBase/ccfqmlbaseunit.h"
#include "qmlBase/ccfqmlbasesoldier.h"
// Singleton
CcfMain *CcfMain::mInstance = NULL;
/*!
Returns instance of CcfMain class. Creates a new one if none exist.
Only one instance of this class is allowed. Optionally takes in a command line
parser object \a cmd, so that code execution can be influenced by command line
arguments.
*/
CcfMain *CcfMain::instance(CcfCommandLineParser *cmd)
{
if (!mInstance)
mInstance = new CcfMain(cmd, 0);
return mInstance;
}
/*!
Main view's constructor, full of important stuff.
Sets all global properties, sizing policy, connects important
signals and slots, reads config file(s).
*/
CcfMain::CcfMain(CcfCommandLineParser *cmd, QWindow *parent) :
QQuickView(parent), CcfError(), mCmdLnParser(cmd)
{
qmlRegisterType<CcfQmlBaseRosterMenu>("QmlBase", 0, 1, "BaseRosterMenu");
qmlRegisterType<CcfQmlBaseScenario>("QmlBase", 0, 1, "BaseScenario");
qmlRegisterType<CcfQmlBaseMap>("QmlBase", 0, 1, "Map");
qmlRegisterType<CcfQmlBaseUnit>("QmlBase", 0, 1, "BaseUnit");
qmlRegisterType<CcfQmlBaseSoldier>("QmlBase", 0, 1, "Soldier");
mLogger = new CcfLogger(this, mCmdLnParser->isDebug());
mGlobal = new CcfGlobal(this);
mGameManager = new CcfGameManager(this);
mEngineHelpers = new CcfEngineHelpers(this);
initConfiguration();
rootContext()->setContextProperty("Global", mGlobal);
rootContext()->setContextProperty("Config", mConfiguration);
rootContext()->setContextProperty("GameManager", mGameManager);
rootContext()->setContextProperty("EngineHelpers", mEngineHelpers);
rootContext()->setContextProperty("Logger", mLogger);
QString pwd = qApp->applicationDirPath() + "/";
rootContext()->setContextProperty("PWD", pwd);
setResizeMode(QQuickView::SizeRootObjectToView);
// connect(this, SIGNAL(sceneResized(QSize)), configuration, SLOT(windowResized(QSize)));
connect(mConfiguration, SIGNAL(sizeModifiedInGame(int,int)), this, SLOT(forceViewportResize(int,int)));
connect(engine(), SIGNAL(quit()), this, SLOT(quit()));
connect(mConfiguration, SIGNAL(maximise()), this, SLOT(showMaximized()));
connect(mConfiguration, SIGNAL(demaximise()), this, SLOT(showNormal()));
connect(mGlobal, SIGNAL(disableQrc(QObject*)), this, SLOT(disableQrc(QObject*)));
}
/*!
Returns true if config is set to be maximised.
*/
bool CcfMain::isConfigMaximised()
{
return mConfiguration->isMaximised();
}
/*!
Orders the view to be resized.
*/
void CcfMain::resizeView(QSize newSize)
{
mConfiguration->windowResized(newSize);
}
/*!
Returns a pointer to CcfLogger instance. Meant for logging, of course.
*/
CcfLogger *CcfMain::logger()
{
return mLogger;
}
/*!
Returns a pointer to CcfConfig instance.
*/
CcfConfig *CcfMain::config()
{
return mConfiguration;
}
/*!
Returns a pointer to CcfGlobal instance.
*/
CcfGlobal *CcfMain::global()
{
return mGlobal;
}
/*!
Returns a pointer to CcfGlobal instance.
*/
CcfGameManager *CcfMain::gameManager()
{
return mGameManager;
}
/*!
Slot invoked when application is ordered to stop.
This is not called when user terminates by the 'x' button on windowing
system, or using process manager.
*/
void CcfMain::quit()
{
mConfiguration->saveConfig();
if (mConfiguration->isErrorState()) {
qWarning(qPrintable(mConfiguration->errorMessage()), NULL);
}
delete mConfiguration;
qApp->quit();
}
/*!
Forces the window to be resized.
Invoked when user changes window size in game preferences.
*/
void CcfMain::forceViewportResize(int width, int height)
{
setGeometry(x(), y(), width, height);
}
/*!
Disables QRC reading on a given QObject (usually a Loader or Repeater).
Very important, enables loading of non-qrc paths in an app that uses QRC.
Usually, if you load any QML code through QRC, all subsequent loads will
also be performed through resource system - even when you specify a path that
is not present in any resource file. This method counters this by clearing
the "qrc mode".
*/
void CcfMain::disableQrc(QObject *object)
{
QQmlContext *context = engine()->contextForObject(object);
context->setBaseUrl(QUrl::fromLocalFile(""));
}
/*!
Initialises the configuration, loading default on error.
*/
bool CcfMain::initConfiguration()
{
mConfiguration = new CcfConfig("config", mLogger, this);
if (mConfiguration->isErrorState()) {
printf("Error while reading configuration file! Message: %s\n",
qPrintable(mConfiguration->errorMessage()));
printf("Loading default configuration... ");
delete mConfiguration;
mConfiguration = new CcfConfig("config_default", mLogger, this);
if (mConfiguration->isErrorState()) {
printf("ERROR: %s\n", qPrintable(mConfiguration->errorMessage()));
return false;
} else {
printf("OK\n");
return true;
}
}
return true;
}