keytops-game-framework 1.0.17 → 1.0.19

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.
@@ -16,6 +16,7 @@ export type ViewAnalyticsInfo = {
16
16
  name: string;
17
17
  group?: string;
18
18
  };
19
+ export type TouchLockToken = symbol;
19
20
  /**
20
21
  * 视图管理类
21
22
  */
@@ -28,6 +29,7 @@ export declare class ViewManager {
28
29
  private _currentStack;
29
30
  private _viewCache;
30
31
  private _currentMaskView;
32
+ private _touchLocks;
31
33
  private _viewEvent;
32
34
  private static created;
33
35
  /**
@@ -94,16 +96,21 @@ export declare class ViewManager {
94
96
  scene<T extends IScene>(): T;
95
97
  /**
96
98
  * 开启当前场景的可交互性
99
+ * @param token closeTouch返回的触摸锁令牌。传入时仅释放该锁,所有锁释放后才会真正开启触摸;不传则清空全部触摸锁并开启触摸。
97
100
  */
98
- openTouch(): void;
101
+ openTouch(token?: TouchLockToken): void;
99
102
  /**
100
103
  * 关闭当前场景的可交互性
101
104
  * @param maskViewConfig 可选参数,默认为空白,要显示的遮罩试图。
102
105
  * @param delay 可选参数,默认200毫秒,显示maskView的延迟时间。
106
+ * @returns 当前触摸锁令牌,可传给openTouch精确释放本次锁。
103
107
  */
104
108
  closeTouch(maskViewConfig?: {
105
109
  prototype: IView;
106
- } | ViewConfig | string, delay?: number): Promise<void>;
110
+ } | ViewConfig | string, delay?: number): Promise<TouchLockToken>;
111
+ private _lockTouch;
112
+ private _applyTouchState;
113
+ private _showMaskView;
107
114
  /**
108
115
  * 当前主视图
109
116
  * @returns
@@ -136,6 +143,7 @@ export declare class ViewManager {
136
143
  } | ViewConfig | string, parent?: IView, ...datas: any[]): Promise<T>;
137
144
  private _addView;
138
145
  private _addViewbyConfig;
146
+ private _loadViewByConfig;
139
147
  /**
140
148
  * 加载视图到内存,之后还需要调用loader.initView接口获取view
141
149
  * @param config 要加载的视图配置|Class|名称
package/dist/index.js CHANGED
@@ -6912,6 +6912,7 @@ class ViewManager {
6912
6912
  this._viewAnalyticsMap = new Map();
6913
6913
  this._currentStack = [];
6914
6914
  this._viewCache = new Map();
6915
+ this._touchLocks = new Set();
6915
6916
  if (ViewManager.created) {
6916
6917
  throw new Error("ViewManager 是单例");
6917
6918
  }
@@ -6965,36 +6966,80 @@ class ViewManager {
6965
6966
  }
6966
6967
  /**
6967
6968
  * 开启当前场景的可交互性
6969
+ * @param token closeTouch返回的触摸锁令牌。传入时仅释放该锁,所有锁释放后才会真正开启触摸;不传则清空全部触摸锁并开启触摸。
6968
6970
  */
