drgame-cc 1.0.49 → 1.0.52

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.
@@ -1,8 +1,7 @@
1
1
  import FocusManager from "./focus/FocusManager";
2
- import FocusVisualLayer from "./focus/FocusVisualLayer";
3
2
  import { AdSdk } from "./ad-sdk/ad-sdk";
4
3
  import { WebBridge } from "./WebBridge";
5
- import { BtnExtra } from "./component/BtnExtra";
4
+ import BtnExtra from "./component/BtnExtra";
6
5
  import AlertTip from "./component/AlertTip";
7
6
  import { Loader } from "./Loader";
8
7
  import { KeyAdapter } from "./KeyAdapter";
@@ -15,10 +14,7 @@ export class UIAdapter {
15
14
  public static focus = FocusManager;
16
15
 
17
16
  // ---- 内部配置 ----
18
- private static _focusZIndex: number = 800;
19
- private static _focusEnabled: boolean = true;
20
-
21
- private static visualLayer: FocusVisualLayer | null = null;
17
+ private static _initialized: boolean = false;
22
18
 
23
19
  // ---- 桥接 / 状态 ----
24
20
  public static webBridge: WebBridge;
@@ -48,78 +44,82 @@ export class UIAdapter {
48
44
  reset: () => { UIAdapter.backHandlers = []; UIAdapter.backFallback = null; },
49
45
  };
50
46
 
51
- private static removeBackHandler(handler: () => boolean | void) {
52
- for (let i = UIAdapter.backHandlers.length - 1; i >= 0; i--) {
53
- if (UIAdapter.backHandlers[i] === handler) UIAdapter.backHandlers.splice(i, 1);
54
- }
55
- }
56
-
57
- private static handleBack(): boolean {
58
- for (let i = UIAdapter.backHandlers.length - 1; i >= 0; i--) {
59
- const h = UIAdapter.backHandlers[i];
60
- if (!h) continue;
61
- if (h() !== false) return true;
62
- }
63
- if (UIAdapter.backFallback) return UIAdapter.backFallback() !== false;
64
- return false;
65
- }
66
-
67
47
  // ========================================================================
68
48
  // 初始化
69
49
  // ========================================================================
70
- public static init(options?: {
71
- focusZIndex?: number;
72
- focusEnabled?: boolean;
73
- enableInput?: boolean;
74
- enableLifecycle?: boolean;
75
- }) {
76
- if (UIAdapter.visualLayer) return;
77
-
78
- if (options) {
79
- if (options.focusZIndex !== undefined) UIAdapter._focusZIndex = options.focusZIndex;
80
- if (options.focusEnabled !== undefined) UIAdapter._focusEnabled = options.focusEnabled;
81
- }
50
+ public static init() {
51
+ if (this._initialized) return;
52
+ this._initialized = true;
82
53
 
83
- // 同步给 FocusVisualLayer
84
- FocusVisualLayer.setFocusZIndex(UIAdapter._focusZIndex);
54
+ this.webBridge = new WebBridge();
55
+ FocusManager.setFocusZIndex(800);
56
+ FocusManager.initVisualLayer();
85
57
 
86
- // 焦点视觉层
87
- if (UIAdapter._focusEnabled) {
88
- const scene = cc.director.getScene();
89
- UIAdapter.visualLayer = FocusVisualLayer.create(scene);
90
- }
58
+ KeyAdapter.onBack = () => this.handleBack();
59
+ KeyAdapter.init();
91
60
 
92
- // 输入(委托给 KeyAdapter)
93
- if (options?.enableInput !== false) {
94
- KeyAdapter.onBack = () => UIAdapter.handleBack();
95
- KeyAdapter.init();
96
- }
61
+ cc.game.on(cc.game.EVENT_HIDE, this.onGameHide, this);
62
+ cc.game.on(cc.game.EVENT_SHOW, this.onGameShow, this);
63
+
64
+ this.hookLoadScene();
65
+ this.back.setFallback(() => this.backHandler());
66
+ this.initToast();
67
+
68
+ // 自动注册当前场景下的所有按钮
69
+ this.autoRegisterScene();
70
+ }
97
71
 
98
- // 生命周期
99
- if (options?.enableLifecycle !== false) {
100
- cc.game.on(cc.game.EVENT_HIDE, UIAdapter.onGameHide, UIAdapter);
101
- cc.game.on(cc.game.EVENT_SHOW, UIAdapter.onGameShow, UIAdapter);
72
+ /** 自动注册当前场景的按钮 */
73
+ private static autoRegisterScene() {
74
+ let scene = cc.director.getScene();
75
+ if (scene && scene.uuid) {
76
+ // 先清理场景已注册的失效 items
77
+ FocusManager.unregisterByRoot(scene);
78
+ // 注册所有 Button
79
+ FocusManager.registerButtons(scene);
80
+ // 查找 BtnExtra
81
+ this.findBtnExtra(scene);
102
82
  }
103
83
  }
104
84
 
85
+ private static async initToast() {
86
+ if (window['cc'] == undefined) return;
87
+ this.toastPool = new cc.NodePool();
88
+ this.prefab = await this.loadPrefab('drres/prefab/Toast');
89
+ }
90
+
105
91
  public static destroy() {
106
- cc.game.off(cc.game.EVENT_HIDE, UIAdapter.onGameHide, UIAdapter);
107
- cc.game.off(cc.game.EVENT_SHOW, UIAdapter.onGameShow, UIAdapter);
92
+ if (!this._initialized) return;
93
+ this._initialized = false;
94
+
95
+ cc.game.off(cc.game.EVENT_HIDE, this.onGameHide, this);
96
+ cc.game.off(cc.game.EVENT_SHOW, this.onGameShow, this);
108
97
  FocusManager.reset();
109
98
  KeyAdapter.destroy();
110
- UIAdapter.backHandlers = [];
111
- UIAdapter.backFallback = null;
112
- if (UIAdapter.visualLayer && UIAdapter.visualLayer.node) {
113
- cc.game?.removePersistRootNode?.(UIAdapter.visualLayer.node);
114
- UIAdapter.visualLayer.node.destroy();
115
- }
116
- UIAdapter.visualLayer = null;
99
+ this.backHandlers = [];
100
+ this.backFallback = null;
117
101
  }
118
102
 
119
103
  // ========================================================================
120
104
  // 内部实现(供 PlatformAdapter 委托调用)
121
105
  // ========================================================================
122
106
 
107
+ private static removeBackHandler(handler: () => boolean | void) {
108
+ for (let i = this.backHandlers.length - 1; i >= 0; i--) {
109
+ if (this.backHandlers[i] === handler) this.backHandlers.splice(i, 1);
110
+ }
111
+ }
112
+
113
+ private static handleBack(): boolean {
114
+ for (let i = this.backHandlers.length - 1; i >= 0; i--) {
115
+ const h = this.backHandlers[i];
116
+ if (!h) continue;
117
+ if (h() !== false) return true;
118
+ }
119
+ if (this.backFallback) return this.backFallback() !== false;
120
+ return false;
121
+ }
122
+
123
123
  static loadPrefab(path: string): Promise<cc.Prefab> {
124
124
  return new Promise((resolve, reject) => {
125
125
  cc.loader.loadRes(path, cc.Prefab, (err, prefab) => {
@@ -130,11 +130,11 @@ export class UIAdapter {
130
130
  }
131
131
 
132
132
  static showTip(text: string, dur = 1500) {
133
- if (!UIAdapter.toastPool || !UIAdapter.prefab) return;
134
- let node = UIAdapter.toastPool.get();
133
+ if (!this.toastPool || !this.prefab) return;
134
+ let node = this.toastPool.get();
135
135
  let toastComp: AlertTip | null = null;
136
136
  if (node == null) {
137
- node = cc.instantiate(UIAdapter.prefab);
137
+ node = cc.instantiate(this.prefab);
138
138
  toastComp = node.getComponent(AlertTip);
139
139
  } else {
140
140
  toastComp = node.getComponent(AlertTip);
@@ -143,13 +143,13 @@ export class UIAdapter {
143
143
  cc.director.getScene().addChild(node, cc.macro.MAX_ZINDEX);
144
144
  }
145
145
  node.setPosition(cc.v2(cc.winSize.width / 2, cc.winSize.height / 2));
146
-
146
+ node.active = true;
147
147
  if (toastComp) toastComp.show(text);
148
148
  setTimeout(() => {
149
149
  if (toastComp) {
150
150
  toastComp.hide(() => {
151
151
  if (node && node.isValid) {
152
- UIAdapter.toastPool.put(node);
152
+ this.toastPool.put(node);
153
153
  }
154
154
  });
155
155
  }
@@ -159,79 +159,73 @@ export class UIAdapter {
159
159
 
160
160
  /** 显示预制体场景(压入预制体场景栈,返回键可回溯) */
161
161
  static showScene(scene: cc.Node, onBack?: () => boolean | void) {
162
- let currentTop = UIAdapter.focus.getTopNodeScope();
162
+ let currentTop = this.focus.getTopNodeScope();
163
163
  if (currentTop && currentTop.nodeType === "scene" && currentTop.node && currentTop.node !== scene && currentTop.node.isValid) {
164
164
  currentTop.node.active = false;
165
165
  }
166
166
  scene.active = true;
167
- // 先切换 scope,确保 activeScopeId 正确
168
- UIAdapter.focus.pushScope(scene.uuid, {
167
+ this.focus.pushScope(scene.uuid, {
169
168
  node: scene,
170
169
  nodeType: "scene",
171
170
  onBack: onBack,
172
171
  skipEnsureFocus: true
173
172
  });
174
- // 再注册按钮,registerButtons 内部会调用 ensureFocus
175
- UIAdapter.focus.registerButtons(scene);
176
- UIAdapter.findBtnExtra(scene);
173
+ this.focus.registerButtons(scene);
174
+ this.findBtnExtra(scene);
177
175
  }
178
176
 
179
177
  /** 关闭预制体场景(从栈中移除,不销毁) */
180
178
  static hideScene(scene: cc.Node) {
181
- UIAdapter.focus.removeScope(scene);
179
+ this.focus.removeScope(scene);
182
180
  scene.active = false;
183
181
  }
184
182
 
185
183
  /** 打开界面注册导航按钮 */
186
184
  static showWindow(window: cc.Node, onBack?: () => boolean | void) {
187
- // 先切换 scope,确保 activeScopeId 正确
188
- UIAdapter.focus.pushScope(window.uuid, {
185
+ this.focus.pushScope(window.uuid, {
189
186
  node: window,
190
187
  nodeType: "window",
191
188
  onBack: onBack,
192
189
  skipEnsureFocus: true
193
190
  });
194
- // 再注册按钮,registerButtons 内部会调用 ensureFocus
195
- UIAdapter.focus.registerButtons(window);
196
- UIAdapter.findBtnExtra(window);
191
+ this.focus.registerButtons(window);
192
+ this.findBtnExtra(window);
197
193
  }
198
194
 
199
195
  /** 关闭界面取消注册按钮事件 */
200
196
  static hideWindow(window: cc.Node) {
201
- UIAdapter.focus.removeScope(window);
197
+ this.focus.removeScope(window);
202
198
  }
203
199
 
204
200
  /** 刷新界面按钮事件 */
205
201
  static refreshFocus(window: cc.Node) {
206
- UIAdapter.focus.registerButtons(window);
207
- UIAdapter.findBtnExtra(window);
202
+ this.focus.registerButtons(window);
203
+ this.findBtnExtra(window);
208
204
  }
209
205
 
210
206
  /** 返回键处理逻辑 */
211
207
  static backHandler(): boolean {
212
- let top = UIAdapter.focus.getTopNodeScope();
208
+ let top = this.focus.getTopNodeScope();
213
209
  if (top && top.node) {
214
210
  let node = top.node;
215
211
  if (!node.isValid) {
216
- UIAdapter.focus.popScope(top.scopeId);
212
+ this.focus.popScope(top.scopeId);
217
213
  return true;
218
214
  }
219
215
 
220
- // 1. 优先用自定义 onBack
221
216
  if (top.onBack) {
222
217
  let handled = top.onBack() !== false;
223
218
  if (handled) {
224
- UIAdapter.focus.removeScope(node);
219
+ this.focus.removeScope(node);
225
220
  }
226
221
  return true;
227
222
  }
228
223
 
229
224
  if (top.nodeType === "window") {
230
- UIAdapter.focus.removeScope(node);
225
+ this.focus.removeScope(node);
231
226
  node.destroy();
232
227
  } else {
233
- // 检查是否是最后一个场景
234
- let scopes = UIAdapter.focus.getScopes();
228
+ let scopes = this.focus.getScopes();
235
229
  let hasPrev = false;
236
230
  for (let i = scopes.length - 1; i >= 1; i--) {
237
231
  if (scopes[i].node && scopes[i].scopeId !== top.scopeId) {
@@ -240,14 +234,12 @@ export class UIAdapter {
240
234
  }
241
235
  }
242
236
  if (!hasPrev) {
243
- // 最后一个场景,直接弹出退出确认
244
- UIAdapter.showExitWin();
237
+ this.showExitWin();
245
238
  return true;
246
239
  }
247
- // 不是最后一个场景,正常关闭
248
- UIAdapter.focus.removeScope(node);
240
+ this.focus.removeScope(node);
249
241
  node.active = false;
250
- let prev = UIAdapter.focus.getTopNodeScope();
242
+ let prev = this.focus.getTopNodeScope();
251
243
  if (prev && prev.node && prev.node.isValid) {
252
244
  prev.node.active = true;
253
245
  }
@@ -255,13 +247,12 @@ export class UIAdapter {
255
247
  return true;
256
248
  }
257
249
 
258
- // 没有界面节点,走原生场景栈
259
- if (UIAdapter.sceneStack.length > 0) {
260
- let prevScene = UIAdapter.sceneStack.pop();
250
+ if (this.sceneStack.length > 0) {
251
+ let prevScene = this.sceneStack.pop();
261
252
  cc.director.loadScene(prevScene);
262
253
  return true;
263
254
  }
264
- UIAdapter.showExitWin();
255
+ this.showExitWin();
265
256
  return true;
266
257
  }
267
258
 
@@ -271,8 +262,7 @@ export class UIAdapter {
271
262
  exitWin.parent = cc.director.getScene();
272
263
  exitWin.setPosition(cc.v2(cc.winSize.width / 2, cc.winSize.height / 2));
273
264
 
274
- // 直接注册焦点,不依赖 ExitWin.onEnable 的间接调用
275
- UIAdapter.focus.pushScope(exitWin.uuid, {
265
+ this.focus.pushScope(exitWin.uuid, {
276
266
  node: exitWin,
277
267
  nodeType: "window",
278
268
  onBack: () => {
@@ -280,19 +270,23 @@ export class UIAdapter {
280
270
  },
281
271
  skipEnsureFocus: true
282
272
  });
283
- UIAdapter.focus.registerButtons(exitWin);
284
- UIAdapter.findBtnExtra(exitWin);
273
+ this.focus.registerButtons(exitWin);
274
+ this.findBtnExtra(exitWin);
285
275
  }
286
276
 
287
- /** 拦截 loadScene 自动记录场景栈 */
288
- static hookLoadScene() {
277
+ /** 拦截 loadScene 自动记录场景栈,并在新场景加载后自动注册按钮 */
278
+ private static hookLoadScene() {
289
279
  const origLoadScene = cc.director.loadScene.bind(cc.director);
290
280
  cc.director.loadScene = (sceneName: string, onLaunched?: () => void) => {
291
281
  let currentScene = cc.director.getScene();
292
282
  if (currentScene && currentScene.name && currentScene.name !== sceneName) {
293
- UIAdapter.sceneStack.push(currentScene.name);
283
+ this.sceneStack.push(currentScene.name);
294
284
  }
295
- return origLoadScene(sceneName, onLaunched);
285
+ return origLoadScene(sceneName, () => {
286
+ // 新场景加载后自动注册按钮
287
+ this.autoRegisterScene();
288
+ if (onLaunched) onLaunched();
289
+ });
296
290
  };
297
291
  }
298
292
 
@@ -303,7 +297,7 @@ export class UIAdapter {
303
297
  let btnExtra = child.getComponent(BtnExtra);
304
298
  if (btnExtra) {
305
299
  let sprite = child.getComponent(cc.Sprite);
306
- UIAdapter.focus.register(child, {
300
+ this.focus.register(child, {
307
301
  onFocus: () => {
308
302
  if (sprite) sprite.spriteFrame = btnExtra.hoverSprite;
309
303
  },
@@ -312,7 +306,7 @@ export class UIAdapter {
312
306
  }
313
307
  });
314
308
  }
315
- UIAdapter.findBtnExtra(child);
309
+ this.findBtnExtra(child);
316
310
  }
317
311
  }
318
312
 
@@ -98,6 +98,11 @@ class AdSdkImpl {
98
98
  });
99
99
  }
100
100
 
101
+ // 桥接适配器的退出事件 → 调用 reporter 上报退出
102
+ adapter.on(AdEvent.Exit, function () {
103
+ Reporter.reportAppExit();
104
+ });
105
+
101
106
  // 桥接适配器的按键事件
102
107
  if (adapter.onKeyEvent) {
103
108
  self._adapterKeyHandler = function (keyEvent) {
@@ -112,8 +117,16 @@ class AdSdkImpl {
112
117
  adapter.onKeyEvent(self._adapterKeyHandler);
113
118
  }
114
119
 
120
+ // 自动初始化数据上报模块(传入 reporter 配置,无配置时使用空对象默认初始化)
121
+ Reporter.init(config.reporter || {});
122
+
115
123
  this._initialized = true;
116
124
  console.log('[AdSdk] Initialized with platform: ' + config.platform);
125
+
126
+ // 上报「启动」事件
127
+ Reporter.reportAppStart();
128
+
129
+ console.log('[AdSdk] Lifecycle reporting bound.');
117
130
  }
118
131
 
119
132
  /**
@@ -164,18 +177,20 @@ class AdSdkImpl {
164
177
  }
165
178
  }
166
179
 
167
- /** 通知游戏暂停 */
180
+ /** 通知游戏暂停(自动上报切后台) */
168
181
  onGamePause() {
169
182
  this._ensureInit();
183
+ Reporter.reportSwitchBackground();
170
184
  const adapter = this._getAdapter();
171
185
  if (adapter.onGamePause) {
172
186
  adapter.onGamePause();
173
187
  }
174
188
  }
175
189
 
176
- /** 通知游戏恢复 */
190
+ /** 通知游戏恢复(自动上报切回) */
177
191
  onGameResume() {
178
192
  this._ensureInit();
193
+ Reporter.reportSwitchBack();
179
194
  const adapter = this._getAdapter();
180
195
  if (adapter.onGameResume) {
181
196
  adapter.onGameResume();
@@ -183,11 +198,12 @@ class AdSdkImpl {
183
198
  }
184
199
 
185
200
  /**
186
- * 退出游戏
201
+ * 退出游戏(自动上报退出事件后执行实际退出)
187
202
  * @returns {Promise<void>}
188
203
  */
189
204
  async exitGame() {
190
205
  this._ensureInit();
206
+ // 通知适配器执行退出,适配器的 onExitConfirmed 回调会触发上报
191
207
  const adapter = this._getAdapter();
192
208
  if (adapter.exitGame) {
193
209
  return adapter.exitGame();
@@ -319,4 +335,4 @@ module.exports = {
319
335
  // 类
320
336
  XiaomiAdapter,
321
337
  EventEmitter,
322
- };
338
+ };
@@ -420,6 +420,9 @@ class XiaomiAdapter {
420
420
 
421
421
  // ── 退出确认 ──
422
422
  case 'onExitConfirmed': {
423
+ // 触发退出回调事件,供 SDK 层调用 reporter 上报
424
+ this._emitter.emit(AdEvent.Exit, {});
425
+
423
426
  if (this._exitResolve) {
424
427
  this._exitResolve();
425
428
  this._exitResolve = null;
@@ -446,4 +449,4 @@ class XiaomiAdapter {
446
449
  }
447
450
  }
448
451
 
449
- module.exports = { XiaomiAdapter };
452
+ module.exports = { XiaomiAdapter };
@@ -10,18 +10,48 @@ export declare const ActionType: {
10
10
  Exposure: '曝光';
11
11
  /** 点击 */
12
12
  Click: '点击';
13
+ /** 开始播放 */
14
+ StartPlay: '开始播放';
15
+ /** 结束播放(需上报时长 action_value) */
16
+ EndPlay: '结束播放';
17
+ /** 分享 */
18
+ Share: '分享';
19
+ /** 订购(需上报成功订购金额) */
20
+ Order: '订购';
21
+ /** 启动 */
22
+ Start: '启动';
23
+ /** 退出 */
24
+ Exit: '退出';
25
+ /** 切后台 */
26
+ SwitchBackground: '切后台';
27
+ /** 切回 */
28
+ SwitchBack: '切回';
13
29
  };
14
30
 
31
+ /** 批量上报配置 */
32
+ export interface IBatchConfig {
33
+ /** 每批最大条数 */
34
+ perNum: number;
35
+ /** 发送间隔(秒) */
36
+ spaceSec: number;
37
+ }
38
+
15
39
  /** 数据上报模块(单例) */
16
40
  export declare const Reporter: {
17
41
  /** 初始化上报模块 */
18
42
  init(config: IReporterConfig): void;
19
43
 
44
+ /** 从后端获取批量上报配置 */
45
+ fetchReportConfig(callback?: (config: IBatchConfig) => void): void;
46
+
20
47
  /** 单次上报 */
21
- report(data: IReportItem): void;
48
+ report(data: IReportItem, immediate?: boolean): void;
22
49
 
23
50
  /** 批量上报 */
24
- reportBatch(dataList: IReportItem[], commonInfo?: IReportItem): void;
51
+ reportBatch(dataList: IReportItem[], commonInfo?: IReportItem, immediate?: boolean): void;
52
+
53
+ /** 立即刷新队列,将队列中所有事件立即发送 */
54
+ flushQueue(): void;
25
55
 
26
56
  /** 获取或创建设备唯一ID */
27
57
  createDeviceId(): string;
@@ -35,6 +65,21 @@ export declare const Reporter: {
35
65
  /** 获取当前公共字段 */
36
66
  getCommonInfo(): Partial<IReportItem>;
37
67
 
68
+ /** 更新用户信息(登录后调用,传入memberId) */
69
+ updateUserInfo(userInfo: Record<string, string>): void;
70
+
71
+ /** 上报「启动」事件 */
72
+ reportAppStart(): void;
73
+
74
+ /** 上报「切后台」事件 */
75
+ reportSwitchBackground(): void;
76
+
77
+ /** 上报「切回」事件 */
78
+ reportSwitchBack(): void;
79
+
80
+ /** 上报「退出」事件 */
81
+ reportAppExit(): void;
82
+
38
83
  /** 获取底层上报实例(高级用法) */
39
84
  readonly instance: any;
40
- };
85
+ };