/
Clipboard.cpp
137 lines (112 loc) · 3.44 KB
/
Clipboard.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
#include "Clipboard.h"
#include "i18n.h"
#include "iselection.h"
#include "igrid.h"
#include "icameraview.h"
#include "imapformat.h"
#include "iclipboard.h"
#include "ishaderclipboard.h"
#include "string/trim.h"
#include "map/Map.h"
#include "brush/FaceInstance.h"
#include "map/algorithm/Import.h"
#include "selection/algorithm/General.h"
#include "selection/algorithm/Transformation.h"
#include "command/ExecutionNotPossible.h"
#include "command/ExecutionFailure.h"
namespace selection
{
namespace clipboard
{
void pasteToMap()
{
if (!module::GlobalModuleRegistry().moduleExists(MODULE_CLIPBOARD))
{
throw cmd::ExecutionNotPossible(_("No clipboard module attached, cannot perform this action."));
}
std::stringstream stream(GlobalClipboard().getString());
map::algorithm::importFromStream(stream);
}
void copy(const cmd::ArgumentList& args)
{
if (FaceInstance::Selection().empty())
{
if (!module::GlobalModuleRegistry().moduleExists(MODULE_CLIPBOARD))
{
throw cmd::ExecutionNotPossible(_("No clipboard module attached, cannot perform this action."));
}
// When exporting to the system clipboard, use the portable format
auto format = GlobalMapFormatManager().getMapFormatByName(map::PORTABLE_MAP_FORMAT_NAME);
// Stream selected objects into a stringstream
std::stringstream out;
GlobalMap().exportSelected(out, format);
// Copy the resulting string to the clipboard
GlobalClipboard().setString(out.str());
}
else
{
algorithm::pickShaderFromSelection(args);
}
}
std::string getMaterialNameFromClipboard()
{
if (!module::GlobalModuleRegistry().moduleExists(MODULE_CLIPBOARD))
{
return std::string();
}
auto candidate = GlobalClipboard().getString();
string::trim(candidate);
// If we get a single line, check if the contents point to a material we know
if (!candidate.empty() && candidate.find('\n') == std::string::npos &&
GlobalMaterialManager().materialExists(candidate))
{
return candidate;
}
return std::string();
}
void paste(const cmd::ArgumentList& args)
{
if (FaceInstance::Selection().empty())
{
auto clipboardMaterial = getMaterialNameFromClipboard();
if (!clipboardMaterial.empty())
{
UndoableCommand undo("pasteMaterialFromClipboard");
// Activate the material name in the shader clipboard, but don't overwrite
// anything there if the material is already matching to not overwrite Face/Patch information
if (GlobalShaderClipboard().getShaderName() != clipboardMaterial)
{
GlobalShaderClipboard().setSourceShader(clipboardMaterial);
}
algorithm::pasteShaderToSelection(args);
return;
}
// Try to parse the map and apply it
UndoableCommand undo("paste");
pasteToMap();
}
else
{
algorithm::pasteShaderToSelection(args);
}
}
void pasteToCamera(const cmd::ArgumentList& args)
{
try
{
auto& camWnd = GlobalCameraManager().getActiveView();
UndoableCommand undo("pasteToCamera");
pasteToMap();
// Work out the delta
Vector3 mid = algorithm::getCurrentSelectionCenter();
Vector3 delta = camWnd.getCameraOrigin().getSnapped(GlobalGrid().getGridSize()) - mid;
// Move to camera
algorithm::translateSelected(delta);
}
catch (const std::runtime_error& ex)
{
rError() << "Cannot paste to camera: " << ex.what() << std::endl;
}
}
} // namespace
} // namespace