Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix: simplify UI dropdown positioning and add color equality guards

esengine07d75705fde3
diff --git a/sdk/src/ui/DropdownPlugin.ts b/sdk/src/ui/DropdownPlugin.ts
index 10713b6d3..e5ccc8863 100644
--- a/sdk/src/ui/DropdownPlugin.ts
+++ b/sdk/src/ui/DropdownPlugin.ts
@@ -192,15 +192,12 @@ export class DropdownPlugin implements Plugin {
destroyOptions(state);
let ddWidth = 0;
- let ddHeight = 0;
if (world.has(entity, UIRect)) {
const ddRect = world.get(entity, UIRect) as UIRectData;
ddWidth = getEffectiveWidth(ddRect, entity) || ddRect.size.x;
- ddHeight = getEffectiveHeight(ddRect, entity) || ddRect.size.y;
} else if (world.has(entity, Sprite)) {
const ddSprite = world.get(entity, Sprite) as SpriteData;
ddWidth = ddSprite.size.x;
- ddHeight = ddSprite.size.y;
}
state.ddWidth = ddWidth;
const totalHeight = dropdown.options.length * DROPDOWN_ITEM_HEIGHT;
@@ -211,12 +208,7 @@ export class DropdownPlugin implements Plugin {
offsetMin: { x: 0, y: 0 },
offsetMax: { x: 0, y: 0 },
size: { x: ddWidth, y: totalHeight },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(listEntity, Transform, {
- position: { x: 0, y: -(ddHeight + totalHeight) / 2, z: 0 },
- rotation: { w: 1, x: 0, y: 0, z: 0 },
- scale: { x: 1, y: 1, z: 1 },
+ pivot: { x: 0.5, y: 1 },
});
world.insert(listEntity, UIRenderer, {
visualType: UIVisualType.SolidColor,
@@ -231,7 +223,6 @@ export class DropdownPlugin implements Plugin {
for (let i = 0; i < dropdown.options.length; i++) {
const optEntity = world.spawn();
- const optY = totalHeight / 2 - (i + 0.5) * DROPDOWN_ITEM_HEIGHT;
world.insert(optEntity, Name, { value: `Option_${i}` });
world.insert(optEntity, UIRect, {
anchorMin: { x: 0, y: 1 },
@@ -239,10 +230,10 @@ export class DropdownPlugin implements Plugin {
offsetMin: { x: 0, y: -i * DROPDOWN_ITEM_HEIGHT },
offsetMax: { x: 0, y: 0 },
size: { x: ddWidth, y: DROPDOWN_ITEM_HEIGHT },
- pivot: { x: 0.5, y: 0.5 },
+ pivot: { x: 0.5, y: 1 },
});
world.insert(optEntity, Transform, {
- position: { x: 0, y: optY, z: 0 },
+ position: { x: 0, y: 0, z: 0 },
rotation: { w: 1, x: 0, y: 0, z: 0 },
scale: { x: 1, y: 1, z: 1 },
});
diff --git a/sdk/src/ui/ImagePlugin.ts b/sdk/src/ui/ImagePlugin.ts
index 1d5617731..64d45bc69 100644
--- a/sdk/src/ui/ImagePlugin.ts
+++ b/sdk/src/ui/ImagePlugin.ts
@@ -1,5 +1,6 @@
import type { App, Plugin } from '../app';
import type { Entity } from '../types';
+import { INVALID_TEXTURE } from '../types';
import { registerComponent } from '../component';
import { defineSystem, Schedule } from '../system';
import { Image, ImageType, FillMethod, FillOrigin } from './Image';
@@ -97,7 +98,10 @@ export class ImagePlugin implements Plugin {
renderer.uvOffset = { x: 0, y: 0 };
renderer.uvScale = { x: 1, y: 1 };
- if (image.imageType === ImageType.Sliced) {
+ const hasTexture = image.texture !== INVALID_TEXTURE && image.texture !== 0;
+ if (!hasTexture) {
+ renderer.visualType = UIVisualType.SolidColor;
+ } else if (image.imageType === ImageType.Sliced) {
renderer.visualType = UIVisualType.NineSlice;
} else {
renderer.visualType = UIVisualType.Image;
diff --git a/sdk/src/ui/uiHelpers.ts b/sdk/src/ui/uiHelpers.ts
index bc1204991..9b867a857 100644
--- a/sdk/src/ui/uiHelpers.ts
+++ b/sdk/src/ui/uiHelpers.ts
@@ -1,5 +1,7 @@
import { Sprite, Parent, Transform } from '../component';
import type { ParentData, SpriteData, TransformData, AnyComponentDef } from '../component';
+import { Image } from './Image';
+import type { ImageData } from './Image';
import type { Entity, Color } from '../types';
import type { World } from '../world';
import { UIRect } from './UIRect';
@@ -380,27 +382,51 @@ export function withChildEntity(
}
}
+function colorEquals(a: Color, b: Color): boolean {
+ return a.r === b.r && a.g === b.g && a.b === b.b && a.a === b.a;
+}
+
export function setEntityColor(world: World, entity: Entity, color: Color): void {
- if (world.has(entity, UIRenderer)) {
- const r = world.get(entity, UIRenderer) as UIRendererData;
- r.color = color;
- world.insert(entity, UIRenderer, r);
+ if (world.has(entity, Image)) {
+ const img = world.get(entity, Image) as ImageData;
+ if (!colorEquals(img.color, color)) {
+ img.color = color;
+ world.insert(entity, Image, img);
+ }
} else if (world.has(entity, Sprite)) {
const s = world.get(entity, Sprite) as SpriteData;
- s.color = color;
- world.insert(entity, Sprite, s);
+ if (!colorEquals(s.color, color)) {
+ s.color = color;
+ world.insert(entity, Sprite, s);
+ }
+ } else if (world.has(entity, UIRenderer)) {
+ const r = world.get(entity, UIRenderer) as UIRendererData;
+ if (!colorEquals(r.color, color)) {
+ r.color = color;
+ world.insert(entity, UIRenderer, r);
+ }
}
}
export function setEntityEnabled(world: World, entity: Entity, enabled: boolean): void {
- if (world.has(entity, UIRenderer)) {
- const r = world.get(entity, UIRenderer) as UIRendererData;
- r.enabled = enabled;
- world.insert(entity, UIRenderer, r);
+ if (world.has(entity, Image)) {
+ const img = world.get(entity, Image) as ImageData;
+ if (img.enabled !== enabled) {
+ img.enabled = enabled;
+ world.insert(entity, Image, img);
+ }
} else if (world.has(entity, Sprite)) {
const s = world.get(entity, Sprite) as SpriteData;
- s.enabled = enabled;
- world.insert(entity, Sprite, s);
+ if (s.enabled !== enabled) {
+ s.enabled = enabled;
+ world.insert(entity, Sprite, s);
+ }
+ } else if (world.has(entity, UIRenderer)) {
+ const r = world.get(entity, UIRenderer) as UIRendererData;
+ if (r.enabled !== enabled) {
+ r.enabled = enabled;
+ world.insert(entity, UIRenderer, r);
+ }
}
}