Skip to content

Commit

Permalink
Update gui code (#27)
Browse files Browse the repository at this point in the history
Opt code and add comment
  • Loading branch information
cptbtptpbcptdtptp authored Dec 19, 2024
1 parent bb3b3e8 commit 3929d73
Show file tree
Hide file tree
Showing 41 changed files with 631 additions and 524 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/2d/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ export { TextHorizontalAlignment, TextVerticalAlignment } from "./enums/TextAlig
export { OverflowMode } from "./enums/TextOverflow";
export * from "./sprite/index";
export * from "./text/index";
export type { ITextRenderer } from "./text/ITextRenderer";
export type { ITextRenderer } from "./text/ITextRenderer";
10 changes: 7 additions & 3 deletions packages/core/src/2d/sprite/SpriteMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class SpriteMask extends Renderer implements ISpriteRenderer {
if (this._customWidth !== undefined) {
return this._customWidth;
} else {
this._dirtyUpdateFlag & SpriteMaskUpdateFlags.AutomaticSize && this._calDefaultSize();
return this._automaticWidth;
}
}
Expand All @@ -92,6 +93,7 @@ export class SpriteMask extends Renderer implements ISpriteRenderer {
if (this._customHeight !== undefined) {
return this._customHeight;
} else {
this._dirtyUpdateFlag & SpriteMaskUpdateFlags.AutomaticSize && this._calDefaultSize();
return this._automaticHeight;
}
}
Expand Down Expand Up @@ -336,6 +338,7 @@ export class SpriteMask extends Renderer implements ISpriteRenderer {
} else {
this._automaticWidth = this._automaticHeight = 0;
}
this._dirtyUpdateFlag &= ~SpriteMaskUpdateFlags.AutomaticSize;
}

@ignoreClone
Expand All @@ -345,9 +348,9 @@ export class SpriteMask extends Renderer implements ISpriteRenderer {
this.shaderData.setTexture(SpriteMask._textureProperty, this.sprite.texture);
break;
case SpriteModifyFlags.size:
this._dirtyUpdateFlag |= SpriteMaskUpdateFlags.AutomaticSize;
if (this._customWidth === undefined || this._customHeight === undefined) {
this._calDefaultSize();
this._dirtyUpdateFlag |= RendererUpdateFlags.AllPositionAndBounds;
this._dirtyUpdateFlag |= RendererUpdateFlags.AllBounds;
}
break;
case SpriteModifyFlags.region:
Expand All @@ -374,9 +377,10 @@ export class SpriteMask extends Renderer implements ISpriteRenderer {
*/
enum SpriteMaskUpdateFlags {
UV = 0x10,
AutomaticSize = 0x20,

/** LocalPosition | WorldPosition | UV */
AllPositionAndUV = 0x13,
/** LocalPosition | WorldPosition | UV | LocalBounds | WorldBounds */
All = 0x1f
All = 0x3f
}
19 changes: 11 additions & 8 deletions packages/core/src/2d/sprite/SpriteRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { SubPrimitiveChunk } from "../../RenderPipeline/SubPrimitiveChunk";
import { SubRenderElement } from "../../RenderPipeline/SubRenderElement";
import { Renderer, RendererUpdateFlags } from "../../Renderer";
import { assignmentClone, deepClone, ignoreClone } from "../../clone/CloneManager";
import { ComponentType } from "../../enums/ComponentType";
import { ShaderProperty } from "../../shader/ShaderProperty";
import { ISpriteAssembler } from "../assembler/ISpriteAssembler";
import { ISpriteRenderer } from "../assembler/ISpriteRenderer";
Expand Down Expand Up @@ -143,7 +142,6 @@ export class SpriteRenderer extends Renderer implements ISpriteRenderer {
this.shaderData.setTexture(SpriteRenderer._textureProperty, null);
}
this._sprite = value;
this._calDefaultSize();
}
}

