Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of StackPanel layout warning #14549

Merged
merged 8 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/dev/gui/src/2D/controls/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,37 @@ export class Container extends Control {
this._measureForChildren.copyFrom(this._currentMeasure);
}

protected _cacheFullyDefinedDim: { width?: boolean; height?: boolean } = {
width: undefined,
height: undefined,
};

protected _getAdaptDimTo(dim: "width" | "height"): boolean {
if (dim === "width") {
return this.adaptWidthToChildren;
} else {
return this.adaptHeightToChildren;
}
}

public isDimensionFullyDefined(dim: "width" | "height"): boolean {
if (!this._isDirty && this._cacheFullyDefinedDim[dim] !== undefined) {
return this._cacheFullyDefinedDim[dim]!;
}
if (this._getAdaptDimTo(dim)) {
for (const child of this.children) {
if (!child.isDimensionFullyDefined(dim)) {
this._cacheFullyDefinedDim[dim] = false;
return false;
}
}
this._cacheFullyDefinedDim[dim] = true;
return true;
}
this._cacheFullyDefinedDim[dim] = super.isDimensionFullyDefined(dim);
return this._cacheFullyDefinedDim[dim]!;
}

/**
* Serializes the current control
* @param serializationObject defined the JSON serialized object
Expand Down
12 changes: 12 additions & 0 deletions packages/dev/gui/src/2D/controls/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2424,6 +2424,18 @@ export class Control implements IAnimatable {
this.getDescendants().forEach((child) => child._markAllAsDirty());
}

public isDimensionFullyDefined(dim: "width" | "height"): boolean {
return this.getDimension(dim).isPixel;
}

public getDimension(dim: "width" | "height"): ValueAndUnit {
if (dim === "width") {
return this._width;
} else {
return this._height;
}
}

/**
* Clones a control and its descendants
* @param host the texture where the control will be instantiated. Can be empty, in which case the control will be created on the same texture
Expand Down
48 changes: 32 additions & 16 deletions packages/dev/gui/src/2D/controls/stackPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { RegisterClass } from "core/Misc/typeStore";
import { serialize } from "core/Misc/decorators";
import type { AdvancedDynamicTexture } from "../advancedDynamicTexture";
import type { ICanvasRenderingContext } from "core/Engines/ICanvas";
import type { TextBlock } from "./textBlock";
import { TextWrapping } from "./textBlock";

/**
* Class used to create a 2D stack panel container
Expand Down Expand Up @@ -165,10 +163,8 @@ export class StackPanel extends Container {
child._top.ignoreAdaptiveScaling = true;
}

if (child._height.isPercentage && !child._automaticSize && !(child as TextBlock).resizeToFit && !(child as Container).adaptHeightToChildren) {
if (!this.ignoreLayoutWarnings) {
Tools.Warn(`Control (Name:${child.name}, UniqueId:${child.uniqueId}) is using height in percentage mode inside a vertical StackPanel`);
}
if (!child.isDimensionFullyDefined("height") && !this.ignoreLayoutWarnings) {
Tools.Warn(`Control (Name:${child.name}, UniqueId:${child.uniqueId}) is using height in percentage mode inside a vertical StackPanel`);
carolhmj marked this conversation as resolved.
Show resolved Hide resolved
} else {
stackHeight += child._currentMeasure.height + child._paddingTopInPixels + child._paddingBottomInPixels + (index < childrenCount - 1 ? this._spacing : 0);
}
Expand All @@ -179,16 +175,8 @@ export class StackPanel extends Container {
child._left.ignoreAdaptiveScaling = true;
}

if (
child._width.isPercentage &&
!child._automaticSize &&
child.getClassName() === "TextBlock" &&
(child as TextBlock).textWrapping !== TextWrapping.Clip &&
!(child as TextBlock).forceResizeWidth
) {
if (!this.ignoreLayoutWarnings) {
Tools.Warn(`Control (Name:${child.name}, UniqueId:${child.uniqueId}) is using width in percentage mode inside a horizontal StackPanel`);
}
if (!child.isDimensionFullyDefined("width") && !this.ignoreLayoutWarnings) {
Tools.Warn(`Control (Name:${child.name}, UniqueId:${child.uniqueId}) is using width in percentage mode inside a horizontal StackPanel`);
} else {
stackWidth += child._currentMeasure.width + child._paddingLeftInPixels + child._paddingRightInPixels + (index < childrenCount - 1 ? this._spacing : 0);
}
Expand Down Expand Up @@ -236,6 +224,34 @@ export class StackPanel extends Container {
super._postMeasure();
}

private _getManualDim(dim: "width" | "height") {
if (dim === "width") {
return this._manualWidth;
} else {
return this._manualHeight;
}
}

public isDimensionFullyDefined(dim: "width" | "height"): boolean {
if (this._isDirty && this._cacheFullyDefinedDim[dim] !== undefined) {
RaananW marked this conversation as resolved.
Show resolved Hide resolved
return this._cacheFullyDefinedDim[dim]!;
}

if (dim === "height" ? this.isVertical : !this.isVertical && !this._getManualDim(dim)) {
for (const child of this._children) {
if (!child.isDimensionFullyDefined(dim)) {
this._cacheFullyDefinedDim[dim] = false;
return false;
}
}
this._cacheFullyDefinedDim[dim] = true;
return true;
}

this._cacheFullyDefinedDim[dim] = this.getDimension(dim).isPixel || this._getAdaptDimTo(dim);
return this._cacheFullyDefinedDim[dim]!;
}

/**
* Serializes the current control
* @param serializationObject defined the JSON serialized object
Expand Down
7 changes: 7 additions & 0 deletions packages/dev/gui/src/2D/controls/textBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,13 @@ export class TextBlock extends Control {
return newHeight;
}

public isDimensionFullyDefined(dim: "width" | "height"): boolean {
if (this.resizeToFit) {
return true;
}
return super.isDimensionFullyDefined(dim);
}

/**
* Given a width constraint applied on the text block, find the expected height
* @returns expected height
Expand Down