Skip to content

Commit af92d22

Browse files
committed
editor: reduce code duplication for all other components as well
1 parent 8412014 commit af92d22

25 files changed

Lines changed: 1764 additions & 4217 deletions

Editor/CameraComponentWindow.cpp

Lines changed: 44 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -103,126 +103,84 @@ void CameraComponentWindow::Create(EditorComponent* _editor)
103103
float step = hei + 2;
104104
float wid = 120;
105105

106+
auto forAllSelectedCameraComponents = [&](auto /* void(nonnull CameraComponent*, wi::gui::EventArgs) */ func) {
107+
return [&](wi::gui::EventArgs args) {
108+
wi::scene::Scene& scene = editor->GetCurrentScene();
109+
for (auto& x : editor->translator.selected)
110+
{
111+
CameraComponent* camera = scene.cameras.GetComponent(x.entity);
112+
if (camera == nullptr)
113+
continue;
114+
func(camera, args);
115+
camera->SetDirty();
116+
}
117+
};
118+
};
119+
106120
farPlaneSlider.Create(100, 10000, 5000, 100000, "Far Plane: ");
107121
farPlaneSlider.SetTooltip("Controls the camera's far clip plane, geometry farther than this will be clipped.");
108122
farPlaneSlider.SetSize(XMFLOAT2(wid, hei));
109123
farPlaneSlider.SetPos(XMFLOAT2(x, y));
110124
farPlaneSlider.SetValue(editor->GetCurrentEditorScene().camera.zFarP);
111-
farPlaneSlider.OnSlide([=](wi::gui::EventArgs args) {
112-
wi::scene::Scene& scene = editor->GetCurrentScene();
113-
for (auto& x : editor->translator.selected)
114-
{
115-
CameraComponent* camera = scene.cameras.GetComponent(x.entity);
116-
if (camera == nullptr)
117-
continue;
118-
camera->zFarP = args.fValue;
119-
camera->SetDirty();
120-
}
121-
});
125+
farPlaneSlider.OnSlide(forAllSelectedCameraComponents([&](auto camera, auto args) {
126+
camera->zFarP = args.fValue;
127+
}));
122128
AddWidget(&farPlaneSlider);
123129

124130
nearPlaneSlider.Create(0.01f, 10, 0.1f, 10000, "Near Plane: ");
125131
nearPlaneSlider.SetTooltip("Controls the camera's near clip plane, geometry closer than this will be clipped.");
126132
nearPlaneSlider.SetSize(XMFLOAT2(wid, hei));
127133
nearPlaneSlider.SetPos(XMFLOAT2(x, y += step));
128134
nearPlaneSlider.SetValue(editor->GetCurrentEditorScene().camera.zNearP);
129-
nearPlaneSlider.OnSlide([=](wi::gui::EventArgs args) {
130-
wi::scene::Scene& scene = editor->GetCurrentScene();
131-
for (auto& x : editor->translator.selected)
132-
{
133-
CameraComponent* camera = scene.cameras.GetComponent(x.entity);
134-
if (camera == nullptr)
135-
continue;
136-
camera->zNearP = args.fValue;
137-
camera->SetDirty();
138-
}
139-
});
135+
nearPlaneSlider.OnSlide(forAllSelectedCameraComponents([&](auto camera, auto args) {
136+
camera->zNearP = args.fValue;
137+
}));
140138
AddWidget(&nearPlaneSlider);
141139

142140
fovSlider.Create(1, 179, 60, 10000, "FOV: ");
143141
fovSlider.SetTooltip("Controls the camera's top-down field of view (in degrees)");
144142
fovSlider.SetSize(XMFLOAT2(wid, hei));
145143
fovSlider.SetPos(XMFLOAT2(x, y += step));
146144
fovSlider.SetValue(editor->GetCurrentEditorScene().camera.fov / XM_PI * 180.f);
147-
fovSlider.OnSlide([=](wi::gui::EventArgs args) {
148-
wi::scene::Scene& scene = editor->GetCurrentScene();
149-
for (auto& x : editor->translator.selected)
150-
{
151-
CameraComponent* camera = scene.cameras.GetComponent(x.entity);
152-
if (camera == nullptr)
153-
continue;
154-
camera->fov = args.fValue / 180.f * XM_PI;
155-
camera->SetDirty();
156-
}
157-
});
145+
fovSlider.OnSlide(forAllSelectedCameraComponents([&](auto camera, auto args) {
146+
camera->fov = args.fValue / 180.f * XM_PI;
147+
}));
158148
AddWidget(&fovSlider);
159149