6969
- openTouch() {
6970
- timer.clear(this, this.closeTouch);
6971
- if (this._currentMaskView) {
6972
- this._currentMaskView.removeFromParent(true);
6973
- this._currentMaskView = null;
6971
+ openTouch(token) {
6972
+ if (token) {
6973
+ this._touchLocks.delete(token);
6974
6974
  }
6975
- if (this._scene) {
6976
- this._scene.touchEnabled = true;
6975
+ else {
6976
+ this._touchLocks.clear();
6977
6977
  }
6978
+ this._applyTouchState();
6978
6979
  }
6979
6980
  /**
6980
6981
  * 关闭当前场景的可交互性
6981
6982
  * @param maskViewConfig 可选参数,默认为空白,要显示的遮罩试图。
6982
6983
  * @param delay 可选参数,默认200毫秒,显示maskView的延迟时间。
6984
+ * @returns 当前触摸锁令牌,可传给openTouch精确释放本次锁。
6983
6985
  */
6984
6986
  closeTouch(maskViewConfig = null, delay = 200) {
6985
6987
  return __awaiter(this, void 0, void 0, function* () {
6986
- if (!this._scene || !this._scene.touchEnabled) {
6987
- return;
6988
+ const token = this._lockTouch();
6989
+ let config = maskViewConfig && this._getViewConfig(maskViewConfig);
6990
+ if (!config) {
6991
+ return token;
6992
+ }
6993
+ if (delay <= 0) {
6994
+ yield this._showMaskView(config, token);
6995
+ return token;
6996
+ }
6997
+ timer.once(delay, this, this._showMaskView, [config, token]);
6998
+ return token;
6999
+ });
7000
+ }
7001
+ _lockTouch() {
7002
+ const token = Symbol("touch-lock");
7003
+ this._touchLocks.add(token);
7004
+ this._applyTouchState();
7005
+ return token;
7006
+ }
7007
+ _applyTouchState() {
7008
+ const locked = this._touchLocks.size > 0;
7009
+ if (!locked) {
7010
+ timer.clear(this, this._showMaskView);
7011
+ if (this._currentMaskView) {
7012
+ this._currentMaskView.removeFromParent(true);
7013
+ this._currentMaskView = null;
6988
7014
  }
6989
- this._scene.touchEnabled = false;
6990
- if (!maskViewConfig || !this._getViewConfig(maskViewConfig)) {
7015
+ }
7016
+ if (this._scene) {
7017
+ this._scene.touchEnabled = !locked;
7018
+ }
7019
+ }
7020
+ _showMaskView(maskViewConfig, token) {
7021
+ return __awaiter(this, void 0, void 0, function* () {
7022
+ if (!this._scene || !this._touchLocks.has(token) || this._currentMaskView) {
6991
7023
  return;
6992
7024
  }
6993
- if (delay <= 0) {
6994
- this._currentMaskView = yield this.addView(maskViewConfig, this._scene);
7025
+ let view = this.getCacheView(maskViewConfig);
7026
+ if (view) {
7027
+ view = yield this._addView(view, this._scene);
7028
+ }
7029
+ else {
7030
+ view = yield this._loadViewByConfig(maskViewConfig, this._scene, "显示遮罩视图错误");
7031
+ this.cacheView(view);
7032
+ }
7033
+ if (!this._touchLocks.has(token)) {
7034
+ view.removeFromParent(true);
6995
7035
  return;
6996
7036
  }
6997
- timer.once(delay, this, this.closeTouch, [maskViewConfig, 0]);
7037
+ this._currentMaskView = view;
7038
+ timer.frameNext(this, () => {
7039
+ if (this._currentMaskView == view && this._touchLocks.has(token)) {
7040
+ view.setData();
7041
+ }
7042
+ });
6998
7043
  });
6999
7044
  }
7000
7045
  /**
@@ -7061,12 +7106,13 @@ class ViewManager {
7061
7106
  }
7062
7107
  _switchViewByConfig(viewConfig, ...datas) {
7063
7108
  return __awaiter(this, void 0, void 0, function* () {
7064
- this.closeTouch();
7109
+ const touchToken = this._lockTouch();
7065
7110
  let loader;
7066
7111
  try {
7067
7112
  loader = yield this.loadView(viewConfig);
7068
7113
  }
7069
7114
  catch (error) {
7115
+ this.openTouch(touchToken);
7070
7116
  console.error(`切换视图错误 code:${error.code}, msg:${error.msg || error.message}`);
7071
7117
  throw new Error(`切换视图错误 code:${error.code}, msg:${error.msg || error.message}`);
7072
7118
  }
@@ -7085,7 +7131,7 @@ class ViewManager {
7085
7131
  this._scene.addView(view, viewConfig.zIndex);
7086
7132
  }
7087
7133
  timer.frameNext(this, () => {
7088
- this.openTouch();
7134
+ this.openTouch(touchToken);
7089
7135
  view.setData(...datas);
7090
7136
  });
7091
7137
  return view;
@@ -7124,14 +7170,31 @@ class ViewManager {
7124
7170
  }
7125
7171
  _addViewbyConfig(config, parent, ...datas) {
7126
7172
  return __awaiter(this, void 0, void 0, function* () {
7127
- this.closeTouch();
7173
+ const touchToken = this._lockTouch();
7174
+ let view;
7175
+ try {
7176
+ view = yield this._loadViewByConfig(config, parent, "添加视图错误");
7177
+ }
7178
+ catch (error) {
7179
+ this.openTouch(touchToken);
7180
+ throw error;
7181
+ }
7182
+ timer.frameNext(this, () => {
7183
+ this.openTouch(touchToken);
7184
+ view.setData(...datas);
7185
+ });
7186
+ return view;
7187
+ });
7188
+ }
7189
+ _loadViewByConfig(config, parent, errorPrefix) {
7190
+ return __awaiter(this, void 0, void 0, function* () {
7128
7191
  let loader;
7129
7192
  try {
7130
7193
  loader = yield this.loadView(config);
7131
7194
  }
7132
7195
  catch (error) {
7133
- console.error(`添加视图错误 code:${error.code}, msg:${error.msg}`);
7134
- throw new Error(`添加视图错误 code:${error.code}, msg:${error.msg}`);
7196
+ console.error(`${errorPrefix} code:${error.code}, msg:${error.msg || error.message}`);
7197
+ throw new Error(`${errorPrefix} code:${error.code}, msg:${error.msg || error.message}`);
7135
7198
  }
7136
7199
  let name = config.name || config.path.substring(config.path.lastIndexOf("/") + 1);
7137
7200
  let view = loader.initView(name, true);
@@ -7139,10 +7202,6 @@ class ViewManager {
7139
7202
  view.config = config;
7140
7203
  view.analyticsInfo = this._viewAnalyticsMap.get(config.name);
7141
7204
  parent.addView(view, config.zIndex);
7142
- timer.frameNext(this, () => {
7143
- this.openTouch();
7144
- view.setData(...datas);
7145
- });
7146
7205
  return view;
7147
7206
  });
7148
7207
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keytops-game-framework",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "description": "cocos creator game framework library",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",