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.

6.8 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    UIBindings.cpp
 * @brief   Implements the UI entry points (see UIBindings.hpp).
 * @details Moved out of WebSDKEntry.cpp, which is the emscripten ENTRY point:
 *          these implementations only ever needed the active context and the
 *          registry, so keeping them there made them web-only for no reason. As
 *          their own TU they compile for a native build too, and the generated
 *          QuickJS wrappers call the same code embind registers.
 */
#include "UIBindings.hpp"
#include "../renderer/plugins/UIElementPlugin.hpp"

#include "ActiveContext.hpp"
#include "../core/EstellaContext.hpp"
#include "../core/World.hpp"
#include "../ecs/GroupOpacityLookup.hpp"
#include "../ecs/Registry.hpp"
#include "../ui/UISystem.hpp"
#include "../resource/ResourceManager.hpp"
#include "../ui/UIRenderOrderSystem.hpp"
#include "../ecs/TransformSystem.hpp"
#include "../ecs/components/Transform.hpp"
#include "../ecs/components/UINode.hpp"
#include "../ecs/components/UIVisual.hpp"
#include "../ecs/FlatGeometry.hpp"
#include <cstring>

namespace esengine {

static EstellaContext& ctx() { return activeCtx(); }


void uiLayout_update(ecs::Registry& registry, f32 boxLeft, f32 boxBottom, f32 boxRight, f32 boxTop,
                     bool propertyDirty) {
    ctx().require<ecs::UISystem>().layoutUpdate(registry, boxLeft, boxBottom, boxRight, boxTop, propertyDirty);
}

void uiHitTest_update(ecs::Registry& registry,
                      f32 originX, f32 originY, f32 originZ,
                      f32 dirX, f32 dirY, f32 dirZ) {
    const ecs::PickRay ray{{originX, originY, originZ}, {dirX, dirY, dirZ}};
    ctx().require<ecs::UISystem>().hitTestUpdate(registry, ray,
                                                 ctx().tryGet<resource::ResourceManager>());
}

void uiHitTest_updateDomains(ecs::Registry& registry,
                             f32 worldOriginX, f32 worldOriginY, f32 worldOriginZ,
                             f32 worldDirX, f32 worldDirY, f32 worldDirZ,
                             f32 screenOriginX, f32 screenOriginY, f32 screenOriginZ,
                             f32 screenDirX, f32 screenDirY, f32 screenDirZ) {
    const ecs::PickRay world{{worldOriginX, worldOriginY, worldOriginZ},
                             {worldDirX, worldDirY, worldDirZ}};
    const ecs::PickRay screen{{screenOriginX, screenOriginY, screenOriginZ},
                              {screenDirX, screenDirY, screenDirZ}};
    ctx().require<ecs::UISystem>().hitTestUpdate(registry, world, screen,
                                                 ctx().tryGet<resource::ResourceManager>());
}

u32 uiHitTest_getHitEntity() {
    return ctx().require<ecs::UISystem>().getHitEntity();
}

u32 uiHitTest_pick(ecs::Registry& registry, f32 worldX, f32 worldY) {
    return ctx().require<ecs::UISystem>().pick(registry, worldX, worldY);
}

u32 uiHitTest_pickAll(ecs::Registry& registry, f32 worldX, f32 worldY) {
    return ctx().require<ecs::UISystem>().pickAll(registry, worldX, worldY);
}

u32 uiHitTest_pickResult(u32 index) {
    return ctx().require<ecs::UISystem>().pickResult(index);
}

// Resolved (Yoga-pass) pixel size of a UI node, for the editor's selection outline.
// The node's world box is this size, pivot-centered on its Transform.
f32 uiNode_computedWidth(ecs::Registry& r, u32 e) {
    auto* n = r.tryGet<ecs::UINode>(Entity::fromRaw(e));
    return n ? n->computed_size_.x : 0.0f;
}
f32 uiNode_computedHeight(ecs::Registry& r, u32 e) {
    auto* n = r.tryGet<ecs::UINode>(Entity::fromRaw(e));
    return n ? n->computed_size_.y : 0.0f;
}

// What the Canvases hold with no uiOrder of its own; the frame borrows it. One table: the order pass and every collect it serves run in turn.
static ecs::UITreeOrder g_uiTreeOrder;

const std::unordered_map<u32, i32>* uiTreeLayers() {
    return &g_uiTreeOrder.layers;
}

void uiRenderOrder_update(ecs::Registry& registry) {
    g_uiTreeOrder.base = UIElementPlugin::UI_BASE_LAYER;
    ecs::uiRenderOrderUpdate(registry, &g_uiTreeOrder);
}

// The SDF text path (TS) reads an entity's UI
// draw order so glyph quads interleave with UI quads. uiRenderOrderUpdate
// assigns uiOrder to every UIVisual in the UI tree (text nodes carry a
// visualType=None UIVisual purely to be ordered). -1 = not a UI node.
i32 ui_getRenderOrder(ecs::Registry& registry, u32 entity) {
    auto* ui = registry.tryGet<ecs::UIVisual>(Entity::fromRaw(entity));
    return ui ? ui->uiOrder : -1;
}

// Same walk, same reason: the text path submits its own draws, so it has to carry
// the owning Canvas' culling bit itself. 0 = not a UI node, drawn by every camera.
u32 ui_getCullBit(ecs::Registry& registry, u32 entity) {
    auto* ui = registry.tryGet<ecs::UIVisual>(Entity::fromRaw(entity));
    return ui ? ui->uiCullBit : 0u;
}

bool ui_isScreenDomain(ecs::Registry& registry, u32 entity) {
    auto* ui = ctx().tryGet<ecs::UISystem>();
    return ui && ui->isScreenDomain(registry, entity);
}

// UINode (CSS box) computed size — its internal computed_size_ is not
// embind-readable, so expose it for TS uiHelpers.
bool getUINodeHiddenInTree(ecs::Registry& registry, u32 entity) {
    auto* node = registry.tryGet<ecs::UINode>(Entity::fromRaw(entity));
    return node && node->hidden_in_tree_;
}

// Subtree opacity / pointer gate, resolved by the layout pass. Same reason as
// the hidden bit: computed state is not embind-readable, and the TS text
// renderer + input router need it to behave like the C++ visuals do.
f32 getUINodeAlphaInTree(ecs::Registry& registry, u32 entity) {
    auto* node = registry.tryGet<ecs::UINode>(Entity::fromRaw(entity));
    return (node ? node->alpha_in_tree_ : 1.0f) * ecs::GroupOpacityLookup(registry).of(Entity::fromRaw(entity));
}

f32 getUINodeComputedWidth(ecs::Registry& registry, u32 entity) {
    auto* node = registry.tryGet<ecs::UINode>(Entity::fromRaw(entity));
    if (!node) return 0.0f;
    return node->computed_size_.x;
}

f32 getUINodeComputedHeight(ecs::Registry& registry, u32 entity) {
    auto* node = registry.tryGet<ecs::UINode>(Entity::fromRaw(entity));
    if (!node) return 0.0f;
    return node->computed_size_.y;
}

bool transform_copyFlatRenderMatrix(ecs::Registry& registry, u32 entity, uintptr_t outputPtr) {
    glm::mat4 matrix;
    if (!outputPtr || !ecs::copyFlatRenderMatrix(registry, Entity::fromRaw(entity), matrix)) return false;
    std::memcpy(reinterpret_cast<void*>(outputPtr), glm::value_ptr(matrix), sizeof(glm::mat4));
    return true;
}

void transform_update(ecs::Registry& registry) {
    esengine::World world{registry, ctx().services(), 0.0f};
    if (auto* ts = ctx().tryGet<ecs::TransformSystem>()) {
        ts->update(world);
    } else {
        ecs::TransformSystem fallback;
        fallback.update(world);
    }
}

}  // namespace esengine