123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- import { Observable } from "babylonjs";
- import { Measure } from "../measure";
- import { ValueAndUnit } from "../valueAndUnit";
- import { Control } from "./control";
- /**
- * Enum that determines the text-wrapping mode to use.
- */
- export enum TextWrapping {
- /**
- * Clip the text when it's larger than Control.width; this is the default mode.
- */
- Clip = 0,
- /**
- * Wrap the text word-wise, i.e. try to add line-breaks at word boundary to fit within Control.width.
- */
- WordWrap = 1,
- /**
- * Ellipsize the text, i.e. shrink with trailing … when text is larger than Control.width.
- */
- Ellipsis,
- }
- /**
- * Class used to create text block control
- */
- export class TextBlock extends Control {
- private _text = "";
- private _textWrapping = TextWrapping.Clip;
- private _textHorizontalAlignment = Control.HORIZONTAL_ALIGNMENT_CENTER;
- private _textVerticalAlignment = Control.VERTICAL_ALIGNMENT_CENTER;
- private _lines: any[];
- private _resizeToFit: boolean = false;
- private _lineSpacing: ValueAndUnit = new ValueAndUnit(0);
- private _outlineWidth: number = 0;
- private _outlineColor: string = "white";
- /**
- * An event triggered after the text is changed
- */
- public onTextChangedObservable = new Observable<TextBlock>();
- /**
- * An event triggered after the text was broken up into lines
- */
- public onLinesReadyObservable = new Observable<TextBlock>();
- /**
- * Return the line list (you may need to use the onLinesReadyObservable to make sure the list is ready)
- */
- public get lines(): any[] {
- return this._lines;
- }
- /**
- * Gets or sets an boolean indicating that the TextBlock will be resized to fit container
- */
- public get resizeToFit(): boolean {
- return this._resizeToFit;
- }
- /**
- * Gets or sets an boolean indicating that the TextBlock will be resized to fit container
- */
- public set resizeToFit(value: boolean) {
- if (this._resizeToFit === value) {
- return;
- }
- this._resizeToFit = value;
- if (this._resizeToFit) {
- this._width.ignoreAdaptiveScaling = true;
- this._height.ignoreAdaptiveScaling = true;
- }
- this._markAsDirty();
- }
- /**
- * Gets or sets a boolean indicating if text must be wrapped
- */
- public get textWrapping(): TextWrapping | boolean {
- return this._textWrapping;
- }
- /**
- * Gets or sets a boolean indicating if text must be wrapped
- */
- public set textWrapping(value: TextWrapping | boolean) {
- if (this._textWrapping === value) {
- return;
- }
- this._textWrapping = +value;
- this._markAsDirty();
- }
- /**
- * Gets or sets text to display
- */
- public get text(): string {
- return this._text;
- }
- /**
- * Gets or sets text to display
- */
- public set text(value: string) {
- if (this._text === value) {
- return;
- }
- this._text = value;
- this._markAsDirty();
- this.onTextChangedObservable.notifyObservers(this);
- }
- /**
- * Gets or sets text horizontal alignment (BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER by default)
- */
- public get textHorizontalAlignment(): number {
- return this._textHorizontalAlignment;
- }
- /**
- * Gets or sets text horizontal alignment (BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER by default)
- */
- public set textHorizontalAlignment(value: number) {
- if (this._textHorizontalAlignment === value) {
- return;
- }
- this._textHorizontalAlignment = value;
- this._markAsDirty();
- }
- /**
- * Gets or sets text vertical alignment (BABYLON.GUI.Control.VERTICAL_ALIGNMENT_CENTER by default)
- */
- public get textVerticalAlignment(): number {
- return this._textVerticalAlignment;
- }
- /**
- * Gets or sets text vertical alignment (BABYLON.GUI.Control.VERTICAL_ALIGNMENT_CENTER by default)
- */
- public set textVerticalAlignment(value: number) {
- if (this._textVerticalAlignment === value) {
- return;
- }
- this._textVerticalAlignment = value;
- this._markAsDirty();
- }
- /**
- * Gets or sets line spacing value
- */
- public set lineSpacing(value: string | number) {
- if (this._lineSpacing.fromString(value)) {
- this._markAsDirty();
- }
- }
- /**
- * Gets or sets line spacing value
- */
- public get lineSpacing(): string | number {
- return this._lineSpacing.toString(this._host);
- }
- /**
- * Gets or sets outlineWidth of the text to display
- */
- public get outlineWidth(): number {
- return this._outlineWidth;
- }
- /**
- * Gets or sets outlineWidth of the text to display
- */
- public set outlineWidth(value: number) {
- if (this._outlineWidth === value) {
- return;
- }
- this._outlineWidth = value;
- this._markAsDirty();
- }
- /**
- * Gets or sets outlineColor of the text to display
- */
- public get outlineColor(): string {
- return this._outlineColor;
- }
- /**
- * Gets or sets outlineColor of the text to display
- */
- public set outlineColor(value: string) {
- if (this._outlineColor === value) {
- return;
- }
- this._outlineColor = value;
- this._markAsDirty();
- }
- /**
- * Creates a new TextBlock object
- * @param name defines the name of the control
- * @param text defines the text to display (emptry string by default)
- */
- constructor(
- /**
- * Defines the name of the control
- */
- public name?: string,
- text: string = "") {
- super(name);
- this.text = text;
- }
- protected _getTypeName(): string {
- return "TextBlock";
- }
- protected _processMeasures(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
- if (!this._fontOffset) {
- this._fontOffset = Control._GetFontOffset(context.font);
- }
- // Prepare lines
- this._lines = this._breakLines(this._currentMeasure.width, context);
- this.onLinesReadyObservable.notifyObservers(this);
- let maxLineWidth: number = 0;
- for (let i = 0; i < this._lines.length; i++) {
- const line = this._lines[i];
- if (line.width > maxLineWidth) {
- maxLineWidth = line.width;
- }
- }
- if (this._resizeToFit) {
- if (this._textWrapping === TextWrapping.Clip) {
- let newWidth = this.paddingLeftInPixels + this.paddingRightInPixels + maxLineWidth;
- if (newWidth !== this._width.internalValue) {
- this._width.updateInPlace(newWidth, ValueAndUnit.UNITMODE_PIXEL);
- this._isDirty = true;
- }
- }
- let newHeight = this.paddingTopInPixels + this.paddingBottomInPixels + this._fontOffset.height * this._lines.length;
- if (newHeight !== this._height.internalValue) {
- this._height.updateInPlace(newHeight, ValueAndUnit.UNITMODE_PIXEL);
- this._isDirty = true;
- }
- }
- super._processMeasures(parentMeasure, context);
- }
- private _drawText(text: string, textWidth: number, y: number, context: CanvasRenderingContext2D): void {
- var width = this._currentMeasure.width;
- var x = 0;
- switch (this._textHorizontalAlignment) {
- case Control.HORIZONTAL_ALIGNMENT_LEFT:
- x = 0;
- break;
- case Control.HORIZONTAL_ALIGNMENT_RIGHT:
- x = width - textWidth;
- break;
- case Control.HORIZONTAL_ALIGNMENT_CENTER:
- x = (width - textWidth) / 2;
- break;
- }
- if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
- context.shadowColor = this.shadowColor;
- context.shadowBlur = this.shadowBlur;
- context.shadowOffsetX = this.shadowOffsetX;
- context.shadowOffsetY = this.shadowOffsetY;
- }
- if (this.outlineWidth) {
- context.strokeText(text, this._currentMeasure.left + x, y);
- }
- context.fillText(text, this._currentMeasure.left + x, y);
- }
- /** @hidden */
- public _draw(context: CanvasRenderingContext2D): void {
- context.save();
- this._applyStates(context);
- // Render lines
- this._renderLines(context);
- context.restore();
- }
- protected _applyStates(context: CanvasRenderingContext2D): void {
- super._applyStates(context);
- if (this.outlineWidth) {
- context.lineWidth = this.outlineWidth;
- context.strokeStyle = this.outlineColor;
- }
- }
- protected _breakLines(refWidth: number, context: CanvasRenderingContext2D): object[] {
- var lines = [];
- var _lines = this.text.split("\n");
- if (this._textWrapping === TextWrapping.Ellipsis) {
- for (var _line of _lines) {
- lines.push(this._parseLineEllipsis(_line, refWidth, context));
- }
- } else if (this._textWrapping === TextWrapping.WordWrap) {
- for (var _line of _lines) {
- lines.push(...this._parseLineWordWrap(_line, refWidth, context));
- }
- } else {
- for (var _line of _lines) {
- lines.push(this._parseLine(_line, context));
- }
- }
- return lines;
- }
- protected _parseLine(line: string = '', context: CanvasRenderingContext2D): object {
- return { text: line, width: context.measureText(line).width };
- }
- protected _parseLineEllipsis(line: string = '', width: number,
- context: CanvasRenderingContext2D): object {
- var lineWidth = context.measureText(line).width;
- if (lineWidth > width) {
- line += '…';
- }
- while (line.length > 2 && lineWidth > width) {
- line = line.slice(0, -2) + '…';
- lineWidth = context.measureText(line).width;
- }
- return { text: line, width: lineWidth };
- }
- protected _parseLineWordWrap(line: string = '', width: number,
- context: CanvasRenderingContext2D): object[] {
- var lines = [];
- var words = line.split(' ');
- var lineWidth = 0;
- for (var n = 0; n < words.length; n++) {
- var testLine = n > 0 ? line + " " + words[n] : words[0];
- var metrics = context.measureText(testLine);
- var testWidth = metrics.width;
- if (testWidth > width && n > 0) {
- lines.push({ text: line, width: lineWidth });
- line = words[n];
- lineWidth = context.measureText(line).width;
- }
- else {
- lineWidth = testWidth;
- line = testLine;
- }
- }
- lines.push({ text: line, width: lineWidth });
- return lines;
- }
- protected _renderLines(context: CanvasRenderingContext2D): void {
- var height = this._currentMeasure.height;
- var rootY = 0;
- switch (this._textVerticalAlignment) {
- case Control.VERTICAL_ALIGNMENT_TOP:
- rootY = this._fontOffset.ascent;
- break;
- case Control.VERTICAL_ALIGNMENT_BOTTOM:
- rootY = height - this._fontOffset.height * (this._lines.length - 1) - this._fontOffset.descent;
- break;
- case Control.VERTICAL_ALIGNMENT_CENTER:
- rootY = this._fontOffset.ascent + (height - this._fontOffset.height * this._lines.length) / 2;
- break;
- }
- rootY += this._currentMeasure.top;
- for (let i = 0; i < this._lines.length; i++) {
- const line = this._lines[i];
- if (i !== 0 && this._lineSpacing.internalValue !== 0) {
- if (this._lineSpacing.isPixel) {
- rootY += this._lineSpacing.getValue(this._host);
- } else {
- rootY = rootY + (this._lineSpacing.getValue(this._host) * this._height.getValueInPixel(this._host, this._cachedParentMeasure.height));
- }
- }
- this._drawText(line.text, line.width, rootY, context);
- rootY += this._fontOffset.height;
- }
- }
- /**
- * 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 {
- super.dispose();
- this.onTextChangedObservable.clear();
- }
- }
|