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.

1.9 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import type { World } from '../../ecs/world';
import type { Entity } from '../../types';
import { UITooltip, type UITooltipData } from '../behavior/tooltip';
import { makeWidgetInteractable } from '../input/interactable';
import { spawnUIEntity, type UINodeInit } from '../core/compose';
import { px } from '../core/dimension';
import { themeColors } from '../theme/tokens';
import { markThemed } from '../theme/theme-style';

/** Attach to an existing control without changing its input configuration. @beta */
export function attachTooltip(world: World, target: Entity, options: Partial<UITooltipData> = {}): void {
    world.insert(target, UITooltip, { ...structuredClone(UITooltip._default), ...options });
}

/** Creation options for a tooltip example target. @beta */
export interface TooltipOptions {
    world: World;
    parent?: Entity;
    node?: UINodeInit;
    content?: string;
    settings?: Partial<UITooltipData>;
}
/** A tooltip target with explicit lifetime. @beta */
export interface TooltipHandle {
    readonly entity: Entity;
    dispose(): void;
}
/** Palette example target. The popup is generated by the behavior at runtime. @beta */
export function createTooltip(opts: TooltipOptions): TooltipHandle {
    const entity = spawnUIEntity({ world: opts.world, parent: opts.parent,
        node: opts.node ?? { width: px(140), height: px(36) }, visual: { color: themeColors().control },
        text: { content: 'Tooltip', color: themeColors().text } });
    markThemed(opts.world, entity, { visual: 'control', text: 'text' });
    makeWidgetInteractable(opts.world, entity);
    attachTooltip(opts.world, entity, { content: opts.content ?? 'More information about this control.', ...opts.settings });
    return { entity, dispose() { if (opts.world.valid(entity)) opts.world.despawn(entity); } };
}