/
Map.cpp
912 lines (733 loc) · 22.5 KB
/
Map.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
#include "Map.h"
#include "i18n.h"
#include <ostream>
#include <fstream>
#include "itextstream.h"
#include "iscenegraph.h"
#include "icameraview.h"
#include "imainframe.h"
#include "idialogmanager.h"
#include "ieventmanager.h"
#include "imodel.h"
#include "ifilesystem.h"
#include "ifiletypes.h"
#include "iselectiongroup.h"
#include "ifilter.h"
#include "icounter.h"
#include "iradiant.h"
#include "imapresource.h"
#include "imapinfofile.h"
#include "iaasfile.h"
#include "igame.h"
#include "imru.h"
#include "imapformat.h"
#include "registry/registry.h"
#include "stream/TextFileInputStream.h"
#include "entitylib.h"
#include "gamelib.h"
#include "os/path.h"
#include "os/file.h"
#include "time/ScopeTimer.h"
#include "brush/BrushModule.h"
#include "scene/BasicRootNode.h"
#include "map/MapFileManager.h"
#include "map/MapPositionManager.h"
#include "map/MapResource.h"
#include "map/algorithm/Import.h"
#include "map/algorithm/Export.h"
#include "scene/Traverse.h"
#include "map/algorithm/MapExporter.h"
#include "model/export/ModelExporter.h"
#include "model/export/ModelScalePreserver.h"
#include "map/algorithm/Skins.h"
#include "messages/ScopedLongRunningOperation.h"
#include "selection/algorithm/Primitives.h"
#include "selection/algorithm/Group.h"
#include "scene/Group.h"
#include "selection/algorithm/Transformation.h"
#include "module/StaticModule.h"
#include "command/ExecutionNotPossible.h"
#include "MapPropertyInfoFileModule.h"
#include "messages/NotificationMessage.h"
#include <fmt/format.h>
#include "scene/ChildPrimitives.h"
namespace map
{
namespace
{
const char* const MAP_UNNAMED_STRING = N_("unnamed.map");
}
Map::Map() :
_lastCopyMapName(""),
_modified(false),
_saveInProgress(false),
_shutdownListener(0)
{}
void Map::clearMapResource()
{
// Map is unnamed or load failed, reset map resource node to empty
_resource->clear();
_resource->getRootNode()->getUndoChangeTracker().save();
// Rename the map to "unnamed" in any case to avoid overwriting the failed map
setMapName(_(MAP_UNNAMED_STRING));
}
void Map::loadMapResourceFromPath(const std::string& path)
{
// Map loading started
emitMapEvent(MapLoading);
_resource = GlobalMapResourceManager().loadFromPath(_mapName);
if (!_resource)
{
return;
}
try
{
if (isUnnamed() || !_resource->load())
{
clearMapResource();
}
}
catch (IMapResource::OperationException& ex)
{
radiant::NotificationMessage::SendError(ex.what());
clearMapResource();
}
// Take the new node and insert it as map root
GlobalSceneGraph().setRoot(_resource->getRootNode());
// Traverse the scenegraph and find the worldspawn
findWorldspawn();
// Associate the Scenegaph with the global RenderSystem
// This usually takes a while since all editor textures are loaded - display a dialog to inform the user
{
radiant::ScopedLongRunningOperation blocker(_("Loading textures..."));
GlobalSceneGraph().root()->setRenderSystem(std::dynamic_pointer_cast<RenderSystem>(
module::GlobalModuleRegistry().getModule(MODULE_RENDERSYSTEM)));
}
// Map loading finished, emit the signal
emitMapEvent(MapLoaded);
}
void Map::setMapName(const std::string& newName)
{
bool mapNameChanged = _mapName != newName;
// Store the name into the member
_mapName = newName;
// Update the map resource's root node, if there is one
if (_resource)
{
_resource->rename(newName);
}
if (mapNameChanged)
{
// Fire the signal to any observers
signal_mapNameChanged().emit();
}
}
sigc::signal<void>& Map::signal_mapNameChanged()
{
return _mapNameChangedSignal;
}
std::string Map::getMapName() const {
return _mapName;
}
bool Map::isUnnamed() const {
return _mapName == _(MAP_UNNAMED_STRING);
}
void Map::onSceneNodeErase(const scene::INodePtr& node)
{
// Detect when worldspawn is removed from the map
if (node == _worldSpawnNode)
{
_worldSpawnNode.reset();
}
}
void Map::setWorldspawn(const scene::INodePtr& node)
{
_worldSpawnNode = node;
}
Map::MapEventSignal Map::signal_mapEvent() const
{
return _mapEvent;
}
const scene::INodePtr& Map::getWorldspawn()
{
return _worldSpawnNode;
}
scene::IMapRootNodePtr Map::getRoot()
{
if (_resource)
{
// Try to cast the node onto a root node and return
return _resource->getRootNode();
}
return scene::IMapRootNodePtr();
}
MapFormatPtr Map::getFormat()
{
return GlobalMapFormatManager().getMapFormatForFilename(_mapName);
}
// free all map elements, reinitialize the structures that depend on them
void Map::freeMap()
{
// Fire the map unloading event,
// This will de-select stuff, clear the pointfile, etc.
emitMapEvent(MapUnloading);
setWorldspawn(scene::INodePtr());
GlobalSceneGraph().setRoot(scene::IMapRootNodePtr());
emitMapEvent(MapUnloaded);
// Reset the resource pointer
_resource.reset();
}
bool Map::isModified() const
{
return _modified;
}
void Map::setModified(bool modifiedFlag)
{
if (_modified != modifiedFlag)
{
_modified = modifiedFlag;
// when the map is modified, let the listeners now
signal_modifiedChanged().emit();
}
// Reset the map save timer
_mapSaveTimer.restart();
}
sigc::signal<void>& Map::signal_modifiedChanged()
{
return _mapModifiedChangedSignal;
}
// move the view to a certain position
void Map::focusViews(const Vector3& point, const Vector3& angles)
{
// Set the camera and the views to the given point
GlobalCameraManager().focusAllCameras(point, angles);
// ortho views might not be present in headless mode
if (module::GlobalModuleRegistry().moduleExists(MODULE_ORTHOVIEWMANAGER))
{
GlobalXYWndManager().setOrigin(point);
}
}
scene::INodePtr Map::findWorldspawn()
{
scene::INodePtr worldspawn;
// Traverse the scenegraph and search for the worldspawn
GlobalSceneGraph().root()->foreachNode([&](const scene::INodePtr& node)
{
if (Node_isWorldspawn(node))
{
worldspawn = node;
return false; // done traversing
}
return true;
});
// Set the worldspawn, might be null if nothing was found
setWorldspawn(worldspawn);
return worldspawn;
}
scene::INodePtr Map::createWorldspawn()
{
scene::INodePtr worldspawn(GlobalEntityModule().createEntity(
GlobalEntityClassManager().findOrInsert("worldspawn", true)));
// We want the world spawn entity to go for the pole position
GlobalSceneGraph().root()->addChildNodeToFront(worldspawn);
return worldspawn;
}
const scene::INodePtr& Map::findOrInsertWorldspawn()
{
// If we don't know any worldspawn yet, and can't find one either,
// let's create one afresh
if (!_worldSpawnNode && findWorldspawn() == nullptr)
{
setWorldspawn(createWorldspawn());
}
return _worldSpawnNode;
}
void Map::load(const std::string& filename) {
rMessage() << "Loading map from " << filename << "\n";
setMapName(filename);
{
util::ScopeTimer timer("map load");
loadMapResourceFromPath(_mapName);
}
rMessage() << "--- LoadMapFile ---\n";
rMessage() << _mapName << "\n";
rMessage() << GlobalCounters().getCounter(counterBrushes).get() << " brushes\n";
rMessage() << GlobalCounters().getCounter(counterPatches).get() << " patches\n";
rMessage() << GlobalCounters().getCounter(counterEntities).get() << " entities\n";
// Let the filtersystem update the filtered status of all instances
GlobalFilterSystem().update();
// Clear the modified flag
setModified(false);
}
bool Map::save(const MapFormatPtr& mapFormat)
{
if (_saveInProgress) return false; // safeguard
_saveInProgress = true;
emitMapEvent(MapSaving);
util::ScopeTimer timer("map save");
bool success = false;
// Save the actual map resource
try
{
_resource->save(mapFormat);
// Clear the modified flag
setModified(false);
success = true;
}
catch (IMapResource::OperationException & ex)
{
radiant::NotificationMessage::SendError(ex.what());
}
emitMapEvent(MapSaved);
_saveInProgress = false;
// Redraw the views, sometimes the backbuffer containing
// the previous frame will remain visible
GlobalMainFrame().updateAllWindows();
return success;
}
void Map::createNewMap()
{
setMapName(_(MAP_UNNAMED_STRING));
loadMapResourceFromPath(_mapName);
SceneChangeNotify();
setModified(false);
focusViews(Vector3(0,0,0), Vector3(0,0,0));
}
IMapExporter::Ptr Map::createMapExporter(IMapWriter& writer,
const scene::IMapRootNodePtr& root, std::ostream& mapStream)
{
return std::make_shared<MapExporter>(writer, root, mapStream, 0);
}
bool Map::import(const std::string& filename)
{
bool success = false;
IMapResourcePtr resource = GlobalMapResourceManager().loadFromPath(filename);
try
{
if (resource->load())
{
// load() returned TRUE, this means that the resource node
// is not the NULL node
const auto& otherRoot = resource->getRootNode();
// Adjust all new names to fit into the existing map namespace
algorithm::prepareNamesForImport(getRoot(), otherRoot);
algorithm::mergeMap(otherRoot);
success = true;
}
SceneChangeNotify();
}
catch (const IMapResource::OperationException& ex)
{
radiant::NotificationMessage::SendError(ex.what());
}
return success;
}
void Map::saveDirect(const std::string& filename, const MapFormatPtr& mapFormat)
{
if (_saveInProgress) return; // safeguard
_saveInProgress = true;
MapFormatPtr format = mapFormat;
if (!mapFormat)
{
format = GlobalMapFormatManager().getMapFormatForFilename(filename);
}
try
{
MapResource::saveFile(*format, GlobalSceneGraph().root(), scene::traverse, filename);
}
catch (const IMapResource::OperationException& ex)
{
radiant::NotificationMessage::SendError(ex.what());
}
_saveInProgress = false;
}
void Map::saveSelected(const std::string& filename, const MapFormatPtr& mapFormat)
{
if (_saveInProgress) return; // safeguard
_saveInProgress = true;
MapFormatPtr format = mapFormat;
if (!format)
{
format = GlobalMapFormatManager().getMapFormatForFilename(filename);
}
try
{
MapResource::saveFile(
*format,
GlobalSceneGraph().root(),
scene::traverseSelected, // TraversalFunc
filename
);
}
catch (const IMapResource::OperationException& ex)
{
radiant::NotificationMessage::SendError(ex.what());
}
_saveInProgress = false;
}
std::string Map::getSaveConfirmationText() const
{
std::string primaryText = fmt::format(_("Save changes to map \"{0}\"\nbefore closing?"), _mapName);
// Display "x seconds" or "x minutes"
int seconds = static_cast<int>(_mapSaveTimer.getSecondsPassed());
std::string timeString;
if (seconds > 120)
{
timeString = fmt::format(_("{0:d} minutes"), (seconds / 60));
}
else
{
timeString = fmt::format(_("{0:d} seconds"), seconds);
}
std::string secondaryText = fmt::format(
_("If you don't save, changes from the last {0}\nwill be lost."), timeString);
std::string confirmText = fmt::format("{0}\n\n{1}", primaryText, secondaryText);
return confirmText;
}
bool Map::askForSave(const std::string& title)
{
if (!isModified())
{
// Map is not modified, return positive
return true;
}
// Ask the user
auto msgBox = GlobalDialogManager().createMessageBox(
title,
getSaveConfirmationText(),
ui::IDialog::MESSAGE_SAVECONFIRMATION
);
auto result = msgBox->run();
if (result == ui::IDialog::RESULT_CANCELLED)
{
return false;
}
if (result == ui::IDialog::RESULT_YES)
{
// The user wants to save the map
if (isUnnamed())
{
// Map still unnamed, try to save the map with a new name
// and take the return value from the other routine.
return saveAs();
}
else
{
// Map is named, save it
save();
}
}
// Default behaviour: allow the save
return true;
}
bool Map::saveAs()
{
if (_saveInProgress) return false; // safeguard
MapFileSelection fileInfo =
MapFileManager::getMapFileSelection(false, _("Save Map"), filetype::TYPE_MAP, getMapName());
if (!fileInfo.fullPath.empty())
{
// Remember the old name, we might need to revert
std::string oldFilename = _mapName;
// Rename the file and try to save
rename(fileInfo.fullPath);
// Try to save the file, this might fail
bool success = save(fileInfo.mapFormat);
if (success)
{
GlobalMRU().insert(fileInfo.fullPath);
}
else if (!success)
{
// Revert the name change if the file could not be saved
rename(oldFilename);
}
return success;
}
else
{
// Invalid filename entered, return false
return false;
}
}
void Map::saveCopyAs()
{
// Let's see if we can remember a
if (_lastCopyMapName.empty()) {
_lastCopyMapName = getMapName();
}
MapFileSelection fileInfo =
MapFileManager::getMapFileSelection(false, _("Save Copy As..."), filetype::TYPE_MAP, _lastCopyMapName);
if (!fileInfo.fullPath.empty())
{
// Remember the last name
_lastCopyMapName = fileInfo.fullPath;
// Return the result of the actual save method
saveDirect(fileInfo.fullPath, fileInfo.mapFormat);
}
}
void Map::loadPrefabAt(const cmd::ArgumentList& args)
{
if (args.size() < 2 || args.size() > 3)
{
rWarning() << "Usage: " << LOAD_PREFAB_AT_CMD <<
" <prefabPath:String> <targetCoords:Vector3> [insertAsGroup:0|1]" << std::endl;
return;
}
auto prefabPath = args[0].getString();
auto targetCoords = args[1].getVector3();
auto insertAsGroup = args.size() > 2 ? args[2].getBoolean() : false;
if (!prefabPath.empty())
{
UndoableCommand undo("loadPrefabAt");
// Deselect everything
GlobalSelectionSystem().setSelectedAll(false);
// Now import the prefab (imported items get selected)
import(prefabPath);
// Switch texture lock on
bool prevTexLockState = GlobalBrush().textureLockEnabled();
GlobalBrush().setTextureLock(true);
// Translate the selection to the given point
selection::algorithm::translateSelected(targetCoords);
// Revert to previous state
GlobalBrush().setTextureLock(prevTexLockState);
// Check whether we should group the prefab parts
if (insertAsGroup && GlobalSelectionSystem().countSelected() > 1)
{
try
{
selection::groupSelected();
}
catch (cmd::ExecutionNotPossible& ex)
{
// Ignore grouping errors on prefab insert, just log the message
rError() << "Error grouping the prefab: " << ex.what() << std::endl;
}
}
}
}
void Map::saveMapCopyAs(const cmd::ArgumentList& args)
{
GlobalMap().saveCopyAs();
}
void Map::registerCommands()
{
GlobalCommandSystem().addCommand("NewMap", Map::newMap);
GlobalCommandSystem().addCommand("OpenMap", Map::openMap, { cmd::ARGTYPE_STRING | cmd::ARGTYPE_OPTIONAL });
GlobalCommandSystem().addCommand("ImportMap", Map::importMap);
GlobalCommandSystem().addCommand(LOAD_PREFAB_AT_CMD, std::bind(&Map::loadPrefabAt, this, std::placeholders::_1),
{ cmd::ARGTYPE_STRING, cmd::ARGTYPE_VECTOR3, cmd::ARGTYPE_INT|cmd::ARGTYPE_OPTIONAL });
GlobalCommandSystem().addCommand("SaveSelectedAsPrefab", Map::saveSelectedAsPrefab);
GlobalCommandSystem().addCommand("SaveMap", Map::saveMap);
GlobalCommandSystem().addCommand("SaveMapAs", Map::saveMapAs);
GlobalCommandSystem().addCommand("SaveMapCopyAs", Map::saveMapCopyAs);
GlobalCommandSystem().addCommand("ExportMap", Map::exportMap);
GlobalCommandSystem().addCommand("SaveSelected", Map::exportSelection);
GlobalCommandSystem().addCommand("ReloadSkins", map::algorithm::reloadSkins);
GlobalCommandSystem().addCommand("ExportSelectedAsModel", map::algorithm::exportSelectedAsModelCmd,
{ cmd::ARGTYPE_STRING,
cmd::ARGTYPE_STRING,
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL,
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL,
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL,
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL,
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL });
}
// Static command targets
void Map::newMap(const cmd::ArgumentList& args)
{
if (GlobalMap().askForSave(_("New Map")))
{
GlobalMap().freeMap();
GlobalMap().createNewMap();
}
}
void Map::openMap(const cmd::ArgumentList& args)
{
if (!GlobalMap().askForSave(_("Open Map"))) return;
std::string candidate;
if (!args.empty())
{
candidate = args[0].getString();
}
else
{
// No arguments passed, get the map file name to load
MapFileSelection fileInfo = MapFileManager::getMapFileSelection(true, _("Open map"), filetype::TYPE_MAP);
candidate = fileInfo.fullPath;
}
std::string mapToLoad;
if (os::fileOrDirExists(candidate))
{
mapToLoad = candidate;
}
else if (!candidate.empty())
{
// Next, try to look up the map in the regular maps path
fs::path mapsPath = GlobalGameManager().getMapPath();
fs::path fullMapPath = mapsPath / candidate;
if (os::fileOrDirExists(fullMapPath.string()))
{
mapToLoad = fullMapPath.string();
}
else
{
throw cmd::ExecutionFailure(fmt::format(_("File doesn't exist: {0}"), fullMapPath.string()));
}
}
if (!mapToLoad.empty())
{
GlobalMRU().insert(mapToLoad);
GlobalMap().freeMap();
GlobalMap().load(mapToLoad);
}
}
void Map::importMap(const cmd::ArgumentList& args)
{
MapFileSelection fileInfo =
MapFileManager::getMapFileSelection(true, _("Import map"), filetype::TYPE_MAP);
if (!fileInfo.fullPath.empty())
{
UndoableCommand undo("mapImport");
GlobalMap().import(fileInfo.fullPath);
}
}
void Map::saveMapAs(const cmd::ArgumentList& args) {
GlobalMap().saveAs();
}
void Map::saveMap(const cmd::ArgumentList& args)
{
if (GlobalMap().isUnnamed())
{
GlobalMap().saveAs();
}
// greebo: Always let the map be saved, regardless of the modified status.
else /*if(GlobalMap().isModified())*/
{
GlobalMap().save();
}
}
void Map::exportMap(const cmd::ArgumentList& args)
{
auto fileInfo = MapFileManager::getMapFileSelection(false, _("Export Map"), filetype::TYPE_MAP_EXPORT);
if (!fileInfo.fullPath.empty())
{
GlobalMap().emitMapEvent(MapSaving);
MapResource::saveFile(*fileInfo.mapFormat,
GlobalSceneGraph().root(),
scene::traverse,
fileInfo.fullPath);
GlobalMap().emitMapEvent(MapSaved);
}
}
void Map::exportSelection(const cmd::ArgumentList& args)
{
MapFileSelection fileInfo =
MapFileManager::getMapFileSelection(false, _("Export selection"), filetype::TYPE_MAP);
if (!fileInfo.fullPath.empty())
{
GlobalMap().saveSelected(fileInfo.fullPath, fileInfo.mapFormat);
}
}
void Map::saveSelectedAsPrefab(const cmd::ArgumentList& args)
{
MapFileSelection fileInfo =
MapFileManager::getMapFileSelection(false, _("Save selected as Prefab"), filetype::TYPE_PREFAB);
if (!fileInfo.fullPath.empty())
{
GlobalMap().saveSelected(fileInfo.fullPath, fileInfo.mapFormat);
}
}
void Map::rename(const std::string& filename) {
if (_mapName != filename) {
setMapName(filename);
SceneChangeNotify();
}
else {
_resource->save();
setModified(false);
}
}
void Map::exportSelected(std::ostream& out)
{
exportSelected(out, getFormat());
}
void Map::exportSelected(std::ostream& out, const MapFormatPtr& format)
{
assert(format);
// Create our main MapExporter walker for traversal
auto writer = format->getMapWriter();
MapExporter exporter(*writer, GlobalSceneGraph().root(), out);
// Pass the traverseSelected function and start writing selected nodes
exporter.exportMap(GlobalSceneGraph().root(), scene::traverseSelected);
}
void Map::emitMapEvent(MapEvent ev)
{
try
{
signal_mapEvent().emit(ev);
}
catch (std::runtime_error & ex)
{
radiant::NotificationMessage::SendError(fmt::format(_("Failure running map event {0}:\n{1}"), ev, ex.what()));
}
}
// RegisterableModule implementation
const std::string& Map::getName() const
{
static std::string _name(MODULE_MAP);
return _name;
}
const StringSet& Map::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_GAMEMANAGER);
_dependencies.insert(MODULE_SCENEGRAPH);
_dependencies.insert(MODULE_MAPINFOFILEMANAGER);
_dependencies.insert(MODULE_FILETYPES);
_dependencies.insert(MODULE_MAPRESOURCEMANAGER);
}
return _dependencies;
}
void Map::initialiseModule(const IApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;
// Register for the startup event
_mapPositionManager.reset(new MapPositionManager);
GlobalSceneGraph().addSceneObserver(this);
// Add the Map-related commands to the EventManager
registerCommands();
_scaledModelExporter.initialise();
_modelScalePreserver.reset(new ModelScalePreserver);
MapFileManager::registerFileTypes();
// Register an info file module to save the map property bag
GlobalMapInfoFileManager().registerInfoFileModule(
std::make_shared<MapPropertyInfoFileModule>()
);
// Free the map right before all modules are shut down
module::GlobalModuleRegistry().signal_modulesUninitialising().connect(
sigc::mem_fun(this, &Map::freeMap)
);
_shutdownListener = GlobalRadiantCore().getMessageBus().addListener(
radiant::IMessage::Type::ApplicationShutdownRequest,
radiant::TypeListener<radiant::ApplicationShutdownRequest>(
sigc::mem_fun(this, &Map::handleShutdownRequest)));
}
void Map::shutdownModule()
{
GlobalRadiantCore().getMessageBus().removeListener(_shutdownListener);
_scaledModelExporter.shutdown();
GlobalSceneGraph().removeSceneObserver(this);
_modelScalePreserver.reset();
_mapPositionManager.reset();
}
void Map::handleShutdownRequest(radiant::ApplicationShutdownRequest& request)
{
if (!askForSave(_("Exit DarkRadiant")))
{
request.deny();
}
}
} // namespace map