160150
focalLengthSlider.Create(0.001f, 100, 1, 10000, "Focal Length: ");
161151
focalLengthSlider.SetTooltip("Controls the depth of field effect's focus distance");
162152
focalLengthSlider.SetSize(XMFLOAT2(wid, hei));
163153
focalLengthSlider.SetPos(XMFLOAT2(x, y += step));
164-
focalLengthSlider.OnSlide([=](wi::gui::EventArgs args) {
165-
wi::scene::Scene& scene = editor->GetCurrentScene();
166-
for (auto& x : editor->translator.selected)
167-
{
168-
CameraComponent* camera = scene.cameras.GetComponent(x.entity);
169-
if (camera == nullptr)
170-
continue;
171-
camera->focal_length = args.fValue;
172-
camera->SetDirty();
173-
}
174-
});
154+
focalLengthSlider.OnSlide(forAllSelectedCameraComponents([&](auto camera, auto args) {
155+
camera->focal_length = args.fValue;
156+
}));
175157
AddWidget(&focalLengthSlider);
176158

177159
apertureSizeSlider.Create(0, 1, 0, 10000, "Aperture Size: ");
178160
apertureSizeSlider.SetTooltip("Controls the depth of field effect's strength");
179161
apertureSizeSlider.SetSize(XMFLOAT2(wid, hei));
180162
apertureSizeSlider.SetPos(XMFLOAT2(x, y += step));
181-
apertureSizeSlider.OnSlide([=](wi::gui::EventArgs args) {
182-
wi::scene::Scene& scene = editor->GetCurrentScene();
183-
for (auto& x : editor->translator.selected)
184-
{
185-
CameraComponent* camera = scene.cameras.GetComponent(x.entity);
186-
if (camera == nullptr)
187-
continue;
188-
camera->aperture_size = args.fValue;
189-
camera->SetDirty();
190-
}
191-
});
163+
apertureSizeSlider.OnSlide(forAllSelectedCameraComponents([&](auto camera, auto args) {
164+
camera->aperture_size = args.fValue;
165+
}));
192166
AddWidget(&apertureSizeSlider);
193167

194168
apertureShapeXSlider.Create(0, 2, 1, 10000, "Aperture Shape X: ");
195169
apertureShapeXSlider.SetTooltip("Controls the depth of field effect's bokeh shape");
196170
apertureShapeXSlider.SetSize(XMFLOAT2(wid, hei));
197171
apertureShapeXSlider.SetPos(XMFLOAT2(x, y += step));
198-
apertureShapeXSlider.OnSlide([=](wi::gui::EventArgs args) {
199-
wi::scene::Scene& scene = editor->GetCurrentScene();
200-
for (auto& x : editor->translator.selected)
201-
{
202-
CameraComponent* camera = scene.cameras.GetComponent(x.entity);
203-
if (camera == nullptr)
204-
continue;
205-
camera->aperture_shape.x = args.fValue;
206-
camera->SetDirty();
207-
}
208-
});
172+
apertureShapeXSlider.OnSlide(forAllSelectedCameraComponents([&](auto camera, auto args) {
173+
camera->aperture_shape.x = args.fValue;
174+
}));
209175
AddWidget(&apertureShapeXSlider);
210176

