@babylonjs/gui 9.32.0 → 9.33.0

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.
@@ -879,6 +879,7 @@ export declare class Control implements IAnimatable, IFocusableControl {
879
879
  */
880
880
  _processObservables(type: number, x: number, y: number, pi: Nullable<PointerInfoBase>, pointerId: number, buttonIndex: number, deltaX?: number, deltaY?: number): boolean;
881
881
  private _getStyleProperty;
882
+ private _getFont;
882
883
  private _prepareFont;
883
884
  /**
884
885
  * A control has a dimension fully defined if that dimension doesn't depend on the parent's dimension.
@@ -909,6 +910,7 @@ export declare class Control implements IAnimatable, IFocusableControl {
909
910
  parse(serializedObject: any, host?: AdvancedDynamicTexture, urlRewriter?: (url: string) => string): Control;
910
911
  /**
911
912
  * Serializes the current control
913
+ * Preserves the live layout and cached font metrics when adaptive scaling is enabled.
912
914
  * @param serializationObject defined the JSON serialized object
913
915
  * @param force if the control should be serialized even if the isSerializable flag is set to false (default false)
914
916
  * @param allowCanvas defines if the control is allowed to use a Canvas2D object to serialize (true by default)
@@ -2138,18 +2138,20 @@ let Control = (() => {
2138
2138
  return prop;
2139
2139
  }
2140
2140
  }
2141
+ _getFont() {
2142
+ return (this._getStyleProperty("fontStyle", "") +
2143
+ " " +
2144
+ this._getStyleProperty("fontWeight", "") +
2145
+ " " +
2146
+ this.fontSizeInPixels +
2147
+ "px " +
2148
+ this._getStyleProperty("fontFamily", "Arial"));
2149
+ }
2141
2150
  _prepareFont() {
2142
2151
  if (!this._font && !this._fontSet) {
2143
2152
  return;
2144
2153
  }
2145
- this._font =
2146
- this._getStyleProperty("fontStyle", "") +
2147
- " " +
2148
- this._getStyleProperty("fontWeight", "") +
2149
- " " +
2150
- this.fontSizeInPixels +
2151
- "px " +
2152
- this._getStyleProperty("fontFamily", "Arial");
2154
+ this._font = this._getFont();
2153
2155
  this._fontOffset = _a._GetFontOffset(this._font, this._host?.getScene()?.getEngine());
2154
2156
  //children need to be refreshed
2155
2157
  const descendants = this.getDescendants();
@@ -2208,6 +2210,7 @@ let Control = (() => {
2208
2210
  }
2209
2211
  /**
2210
2212
  * Serializes the current control
2213
+ * Preserves the live layout and cached font metrics when adaptive scaling is enabled.
2211
2214
  * @param serializationObject defined the JSON serialized object
2212
2215
  * @param force if the control should be serialized even if the isSerializable flag is set to false (default false)
2213
2216
  * @param allowCanvas defines if the control is allowed to use a Canvas2D object to serialize (true by default)
@@ -2216,45 +2219,46 @@ let Control = (() => {
2216
2219
  if (!this.isSerializable && !force) {
2217
2220
  return;
2218
2221
  }
2219
- let idealWidth = 0;
2220
- let idealHeight = 0;
2221
- // the host's ideal width and height are influencing the serialization, as they are used in getValue() of ValueAndUnit.
2222
- // for a proper serialization, we need to temporarily set them to 0 and re-set them back afterwards.
2223
- if (this.host) {
2224
- idealHeight = this.host.idealHeight;
2225
- idealWidth = this.host.idealWidth;
2226
- this.host.idealWidth = 0;
2227
- this.host.idealHeight = 0;
2228
- }
2229
- SerializationHelper.Serialize(this, serializationObject);
2230
- serializationObject.name = this.name;
2231
- serializationObject.className = this.getClassName();
2232
- // Call prepareFont to guarantee the font is properly set before serializing
2233
- if (allowCanvas) {
2234
- this._prepareFont();
2235
- }
2236
- if (this._fontFamily) {
2237
- serializationObject.fontFamily = this._fontFamily;
2238
- }
2239
- if (this.fontSize) {
2240
- serializationObject.fontSize = this.fontSize;
2241
- }
2242
- if (this.fontWeight) {
2243
- serializationObject.fontWeight = this.fontWeight;
2244
- }
2245
- if (this.fontStyle) {
2246
- serializationObject.fontStyle = this.fontStyle;
2247
- }
2248
- if (this._gradient) {
2249
- serializationObject.gradient = {};
2250
- this._gradient.serialize(serializationObject.gradient);
2222
+ const host = this._host;
2223
+ const idealWidth = host?.idealWidth ?? 0;
2224
+ const idealHeight = host?.idealHeight ?? 0;
2225
+ // Serialize declared values without the setters invalidating the entire GUI for each control.
2226
+ if (host) {
2227
+ host._idealWidth = 0;
2228
+ host._idealHeight = 0;
2229
+ }
2230
+ try {
2231
+ SerializationHelper.Serialize(this, serializationObject);
2232
+ serializationObject.name = this.name;
2233
+ serializationObject.className = this.getClassName();
2234
+ if (allowCanvas && (this._font || this._fontSet)) {
2235
+ // Compute unscaled metrics without changing the live font cache or dirtying descendants.
2236
+ serializationObject.fontOffset = _a._GetFontOffset(this._getFont(), host?.getScene()?.getEngine());
2237
+ }
2238
+ if (this._fontFamily) {
2239
+ serializationObject.fontFamily = this._fontFamily;
2240
+ }
2241
+ if (this.fontSize) {
2242
+ serializationObject.fontSize = this.fontSize;
2243
+ }
2244
+ if (this.fontWeight) {
2245
+ serializationObject.fontWeight = this.fontWeight;
2246
+ }
2247
+ if (this.fontStyle) {
2248
+ serializationObject.fontStyle = this.fontStyle;
2249
+ }
2250
+ if (this._gradient) {
2251
+ serializationObject.gradient = {};
2252
+ this._gradient.serialize(serializationObject.gradient);
2253
+ }
2254
+ // Animations
2255
+ SerializationHelper.AppendSerializedAnimations(this, serializationObject);
2251
2256
  }
2252
- // Animations
2253
- SerializationHelper.AppendSerializedAnimations(this, serializationObject);
2254
- // re-set the ideal width and height
2255
- if (this.host) {
2256
- this.host.idealWidth = idealWidth;
2257
- this.host.idealHeight = idealHeight;
2257
+ finally {
2258
+ if (host) {
2259
+ host._idealWidth = idealWidth;
2260
+ host._idealHeight = idealHeight;
2261
+ }
2258
2262
  }
2259
2263
  }
2260
2264
  /**