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.

refactor(ui): delete legacy ScrollViewPlugin + ScrollView component (REARCH_GUI unification S4c)

esenginef90ec576c059
diff --git a/sdk/src/core-ui.ts b/sdk/src/core-ui.ts
index 5b779aa0e..a88395624 100644
--- a/sdk/src/core-ui.ts
+++ b/sdk/src/core-ui.ts
@@ -68,7 +68,6 @@ export {
FillOrigin,
Draggable,
DragState,
- ScrollView,
FillDirection,
Focusable,
FocusManager,
@@ -95,7 +94,6 @@ export {
type ImageData,
type DraggableData,
type DragStateData,
- type ScrollViewData,
type FocusableData,
type SafeAreaData,
UIThemeRes,
diff --git a/sdk/src/systemLabels.ts b/sdk/src/systemLabels.ts
index 3f8172097..b26f77599 100644
--- a/sdk/src/systemLabels.ts
+++ b/sdk/src/systemLabels.ts
@@ -13,7 +13,6 @@ export const PluginName = {
Text: 'text',
Image: 'image',
Focus: 'focus',
- ScrollView: 'scrollView',
Drag: 'drag',
SafeArea: 'safeArea',
TextInput: 'textInput',
@@ -24,7 +23,6 @@ export const SystemLabel = {
UILayoutLate: 'UILayoutLateSystem',
UIRenderOrder: 'UIRenderOrderSystem',
UIInteraction: 'UIInteractionSystem',
- ScrollView: 'ScrollViewSystem',
ListView: 'ListViewSystem',
Text: 'TextSystem',
Image: 'ImageSystem',
diff --git a/sdk/src/ui/ScrollView.ts b/sdk/src/ui/ScrollView.ts
deleted file mode 100644
index 64f4776bf..000000000
--- a/sdk/src/ui/ScrollView.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-// SPDX-License-Identifier: LicenseRef-PolyForm-Noncommercial-1.0.0
-// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
-import { defineComponent } from '../component';
-import type { Entity } from '../types';
-
-export interface ScrollViewData {
- contentEntity: Entity;
- horizontalEnabled: boolean;
- verticalEnabled: boolean;
- contentWidth: number;
- contentHeight: number;
- scrollX: number;
- scrollY: number;
- inertia: boolean;
- decelerationRate: number;
- elastic: boolean;
- wheelSensitivity: number;
-}
-
-export const ScrollView = defineComponent<ScrollViewData>('ScrollView', {
- contentEntity: 0 as Entity,
- horizontalEnabled: false,
- verticalEnabled: true,
- contentWidth: 0,
- contentHeight: 0,
- scrollX: 0,
- scrollY: 0,
- inertia: true,
- decelerationRate: 0.135,
- elastic: true,
- wheelSensitivity: 0.1,
-}, { entityFields: ['contentEntity'] });
diff --git a/sdk/src/ui/ScrollViewPlugin.ts b/sdk/src/ui/ScrollViewPlugin.ts
deleted file mode 100644
index edf69248a..000000000
--- a/sdk/src/ui/ScrollViewPlugin.ts
+++ /dev/null
@@ -1,322 +0,0 @@
-// SPDX-License-Identifier: LicenseRef-PolyForm-Noncommercial-1.0.0
-// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
-import type { App, Plugin } from '../app';
-import { registerComponent, Transform, Children, Sprite } from '../component';
-import type { TransformData, ChildrenData, SpriteData } from '../component';
-import { defineSystem, Schedule } from '../system';
-import { Res, Time } from '../resource';
-import type { TimeData } from '../resource';
-import { Input } from '../input';
-import type { InputState } from '../input';
-import type { Entity } from '../types';
-import { ScrollView } from './ScrollView';
-import type { ScrollViewData } from './ScrollView';
-import { UIRect } from './core/ui-rect';
-import type { UIRectData } from './core/ui-rect';
-import { Interactable } from './behavior/interactable';
-import { UIMask, MaskMode } from './core/ui-mask';
-import { UIInteraction } from './behavior/interactable';
-import type { UIInteractionData } from './behavior/interactable';
-import { UICameraInfo } from './UICameraInfo';
-import type { UICameraData } from './UICameraInfo';
-import { isEditor, isPlayMode } from '../env';
-import { getEffectiveWidth, getEffectiveHeight, ensureComponent, withChildEntity, EntityStateMap } from './uiHelpers';
-import {
- SCROLL_MAX_DT, SCROLL_VELOCITY_THRESHOLD,
- SCROLL_VELOCITY_LERP_SPEED, SCROLL_ELASTIC_SMOOTH_TIME,
- SCROLL_ELASTIC_SNAP_THRESHOLD,
- SCROLL_MAX_OVERSCROLL_RATIO, SCROLL_MAX_VELOCITY_RATIO,
- SCROLL_POSITION_EPSILON,
-} from './uiConstants';
-import { SystemLabel, PluginName } from '../systemLabels';
-
-function rubberBand(pos: number, min: number, max: number, viewSize: number): number {
- if (viewSize <= 0) return 1;
- if (pos < min) {
- return Math.max(0, 1 - (min - pos) / viewSize);
- } else if (pos > max) {
- return Math.max(0, 1 - (pos - max) / viewSize);
- }
- return 1;
-}
-
-function smoothDamp(
- current: number, target: number, currentVelocity: number,
- smoothTime: number, dt: number,
-): { value: number; velocity: number } {
- smoothTime = Math.max(0.0001, smoothTime);
- const omega = 2 / smoothTime;
- const x = omega * dt;
- const exp = 1 / (1 + x + 0.48 * x * x + 0.235 * x * x * x);
- const change = current - target;
- const temp = (currentVelocity + omega * change) * dt;
- const newVelocity = (currentVelocity - omega * temp) * exp;
- const newValue = target + (change + temp) * exp;
- if ((target - current > 0) === (newValue > target)) {
- return { value: target, velocity: 0 };
- }
- return { value: newValue, velocity: newVelocity };
-}
-
-interface ScrollState {
- isDragging: boolean;
- lastWorldX: number;
- lastWorldY: number;
- velocityX: number;
- velocityY: number;
- contentBaseX: number;
- contentBaseY: number;
- lastAppliedX: number;
- lastAppliedY: number;
-}
-
-export class ScrollViewPlugin implements Plugin {
- name = PluginName.ScrollView;
- dependencies = [PluginName.UILayout];
-
- private cleanup_: (() => void) | null = null;
-
- cleanup(): void {
- if (this.cleanup_) {
- this.cleanup_();
- this.cleanup_ = null;
- }
- }
-
- build(app: App): void {
- registerComponent('ScrollView', ScrollView);
-
- const world = app.world;
- const states = new EntityStateMap<ScrollState>();
- const worldMouse = { x: 0, y: 0 };
-
- app.addSystemToSchedule(Schedule.PostUpdate, defineSystem(
- [Res(Input), Res(UICameraInfo), Res(Time)],
- (input: InputState, camera: UICameraData, time: TimeData) => {
- const editorSceneView = isEditor() && !isPlayMode();
-
- const dt = Math.min(time.delta, SCROLL_MAX_DT);
-
- states.cleanup(world);
-
- if (camera.valid) {
- worldMouse.x = camera.worldMouseX;
- worldMouse.y = camera.worldMouseY;
- }
-
- const entities = world.getEntitiesWithComponents([ScrollView, UIRect]);
- for (const entity of entities) {
- ensureComponent(world, entity, UIMask, { enabled: true, mode: MaskMode.Scissor });
-
- const sv = world.get(entity, ScrollView) as ScrollViewData;
- const rect = world.get(entity, UIRect) as UIRectData;
-
- const state = states.ensureInit(entity, () => ({
- isDragging: false, lastWorldX: 0, lastWorldY: 0,
- velocityX: 0, velocityY: 0,
- contentBaseX: NaN, contentBaseY: NaN,
- lastAppliedX: NaN, lastAppliedY: NaN,
- }));
-
- const viewW = getEffectiveWidth(rect, entity);
- const viewH = getEffectiveHeight(rect, entity);
-
- let effectiveContentW = sv.contentWidth;
- let effectiveContentH = sv.contentHeight;
- if ((effectiveContentW === 0 || effectiveContentH === 0)
- && sv.contentEntity !== 0 && world.valid(sv.contentEntity)
- && world.has(sv.contentEntity, Children)) {
- const kids = (world.get(sv.contentEntity, Children) as ChildrenData).entities;
- let minX = 0, maxX = 0, minY = 0, maxY = 0;
- for (const child of kids) {
- if (!world.valid(child) || !world.has(child, Transform)) continue;
- const ct = world.get(child, Transform) as TransformData;
- let cw = 0, ch = 0, px = 0.5, py = 0.5;
- if (world.has(child, UIRect)) {
- const cr = world.get(child, UIRect) as UIRectData;
- cw = getEffectiveWidth(cr, child) || cr.size.x;
- ch = getEffectiveHeight(cr, child) || cr.size.y;
- px = cr.pivot.x;
- py = cr.pivot.y;
- } else if (world.has(child, Sprite)) {
- const sp = world.get(child, Sprite) as SpriteData;
- cw = sp.size.x;
- ch = sp.size.y;
- }
- const left = ct.position.x - cw * px;
- const right = ct.position.x + cw * (1 - px);
- const bottom = ct.position.y - ch * py;
- const top = ct.position.y + ch * (1 - py);
- if (left < minX) minX = left;
- if (right > maxX) maxX = right;
- if (bottom < minY) minY = bottom;
- if (top > maxY) maxY = top;
- }
- if (effectiveContentW === 0) effectiveContentW = maxX - minX;
- if (effectiveContentH === 0) effectiveContentH = maxY - minY;
- }
-
- const maxScrollX = Math.max(0, effectiveContentW - viewW);
- const maxScrollY = Math.max(0, effectiveContentH - viewH);
-
- // --- Input ---
- if (!editorSceneView && camera.valid) {
- ensureComponent(world, entity, Interactable, { enabled: true, blockRaycast: true });
-
- const interaction = world.has(entity, UIInteraction)
- ? world.get(entity, UIInteraction) as UIInteractionData
- : null;
-
- if (interaction?.justPressed) {
- state.isDragging = true;
- state.lastWorldX = worldMouse.x;
- state.lastWorldY = worldMouse.y;
- state.velocityX = 0;
- state.velocityY = 0;
- }
-
- if (state.isDragging && input.isMouseButtonDown(0)) {
- const dx = worldMouse.x - state.lastWorldX;
- const dy = worldMouse.y - state.lastWorldY;
- const lerpFactor = Math.min(1, dt * SCROLL_VELOCITY_LERP_SPEED);
-
- if (sv.horizontalEnabled) {
- const rbX = rubberBand(sv.scrollX, 0, maxScrollX, viewW);
- sv.scrollX -= dx * rbX;
- if (dx === 0) {
- state.velocityX = 0;
- } else {
- const instantVx = dt > 0 ? -dx * rbX / dt : 0;
- state.velocityX += (instantVx - state.velocityX) * lerpFactor;
- }
- }
- if (sv.verticalEnabled) {
- const rbY = rubberBand(sv.scrollY, 0, maxScrollY, viewH);
- sv.scrollY += dy * rbY;
- if (dy === 0) {
- state.velocityY = 0;
- } else {
- const instantVy = dt > 0 ? dy * rbY / dt : 0;
- state.velocityY += (instantVy - state.velocityY) * lerpFactor;
- }
- }
-
- state.lastWorldX = worldMouse.x;
- state.lastWorldY = worldMouse.y;
- }
-
- if (state.isDragging && input.isMouseButtonReleased(0)) {
- state.isDragging = false;
- const maxVelX = viewW * SCROLL_MAX_VELOCITY_RATIO;
- const maxVelY = viewH * SCROLL_MAX_VELOCITY_RATIO;
- state.velocityX = Math.max(-maxVelX, Math.min(state.velocityX, maxVelX));
- state.velocityY = Math.max(-maxVelY, Math.min(state.velocityY, maxVelY));
- }
-
- if (interaction?.hovered) {
- const scroll = input.getScrollDelta();
- if (sv.verticalEnabled && scroll.y !== 0) {
- const rbY = rubberBand(sv.scrollY, 0, maxScrollY, viewH);
- sv.scrollY += scroll.y * sv.wheelSensitivity * rbY;
- state.velocityY = 0;
- }
- if (sv.horizontalEnabled && scroll.x !== 0) {
- const rbX = rubberBand(sv.scrollX, 0, maxScrollX, viewW);
- sv.scrollX += scroll.x * sv.wheelSensitivity * rbX;
- state.velocityX = 0;
- }
- }
- }
-
- // --- Inertia & Elastic (after release) ---
- if (!state.isDragging && dt > 0) {
- const overX = sv.scrollX < 0 || sv.scrollX > maxScrollX;
- const overY = sv.scrollY < 0 || sv.scrollY > maxScrollY;
-
- if (sv.elastic && overX) {
- const targetX = sv.scrollX < 0 ? 0 : maxScrollX;
- const r = smoothDamp(sv.scrollX, targetX, state.velocityX, SCROLL_ELASTIC_SMOOTH_TIME, dt);
- sv.scrollX = r.value;
- state.velocityX = r.velocity;
- if (Math.abs(sv.scrollX - targetX) < SCROLL_ELASTIC_SNAP_THRESHOLD
- && Math.abs(state.velocityX) < SCROLL_VELOCITY_THRESHOLD) {
- sv.scrollX = targetX;
- state.velocityX = 0;
- }
- } else if (sv.inertia && sv.horizontalEnabled) {
- if (Math.abs(state.velocityX) > SCROLL_VELOCITY_THRESHOLD) {
- state.velocityX *= Math.pow(sv.decelerationRate, dt);
- sv.scrollX += state.velocityX * dt;
- } else {
- state.velocityX = 0;
- }
- }
-
- if (sv.elastic && overY) {
- const targetY = sv.scrollY < 0 ? 0 : maxScrollY;
- const r = smoothDamp(sv.scrollY, targetY, state.velocityY, SCROLL_ELASTIC_SMOOTH_TIME, dt);
- sv.scrollY = r.value;
- state.velocityY = r.velocity;
- if (Math.abs(sv.scrollY - targetY) < SCROLL_ELASTIC_SNAP_THRESHOLD
- && Math.abs(state.velocityY) < SCROLL_VELOCITY_THRESHOLD) {
- sv.scrollY = targetY;
- state.velocityY = 0;
- }
- } else if (sv.inertia && sv.verticalEnabled) {
- if (Math.abs(state.velocityY) > SCROLL_VELOCITY_THRESHOLD) {
- state.velocityY *= Math.pow(sv.decelerationRate, dt);
- sv.scrollY += state.velocityY * dt;
- } else {
- state.velocityY = 0;
- }
- }
-
- if (!sv.elastic) {
- const prevX = sv.scrollX, prevY = sv.scrollY;
- sv.scrollX = Math.max(0, Math.min(sv.scrollX, maxScrollX));
- sv.scrollY = Math.max(0, Math.min(sv.scrollY, maxScrollY));
- if (sv.scrollX !== prevX) state.velocityX = 0;
- if (sv.scrollY !== prevY) state.velocityY = 0;
- }
- }
-
- // Safety clamp overscroll
- if (sv.elastic) {
- const maxOverX = viewW * SCROLL_MAX_OVERSCROLL_RATIO;
- const maxOverY = viewH * SCROLL_MAX_OVERSCROLL_RATIO;
- sv.scrollX = Math.max(-maxOverX, Math.min(sv.scrollX, maxScrollX + maxOverX));
- sv.scrollY = Math.max(-maxOverY, Math.min(sv.scrollY, maxScrollY + maxOverY));
- }
-
- withChildEntity(world, sv.contentEntity, (contentEntity) => {
- if (!world.has(contentEntity, Transform)) return;
- const lt = world.get(contentEntity, Transform) as TransformData;
- const curX = lt.position.x;
- const curY = lt.position.y;
-
- if (isNaN(state.lastAppliedX) || Math.abs(curX - state.lastAppliedX) > SCROLL_POSITION_EPSILON) {
- state.contentBaseX = curX;
- }
- if (isNaN(state.lastAppliedY) || Math.abs(curY - state.lastAppliedY) > SCROLL_POSITION_EPSILON) {
- state.contentBaseY = curY;
- }
-
- lt.position.x = state.contentBaseX - sv.scrollX;
- lt.position.y = state.contentBaseY + sv.scrollY;
-
- state.lastAppliedX = lt.position.x;
- state.lastAppliedY = lt.position.y;
- world.insert(contentEntity, Transform, lt);
- });
- }
- },
- { name: 'ScrollViewSystem' }
- ), { runAfter: [SystemLabel.UILayoutLate], runBefore: [SystemLabel.UIRenderOrder] });
-
- this.cleanup_ = () => {
- states.clear();
- };
- }
-}
-
-export const scrollViewPlugin = new ScrollViewPlugin();
diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.ts
index 35c8b8083..e06cfb39b 100644
--- a/sdk/src/ui/index.ts
+++ b/sdk/src/ui/index.ts
@@ -307,13 +307,6 @@ export { ImagePlugin, imagePlugin } from './ImagePlugin';
export { DragPlugin, dragPlugin } from './DragPlugin';
-export {
- ScrollView,
- type ScrollViewData,
-} from './ScrollView';
-
-export { ScrollViewPlugin, scrollViewPlugin } from './ScrollViewPlugin';
-
export { FocusPlugin, focusPlugin } from './FocusPlugin';
export {
diff --git a/sdk/src/ui/layout/layout.ts b/sdk/src/ui/layout/layout.ts
index de111b387..2c3e7a2a5 100644
--- a/sdk/src/ui/layout/layout.ts
+++ b/sdk/src/ui/layout/layout.ts
@@ -88,7 +88,7 @@ export class UILayoutPlugin implements Plugin {
[],
() => { module.transform_update(registry); },
{ name: 'UITransformFinalSystem' }
- ), { runAfter: [SystemLabel.ScrollView, SystemLabel.ListView], runBefore: [SystemLabel.UIRenderOrder] });
+ ), { runAfter: [SystemLabel.ListView], runBefore: [SystemLabel.UIRenderOrder] });
}
}
diff --git a/sdk/src/ui/render/render-order.ts b/sdk/src/ui/render/render-order.ts
index 5cbab6b32..398d2179d 100644
--- a/sdk/src/ui/render/render-order.ts
+++ b/sdk/src/ui/render/render-order.ts
@@ -10,7 +10,7 @@ export class UIRenderOrderPlugin implements Plugin {
dependencies = [PluginName.UILayout];
after = [
PluginName.Text, PluginName.Image, PluginName.UIMask,
- PluginName.UIInteraction, PluginName.ScrollView,
+ PluginName.UIInteraction,
];
build(app: App): void {
diff --git a/sdk/src/uiPlugins.ts b/sdk/src/uiPlugins.ts
index 23dc6ed18..b78204743 100644
--- a/sdk/src/uiPlugins.ts
+++ b/sdk/src/uiPlugins.ts
@@ -8,7 +8,6 @@ import { uiLayoutPlugin } from './ui/UILayoutPlugin';
import { textInputPlugin } from './ui/TextInputPlugin';
import { imagePlugin } from './ui/ImagePlugin';
import { dragPlugin } from './ui/DragPlugin';
-import { scrollViewPlugin } from './ui/ScrollViewPlugin';
import { focusPlugin } from './ui/FocusPlugin';
import { safeAreaPlugin } from './ui/SafeAreaPlugin';
import { uiRenderOrderPlugin } from './ui/UIRenderOrderPlugin';
@@ -19,7 +18,7 @@ import type { Plugin } from './app';
export const uiPlugins: Plugin[] = [
textPlugin, uiMaskPlugin, uiLayoutPlugin,
imagePlugin,
- uiInteractionPlugin, uiBehaviorPlugin, dragPlugin, scrollViewPlugin,
+ uiInteractionPlugin, uiBehaviorPlugin, dragPlugin,
textInputPlugin,
focusPlugin, safeAreaPlugin,
uiRenderOrderPlugin,
diff --git a/sdk/src/webAppFactory.ts b/sdk/src/webAppFactory.ts
index 62a46ff4d..778970cb4 100644
--- a/sdk/src/webAppFactory.ts
+++ b/sdk/src/webAppFactory.ts
@@ -25,7 +25,6 @@ export { textInputPlugin, TextInputPlugin } from './ui/TextInputPlugin';
export { imagePlugin, ImagePlugin } from './ui/ImagePlugin';
export { dragPlugin, DragPlugin } from './ui/DragPlugin';
-export { scrollViewPlugin, ScrollViewPlugin } from './ui/ScrollViewPlugin';
export { focusPlugin, FocusPlugin } from './ui/FocusPlugin';
export { safeAreaPlugin, SafeAreaPlugin } from './ui/SafeAreaPlugin';
diff --git a/sdk/tests/scrollview.test.ts b/sdk/tests/scrollview.test.ts
deleted file mode 100644
index ecc835f8b..000000000
--- a/sdk/tests/scrollview.test.ts
+++ /dev/null
@@ -1,828 +0,0 @@
-// SPDX-License-Identifier: LicenseRef-PolyForm-Noncommercial-1.0.0
-// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
-import { describe, it, expect, beforeAll } from 'vitest';
-import { App } from '../src/app';
-import { Transform, Sprite, Canvas } from '../src/component';
-import type { TransformData } from '../src/component';
-import { UIRect } from '../src/ui/core/ui-rect';
-import { UICameraInfo } from '../src/ui/UICameraInfo';
-import { Input, InputState } from '../src/input';
-import { UIEvents, UIEventQueue } from '../src/ui/core/events';
-import { uiLayoutPlugin } from '../src/ui/UILayoutPlugin';
-import { uiRenderOrderPlugin } from '../src/ui/UIRenderOrderPlugin';
-import { scrollViewPlugin } from '../src/ui/ScrollViewPlugin';
-import { uiInteractionPlugin } from '../src/ui/UIInteractionPlugin';
-import { ScrollView } from '../src/ui/ScrollView';
-import type { ScrollViewData } from '../src/ui/ScrollView';
-import { UIInteraction } from '../src/ui/behavior/interactable';
-import { Interactable } from '../src/ui/behavior/interactable';
-import type { ESEngineModule, CppRegistry } from '../src/wasm';
-import { loadWasmModule, HAS_WASM } from './helpers/loadWasm';
-
-describe.skipIf(!HAS_WASM)('ScrollView (WASM)', () => {
- let module: ESEngineModule;
-
- beforeAll(async () => {
- module = await loadWasmModule();
- });
-
- function createApp() {
- const app = App.new();
- const registry = new module.Registry() as unknown as CppRegistry;
- app.connectCpp(registry, module);
-
- const vp = new Float32Array(16);
- vp[0] = 1 / 400;
- vp[5] = 1 / 300;
- vp[10] = 1;
- vp[15] = 1;
- app.insertResource(UICameraInfo, {
- viewProjection: vp,
- vpX: 0, vpY: 0, vpW: 800, vpH: 600,
- screenW: 800, screenH: 600,
- worldLeft: -400, worldBottom: -300, worldRight: 400, worldTop: 300,
- worldMouseX: 0, worldMouseY: 0,
- valid: true,
- });
- app.insertResource(Input, new InputState());
- app.insertResource(UIEvents, new UIEventQueue());
-
- app.addPlugin(uiLayoutPlugin);
- app.addPlugin(uiRenderOrderPlugin);
- app.addPlugin(uiInteractionPlugin);
- app.addPlugin(scrollViewPlugin);
-
- return { app, registry };
- }
-
- function disposeApp(app: App, registry: CppRegistry): void {
- const world = app.world;
- for (const e of world.getAllEntities()) {
- try { world.despawn(e); } catch (_) {}
- }
- world.disconnectCpp();
- (registry as any).delete();
- }
-
- function createScrollView(app: App, contentHeight: number) {
- const world = app.world;
-
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(root, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
-
- const scrollEntity = world.spawn();
- world.setParent(scrollEntity, root);
- world.insert(scrollEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(scrollEntity, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 }, anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 300, y: 200 }, pivot: { x: 0.5, y: 0.5 },
- });
-
- const contentEntity = world.spawn();
- world.setParent(contentEntity, scrollEntity);
- world.insert(contentEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(contentEntity, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
- });
-
- world.insert(scrollEntity, ScrollView, {
- contentEntity,
- horizontalEnabled: false,
- verticalEnabled: true,
- contentWidth: 0,
- contentHeight,
- scrollX: 0,
- scrollY: 0,
- inertia: true,
- decelerationRate: 0.135,
- });
-
- return { root, scrollEntity, contentEntity };
- }
-
- it('scrollY clamps between 0 and maxScrollY when elastic=false', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 600);
- const world = app.world;
-
- const sv0 = world.get(scrollEntity, ScrollView) as ScrollViewData;
- sv0.elastic = false;
- world.insert(scrollEntity, ScrollView, sv0);
-
- await app.tick(1 / 60);
-
- const sv = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(sv.scrollY).toBe(0);
-
- sv.scrollY = 500;
- world.insert(scrollEntity, ScrollView, sv);
- await app.tick(1 / 60);
-
- const svAfter = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(svAfter.scrollY).toBeLessThanOrEqual(400);
- expect(svAfter.scrollY).toBeGreaterThanOrEqual(0);
-
- disposeApp(app, registry);
- });
-
- it('contentHeight=0 prevents scrolling when elastic=false', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 0);
- const world = app.world;
-
- const sv0 = world.get(scrollEntity, ScrollView) as ScrollViewData;
- sv0.elastic = false;
- world.insert(scrollEntity, ScrollView, sv0);
-
- await app.tick(1 / 60);
-
- const sv = world.get(scrollEntity, ScrollView) as ScrollViewData;
- sv.scrollY = 100;
- world.insert(scrollEntity, ScrollView, sv);
- await app.tick(1 / 60);
-
- const svAfter = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(svAfter.scrollY).toBe(0);
-
- disposeApp(app, registry);
- });
-
- it('content entity position updates with scrollY', async () => {
- const { app, registry } = createApp();
- const { scrollEntity, contentEntity } = createScrollView(app, 600);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- const sv = world.get(scrollEntity, ScrollView) as ScrollViewData;
- sv.scrollY = 200;
- world.insert(scrollEntity, ScrollView, sv);
- await app.tick(1 / 60);
-
- const contentTransform = world.get(contentEntity, Transform) as TransformData;
- expect(contentTransform.position.y).toBe(200);
-
- disposeApp(app, registry);
- });
-
- it('wheel scroll down (positive deltaY) should increase scrollY', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 600);
- const world = app.world;
-
- // Tick once to init layout
- await app.tick(1 / 60);
-
- // Manually set UIInteraction.hovered and inject scroll delta
- // to test scroll direction without full hit test
- world.insert(scrollEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
- world.insert(scrollEntity, UIInteraction, { hovered: true, pressed: false, justPressed: false, justReleased: false });
-
- const inputState = app.getResource(Input);
- // Simulate wheel scroll down: browser gives positive deltaY
- inputState.scrollDeltaY = 120;
-
- await app.tick(1 / 60);
-
- const sv = world.get(scrollEntity, ScrollView) as ScrollViewData;
- // Scrolling down → scrollY should increase (content moves up, revealing below)
- expect(sv.scrollY).toBeGreaterThan(0);
-
- disposeApp(app, registry);
- });
-
- it('rubber-band limits over-scroll during wheel input', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 600);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- world.insert(scrollEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
- world.insert(scrollEntity, UIInteraction, { hovered: true, pressed: false, justPressed: false, justReleased: false });
-
- const inputState = app.getResource(Input);
-
- // Scroll far past top (negative) with many wheel events
- for (let i = 0; i < 100; i++) {
- inputState.scrollDeltaY = -120;
- await app.tick(1 / 60);
- }
-
- const sv1 = world.get(scrollEntity, ScrollView) as ScrollViewData;
- // viewH=200: rubber-band should limit over-scroll to roughly viewH
- expect(sv1.scrollY).toBeLessThan(0);
- expect(sv1.scrollY).toBeGreaterThan(-200);
-
- disposeApp(app, registry);
- });
-
- it('elastic bounces back during continuous small wheel deltas (momentum scroll)', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 600);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- world.insert(scrollEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
- world.insert(scrollEntity, UIInteraction, { hovered: true, pressed: false, justPressed: false, justReleased: false });
-
- const inputState = app.getResource(Input);
-
- // Initial strong scroll past top
- inputState.scrollDeltaY = -200;
- await app.tick(1 / 60);
-
- const sv1 = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(sv1.scrollY).toBeLessThan(0);
-
- // Simulate OS momentum: decaying small scroll deltas (never exactly 0)
- for (let i = 0; i < 60; i++) {
- inputState.scrollDeltaY = -1;
- await app.tick(1 / 60);
- }
-
- const svFinal = world.get(scrollEntity, ScrollView) as ScrollViewData;
- // Elastic should have pulled scrollY back toward 0 despite continuous small deltas
- expect(Math.abs(svFinal.scrollY)).toBeLessThan(10);
-
- disposeApp(app, registry);
- });
-
- it('elastic bounces back even while hover remains active (no more wheel events)', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 600);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- world.insert(scrollEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
- world.insert(scrollEntity, UIInteraction, { hovered: true, pressed: false, justPressed: false, justReleased: false });
-
- const inputState = app.getResource(Input);
- inputState.scrollDeltaY = -120;
- await app.tick(1 / 60);
-
- const sv1 = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(sv1.scrollY).toBeLessThan(0);
-
- // Stop scrolling but keep hovered=true (cursor still over ScrollView)
- inputState.scrollDeltaY = 0;
- for (let i = 0; i < 60; i++) {
- await app.tick(1 / 60);
- }
- const svFinal = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(Math.abs(svFinal.scrollY)).toBeLessThan(1);
-
- disposeApp(app, registry);
- });
-
- it('elastic: over-scroll past top should NOT instantly clamp, then spring back', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 600);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- // Set scrollY to negative (over-scrolled past top)
- const sv = world.get(scrollEntity, ScrollView) as ScrollViewData;
- sv.scrollY = -50;
- world.insert(scrollEntity, ScrollView, sv);
-
- // After one tick, elastic spring should NOT hard-clamp to 0
- // It should be between -50 and 0 (partially pulled back)
- await app.tick(1 / 60);
- const sv1 = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(sv1.scrollY).toBeGreaterThan(-50);
- expect(sv1.scrollY).toBeLessThan(0); // NOT instantly clamped to 0
-
- // After many ticks, should converge close to 0
- for (let i = 0; i < 120; i++) {
- await app.tick(1 / 60);
- }
- const svFinal = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(Math.abs(svFinal.scrollY)).toBeLessThan(1);
-
- disposeApp(app, registry);
- });
-
- it('wheel scroll UP past top should bounce back to 0', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 600);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- world.insert(scrollEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
- world.insert(scrollEntity, UIInteraction, { hovered: true, pressed: false, justPressed: false, justReleased: false });
-
- const inputState = app.getResource(Input);
- inputState.scrollDeltaY = -120;
-
- await app.tick(1 / 60);
-
- const sv1 = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(sv1.scrollY).toBeLessThan(0);
-
- inputState.scrollDeltaY = 0;
-
- for (let i = 0; i < 120; i++) {
- world.insert(scrollEntity, UIInteraction, { hovered: false, pressed: false, justPressed: false, justReleased: false });
- await app.tick(1 / 60);
- }
- const svFinal = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(Math.abs(svFinal.scrollY)).toBeLessThan(1);
-
- disposeApp(app, registry);
- });
-
- it('wheel scroll DOWN past bottom should bounce back to maxScrollY', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 600);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- world.insert(scrollEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
- world.insert(scrollEntity, UIInteraction, { hovered: true, pressed: false, justPressed: false, justReleased: false });
-
- const inputState = app.getResource(Input);
- for (let i = 0; i < 50; i++) {
- inputState.scrollDeltaY = 120;
- await app.tick(1 / 60);
- }
-
- const svMid = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(svMid.scrollY).toBeGreaterThan(400);
-
- inputState.scrollDeltaY = 0;
- for (let i = 0; i < 120; i++) {
- world.insert(scrollEntity, UIInteraction, { hovered: false, pressed: false, justPressed: false, justReleased: false });
- await app.tick(1 / 60);
- }
- const svFinal = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(Math.abs(svFinal.scrollY - 400)).toBeLessThan(1);
-
- disposeApp(app, registry);
- });
-
- it('elastic: over-scroll past bottom should NOT instantly clamp, then spring back', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 600);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- // maxScrollY = 600 - 200 = 400; set past max
- const sv = world.get(scrollEntity, ScrollView) as ScrollViewData;
- sv.scrollY = 450;
- world.insert(scrollEntity, ScrollView, sv);
-
- await app.tick(1 / 60);
- const sv1 = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(sv1.scrollY).toBeLessThan(450);
- expect(sv1.scrollY).toBeGreaterThan(400); // NOT instantly clamped to 400
-
- for (let i = 0; i < 120; i++) {
- await app.tick(1 / 60);
- }
- const svFinal = world.get(scrollEntity, ScrollView) as ScrollViewData;
- expect(Math.abs(svFinal.scrollY - 400)).toBeLessThan(1);
-
- disposeApp(app, registry);
- });
-
- it('scroll offset should add to UIRect base position, not replace it', async () => {
- const { app, registry } = createApp();
- const world = app.world;
-
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(root, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
-
- const scrollEntity = world.spawn();
- world.setParent(scrollEntity, root);
- world.insert(scrollEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(scrollEntity, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 }, anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 300, y: 200 }, pivot: { x: 0.5, y: 0.5 },
- });
-
- const contentEntity = world.spawn();
- world.setParent(contentEntity, scrollEntity);
- world.insert(contentEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(contentEntity, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 0, y: 0 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 300, y: 600 }, pivot: { x: 0, y: 0 },
- });
-
- // Tick WITHOUT ScrollView to get the UIRect-computed base position
- await app.tick(1 / 60);
- const baseTransform = world.get(contentEntity, Transform) as TransformData;
- const baseY = baseTransform.position.y;
- const baseX = baseTransform.position.x;
-
- // Now add ScrollView with scrollY=0
- world.insert(scrollEntity, ScrollView, {
- contentEntity,
- horizontalEnabled: false,
- verticalEnabled: true,
- contentWidth: 0,
- contentHeight: 600,
- scrollX: 0,
- scrollY: 0,
- inertia: true,
- decelerationRate: 0.135,
- });
-
- await app.tick(1 / 60);
- const afterScrollView = world.get(contentEntity, Transform) as TransformData;
- // With scrollY=0, content should remain at its UIRect base position
- expect(afterScrollView.position.y).toBeCloseTo(baseY, 0);
- expect(afterScrollView.position.x).toBeCloseTo(baseX, 0);
-
- // Now scroll: position should be base + scrollOffset
- const sv = world.get(scrollEntity, ScrollView) as ScrollViewData;
- sv.scrollY = 100;
- world.insert(scrollEntity, ScrollView, sv);
- await app.tick(1 / 60);
- const scrolledTransform = world.get(contentEntity, Transform) as TransformData;
- expect(scrolledTransform.position.y).toBeCloseTo(baseY + 100, 0);
-
- disposeApp(app, registry);
- });
-
- it('sustained large scroll deltas should not push content entirely out of viewport', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 600);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- world.insert(scrollEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
- world.insert(scrollEntity, UIInteraction, { hovered: true, pressed: false, justPressed: false, justReleased: false });
-
- const inputState = app.getResource(Input);
-
- // Simulate sustained trackpad momentum: large deltas across multiple frames
- // maxScrollY = 600 - 200 = 400; viewH = 200
- for (let i = 0; i < 10; i++) {
- inputState.scrollDeltaY = 3000;
- await app.tick(1 / 60);
- }
-
- const sv = world.get(scrollEntity, ScrollView) as ScrollViewData;
- // maxScrollY=400, viewH=200; over-scroll should be capped to half the viewport
- // so content always remains substantially visible
- expect(sv.scrollY).toBeLessThanOrEqual(400 + 200 * 0.5);
- expect(sv.scrollY).toBeGreaterThanOrEqual(-200 * 0.5);
-
- disposeApp(app, registry);
- });
-
- it('content position should not accumulate across frames (no UIRect on content)', async () => {
- const { app, registry } = createApp();
- const world = app.world;
-
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(root, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
-
- const scrollEntity = world.spawn();
- world.setParent(scrollEntity, root);
- world.insert(scrollEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(scrollEntity, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 }, anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 300, y: 200 }, pivot: { x: 0.5, y: 0.5 },
- });
-
- // Content entity WITHOUT UIRect — only Transform
- const contentEntity = world.spawn();
- world.setParent(contentEntity, scrollEntity);
- world.insert(contentEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
-
- world.insert(scrollEntity, ScrollView, {
- contentEntity,
- horizontalEnabled: false,
- verticalEnabled: true,
- contentWidth: 0,
- contentHeight: 600,
- scrollX: 0,
- scrollY: 0,
- inertia: true,
- decelerationRate: 0.135,
- elastic: false,
- });
-
- // Set scrollY = 100 and tick multiple frames
- const sv0 = world.get(scrollEntity, ScrollView) as ScrollViewData;
- sv0.scrollY = 100;
- world.insert(scrollEntity, ScrollView, sv0);
-
- // Tick 3 times with constant scrollY — position should stay the same
- await app.tick(1 / 60);
- const pos1 = (world.get(contentEntity, Transform) as TransformData).position.y;
-
- await app.tick(1 / 60);
- const pos2 = (world.get(contentEntity, Transform) as TransformData).position.y;
-
- await app.tick(1 / 60);
- const pos3 = (world.get(contentEntity, Transform) as TransformData).position.y;
-
- // All 3 frames should produce the same position (no accumulation)
- expect(pos2).toBeCloseTo(pos1, 1);
- expect(pos3).toBeCloseTo(pos1, 1);
-
- disposeApp(app, registry);
- });
-
- it('auto contentHeight from children when contentHeight=0', async () => {
- const { app, registry } = createApp();
- const world = app.world;
-
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(root, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
-
- const scrollEntity = world.spawn();
- world.setParent(scrollEntity, root);
- world.insert(scrollEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(scrollEntity, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 }, anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 300, y: 200 }, pivot: { x: 0.5, y: 0.5 },
- });
-
- const contentEntity = world.spawn();
- world.setParent(contentEntity, scrollEntity);
- world.insert(contentEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
-
- // Tick once so layout is established before adding children
- await app.tick(1 / 60);
-
- // Add 5 child items WITHOUT UIRect (using Sprite for size), manually positioned
- // Children of contentEntity: no UIRect means C++ layout won't override position
- for (let i = 0; i < 5; i++) {
- const child = world.spawn();
- world.setParent(child, contentEntity);
- world.insert(child, Transform, {
- position: { x: 0, y: -i * 100, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(child, Sprite, {
- texture: 0, color: { r: 1, g: 1, b: 1, a: 1 },
- size: { x: 300, y: 100 },
- uvOffset: { x: 0, y: 0 }, uvScale: { x: 1, y: 1 },
- layer: 0, flipX: false, flipY: false, material: 0,
- });
- }
-
- // contentHeight=0 → auto-compute from children
- world.insert(scrollEntity, ScrollView, {
- contentEntity,
- horizontalEnabled: false,
- verticalEnabled: true,
- contentWidth: 0,
- contentHeight: 0,
- scrollX: 0,
- scrollY: 0,
- inertia: true,
- decelerationRate: 0.135,
- elastic: false,
- });
-
- // Set scrollY to large value; clamp should use auto-computed contentHeight
- const sv = world.get(scrollEntity, ScrollView) as ScrollViewData;
- sv.scrollY = 9999;
- world.insert(scrollEntity, ScrollView, sv);
- await app.tick(1 / 60);
-
- const svAfter = world.get(scrollEntity, ScrollView) as ScrollViewData;
- // Children span from y=50 (top of first) to y=-450 (bottom of last, pivot center)
- // total height = 500; viewH = 200; maxScroll = 300
- expect(svAfter.scrollY).toBeGreaterThan(0);
- expect(svAfter.scrollY).toBeLessThanOrEqual(350);
-
- disposeApp(app, registry);
- });
-
- it('drag should produce 1:1 movement between mouse delta and content displacement', async () => {
- const { app, registry } = createApp();
- const { scrollEntity, contentEntity } = createScrollView(app, 600);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- const inputState = app.getResource(Input);
- world.insert(scrollEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
-
- const initialPos = (world.get(contentEntity, Transform) as TransformData).position.y;
- const cam = app.getResource(UICameraInfo);
-
- // Frame 1: Press at mouseY=300 → worldMouseY = 300-300 = 0
- inputState.mouseX = 400;
- inputState.mouseY = 300;
- inputState.mouseButtons.add(0);
- inputState.mouseButtonsPressed.add(0);
- world.insert(scrollEntity, UIInteraction, {
- hovered: true, pressed: true, justPressed: true, justReleased: false,
- });
- await app.tick(1 / 60);
-
- const posAfterPress = (world.get(contentEntity, Transform) as TransformData).position.y;
- console.log('=== Drag tracking ===');
- console.log(`initialPos=${initialPos}, posAfterPress=${posAfterPress}, worldMouseY=${cam.worldMouseY}`);
- expect(posAfterPress).toBeCloseTo(initialPos, 0);
-
- // Frame 2: Drag up 50px → mouseY=250 → worldMouseY=50
- inputState.mouseButtonsPressed.clear();
- inputState.mouseY = 250;
- world.insert(scrollEntity, UIInteraction, {
- hovered: true, pressed: true, justPressed: false, justReleased: false,
- });
- await app.tick(1 / 60);
-
- const posAfterDrag50 = (world.get(contentEntity, Transform) as TransformData).position.y;
- const svDrag50 = world.get(scrollEntity, ScrollView) as ScrollViewData;
- console.log(`After 50px drag: worldMouseY=${cam.worldMouseY}, scrollY=${svDrag50.scrollY}, contentY=${posAfterDrag50}`);
- // 50px mouse movement → 50 units of scroll → 50 units of content movement
- expect(svDrag50.scrollY).toBeCloseTo(50, 0);
- expect(posAfterDrag50 - initialPos).toBeCloseTo(50, 0);
-
- // Frame 3: Drag up another 30px → mouseY=220 → worldMouseY=80
- inputState.mouseY = 220;
- world.insert(scrollEntity, UIInteraction, {
- hovered: true, pressed: true, justPressed: false, justReleased: false,
- });
- await app.tick(1 / 60);
-
- const posAfterDrag80 = (world.get(contentEntity, Transform) as TransformData).position.y;
- const svDrag80 = world.get(scrollEntity, ScrollView) as ScrollViewData;
- console.log(`After 80px total: scrollY=${svDrag80.scrollY}, contentY=${posAfterDrag80}, delta from initial=${posAfterDrag80 - initialPos}`);
- expect(svDrag80.scrollY).toBeCloseTo(80, 0);
- expect(posAfterDrag80 - initialPos).toBeCloseTo(80, 0);
-
- // Frame 4: Mouse stops (same position), still held
- world.insert(scrollEntity, UIInteraction, {
- hovered: true, pressed: true, justPressed: false, justReleased: false,
- });
- await app.tick(1 / 60);
-
- const posAfterStop = (world.get(contentEntity, Transform) as TransformData).position.y;
- console.log(`After stop: contentY=${posAfterStop}, drift=${posAfterStop - posAfterDrag80}`);
- expect(posAfterStop).toBeCloseTo(posAfterDrag80, 1);
-
- // Frame 5: Release
- inputState.mouseButtons.delete(0);
- inputState.mouseButtonsReleased.add(0);
- world.insert(scrollEntity, UIInteraction, {
- hovered: true, pressed: false, justPressed: false, justReleased: true,
- });
- await app.tick(1 / 60);
-
- // Frame 6-7: After release with velocity=0, content should stay
- inputState.mouseButtonsReleased.clear();
- for (let i = 0; i < 5; i++) {
- world.insert(scrollEntity, UIInteraction, {
- hovered: false, pressed: false, justPressed: false, justReleased: false,
- });
- await app.tick(1 / 60);
- }
-
- const posAfterRelease = (world.get(contentEntity, Transform) as TransformData).position.y;
- console.log(`After release+5 frames: contentY=${posAfterRelease}, drift from stop=${posAfterRelease - posAfterStop}`);
- expect(posAfterRelease).toBeCloseTo(posAfterDrag80, 1);
-
- disposeApp(app, registry);
- });
-
- it('inertia after fast swipe should not exceed a few viewports of travel', async () => {
- const { app, registry } = createApp();
- const { scrollEntity } = createScrollView(app, 5000);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- world.insert(scrollEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
-
- const inputState = app.getResource(Input);
-
- // worldMouseY = 300 - mouseY for this ortho setup
- // Frame 1: justPressed, worldMouseY=0 → mouseY=300
- inputState.mouseX = 400;
- inputState.mouseY = 300;
- inputState.mouseButtons.add(0);
- inputState.mouseButtonsPressed.add(0);
- world.insert(scrollEntity, UIInteraction, { hovered: true, pressed: true, justPressed: true, justReleased: false });
- await app.tick(1 / 60);
-
- // Frames 2-6: fast upward drag, worldMouseY increases by 100/frame
- // worldMouseY = i*100 → mouseY = 300 - i*100
- inputState.mouseButtonsPressed.clear();
- for (let i = 1; i <= 5; i++) {
- inputState.mouseY = 300 - i * 100;
- world.insert(scrollEntity, UIInteraction, { hovered: true, pressed: true, justPressed: false, justReleased: false });
- await app.tick(1 / 60);
- }
-
- // Release
- inputState.mouseButtons.delete(0);
- inputState.mouseButtonsReleased.add(0);
- world.insert(scrollEntity, UIInteraction, { hovered: true, pressed: false, justPressed: false, justReleased: true });
- await app.tick(1 / 60);
-
- const svAtRelease = world.get(scrollEntity, ScrollView) as ScrollViewData;
- const scrollAtRelease = svAtRelease.scrollY;
-
- // Let inertia run for 120 frames (2 seconds)
- inputState.mouseButtonsReleased.clear();
- for (let i = 0; i < 120; i++) {
- world.insert(scrollEntity, UIInteraction, { hovered: false, pressed: false, justPressed: false, justReleased: false });
- await app.tick(1 / 60);
- }
-
- const svAfter = world.get(scrollEntity, ScrollView) as ScrollViewData;
- const inertiaTravel = Math.abs(svAfter.scrollY - scrollAtRelease);
-
- // viewH=200; inertia travel should be bounded to ~3x viewport at most
- expect(inertiaTravel).toBeLessThan(200 * 3);
-
- disposeApp(app, registry);
- });
-});
diff --git a/sdk/tests/system-ordering.test.ts b/sdk/tests/system-ordering.test.ts
index 8f0d87711..dfa57147f 100644
--- a/sdk/tests/system-ordering.test.ts
+++ b/sdk/tests/system-ordering.test.ts
@@ -176,7 +176,7 @@ describe('System Dependency Ordering', () => {
runBefore: ['UITransformFinalSystem'],
});
app.addSystemToSchedule(Schedule.PostUpdate, transformFinal, {
- runAfter: ['ScrollViewSystem', 'ListViewSystem'],
+ runAfter: ['ListViewSystem'],
runBefore: ['UIRenderOrderSystem'],
});