211177
apertureShapeYSlider.Create(0, 2, 1, 10000, "Aperture Shape Y: ");
212178
apertureShapeYSlider.SetTooltip("Controls the depth of field effect's bokeh shape");
213179
apertureShapeYSlider.SetSize(XMFLOAT2(wid, hei));
214180
apertureShapeYSlider.SetPos(XMFLOAT2(x, y += step));
215-
apertureShapeYSlider.OnSlide([=](wi::gui::EventArgs args) {
216-
wi::scene::Scene& scene = editor->GetCurrentScene();
217-
for (auto& x : editor->translator.selected)
218-
{
219-
CameraComponent* camera = scene.cameras.GetComponent(x.entity);
220-
if (camera == nullptr)
221-
continue;
222-
camera->aperture_shape.y = args.fValue;
223-
camera->SetDirty();
224-
}
225-
});
181+
apertureShapeYSlider.OnSlide(forAllSelectedCameraComponents([&](auto camera, auto args) {
182+
camera->aperture_shape.y = args.fValue;
183+
}));
226184
AddWidget(&apertureShapeYSlider);
227185

228186
renderButton.Create("RenderToTexture");
@@ -265,49 +223,23 @@ void CameraComponentWindow::Create(EditorComponent* _editor)
265223

266224
resolutionXSlider.Create(128, 2048, 256, 2048 - 128, "Render Width: ");
267225
resolutionXSlider.SetTooltip("Set the render resolution Width");
268-
resolutionXSlider.OnSlide([=](wi::gui::EventArgs args) {
269-
wi::scene::Scene& scene = editor->GetCurrentScene();
270-
for (auto& x : editor->translator.selected)
271-
{
272-
CameraComponent* camera = scene.cameras.GetComponent(x.entity);
273-
if (camera == nullptr)
274-
continue;
275-
camera->render_to_texture.resolution.x = (uint32_t)args.iValue;
276-
camera->SetDirty();
277-
}
278-
});
226+
resolutionXSlider.OnSlide(forAllSelectedCameraComponents([&](auto camera, auto args) {
227+
camera->render_to_texture.resolution.x = (uint32_t)args.iValue;
228+
}));
279229
AddWidget(&resolutionXSlider);
280230

281231
resolutionYSlider.Create(128, 2048, 256, 2048 - 128, "Render Height: ");
282232
resolutionYSlider.SetTooltip("Set the render resolution Height");
283-
resolutionYSlider.OnSlide([=](wi::gui::EventArgs args) {
284-
wi::scene::Scene& scene = editor->GetCurrentScene();
285-
for (auto& x : editor->translator.selected)
286-
{
287-
CameraComponent* camera = scene.cameras.GetComponent(x.entity);
288-
if (camera == nullptr)
289-
continue;
290-
camera->render_to_texture.resolution.y = (uint32_t)args.iValue;
291-
camera->SetDirty();
292-
}
293-
});
233+
resolutionYSlider.OnSlide(forAllSelectedCameraComponents([&](auto camera, auto args) {
234+
camera->render_to_texture.resolution.y = (uint32_t)args.iValue;
235+
}));
294236
AddWidget(&resolutionYSlider);
295237

296238
samplecountSlider.Create(1, 8, 1, 7, "Sample count: ");
297239
samplecountSlider.SetTooltip("Set the render resolution sample count (MSAA)");
298-
samplecountSlider.OnSlide([=](wi::gui::EventArgs args) {
299-
wi::scene::Scene& scene = editor->GetCurrentScene();
300-
uint32_t samplecount = wi::math::GetNextPowerOfTwo((uint32_t)args.iValue);
301-
samplecountSlider.SetValue((int)samplecount);
302-
for (auto& x : editor->translator.selected)
303-
{
304-
CameraComponent* camera = scene.cameras.GetComponent(x.entity);
305-
if (camera == nullptr)
306-
continue;
307-
camera->render_to_texture.sample_count = samplecount;
308-
camera->SetDirty();
309-
}
310-
});
240+
samplecountSlider.OnSlide(forAllSelectedCameraComponents([&](auto camera, auto args) {
241+
camera->render_to_texture.sample_count = wi::math::GetNextPowerOfTwo((uint32_t)args.iValue);
242+
}));
311243
AddWidget(&samplecountSlider);
312244

