/
CameraSettings.cpp
318 lines (261 loc) · 8.95 KB
/
CameraSettings.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
#include "CameraSettings.h"
#include "i18n.h"
#include "ieventmanager.h"
#include "ipreferencesystem.h"
#include "registry/registry.h"
#include "util/ScopedBoolLock.h"
#include "CameraWndManager.h"
#include "string/convert.h"
#include "wxutil/dialog/MessageBox.h"
namespace ui
{
CameraSettings::CameraSettings() :
_callbackActive(false),
_movementSpeed(registry::getValue<int>(RKEY_MOVEMENT_SPEED)),
_angleSpeed(registry::getValue<int>(RKEY_ROTATION_SPEED)),
_invertMouseVerticalAxis(registry::getValue<bool>(RKEY_INVERT_MOUSE_VERTICAL_AXIS)),
_discreteMovement(registry::getValue<bool>(RKEY_DISCRETE_MOVEMENT)),
_cameraDrawMode(RENDER_MODE_TEXTURED),
_cubicScale(registry::getValue<int>(RKEY_CUBIC_SCALE)),
_farClipEnabled(registry::getValue<bool>(RKEY_ENABLE_FARCLIP)),
_solidSelectionBoxes(registry::getValue<bool>(RKEY_SOLID_SELECTION_BOXES)),
_toggleFreelook(registry::getValue<bool>(RKEY_TOGGLE_FREE_MOVE)),
_gridEnabled(registry::getValue<bool>(RKEY_CAMERA_GRID_ENABLED)),
_gridSpacing(registry::getValue<int>(RKEY_CAMERA_GRID_SPACING))
{
// Constrain the cubic scale to a fixed value
if (_cubicScale > MAX_CUBIC_SCALE) {
_cubicScale = MAX_CUBIC_SCALE;
}
if (_gridSpacing > 512)
{
_gridSpacing = 512;
}
else if (_gridSpacing < 4)
{
_gridSpacing = 4;
}
// Initialise the draw mode from the integer value stored in the registry
importDrawMode(registry::getValue<int>(RKEY_DRAWMODE));
// Connect self to the according registry keys
observeKey(RKEY_MOVEMENT_SPEED);
observeKey(RKEY_ROTATION_SPEED);
observeKey(RKEY_INVERT_MOUSE_VERTICAL_AXIS);
observeKey(RKEY_DISCRETE_MOVEMENT);
observeKey(RKEY_ENABLE_FARCLIP);
observeKey(RKEY_DRAWMODE);
observeKey(RKEY_SOLID_SELECTION_BOXES);
observeKey(RKEY_TOGGLE_FREE_MOVE);
observeKey(RKEY_CAMERA_GRID_ENABLED);
observeKey(RKEY_CAMERA_GRID_SPACING);
// greebo: Add the preference settings
constructPreferencePage();
}
void CameraSettings::observeKey(const std::string& key)
{
GlobalRegistry().signalForKey(key).connect(
sigc::mem_fun(this, &CameraSettings::keyChanged)
);
}
void CameraSettings::constructPreferencePage()
{
IPreferencePage& page = GlobalPreferenceSystem().getPage(_("Settings/Camera"));
// Add the sliders for the movement and angle speed and connect them to the observer
page.appendSlider(_("Movement Speed (game units)"), RKEY_MOVEMENT_SPEED, 1, MAX_CAMERA_SPEED, 1, 1);
page.appendSlider(_("Rotation Speed"), RKEY_ROTATION_SPEED, 1, 180, 1, 10);
// Add the checkboxes and connect them with the registry key and the according observer
page.appendCheckBox(_("Freelook mode can be toggled"), RKEY_TOGGLE_FREE_MOVE);
page.appendCheckBox(_("Discrete movement (non-freelook mode)"), RKEY_DISCRETE_MOVEMENT);
page.appendCheckBox(_("Enable far-clip plane (hides distant objects)"), RKEY_ENABLE_FARCLIP);
// Add the "inverse mouse vertical axis in free-look mode" preference
page.appendCheckBox(_("Invert mouse vertical axis (freelook mode)"), RKEY_INVERT_MOUSE_VERTICAL_AXIS);
// States whether the selection boxes are stippled or not
page.appendCheckBox(_("Solid selection boxes"), RKEY_SOLID_SELECTION_BOXES);
// Whether to show the toolbar (to please the screenspace addicts)
page.appendCheckBox(_("Show camera toolbar"), RKEY_SHOW_CAMERA_TOOLBAR);
page.appendCheckBox(_("Draw 3D grid"), RKEY_CAMERA_GRID_ENABLED);
ComboBoxValueList gridSpacings;
for (int i = 4; i <= 512; i *= 2)
{
gridSpacings.push_back(string::to_string(i));
}
page.appendCombo(_("Grid spacing"), RKEY_CAMERA_GRID_SPACING, gridSpacings, true);
}
bool CameraSettings::showCameraToolbar() const
{
// TODO: There must be a less verbose way of introducing a new RKEY with a
// default behaviour if unset.
if (!GlobalRegistry().keyExists(RKEY_SHOW_CAMERA_TOOLBAR))
{
registry::setValue(RKEY_SHOW_CAMERA_TOOLBAR, true);
}
return registry::getValue<bool>(RKEY_SHOW_CAMERA_TOOLBAR);
}
bool CameraSettings::gridEnabled() const
{
return _gridEnabled;
}
int CameraSettings::gridSpacing() const
{
return _gridSpacing;
}
void CameraSettings::importDrawMode(const int mode)
{
switch (mode) {
case 0:
_cameraDrawMode = RENDER_MODE_WIREFRAME;
break;
case 1:
_cameraDrawMode = RENDER_MODE_SOLID;
break;
case 2:
_cameraDrawMode = RENDER_MODE_TEXTURED;
break;
case 3:
_cameraDrawMode = RENDER_MODE_LIGHTING;
break;
default:
_cameraDrawMode = RENDER_MODE_TEXTURED;
}
// Enabling camera modes may throw an exception if there is a problem with
// GL shaders.
try
{
GlobalRenderSystem().setShaderProgram(
_cameraDrawMode == RENDER_MODE_LIGHTING
? RenderSystem::SHADER_PROGRAM_INTERACTION
: RenderSystem::SHADER_PROGRAM_NONE
);
}
catch (const std::runtime_error& e)
{
wxutil::Messagebox::ShowError("Failed to enable lighting mode:\n\n"
+ std::string(e.what()));
}
_sigRenderModeChanged.emit();
}
void CameraSettings::keyChanged()
{
// Check for iterative loops
if (_callbackActive) {
return;
}
util::ScopedBoolLock lock(_callbackActive);
// Load the values from the registry
_toggleFreelook = registry::getValue<bool>(RKEY_TOGGLE_FREE_MOVE);
_movementSpeed = registry::getValue<int>(RKEY_MOVEMENT_SPEED);
_angleSpeed = registry::getValue<int>(RKEY_ROTATION_SPEED);
_invertMouseVerticalAxis = registry::getValue<bool>(RKEY_INVERT_MOUSE_VERTICAL_AXIS);
_farClipEnabled = registry::getValue<bool>(RKEY_ENABLE_FARCLIP);
_solidSelectionBoxes = registry::getValue<bool>(RKEY_SOLID_SELECTION_BOXES);
_gridEnabled = registry::getValue<bool>(RKEY_CAMERA_GRID_ENABLED);
_gridSpacing = registry::getValue<int>(RKEY_CAMERA_GRID_SPACING);
// Determine the draw mode represented by the integer registry value
importDrawMode(registry::getValue<int>(RKEY_DRAWMODE));
// Check if a global camwindow is set
CamWndPtr cam = GlobalCamera().getActiveCamWnd();
GlobalCamera().foreachCamWnd([&](CamWnd& cam)
{
bool freeMovedWasEnabled = cam.freeMoveEnabled();
// Disable free move if it was enabled during key change (e.g. LightingMode Toggle)
if (freeMovedWasEnabled)
{
cam.disableFreeMove();
}
// Disconnect the handlers for the old state and re-connect after reading the registry value
cam.removeHandlersMove();
// Check the value and take the according actions
_discreteMovement = registry::getValue<bool>(RKEY_DISCRETE_MOVEMENT);
// Reconnect the new handlers
cam.addHandlersMove();
cam.setFarClipPlaneEnabled(_farClipEnabled);
cam.setFarClipPlaneDistance(calculateFarPlaneDistance(_cubicScale));
if (freeMovedWasEnabled)
{
cam.enableFreeMove();
}
});
// Call the update method in case the render mode has changed
GlobalCamera().update();
}
CameraDrawMode CameraSettings::getRenderMode() const
{
return _cameraDrawMode;
}
void CameraSettings::setRenderMode(const CameraDrawMode& mode)
{
// Write the value into the registry, this should trigger the keyChanged()
// callback that in turn calls the update functions
registry::setValue(RKEY_DRAWMODE, static_cast<int>(mode));
}
void CameraSettings::toggleLightingMode()
{
// switch between textured and lighting mode
setRenderMode(
(_cameraDrawMode == RENDER_MODE_LIGHTING)
? RENDER_MODE_TEXTURED
: RENDER_MODE_LIGHTING
);
}
bool CameraSettings::toggleFreelook() const {
return _toggleFreelook;
}
bool CameraSettings::farClipEnabled() const
{
return _farClipEnabled;
}
bool CameraSettings::solidSelectionBoxes() const {
return _solidSelectionBoxes;
}
int CameraSettings::cubicScale() const {
return _cubicScale;
}
int CameraSettings::movementSpeed() const {
return _movementSpeed;
}
int CameraSettings::angleSpeed() const {
return _angleSpeed;
}
bool CameraSettings::invertMouseVerticalAxis() const {
return _invertMouseVerticalAxis;
}
bool CameraSettings::discreteMovement() const {
return _discreteMovement;
}
void CameraSettings::setCubicScale(int scale)
{
// Update the internal value
_cubicScale = scale;
// Constrain the value to [1..MAX_CUBIC_SCALE]
if (_cubicScale>MAX_CUBIC_SCALE) {
_cubicScale = MAX_CUBIC_SCALE;
}
if (_cubicScale < 1) {
_cubicScale = 1;
}
// Store to registry
registry::setValue(RKEY_CUBIC_SCALE, _cubicScale);
}
void CameraSettings::setFarClip(bool farClipEnabled)
{
registry::setValue(RKEY_ENABLE_FARCLIP, farClipEnabled);
}
int CameraSettings::fontSize() const
{
return registry::getValue<int>(RKEY_CAMERA_FONT_SIZE);
}
IGLFont::Style CameraSettings::fontStyle() const
{
return registry::getValue<std::string>(RKEY_CAMERA_FONT_STYLE) == "Sans" ?
IGLFont::Style::Sans : IGLFont::Style::Mono;
}
void CameraSettings::toggleFarClip(bool)
{
setFarClip(!_farClipEnabled);
}
} // namespace
ui::CameraSettings* getCameraSettings()
{
static ui::CameraSettings _cameraSettings;
return &_cameraSettings;
}