drgame-cc 1.0.46 → 1.0.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/drscript/focus/FocusManager.ts +49 -52
- package/package.json +1 -1
|
@@ -31,8 +31,8 @@ export default class FocusManager {
|
|
|
31
31
|
private static itemMap: { [uuid: string]: FocusRuntimeItem } = {};
|
|
32
32
|
private static scopeMap: { [uuid: string]: string } = {};
|
|
33
33
|
private static currentNodeUuid: string = "";
|
|
34
|
-
private static scopes: ScopeInfo[] = [
|
|
35
|
-
private static activeScopeId: string = "
|
|
34
|
+
private static scopes: ScopeInfo[] = [];
|
|
35
|
+
private static activeScopeId: string = "";
|
|
36
36
|
private static listeners: FocusListener[] = [];
|
|
37
37
|
|
|
38
38
|
private static readonly DEFAULT_FOCUS_SCALE = 1.08;
|
|
@@ -46,7 +46,7 @@ export default class FocusManager {
|
|
|
46
46
|
let uuid = node.uuid;
|
|
47
47
|
this.getNodeBaseScale(node);
|
|
48
48
|
|
|
49
|
-
let scopeId = options.scopeId || (component && component.scopeId) || this.activeScopeId
|
|
49
|
+
let scopeId = options.scopeId || (component && component.scopeId) || this.activeScopeId;
|
|
50
50
|
this.scopeMap[uuid] = scopeId;
|
|
51
51
|
|
|
52
52
|
let item: FocusRuntimeItem = {
|
|
@@ -127,7 +127,7 @@ export default class FocusManager {
|
|
|
127
127
|
}
|
|
128
128
|
this.scopes.push({ scopeId, lastNodeUuid: "", node: options?.node, nodeType: options?.nodeType, onBack: options?.onBack });
|
|
129
129
|
this.activeScopeId = scopeId;
|
|
130
|
-
this.
|
|
130
|
+
this.clearFocus();
|
|
131
131
|
if (!options?.skipEnsureFocus) this.ensureFocus();
|
|
132
132
|
}
|
|
133
133
|
|
|
@@ -154,17 +154,17 @@ export default class FocusManager {
|
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
let active = this.scopes[this.scopes.length - 1];
|
|
157
|
-
this.activeScopeId = active ? active.scopeId : "
|
|
157
|
+
this.activeScopeId = active ? active.scopeId : "";
|
|
158
158
|
let restored = active && active.lastNodeUuid ? this.focus(active.lastNodeUuid) : false;
|
|
159
159
|
if (!restored) {
|
|
160
|
-
this.
|
|
160
|
+
this.clearFocus();
|
|
161
161
|
this.ensureFocus();
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
/**
|
|
165
|
+
/** 获取最顶层绑定了节点的作用域 */
|
|
166
166
|
public static getTopNodeScope(): ScopeInfo | null {
|
|
167
|
-
for (let i = this.scopes.length - 1; i >=
|
|
167
|
+
for (let i = this.scopes.length - 1; i >= 0; i--) {
|
|
168
168
|
if (this.scopes[i].node) return this.scopes[i];
|
|
169
169
|
}
|
|
170
170
|
return null;
|
|
@@ -182,27 +182,38 @@ export default class FocusManager {
|
|
|
182
182
|
this.itemMap = {};
|
|
183
183
|
this.scopeMap = {};
|
|
184
184
|
this.currentNodeUuid = "";
|
|
185
|
-
this.scopes = [
|
|
186
|
-
this.activeScopeId = "
|
|
185
|
+
this.scopes = [];
|
|
186
|
+
this.activeScopeId = "";
|
|
187
187
|
this.emitFocusChanged(null, old);
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
-
public static focus(target: string | cc.Node): boolean {
|
|
190
|
+
public static focus(target: string | cc.Node, silent: boolean = false): boolean {
|
|
191
191
|
this.cleanup();
|
|
192
192
|
if (!target) return false;
|
|
193
|
-
let
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
193
|
+
let uuid = typeof target == "string" ? target : target.uuid;
|
|
194
|
+
let item = this.itemMap[uuid];
|
|
195
|
+
if (!item || !this.isItemActive(item) || !this.isInActiveScope(item)) return false;
|
|
196
|
+
|
|
197
|
+
let previous: FocusRuntimeItem | null = this.getCurrentFocusItem();
|
|
198
|
+
if (uuid == this.currentNodeUuid && uuid) {
|
|
199
|
+
let current = this.getCurrentFocusItem();
|
|
200
|
+
if (current) {
|
|
201
|
+
this.setFocusVisual(current, true);
|
|
202
|
+
// 总是触发位置更新,即使静默模式也要更新视觉位置
|
|
203
|
+
this.emitFocusChanged(current, previous || current);
|
|
202
204
|
}
|
|
205
|
+
return true;
|
|
203
206
|
}
|
|
204
|
-
if (
|
|
205
|
-
this.
|
|
207
|
+
if (previous) this.setFocusVisual(previous, false);
|
|
208
|
+
this.currentNodeUuid = uuid || "";
|
|
209
|
+
let current: FocusRuntimeItem | null = this.getCurrentFocusItem();
|
|
210
|
+
if (current) {
|
|
211
|
+
let scope = this.getActiveScope();
|
|
212
|
+
if (scope) scope.lastNodeUuid = current.node.uuid;
|
|
213
|
+
this.setFocusVisual(current, true);
|
|
214
|
+
}
|
|
215
|
+
// 总是触发位置更新,确保视觉层收到通知
|
|
216
|
+
this.emitFocusChanged(current, previous);
|
|
206
217
|
return true;
|
|
207
218
|
}
|
|
208
219
|
|
|
@@ -237,7 +248,7 @@ export default class FocusManager {
|
|
|
237
248
|
}
|
|
238
249
|
|
|
239
250
|
if (best) {
|
|
240
|
-
this.
|
|
251
|
+
this.focus(best.node.uuid);
|
|
241
252
|
return true;
|
|
242
253
|
}
|
|
243
254
|
return this.moveByOrder(direction == "left" || direction == "down" ? -1 : 1);
|
|
@@ -275,26 +286,11 @@ export default class FocusManager {
|
|
|
275
286
|
if (index >= 0) this.listeners.splice(index, 1);
|
|
276
287
|
}
|
|
277
288
|
|
|
278
|
-
|
|
279
|
-
if (uuid == this.currentNodeUuid && uuid) {
|
|
280
|
-
let current = this.getCurrentFocusItem();
|
|
281
|
-
if (current && !silent) {
|
|
282
|
-
this.setFocusVisual(current, true);
|
|
283
|
-
this.emitFocusChanged(current, current);
|
|
284
|
-
}
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
289
|
+
public static clearFocus() {
|
|
287
290
|
let previous: FocusRuntimeItem | null = this.getCurrentFocusItem();
|
|
288
291
|
if (previous) this.setFocusVisual(previous, false);
|
|
289
|
-
this.currentNodeUuid =
|
|
290
|
-
|
|
291
|
-
if (current) {
|
|
292
|
-
let scope = this.getActiveScope();
|
|
293
|
-
if (scope) scope.lastNodeUuid = current.node.uuid;
|
|
294
|
-
this.setFocusVisual(current, true);
|
|
295
|
-
}
|
|
296
|
-
if (!silent) this.emitFocusChanged(current, previous);
|
|
297
|
-
if (silent && previous) this.emitFocusChanged(null, previous);
|
|
292
|
+
this.currentNodeUuid = "";
|
|
293
|
+
if (previous) this.emitFocusChanged(null, previous);
|
|
298
294
|
}
|
|
299
295
|
|
|
300
296
|
private static setFocusVisual(item: FocusRuntimeItem, focused: boolean, immediate: boolean = false) {
|
|
@@ -326,7 +322,8 @@ export default class FocusManager {
|
|
|
326
322
|
if (spriteFrame) sprite.spriteFrame = spriteFrame;
|
|
327
323
|
}
|
|
328
324
|
|
|
329
|
-
private static
|
|
325
|
+
private static _ensureFocusRetry: number = 0;
|
|
326
|
+
private static readonly _ENSURE_FOCUS_MAX_RETRY = 3;
|
|
330
327
|
|
|
331
328
|
private static ensureFocus() {
|
|
332
329
|
this.cleanup();
|
|
@@ -336,23 +333,23 @@ export default class FocusManager {
|
|
|
336
333
|
for (let i = 0; i < this.items.length; i++) {
|
|
337
334
|
let item = this.items[i];
|
|
338
335
|
if (this.isItemActive(item) && this.isInActiveScope(item)) {
|
|
339
|
-
this.
|
|
336
|
+
this.focus(item.node.uuid);
|
|
337
|
+
this._ensureFocusRetry = 0;
|
|
340
338
|
return;
|
|
341
339
|
}
|
|
342
340
|
}
|
|
343
341
|
// 有注册的按钮但暂无活跃节点,可能是节点尚未完全初始化,延迟重试
|
|
344
|
-
if (this.items.length > 0 &&
|
|
345
|
-
this.
|
|
342
|
+
if (this.items.length > 0 && this._ensureFocusRetry < this._ENSURE_FOCUS_MAX_RETRY) {
|
|
343
|
+
this._ensureFocusRetry++;
|
|
346
344
|
setTimeout(() => {
|
|
347
|
-
this._ensureFocusPending = false;
|
|
348
345
|
this.ensureFocus();
|
|
349
346
|
}, 50);
|
|
347
|
+
return;
|
|
350
348
|
}
|
|
351
|
-
//
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
}
|
|
349
|
+
// 超过重试次数/无注册项,清除焦点状态
|
|
350
|
+
this._ensureFocusRetry = 0;
|
|
351
|
+
this.currentNodeUuid = "";
|
|
352
|
+
this.emitFocusChanged(null, null);
|
|
356
353
|
}
|
|
357
354
|
|
|
358
355
|
private static moveByOrder(step: number): boolean {
|
|
@@ -369,7 +366,7 @@ export default class FocusManager {
|
|
|
369
366
|
}
|
|
370
367
|
}
|
|
371
368
|
let next = (index + step + activeItems.length) % activeItems.length;
|
|
372
|
-
this.
|
|
369
|
+
this.focus(activeItems[next].node.uuid);
|
|
373
370
|
return true;
|
|
374
371
|
}
|
|
375
372
|
|