313245

Editor/CameraWindow.cpp

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ void CameraWindow::Create(EditorComponent* _editor)
5050

5151
ResetCam();
5252

53+
auto updateCamera = [&](auto func) {
54+
return [&](wi::gui::EventArgs args) {
55+
Scene& scene = editor->GetCurrentScene();
56+
CameraComponent& camera = editor->GetCurrentEditorScene().camera;
57+
func(camera, args);
58+
camera.UpdateCamera();
59+
camera.SetDirty();
60+
};
61+
};
62+
5363
farPlaneSlider.Create(100, 10000, 5000, 100000, "Far Plane: ");
5464
farPlaneSlider.SetTooltip("Controls the camera's far clip plane, geometry farther than this will be clipped.");
5565
farPlaneSlider.SetSize(XMFLOAT2(wid, hei));
@@ -59,15 +69,11 @@ void CameraWindow::Create(EditorComponent* _editor)
5969
editor->GetCurrentEditorScene().camera.zFarP = editor->main->config.GetSection("camera").GetFloat("far");
6070
}
6171
farPlaneSlider.SetValue(editor->GetCurrentEditorScene().camera.zFarP);
62-
farPlaneSlider.OnSlide([&](wi::gui::EventArgs args) {
63-
Scene& scene = editor->GetCurrentScene();
64-
CameraComponent& camera = editor->GetCurrentEditorScene().camera;
72+
farPlaneSlider.OnSlide(updateCamera([&](auto camera, auto args) {
6573
camera.zFarP = args.fValue;
66-
camera.UpdateCamera();
67-
camera.SetDirty();
6874
editor->main->config.GetSection("camera").Set("far", args.fValue);
6975
editor->main->config.Commit();
70-
});
76+
}));
7177
AddWidget(&farPlaneSlider);
7278

7379
nearPlaneSlider.Create(0.01f, 10, 0.1f, 10000, "Near Plane: ");
@@ -79,15 +85,11 @@ void CameraWindow::Create(EditorComponent* _editor)
7985
editor->GetCurrentEditorScene().camera.zNearP = editor->main->config.GetSection("camera").GetFloat("near");
8086
}
8187
nearPlaneSlider.SetValue(editor->GetCurrentEditorScene().camera.zNearP);
82-
nearPlaneSlider.OnSlide([&](wi::gui::EventArgs args) {
83-
Scene& scene = editor->GetCurrentScene();
84-
CameraComponent& camera = editor->GetCurrentEditorScene().camera;
88+
nearPlaneSlider.OnSlide(updateCamera([&](auto camera, auto args) {
8589
camera.zNearP = args.fValue;
86-
camera.UpdateCamera();
87-
camera.SetDirty();
8890
editor->main->config.GetSection("camera").Set("near", args.fValue);
8991
editor->main->config.Commit();
90-
});
92+
}));
9193
AddWidget(&nearPlaneSlider);
9294

9395
fovSlider.Create(1, 179, 60, 10000, "FOV: ");
@@ -99,67 +101,47 @@ void CameraWindow::Create(EditorComponent* _editor)
99101
editor->GetCurrentEditorScene().camera.fov = editor->main->config.GetSection("camera").GetFloat("fov") / 180.f * XM_PI;
100102
}
101103
fovSlider.SetValue(editor->GetCurrentEditorScene().camera.fov / XM_PI * 180.f);
102-
fovSlider.OnSlide([&](wi::gui::EventArgs args) {
103-
Scene& scene = editor->GetCurrentScene();
104-
CameraComponent& camera = editor->GetCurrentEditorScene().camera;
104+
fovSlider.OnSlide(updateCamera([&](auto camera, auto args) {
105105
camera.fov = args.fValue / 180.f * XM_PI;
106-
camera.UpdateCamera();
107-
camera.SetDirty();
108106
editor->main->config.GetSection("camera").Set("fov", args.fValue);
109107
editor->main->config.Commit();
110-
});
108+
}));
111109
AddWidget(&fovSlider);
112110

