Repository navigation
Expand file tree
/
Copy pathPluginExample.cpp
More file actions
612 lines (542 loc) · 15.5 KB
/
Copy pathPluginExample.cpp
File metadata and controls
612 lines (542 loc) · 15.5 KB
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
#include <DearModdingUI/Client.h>
#include <DearModdingUI/IconGlyphs.h>
#include <DearModdingUI/UI.h>
#include <array>
#include <chrono>
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace
{
// Plugin settings state.
struct LightingSettings
{
bool enableGodRays{ true };
bool interiorFog{ true };
float godRayIntensity{ 1.0f };
float shadowSoftness{ 0.75f };
int activePresetIndex{ 0 };
};
LightingSettings g_settings;
const LightingSettings g_defaults;
// Global client instance. Must outlive the game process.
dmui::Client g_client{
"demo_lighting_overhaul",
"Commonwealth Lighting",
dmui::Version{ 1, 0 },
"sun-horizon"
};
// Frame time history for telemetry visualization.
std::array<float, 60> g_frameTimeHistory{};
size_t g_frameHistoryOffset{ 0 };
// Preset options for choice controls.
const std::array g_presetOptions{
dmui::ChoiceOption<int>{ 0, "Vanilla Enhanced", "vanilla", true },
dmui::ChoiceOption<int>{ 1, "Bleak Wasteland", "bleak", true },
dmui::ChoiceOption<int>{ 2, "Vibrant Commonwealth", "vibrant", true },
dmui::ChoiceOption<int>{ 3, "Cinematic Noir", "noir", true }
};
// Client categories with icons and sort keys.
constexpr dmui::CategoryDescriptor kLightingCategory{
.id = "lighting",
.displayName = "Lighting & Atmosphere",
.sortKey = 0,
.iconName = "sun-horizon"
};
constexpr dmui::CategoryDescriptor kPresetsCategory{
.id = "presets",
.displayName = "Color Presets",
.sortKey = 1,
.iconName = "palette"
};
constexpr dmui::CategoryDescriptor kToolsCategory{
.id = "tools",
.displayName = "Diagnostics & Tools",
.sortKey = 2,
.iconName = "chart-line"
};
// Page 1: Overview & Status
void DrawOverviewPage(dmui::Client& a_client)
{
(void)a_client.DrawSectionHeader("Active Environment");
(void)dmui::DrawStyledText(
a_client,
"Lighting runtime operating normally with hardware acceleration.",
{
.tone = dmui::TextTone::kSuccess,
.wrapped = true
});
dmui::ui::Spacing();
(void)dmui::DrawLabeledValue(
a_client,
"Volumetric Engine",
"Active (Direct3D 11)",
{
.valueStyle = { .tone = dmui::TextTone::kSuccess },
.spacingScale = 2.0f
});
(void)dmui::DrawLabeledValue(
a_client,
"Selected Preset",
g_presetOptions[static_cast<size_t>(g_settings.activePresetIndex)].label.c_str(),
{
.valueStyle = { .tone = dmui::TextTone::kAccent },
.spacingScale = 2.0f
});
(void)dmui::DrawLabeledValue(
a_client,
"Interior Occlusion",
g_settings.interiorFog ? "Enabled" : "Disabled",
{
.valueStyle = {
.tone = g_settings.interiorFog ?
dmui::TextTone::kSuccess :
dmui::TextTone::kMuted
},
.spacingScale = 2.0f
});
dmui::ui::Spacing();
dmui::ui::Separator();
dmui::ui::Spacing();
if (dmui::ui::Button("Cycle Active Preset"))
{
g_settings.activePresetIndex = static_cast<int>(
(static_cast<size_t>(g_settings.activePresetIndex) + 1) %
g_presetOptions.size());
(void)a_client.PostNotification(
DMUI_STATUS_SEVERITY_SUCCESS,
"Weather preset switched successfully.",
3000);
}
}
// Page 2: Two-column settings table
void DrawLightingSettingsPage(dmui::Client& a_client)
{
(void)a_client.DrawSectionHeader("Volumetrics and Shading");
dmui::SettingsTableScope table{ a_client, "lighting_table" };
if (!table.Visible())
return;
// Setting 1: God rays checkbox
{
dmui::FieldScope row{
a_client,
"god_rays",
"God Rays (Sunshafts)",
"Enables volumetric light shafts cast by the sun and moon."
};
if (row.Visible())
{
(void)dmui::ui::Checkbox("##god_rays", &g_settings.enableGodRays);
const auto resetClicked = row.End(
true,
g_settings.enableGodRays != g_defaults.enableGodRays);
if (resetClicked.value_or(false))
g_settings.enableGodRays = g_defaults.enableGodRays;
}
}
// Setting 2: Interior fog checkbox
{
dmui::FieldScope row{
a_client,
"interior_fog",
"Interior Atmospheric Fog",
"Enables volumetric dust and fog scattering inside interiors."
};
if (row.Visible())
{
(void)dmui::ui::Checkbox("##interior_fog", &g_settings.interiorFog);
const auto resetClicked = row.End(
true,
g_settings.interiorFog != g_defaults.interiorFog);
if (resetClicked.value_or(false))
g_settings.interiorFog = g_defaults.interiorFog;
}
}
// Setting 3: God ray intensity slider
{
dmui::FieldScope row{
a_client,
"ray_intensity",
"Light Shaft Intensity",
"Scales brightness multiplier for directional light scattering."
};
if (row.Visible())
{
float minVal{ 0.1f };
float maxVal{ 3.0f };
(void)dmui::ui::SliderScalar(
"##ray_intensity",
&g_settings.godRayIntensity,
&minVal,
&maxVal,
"%.2fx");
const auto resetClicked = row.End(
true,
g_settings.godRayIntensity != g_defaults.godRayIntensity);
if (resetClicked.value_or(false))
g_settings.godRayIntensity = g_defaults.godRayIntensity;
}
}
// Setting 4: Shadow softness slider
{
dmui::FieldScope row{
a_client,
"shadow_softness",
"Ambient Shadow Softness",
"Controls penumbra blur filter radius on contact shadows."
};
if (row.Visible())
{
float minVal{ 0.0f };
float maxVal{ 1.0f };
(void)dmui::ui::SliderScalar(
"##shadow_softness",
&g_settings.shadowSoftness,
&minVal,
&maxVal,
"%.2f");
const auto resetClicked = row.End(
true,
g_settings.shadowSoftness != g_defaults.shadowSoftness);
if (resetClicked.value_or(false))
g_settings.shadowSoftness = g_defaults.shadowSoftness;
}
}
(void)table.End();
}
// Page 3: Presets dropdown
void DrawPresetsPage(dmui::Client& a_client)
{
(void)a_client.DrawSectionHeader("Preset Profiles");
dmui::ui::TextUnformatted(
"Choose a curated lighting preset for the active worldspace.");
dmui::ui::Spacing();
auto choice = dmui::DrawChoice(
"lighting_preset_choice",
g_settings.activePresetIndex,
g_presetOptions,
"Custom",
"Active Color Grade");
if (choice.changed && choice.selected)
{
g_settings.activePresetIndex = *choice.selected;
(void)a_client.PostNotification(
DMUI_STATUS_SEVERITY_INFO,
"Selected profile applied to local cache.",
2500);
}
dmui::ui::Spacing();
dmui::ui::Separator();
dmui::ui::Spacing();
switch (g_settings.activePresetIndex)
{
case 0:
(void)dmui::DrawStyledText(
a_client,
"Vanilla Enhanced: True to original art direction with improved volumetric depth.",
{ .tone = dmui::TextTone::kMuted, .wrapped = true });
break;
case 1:
(void)dmui::DrawStyledText(
a_client,
"Bleak Wasteland: Desaturated color tones with cold ambient fog.",
{ .tone = dmui::TextTone::kMuted, .wrapped = true });
break;
case 2:
(void)dmui::DrawStyledText(
a_client,
"Vibrant Commonwealth: Warmer highlights, deeper greens, and vibrant sky gradients.",
{ .tone = dmui::TextTone::kMuted, .wrapped = true });
break;
case 3:
(void)dmui::DrawStyledText(
a_client,
"Cinematic Noir: Crushed blacks with high contrast directional key lights.",
{ .tone = dmui::TextTone::kMuted, .wrapped = true });
break;
default:
break;
}
}
// Page 4: Telemetry & diagnostics
void DrawDiagnosticsPage(dmui::Client& a_client)
{
(void)a_client.DrawSectionHeader("Performance & Telemetry");
dmui::ui::TextUnformatted("Frame duration samples (last 60 frames, ms):");
dmui::ui::PlotLines(
"##frametime_plot",
g_frameTimeHistory.data(),
static_cast<uint32_t>(g_frameTimeHistory.size()),
static_cast<uint32_t>(g_frameHistoryOffset),
nullptr,
0.0f,
33.3f,
dmui::ui::Vec2{ 0.0f, 80.0f });
dmui::ui::Spacing();
if (dmui::ui::Button("Trigger Test Warning"))
{
(void)a_client.SetStatus(
DMUI_STATUS_SEVERITY_WARNING,
"Ambient occlusion resolution clamped due to VRAM threshold.");
}
dmui::ui::SameLine();
if (dmui::ui::Button("Clear Status Message"))
{
(void)a_client.SetStatus(
DMUI_STATUS_SEVERITY_INFO,
"All systems operating within nominal limits.");
}
}
}
namespace
{
// Page 5: A focused overlay console with history, Tab completion, and a caret-anchored popup.
constexpr std::array<const char*, 5> kCommands{
"player.additem", "player.setav", "player.placeatme", "tgm", "tcl"
};
struct ConsoleState
{
std::string input;
std::vector<std::string> history;
size_t historyIndex{};
std::vector<const char*> matches;
size_t selected{};
bool popupOpen{};
dmui::ui::TextEditFlags reload{};
size_t reloadCursor{};
DMUI_PageHandle page{ DMUI_INVALID_PAGE_HANDLE };
bool open{};
bool activate{};
};
ConsoleState g_console;
void OpenConsole()
{
if (g_console.open || !g_client.RequestFrame(g_console.page))
return;
if (!g_client.RequestOverlayFocus(g_console.page))
{
(void)g_client.ReleaseFrame(g_console.page);
return;
}
g_console.open = g_console.activate = true;
}
void CloseConsole()
{
if (!g_console.open)
return;
// Releasing the last frame demand also ends focus.
(void)g_client.ReleaseFrame(g_console.page);
g_console.open = false;
g_console.popupOpen = false;
}
void ReplaceInput(std::string_view a_text)
{
// Applied to the active field next frame, before that frame's typing.
g_console.input = a_text;
g_console.reload = dmui::ui::TextEditFlags::kReload;
g_console.reloadCursor = g_console.input.size();
}
void RefreshMatches()
{
g_console.matches.clear();
for (const auto* command : kCommands)
{
if (!g_console.input.empty() &&
std::string_view{ command }.starts_with(g_console.input))
g_console.matches.push_back(command);
}
g_console.selected = 0;
g_console.popupOpen = !g_console.matches.empty();
}
void DrawConsolePage(dmui::Client& a_client)
{
using Flags = dmui::ui::TextEditFlags;
using Events = dmui::ui::TextEditEvents;
// Escape, the shell, a load, or the host can end focus; any loss closes the console.
const auto focus = a_client.QueryOverlayFocus(g_console.page);
if (!focus || !focus->focused)
{
CloseConsole();
return;
}
const auto activate = std::exchange(g_console.activate, false) ? Flags::kRequestFocus : Flags::kNone;
for (const auto& line : g_console.history)
dmui::ui::TextUnformatted(line);
auto flags = Flags::kHistoryKeys | Flags::kCompletionKey |
Flags::kKeepFocusOnSubmit | activate | g_console.reload;
// Escape closes the popup here; otherwise the host leaves the field, then ends focus.
if (g_console.popupOpen)
flags |= Flags::kCaptureCancel;
dmui::ui::TextEditState state{};
const auto edited = dmui::ui::InputTextEditor(
"##command", "Type a command", g_console.input, flags,
g_console.reloadCursor, state);
g_console.reload = Flags::kNone;
const auto has = [&](Events a_event) {
return dmui::ui::HasTextEditEvent(state, a_event);
};
if (edited)
RefreshMatches();
if (has(Events::kCompletion))
{
if (g_console.popupOpen)
{
ReplaceInput(g_console.matches[g_console.selected]);
g_console.popupOpen = false;
}
else
RefreshMatches();
}
const auto step = has(Events::kHistoryPrevious) ? -1 : has(Events::kHistoryNext) ? 1 : 0;
if (step != 0 && g_console.popupOpen)
{
const auto count = g_console.matches.size();
g_console.selected = (g_console.selected + count + step) % count;
}
else if (step != 0 && !g_console.history.empty())
{
const auto last = g_console.history.size();
g_console.historyIndex = step < 0 ?
(g_console.historyIndex > 0 ? g_console.historyIndex - 1 : 0) :
(std::min)(g_console.historyIndex + 1, last);
ReplaceInput(g_console.historyIndex < last ? g_console.history[g_console.historyIndex] : "");
}
if (has(Events::kCanceled))
g_console.popupOpen = false;
if (has(Events::kSubmitted) && !g_console.input.empty())
{
g_console.history.push_back(g_console.input);
g_console.historyIndex = g_console.history.size();
ReplaceInput("");
g_console.popupOpen = false;
}
// Tooltips never take focus, so typing continues in the field.
if (g_console.popupOpen && state.active &&
dmui::ui::BeginTooltipAt({ state.caretPosition.x, state.caretPosition.y + state.lineHeight }))
{
for (size_t index = 0; index < g_console.matches.size(); ++index)
(void)dmui::ui::Selectable(g_console.matches[index], index == g_console.selected);
dmui::ui::EndTooltip();
}
}
}
// Plugin entry point called during F4SE kPostPostLoad.
void InitializeDearModdingUI()
{
// Attempt connection to host menu.
if (!g_client.Connect())
return;
// Register categories for navigation organization.
(void)g_client.AddCategory(kLightingCategory);
(void)g_client.AddCategory(kPresetsCategory);
(void)g_client.AddCategory(kToolsCategory);
// Register individual pages under their respective categories.
(void)g_client.AddPage(
{
.id = "overview",
.displayName = "Overview",
.categoryId = kLightingCategory.id,
.summary = "Status and active lighting summary.",
.sortKey = 0,
.iconName = "info"
},
[] {
DrawOverviewPage(g_client);
});
(void)g_client.AddPage(
{
.id = "settings",
.displayName = "Volumetrics & Sun",
.categoryId = kLightingCategory.id,
.summary = "Adjust light scattering and shadow parameters.",
.sortKey = 1,
.iconName = "sliders"
},
[] {
DrawLightingSettingsPage(g_client);
});
(void)g_client.AddPage(
{
.id = "presets",
.displayName = "Color Grading",
.categoryId = kPresetsCategory.id,
.summary = "Select atmospheric and color grading profiles.",
.sortKey = 0,
.iconName = "palette"
},
[] {
DrawPresetsPage(g_client);
});
(void)g_client.AddPage(
{
.id = "diagnostics",
.displayName = "Telemetry",
.categoryId = kToolsCategory.id,
.summary = "Performance charts and health observations.",
.sortKey = 0,
.iconName = "activity"
},
[] {
DrawDiagnosticsPage(g_client);
});
// The console is a standalone overlay that takes input only while focused.
if (const auto console = g_client.AddPage(
{
.id = "console",
.displayName = "Console",
.summary = "Command input with history and completion.",
.kind = DMUI_PAGE_KIND_OVERLAY,
.iconName = "terminal-window"
},
[] {
DrawConsolePage(g_client);
}))
{
g_console.page = *console;
(void)g_client.ConfigureOverlay(g_console.page, {
.anchor = DMUI_OVERLAY_ANCHOR_FREE,
.offset = { 32.0f, 32.0f },
.size = { 720.0f, 320.0f },
.minimumSize = { 360.0f, 160.0f },
.opacity = 0.94f,
.contentScale = 1.0f,
.backgroundVisible = 1,
.borderVisible = 1,
.allowArrangement = 1
});
(void)g_client.AddHotkeyAction(
"toggle_console",
"Toggle console",
"Grave",
[](bool a_pressed) {
if (a_pressed)
g_console.open ? CloseConsole() : OpenConsole();
});
}
// Register a quick action in the host command palette or action list.
(void)g_client.AddAction(
"reload_presets",
"Reload Lighting Presets",
"arrow-clockwise",
"Reloads INI configurations and color LUTs from disk.",
[] {
(void)g_client.PostNotification(
DMUI_STATUS_SEVERITY_SUCCESS,
"Lighting presets reloaded from disk.",
3000);
});
// Register a frame observer to sample frametimes while playing.
(void)g_client.AddFrameObserver(
[] {
static auto s_lastTime = std::chrono::steady_clock::now();
const auto now = std::chrono::steady_clock::now();
const float deltaMs =
std::chrono::duration<float, std::milli>(now - s_lastTime).count();
s_lastTime = now;
g_frameTimeHistory[g_frameHistoryOffset] = deltaMs;
g_frameHistoryOffset =
(g_frameHistoryOffset + 1) % g_frameTimeHistory.size();
});
}