Expand Down Expand Up @@ -171,6 +169,7 @@ export class SpriteRenderer extends Renderer implements ISpriteRenderer {
if (this._customWidth !== undefined) {
return this._customWidth;
} else {
this._dirtyUpdateFlag & SpriteRendererUpdateFlags.AutomaticSize && this._calDefaultSize();
return this._automaticWidth;
}
}
Expand All @@ -180,7 +179,7 @@ export class SpriteRenderer extends Renderer implements ISpriteRenderer {
this._customWidth = value;
this._dirtyUpdateFlag |=
this._drawMode === SpriteDrawMode.Tiled
? SpriteRendererUpdateFlags.All
? SpriteRendererUpdateFlags.AllPositionBoundsUVAndColor
: RendererUpdateFlags.AllPositionAndBounds;
}
}
Expand All @@ -196,6 +195,7 @@ export class SpriteRenderer extends Renderer implements ISpriteRenderer {
if (this._customHeight !== undefined) {
return this._customHeight;
} else {
this._dirtyUpdateFlag & SpriteRendererUpdateFlags.AutomaticSize && this._calDefaultSize();
return this._automaticHeight;
}
}
Expand All @@ -205,7 +205,7 @@ export class SpriteRenderer extends Renderer implements ISpriteRenderer {
this._customHeight = value;
this._dirtyUpdateFlag |=
this._drawMode === SpriteDrawMode.Tiled
? SpriteRendererUpdateFlags.All
? SpriteRendererUpdateFlags.AllPositionBoundsUVAndColor
: RendererUpdateFlags.AllPositionAndBounds;
}
}
Expand Down Expand Up @@ -267,7 +267,6 @@ export class SpriteRenderer extends Renderer implements ISpriteRenderer {
*/
constructor(entity: Entity) {
super(entity);
this._componentType = ComponentType.SpriteRenderer;
this.drawMode = SpriteDrawMode.Simple;
this._dirtyUpdateFlag |= SpriteRendererUpdateFlags.Color;
this.setMaterial(this._engine._basicResources.spriteDefaultMaterial);
Expand Down Expand Up @@ -410,6 +409,7 @@ export class SpriteRenderer extends Renderer implements ISpriteRenderer {
} else {
this._automaticWidth = this._automaticHeight = 0;
}
this._dirtyUpdateFlag &= ~SpriteRendererUpdateFlags.AutomaticSize;
}

@ignoreClone
Expand All @@ -419,8 +419,8 @@ export class SpriteRenderer extends Renderer implements ISpriteRenderer {
this.shaderData.setTexture(SpriteRenderer._textureProperty, this.sprite.texture);
break;
case SpriteModifyFlags.size:
this._dirtyUpdateFlag |= SpriteRendererUpdateFlags.AutomaticSize;
if (this._customWidth === undefined || this._customHeight === undefined) {
this._calDefaultSize();
this._dirtyUpdateFlag |= RendererUpdateFlags.AllBounds;
}
switch (this._drawMode) {
Expand Down Expand Up @@ -479,11 +479,14 @@ export class SpriteRenderer extends Renderer implements ISpriteRenderer {
enum SpriteRendererUpdateFlags {
UV = 0x10,
Color = 0x20,
AutomaticSize = 0x40,

/** LocalPosition | WorldPosition | UV */
AllPositionAndUV = 0x13,
/** LocalPosition | WorldPosition | UV | Color */
AllPositionUVAndColor = 0x33,
/** LocalPosition | WorldPosition | UV | Color | LocalBounds | WorldBounds */
All = 0x3f
/** LocalPosition | WorldPosition | LocalBounds | WorldBounds | UV | Color */
AllPositionBoundsUVAndColor = 0x3f,
/** LocalPosition | WorldPosition | LocalBounds | WorldBounds | UV | Color | AutomaticSize */
All = 0x7f
}
5 changes: 1 addition & 4 deletions packages/core/src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,10 +760,7 @@ export class Camera extends Component {
//@ts-ignore
this._viewport._onValueChanged = null;
this.engine.canvas._sizeUpdateFlagManager.removeListener(this._onPixelViewportChanged);
if (this._updateFlagManager) {
this._updateFlagManager.removeAllListeners();
this._updateFlagManager = null;
}
this._updateFlagManager = null;

this._entity = null;
this._globalShaderMacro = null;
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import { EngineObject } from "./base";
import { assignmentClone, ignoreClone } from "./clone/CloneManager";
import { Entity } from "./Entity";
import { ActiveChangeFlag } from "./enums/ActiveChangeFlag";
import { ComponentType } from "./enums/ComponentType";
import { Scene } from "./Scene";

/**
* The base class of the components.
*/
export class Component extends EngineObject {
/** @internal */
@ignoreClone
_componentType: ComponentType = ComponentType.Component;
/** @internal */
@ignoreClone
_entity: Entity;
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/ComponentsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ComponentsManager {
/** @internal */
_overlayCanvases: DisorderedArray<IUICanvas> = new DisorderedArray();
/* @internal */
_overlayCanvasesSortingFlag: boolean = false;
_overlayCanvasesSortingDirty: boolean = false;
/** @internal */
_canvases: DisorderedArray<IUICanvas> = new DisorderedArray();

Expand Down Expand Up @@ -81,7 +81,7 @@ export class ComponentsManager {
let canvases: DisorderedArray<IUICanvas>;
if (isOverlay) {
canvases = this._overlayCanvases;
this._overlayCanvasesSortingFlag = true;
this._overlayCanvasesSortingDirty = true;
} else {
canvases = this._canvases;
}
Expand All @@ -93,7 +93,7 @@ export class ComponentsManager {
let canvases: DisorderedArray<IUICanvas>;
if (isOverlay) {
canvases = this._overlayCanvases;
this._overlayCanvasesSortingFlag = true;
this._overlayCanvasesSortingDirty = true;
} else {
canvases = this._canvases;
}
Expand All @@ -102,14 +102,14 @@ export class ComponentsManager {
uiCanvas._canvasIndex = -1;
}

sortUICanvases(): void {
if (this._overlayCanvasesSortingFlag) {
sortOverlayUICanvases(): void {
if (this._overlayCanvasesSortingDirty) {
const overlayCanvases = this._overlayCanvases;
overlayCanvases.sort((a, b) => a.sortOrder - b.sortOrder);
for (let i = 0, n = overlayCanvases.length; i < n; i++) {
overlayCanvases.get(i)._canvasIndex = i;
}
this._overlayCanvasesSortingFlag = false;
this._overlayCanvasesSortingDirty = false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ export class Engine extends EventDispatcher {

const uiCanvas = componentsManager._overlayCanvases;
if (uiCanvas.length > 0) {
componentsManager.sortUICanvases();
componentsManager.sortOverlayUICanvases();
UIUtils.render(this, uiCanvas);
}
}
Expand Down
9 changes: 3 additions & 6 deletions packages/core/src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,7 @@ export class Entity extends EngineObject {
}

this.isActive = false;
if (this._updateFlagManager) {
this._updateFlagManager.removeAllListeners();
this._updateFlagManager = null;
}
this._updateFlagManager = null;
}

/**
Expand Down Expand Up @@ -626,7 +623,7 @@ export class Entity extends EngineObject {
*/
_setParentChange() {
this._transform._parentChange();
this._dispatchModify(EntityModifyFlags.Parent);
this._dispatchModify(EntityModifyFlags.Parent, this);
}

/**
Expand Down Expand Up @@ -792,7 +789,7 @@ export class Entity extends EngineObject {
}
}
}
this._dispatchModify(EntityModifyFlags.SiblingIndex);
this._dispatchModify(EntityModifyFlags.SiblingIndex, this);
}

//--------------------------------------------------------------deprecated----------------------------------------------------------------
Expand Down
14 changes: 12 additions & 2 deletions packages/core/src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { SubRenderElement } from "./RenderPipeline/SubRenderElement";
import { Transform } from "./Transform";
import { assignmentClone, deepClone, ignoreClone } from "./clone/CloneManager";
import { IComponentCustomClone } from "./clone/ComponentCloner";
import { ComponentType } from "./enums/ComponentType";
import { SpriteMaskLayer } from "./enums/SpriteMaskLayer";
import { Material } from "./material";
import { ShaderMacro, ShaderProperty } from "./shader";
Expand Down Expand Up @@ -67,6 +66,8 @@ export class Renderer extends Component implements IComponentCustomClone {
@ignoreClone
protected _localBounds: BoundingBox = new BoundingBox();
@ignoreClone
protected _customLocalBounds: BoundingBox;
@ignoreClone
protected _bounds: BoundingBox = new BoundingBox();
@ignoreClone
protected _transformEntity: Entity;
Expand Down Expand Up @@ -142,13 +143,23 @@ export class Renderer extends Component implements IComponentCustomClone {
* The local bounding volume of the renderer.
*/
get localBounds(): BoundingBox {
if (this._customLocalBounds) return this._customLocalBounds;
if (this._dirtyUpdateFlag & RendererUpdateFlags.LocalBounds) {
this._updateLocalBounds(this._localBounds);
this._dirtyUpdateFlag &= ~RendererUpdateFlags.LocalBounds;
}
return this._localBounds;
}

set localBounds(value: BoundingBox) {
if (value) {
(this._customLocalBounds ||= new BoundingBox()).copyFrom(value);
} else {
this._customLocalBounds = null;
}
this._dirtyUpdateFlag |= RendererUpdateFlags.WorldBounds;
}

/**
* The world bounding volume of the renderer.
*/
Expand Down Expand Up @@ -178,7 +189,6 @@ export class Renderer extends Component implements IComponentCustomClone {
super(entity);
const prototype = Renderer.prototype;
const shaderData = this.shaderData;
this._componentType = ComponentType.Renderer;
this._overrideUpdate = this.update !== prototype.update;

this._addResourceReferCount(this.shaderData, 1);
Expand Down
23 changes: 11 additions & 12 deletions packages/core/src/Transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BoolUpdateFlag } from "./BoolUpdateFlag";
import { Component } from "./Component";
import { Entity } from "./Entity";
import { assignmentClone, deepClone, ignoreClone } from "./clone/CloneManager";
import { Logger } from "./base";

/**
* Used to implement transformation related functions.
Expand All @@ -26,35 +25,35 @@ export class Transform extends Component {
@deepClone
protected _rotationQuaternion: Quaternion = new Quaternion();
@deepClone
private _scale: Vector3 = new Vector3(1, 1, 1);
protected _scale: Vector3 = new Vector3(1, 1, 1);
@assignmentClone
private _localUniformScaling: boolean = true;
protected _localUniformScaling: boolean = true;
@deepClone
protected _worldPosition: Vector3 = new Vector3();
@deepClone
protected _worldRotation: Vector3 = new Vector3();
@deepClone
private _worldRotationQuaternion: Quaternion = new Quaternion();
protected _worldRotationQuaternion: Quaternion = new Quaternion();
@assignmentClone
private _worldUniformScaling: boolean = true;
protected _worldUniformScaling: boolean = true;
@deepClone
private _lossyWorldScale: Vector3 = new Vector3(1, 1, 1);
protected _lossyWorldScale: Vector3 = new Vector3(1, 1, 1);
@deepClone
protected _localMatrix: Matrix = new Matrix();
@deepClone
protected _worldMatrix: Matrix = new Matrix();
@ignoreClone
private _worldForward: Vector3 = null;
protected _worldForward: Vector3 = null;
@ignoreClone
private _worldRight: Vector3 = null;
protected _worldRight: Vector3 = null;
@ignoreClone
private _worldUp: Vector3 = null;
protected _worldUp: Vector3 = null;

@ignoreClone
private _isParentDirty: boolean = true;
protected _isParentDirty: boolean = true;
@ignoreClone
private _parentTransformCache: Transform = null;
private _dirtyFlag: number = TransformModifyFlags.WmWpWeWqWs;
protected _parentTransformCache: Transform = null;
protected _dirtyFlag: number = TransformModifyFlags.WmWpWeWqWs;

/**
* Local position.
Expand Down
7 changes: 0 additions & 7 deletions packages/core/src/UpdateFlagManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ export class UpdateFlagManager {
Utils.removeFromArray(this._listeners, listener);
}

/**
* Remove all listeners.
*/
removeAllListeners(): void {
this._listeners.length = 0;
}

/**
* Dispatch a event.
* @param type - Event type, usually in the form of enumeration
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export { BackgroundMode } from "./enums/BackgroundMode";
export { BackgroundTextureFillMode } from "./enums/BackgroundTextureFillMode";
export { DepthTextureMode } from "./enums/DepthTextureMode";
export { FogMode } from "./enums/FogMode";
export { ComponentType } from "./enums/ComponentType";
export { CameraClearFlags } from "./enums/CameraClearFlags";
export { CameraType } from "./enums/CameraType";
export { CameraModifyFlags } from "./enums/CameraModifyFlags";
Expand Down
Loading

0 comments on commit 3929d73

Please sign in to comment.