113111
focalLengthSlider.Create(0.001f, 100, 1, 10000, "Focal Length: ");
114112
focalLengthSlider.SetTooltip("Controls the depth of field effect's focus distance.\nYou can also refocus by holding the C key and picking in the scene with the left mouse button.");
115113
focalLengthSlider.SetSize(XMFLOAT2(wid, hei));
116114
focalLengthSlider.SetPos(XMFLOAT2(x, y += step));
117-
focalLengthSlider.OnSlide([&](wi::gui::EventArgs args) {
118-
Scene& scene = editor->GetCurrentScene();
119-
CameraComponent& camera = editor->GetCurrentEditorScene().camera;
115+
focalLengthSlider.OnSlide(updateCamera([&](auto camera, auto args) {
120116
camera.focal_length = args.fValue;
121-
camera.UpdateCamera();
122-
camera.SetDirty();
123-
});
117+
}));
124118
AddWidget(&focalLengthSlider);
125119

126120
apertureSizeSlider.Create(0, 1, 0, 10000, "Aperture Size: ");
127121
apertureSizeSlider.SetTooltip("Controls the depth of field effect's strength");
128122
apertureSizeSlider.SetSize(XMFLOAT2(wid, hei));
129123
apertureSizeSlider.SetPos(XMFLOAT2(x, y += step));
130-
apertureSizeSlider.OnSlide([&](wi::gui::EventArgs args) {
131-
Scene& scene = editor->GetCurrentScene();
132-
CameraComponent& camera = editor->GetCurrentEditorScene().camera;
124+
apertureSizeSlider.OnSlide(updateCamera([&](auto camera, auto args) {
133125
camera.aperture_size = args.fValue;
134-
camera.UpdateCamera();
135-
camera.SetDirty();
136-
});
126+
}));
137127
AddWidget(&apertureSizeSlider);
138128

139129
apertureShapeXSlider.Create(0, 2, 1, 10000, "Aperture Shape X: ");
140130
apertureShapeXSlider.SetTooltip("Controls the depth of field effect's bokeh shape");
141131
apertureShapeXSlider.SetSize(XMFLOAT2(wid, hei));
142132
apertureShapeXSlider.SetPos(XMFLOAT2(x, y += step));
143-
apertureShapeXSlider.OnSlide([&](wi::gui::EventArgs args) {
144-
Scene& scene = editor->GetCurrentScene();
145-
CameraComponent& camera = editor->GetCurrentEditorScene().camera;
133+
apertureShapeXSlider.OnSlide(updateCamera([&](auto camera, auto args) {
146134
camera.aperture_shape.x = args.fValue;
147-
camera.UpdateCamera();
148-
camera.SetDirty();
149-
});
135+
}));
150136
AddWidget(&apertureShapeXSlider);
151137

152138
apertureShapeYSlider.Create(0, 2, 1, 10000, "Aperture Shape Y: ");
153139
apertureShapeYSlider.SetTooltip("Controls the depth of field effect's bokeh shape");
154140
apertureShapeYSlider.SetSize(XMFLOAT2(wid, hei));
155141
apertureShapeYSlider.SetPos(XMFLOAT2(x, y += step));
156-
apertureShapeYSlider.OnSlide([&](wi::gui::EventArgs args) {
157-
Scene& scene = editor->GetCurrentScene();
158-
CameraComponent& camera = editor->GetCurrentEditorScene().camera;
142+
apertureShapeYSlider.OnSlide(updateCamera([&](auto camera, auto args) {
159143
camera.aperture_shape.y = args.fValue;
160-
camera.UpdateCamera();
161-
camera.SetDirty();
162-
});
144+
}));
163145
AddWidget(&apertureShapeYSlider);
164146

165147
movespeedSlider.Create(1, 100, 10, 10000, "Movement Speed: ");

0 commit comments

Comments
 (0)