-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransitionController.cpp
More file actions
307 lines (276 loc) · 11.7 KB
/
Copy pathTransitionController.cpp
File metadata and controls
307 lines (276 loc) · 11.7 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
#include "TransitionController.h"
#include <QDebug>
#include <QtMath>
#include <QTimer>
TransitionController::TransitionController(QObject *parent)
: QObject(parent)
{
qDebug() << "[TransitionController] created, all layers inactive";
}
// ==================== Debug tp 覆盖 ====================
void TransitionController::setDebugCloudTP(float v)
{
v = qBound(0.0f, v, 1.0f);
if (qFuzzyCompare(m_debugCloudTP, v)) return;
m_debugCloudTP = v;
if (v > 0.001f) {
m_cloudTP = v;
emit cloudTPChanged();
} else if (m_cloudActive) {
m_cloudTP = 1.0f;
emit cloudTPChanged();
}
qDebug() << "[TransitionController] debugCloudTP =" << v;
emit debugCloudTPChanged();
}
void TransitionController::setDebugWeatherTP(float v)
{
v = qBound(0.0f, v, 1.0f);
if (qFuzzyCompare(m_debugWeatherTP, v)) return;
m_debugWeatherTP = v;
if (v > 0.001f) {
m_weatherTP = v;
emit weatherTPChanged();
} else if (m_weatherActive) {
m_weatherTP = 1.0f;
emit weatherTPChanged();
}
qDebug() << "[TransitionController] debugWeatherTP =" << v;
emit debugWeatherTPChanged();
}
void TransitionController::setDebugFogTP(float v)
{
v = qBound(0.0f, v, 1.0f);
if (qFuzzyCompare(m_debugFogTP, v)) return;
m_debugFogTP = v;
if (v > 0.001f) {
m_fogTP = v;
emit fogTPChanged();
} else if (m_fogActive) {
m_fogTP = 1.0f;
emit fogTPChanged();
}
qDebug() << "[TransitionController] debugFogTP =" << v;
emit debugFogTPChanged();
}
void TransitionController::setFadeInEnabled(bool v)
{
if (m_fadeInEnabled == v) return;
m_fadeInEnabled = v;
qDebug() << "[TransitionController] fadeInEnabled =" << v;
emit fadeInEnabledChanged();
}
// ==================== 辅助 ====================
float TransitionController::debugTPFor(const char *name) const
{
float v = -1.0f;
if (name == QStringLiteral("cloud")) v = m_debugCloudTP;
else if (name == QStringLiteral("weather")) v = m_debugWeatherTP;
else if (name == QStringLiteral("fog")) v = m_debugFogTP;
// 0=自动模式,返回-1表示无覆盖
return (v > 0.001f) ? v : -1.0f;
}
static const char *nameForSignals(bool cloud, bool weather, bool fog)
{
if (cloud) return "cloud";
if (weather) return "weather";
if (fog) return "fog";
return "?";
}
// ==================== setSkyState ====================
void TransitionController::setSkyState(const SkyState &s)
{
++m_transitionId;
SkyState prev = m_prev;
m_prev = s;
const float kActivate = 0.015f;
const float kDeactivate = 0.005f;
// === Cloud ===
bool cloudNow = s.cloudCoverage > kActivate;
bool cloudWas = prev.cloudCoverage > kDeactivate;
qDebug() << "[TransitionController] cloud: now=" << cloudNow
<< "was=" << cloudWas << "active=" << m_cloudActive
<< "cov=" << s.cloudCoverage;
if (cloudNow && !cloudWas) {
activateLayer(m_cloudTP, m_cloudActive, "cloud", m_transitionId,
debugTPFor("cloud"), 100);
} else if (!cloudNow && cloudWas) {
deactivateLayer(m_cloudTP, m_cloudActive, "cloud", m_transitionId, 600);
} else if (cloudNow) {
if (!m_cloudActive) { m_cloudActive = true; emit cloudActiveChanged(); }
// 自动模式:确保 tp=1.0(防止 delay timer 被 slider 拖动取消)
if (debugTPFor("cloud") < 0 && m_cloudTP < 0.99f) {
m_cloudTP = 1.0f; emit cloudTPChanged();
}
float d = qAbs(s.cloudCoverage - prev.cloudCoverage);
if (d > 0.01f) qDebug() << "[TransitionController] cloudCoverage:" << prev.cloudCoverage << "->" << s.cloudCoverage;
} else {
if (m_cloudActive) deactivateLayer(m_cloudTP, m_cloudActive, "cloud", m_transitionId, 600);
}
// === Weather ===
float wi = s.rainIntensity + s.snowIntensity;
float pw = prev.rainIntensity + prev.snowIntensity;
bool weatherNow = wi > kActivate;
bool weatherWas = pw > kDeactivate;
qDebug() << "[TransitionController] weather: now=" << weatherNow
<< "was=" << weatherWas << "active=" << m_weatherActive
<< "int=" << wi;
if (weatherNow && !weatherWas) {
int wDelay = cloudNow ? 400 : 100;
activateLayer(m_weatherTP, m_weatherActive, "weather", m_transitionId,
debugTPFor("weather"), wDelay);
} else if (!weatherNow && weatherWas) {
deactivateLayer(m_weatherTP, m_weatherActive, "weather", m_transitionId, 600);
} else if (weatherNow) {
if (!m_weatherActive) { m_weatherActive = true; emit weatherActiveChanged(); }
// 自动模式:确保 tp=1.0
if (debugTPFor("weather") < 0 && m_weatherTP < 0.99f) {
m_weatherTP = 1.0f; emit weatherTPChanged();
}
float d = qAbs(wi - pw);
if (d > 0.01f) qDebug() << "[TransitionController] weatherIntensity:" << pw << "->" << wi;
} else {
if (m_weatherActive) deactivateLayer(m_weatherTP, m_weatherActive, "weather", m_transitionId, 600);
}
// === Fog ===
bool fogNow = s.fogDensity > kActivate;
bool fogWas = prev.fogDensity > kDeactivate;
qDebug() << "[TransitionController] fog: now=" << fogNow
<< "was=" << fogWas << "active=" << m_fogActive
<< "den=" << s.fogDensity;
if (fogNow && !fogWas) {
activateLayer(m_fogTP, m_fogActive, "fog", m_transitionId,
debugTPFor("fog"), 100);
} else if (!fogNow && fogWas) {
deactivateLayer(m_fogTP, m_fogActive, "fog", m_transitionId, 400);
} else if (fogNow) {
if (!m_fogActive) { m_fogActive = true; emit fogActiveChanged(); }
// 自动模式:确保 tp=1.0
if (debugTPFor("fog") < 0 && m_fogTP < 0.99f) {
m_fogTP = 1.0f; emit fogTPChanged();
}
} else {
if (m_fogActive) deactivateLayer(m_fogTP, m_fogActive, "fog", m_transitionId, 400);
}
// === Lightning ===
bool ln = s.lightningProb > 0.0f;
if (ln != m_lightningActive) {
m_lightningActive = ln;
qDebug() << "[TransitionController] lightning:" << (ln ? "ON" : "OFF");
emit lightningActiveChanged();
}
}
// ==================== 激活 ====================
void TransitionController::activateLayer(float &tp, bool &active,
const char *name, int localId,
float debugOverride, float delayMs)
{
// debug 覆盖(始终即时)
if (debugOverride >= 0.0f) {
active = true;
if (name == QStringLiteral("cloud")) emit cloudActiveChanged();
else if (name == QStringLiteral("weather")) emit weatherActiveChanged();
else if (name == QStringLiteral("fog")) emit fogActiveChanged();
tp = debugOverride;
if (name == QStringLiteral("cloud")) emit cloudTPChanged();
else if (name == QStringLiteral("weather")) emit weatherTPChanged();
else if (name == QStringLiteral("fog")) emit fogTPChanged();
qDebug() << "[TransitionController] activate(" << name
<< ") active=true, tp=debug(" << debugOverride << ")";
return;
}
int dly = m_fadeInEnabled ? static_cast<int>(delayMs) : 0;
// fadeIn=false + delay=0 → 完全即时,不用 timer
if (dly <= 0 && !m_fadeInEnabled) {
active = true;
tp = 1.0f;
if (name == QStringLiteral("cloud")) { emit cloudActiveChanged(); emit cloudTPChanged(); }
else if (name == QStringLiteral("weather")) { emit weatherActiveChanged(); emit weatherTPChanged(); }
else if (name == QStringLiteral("fog")) { emit fogActiveChanged(); emit fogTPChanged(); }
qDebug() << "[TransitionController] activate(" << name
<< ") active=true, tp=1.0 (instant)";
return;
}
// 有延迟 → QTimer
int id = localId;
QTimer::singleShot(dly, this, [this, id, dly, &tp, &active, name]() {
if (m_transitionId != id) {
qDebug() << "[TransitionController] activate(" << name
<< ") delay CANCELLED";
return;
}
active = true;
if (name == QStringLiteral("cloud")) emit cloudActiveChanged();
else if (name == QStringLiteral("weather")) emit weatherActiveChanged();
else if (name == QStringLiteral("fog")) emit fogActiveChanged();
if (m_fadeInEnabled) {
// 渐变:tp=0(组件已创建),再等一帧设 1→Behavior 动画
qDebug() << "[TransitionController] activate(" << name
<< ") active=true, schedule tp=1.0";
int id2 = m_transitionId;
QTimer::singleShot(16, this, [this, id2, &tp, name]() {
if (m_transitionId != id2) {
qDebug() << "[TransitionController] activate(" << name
<< ") tp timer CANCELLED";
return;
}
tp = 1.0f;
if (name == QStringLiteral("cloud")) emit cloudTPChanged();
else if (name == QStringLiteral("weather")) emit weatherTPChanged();
else if (name == QStringLiteral("fog")) emit fogTPChanged();
qDebug() << "[TransitionController] activate(" << name
<< ") tp timer FIRED → tp=1.0";
});
} else {
// 即时(但有延迟)
tp = 1.0f;
if (name == QStringLiteral("cloud")) emit cloudTPChanged();
else if (name == QStringLiteral("weather")) emit weatherTPChanged();
else if (name == QStringLiteral("fog")) emit fogTPChanged();
qDebug() << "[TransitionController] activate(" << name
<< ") tp=1.0 (delayed " << dly << "ms)";
}
});
}
// ==================== 去激活 ====================
void TransitionController::deactivateLayer(float &tp, bool &active,
const char *name, int localId,
float durationMs)
{
// 先设 tp=0 → Behavior 开始淡出动画
tp = 0.0f;
if (name == QStringLiteral("cloud")) emit cloudTPChanged();
else if (name == QStringLiteral("weather")) emit weatherTPChanged();
else if (name == QStringLiteral("fog")) emit fogTPChanged();
int dur = static_cast<int>(durationMs);
qDebug() << "[TransitionController] deactivate(" << name
<< ") tp=0, active=true (destroy in" << dur << "ms)";
// 延迟销毁:等 Behavior 动画播完
int id = localId;
QTimer::singleShot(dur, this, [this, id, &active, name]() {
if (m_transitionId != id) {
qDebug() << "[TransitionController] deactivate(" << name
<< ") timer CANCELLED";
return;
}
active = false;
if (name == QStringLiteral("cloud")) emit cloudActiveChanged();
else if (name == QStringLiteral("weather")) emit weatherActiveChanged();
else if (name == QStringLiteral("fog")) emit fogActiveChanged();
qDebug() << "[TransitionController] deactivate(" << name
<< ") timer FIRED → active=false";
});
}
// ==================== 状态转储 ====================
QString TransitionController::dumpState() const
{
return QStringLiteral(
"[TransitionController] cloud: a=%1 tp=%2 dtp=%3 | "
"weather: a=%4 tp=%5 dtp=%6 | "
"fog: a=%7 tp=%8 dtp=%9 | "
"lightning: a=%10 | fadeIn=%11")
.arg(m_cloudActive).arg(m_cloudTP,0,'f',2).arg(m_debugCloudTP,0,'f',2)
.arg(m_weatherActive).arg(m_weatherTP,0,'f',2).arg(m_debugWeatherTP,0,'f',2)
.arg(m_fogActive).arg(m_fogTP,0,'f',2).arg(m_debugFogTP,0,'f',2)
.arg(m_lightningActive).arg(m_fadeInEnabled);
}