Jelajahi Sumber

TextBlock: provide expected height

Adrien Tétar 7 tahun lalu
induk
melakukan
0376f675a7
1 mengubah file dengan 35 tambahan dan 8 penghapusan
  1. 35 8
      gui/src/2D/controls/textBlock.ts

+ 35 - 8
gui/src/2D/controls/textBlock.ts

@@ -249,29 +249,35 @@ module BABYLON.GUI {
         }
         }
 
 
         protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
         protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
-            this._lines = [];
+            this._lines = this._breakLines(this._currentMeasure.width, context);
+            this.onLinesReadyObservable.notifyObservers(this);
+        }
+
+        protected _breakLines(refWidth: number, context: CanvasRenderingContext2D): object[] {
+            var lines = [];
             var _lines = this.text.split("\n");
             var _lines = this.text.split("\n");
 
 
             if (this._textWrapping && !this._resizeToFit) {
             if (this._textWrapping && !this._resizeToFit) {
                 for (var _line of _lines) {
                 for (var _line of _lines) {
-                    this._lines.push(this._parseLineWithTextWrapping(_line, context));
+                    lines.push(...this._parseLineWithTextWrapping(_line, refWidth, context));
                 }
                 }
             } else {
             } else {
                 for (var _line of _lines) {
                 for (var _line of _lines) {
-                    this._lines.push(this._parseLine(_line, context));
+                    lines.push(this._parseLine(_line, context));
                 }
                 }
             }
             }
 
 
-            this.onLinesReadyObservable.notifyObservers(this);
+            return lines;
         }
         }
 
 
         protected _parseLine(line: string = '', context: CanvasRenderingContext2D): object {
         protected _parseLine(line: string = '', context: CanvasRenderingContext2D): object {
             return { text: line, width: context.measureText(line).width };
             return { text: line, width: context.measureText(line).width };
         }
         }
 
 
-        protected _parseLineWithTextWrapping(line: string = '', context: CanvasRenderingContext2D): object {
+        protected _parseLineWithTextWrapping(line: string = '', width: number,
+                                             context: CanvasRenderingContext2D): object[] {
+            var lines = [];
             var words = line.split(' ');
             var words = line.split(' ');
-            var width = this._currentMeasure.width;
             var lineWidth = 0;
             var lineWidth = 0;
 
 
             for (var n = 0; n < words.length; n++) {
             for (var n = 0; n < words.length; n++) {
@@ -279,7 +285,7 @@ module BABYLON.GUI {
                 var metrics = context.measureText(testLine);
                 var metrics = context.measureText(testLine);
                 var testWidth = metrics.width;
                 var testWidth = metrics.width;
                 if (testWidth > width && n > 0) {
                 if (testWidth > width && n > 0) {
-                    this._lines.push({ text: line, width: lineWidth });
+                    lines.push({ text: line, width: lineWidth });
                     line = words[n];
                     line = words[n];
                     lineWidth = context.measureText(line).width;
                     lineWidth = context.measureText(line).width;
                 }
                 }
@@ -288,8 +294,9 @@ module BABYLON.GUI {
                     line = testLine;
                     line = testLine;
                 }
                 }
             }
             }
+            lines.push({ text: line, width: lineWidth });
 
 
-            return { text: line, width: lineWidth };
+            return lines;
         }
         }
 
 
         protected _renderLines(context: CanvasRenderingContext2D): void {
         protected _renderLines(context: CanvasRenderingContext2D): void {
@@ -339,6 +346,26 @@ module BABYLON.GUI {
             }
             }
         }
         }
 
 
+        /**
+         * Given a width constraint applied on the text block, find the expected height
+         * @returns expected height
+         */
+        public computeExpectedHeight(): number {
+            if (this.text && this.widthInPixels) {
+                const context = document.createElement('canvas').getContext('2d');
+                if (context) {
+                    this._applyStates(context);
+                    if (!this._fontOffset) {
+                        this._fontOffset = Control._GetFontOffset(context.font);
+                    }
+                    const lines = this._lines ? this._lines : this._breakLines(
+                        this.widthInPixels - this.paddingLeftInPixels - this.paddingRightInPixels, context);
+                    return this.paddingTopInPixels + this.paddingBottomInPixels + this._fontOffset.height * lines.length;
+                }
+            }
+            return 0;
+        }
+
         dispose(): void {
         dispose(): void {
             super.dispose();
             super.dispose();