Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 00ff0fd66105ce58416f2cff1eea22a2957b4b88 (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
   editorwindow.h
   This file is part of the Project::OSiRiON world editor 
   and is distributed under the terms and conditions of 
   the GNU General Public License version 2
*/

#include "editorwindow.h"
#include "entitywidget.h"
#include "inistream.h"
#include "mapwidget.h"
#include "sidebar.h"

#include <QFile>
#include <QSplitter>
#include <QTextStream>
#include <QDebug>

#include <QtGui>
#include <QApplication>
#include <QMessageBox>
 
namespace editor
{

EditorWindow::EditorWindow(QWidget *parent) : QWidget(parent)
{
	editorwindow_splitter = new QSplitter(this);
	
	editorwindow_mapwidget = new MapWidget();
	editorwindow_sidebar = new SideBar();
	
	editorwindow_splitter->addWidget(editorwindow_sidebar);
	editorwindow_splitter->addWidget(editorwindow_mapwidget);
	
	connect(editorwindow_mapwidget, SIGNAL(propertiesChanged(EntityProperties *)), editorwindow_sidebar, SLOT(setProperties(EntityProperties *)));
	connect(editorwindow_sidebar, SIGNAL(entityChanged()), editorwindow_mapwidget, SLOT(resizeChildren()));
	connect(editorwindow_sidebar, SIGNAL(zonePropertiesClicked()), this, SLOT(showZoneProperties()));
}

QSize EditorWindow::sizeHint () const
{
	return QSize(768, 512);
}

void EditorWindow::resizeEvent (QResizeEvent *event)
{
	editorwindow_splitter->resize(width(), height());
}

bool EditorWindow::loadFile(const QString &filename)
{
	QFile file(filename);
	if (!file.open(QFile::ReadOnly | QFile::Text)) {
		QMessageBox::warning(this, tr("Open file"),
				tr("Could not open file %1:\n%2.")
				.arg(filename)
				.arg(file.errorString()));
		return false;
	}

	QTextStream textstream(&file);
	QApplication::setOverrideCursor(Qt::WaitCursor);
	
	IniStream ini;
	
	EntityWidget *entity = 0;
	bool in_entity = false;
	bool in_subsection = false;
	float x, y, z;
	float f;
	QString str;
	
	while (ini.getline(textstream)) {
		
		if (ini.got_section()) {
			
			if (ini.got_section("zone")) {
				editorwindow_zoneproperties.set_comment(ini.comment());
				in_entity = false;
				
			} else if (ini.got_section("entity")) {
				in_entity = true;
				in_subsection = false;
				
			} else if (ini.got_section("jumpgate")) {
				in_entity = true;
				in_subsection = false;
				
			} else if (ini.got_section("jumppoint")) {
				in_entity = true;
				in_subsection = false;
				
			} else if (ini.got_section("navpoint")) {
				in_entity = true;
				in_subsection = false;
				
			} else if (ini.got_section("station")) {
				in_entity = true;
				in_subsection = false;
				
			} else if (ini.got_section("star")) {
				in_entity = true;
				in_subsection = false;
				
			} else if (ini.got_section("planet")) {
				in_entity = true;
				in_subsection = false;
				
			} else if (ini.got_section("racetrack")) {
				in_entity = true;
				in_subsection = false;
				
			} else if (ini.got_section("checkpoint")) {
				in_entity = true;
				in_subsection = false;
				
			} else if (ini.got_section("cargo")) {
				in_entity = false;
				in_subsection = true;
				
			} else if (ini.got_section("ship")) {
				in_entity = false;
				in_subsection = true;

			} else if (ini.got_section("weapon")) {
				in_entity = false;
				in_subsection = true;
			
			} else if (ini.got_section("platform")) {
				in_entity = true;
				in_subsection = false;
			
			} else if (ini.got_section("patrol")) {
				in_entity = true;
				in_subsection = false;
				
			} else if (ini.got_section("waypoint")) {
				in_entity = false;
				in_subsection = true;
				
			} else if (ini.got_section("npc")) {
				in_entity = false;
				in_subsection = true;
				
			} else {
				entity = 0;
				in_entity = false;
				in_subsection = false;
			}
			
			if (in_entity) {
				entity = editorwindow_mapwidget->addEntity();
				entity->properties()->set_type(ini.section());
				entity->properties()->set_comment(ini.comment());
			}
			if (entity && in_subsection) {
				entity->properties()->add_subsection_header(ini.section(), ini.comment());
			}

		} else if(ini.got_key()) {
			
			if (in_entity) {
				// read entity properties into the MapWidget instance
				
				if (ini.got_key_vector3f("location" , x, y, z)) {
					entity->properties()->set_comment("location", ini.comment());
					entity->properties()->set_location(x, y, z);
					//qDebug() << "got location " << x << " " << y << " " << z;					
					
				} else if (ini.got_key_string("label", str)) {
					entity->properties()->set_comment("label", ini.comment());
					entity->properties()->set_label(str);
					
				} else if (ini.got_key_string("template", str)) {
					entity->properties()->set_comment("template", ini.comment());
					entity->properties()->set_template(0, str);
					
				} else if (ini.got_key_string("ship", str)) {
					entity->properties()->set_comment("template", ini.comment());
					entity->properties()->set_template(1, str);
					
				} else if (ini.got_key_string("name", str)) {
					entity->properties()->set_comment("name", ini.comment());
					entity->properties()->set_name(str);
					
				} else if (ini.got_key_float("radius", f)) {
					entity->properties()->set_comment("radius", ini.comment());
					entity->properties()->set_radius(f);
				
				} else if (ini.got_key_vector3f("angles" , x, y, z)) {
					// the angles key param order is pitch yaw roll
					// the in-memory order is yaw pitch roll
					entity->properties()->set_comment(ini.key(), ini.comment());
					entity->properties()->set_angles(y, x, z);
									
				} else if (ini.got_key_float("yaw", x)) {
					entity->properties()->set_yaw(x);
					
				} else if (ini.got_key_float("pitch", y)) {
					entity->properties()->set_pitch(y);
					
				} else if (ini.got_key_float("roll", z)) {
					entity->properties()->set_roll(z);
					
				} else if (ini.got_key_string("info", str)) {
					QString comment = entity->properties()->comment("info");
					comment += ini.comment();
					entity->properties()->set_comment("info", comment);
					entity->properties()->add_info(str);

				} else if (ini.got_key()) {
					entity->properties()->add_value(ini.key(), ini.value(), ini.comment());
				}
			
			} else if (entity && in_subsection) {
				entity->properties()->add_subsection_value(ini.key(), ini.value(), ini.comment());
				
			} else if (ini.in_section("zone")) {
				
				if (ini.got_key_string("name", str)) {
					editorwindow_zoneproperties.set_comment("name", ini.comment());
					editorwindow_zoneproperties.set_name(str);
					editorwindow_sidebar->setZoneName(str);
				
				} else if (ini.got_key_string("info", str)) {
					QString comment = editorwindow_zoneproperties.comment("info");
					comment += ini.comment();
					editorwindow_zoneproperties.set_comment("info", comment);
					editorwindow_zoneproperties.add_info(str);

				} else if (ini.got_key()) {
					editorwindow_zoneproperties.add_value(ini.key(), ini.value(), ini.comment());
				}
			}
			
		}
	}
	
	QApplication::restoreOverrideCursor();
	editorwindow_filename = filename;
	
	editorwindow_mapwidget->resizeChildren();
	editorwindow_mapwidget->update();

	return true;
}

bool EditorWindow::saveFile(const QString &filename)
{
	QFile file(filename);
	if (!file.open(QFile::WriteOnly | QFile::Text)) {
		QMessageBox::warning(this, tr("Save file"),
				tr("Cannot write file %1:\n%2.")
				.arg(filename)
				.arg(file.errorString()));
		return false;
	}

	QTextStream textstream(&file);
	
	QApplication::setOverrideCursor(Qt::WaitCursor);
	
	editorwindow_zoneproperties.save(textstream);
	editorwindow_mapwidget->save(textstream);
	
	QApplication::restoreOverrideCursor();

	editorwindow_filename = filename;
	return true;

}

void EditorWindow::addEntity()
{
	EntityWidget *entitywidget = editorwindow_mapwidget->addEntity();
	editorwindow_mapwidget->select(entitywidget);
}

void EditorWindow::duplicateSelected()
{
	editorwindow_mapwidget->duplicateSelected();
}

void EditorWindow::deleteSelected()
{
	editorwindow_mapwidget->deleteSelected();
}

void EditorWindow::showZoneProperties()
{
		QMessageBox::information(this, tr("Zone properties"), tr("Needs to be implemented"));
}

}