#pragma once #include #include #include #include namespace qjs { class Value; } namespace frostbite2D { /** * @brief Basic bridge object between QuickJS and engine Vec2. */ class JsVec2 { public: double x = 0.0; double y = 0.0; JsVec2() = default; JsVec2(double xValue, double yValue) : x(xValue), y(yValue) {} explicit JsVec2(const Vec2& value) : x(value.x), y(value.y) {} Vec2 toNative() const { return Vec2(static_cast(x), static_cast(y)); } }; /** * @brief Basic bridge base between QuickJS and engine Actor. * * This type only exposes the smallest stable surface that all script-side * Actor wrappers need, so engine bindings and Game-side bindings can share it. */ class JsActor { public: JsActor() : actor_(MakePtr()) {} explicit JsActor(Ptr actor) : actor_(std::move(actor)) { if (!actor_) { actor_ = MakePtr(); } } std::string GetName() const { return actor_->GetName(); } void SetName(const std::string& name) { actor_->SetName(name); } std::shared_ptr GetPosition() const { return std::make_shared(actor_->GetPosition()); } void SetPosition(const std::shared_ptr& position) { if (position) { actor_->SetPosition(position->toNative()); } } void SetPositionXY(double x, double y) { actor_->SetPosition(static_cast(x), static_cast(y)); } double GetRotation() const { return actor_->GetRotation(); } void SetRotation(double rotation) { actor_->SetRotation(static_cast(rotation)); } std::shared_ptr GetScale() const { return std::make_shared(actor_->GetScale()); } void SetScale(const std::shared_ptr& scale) { if (scale) { actor_->SetScale(scale->toNative()); } } void SetScaleValue(double scale) { actor_->SetScale(static_cast(scale)); } bool IsVisible() const { return actor_->IsVisible(); } void SetVisible(bool visible) { actor_->SetVisible(visible); } void AddChild(const std::shared_ptr& child) { if (!actor_ || !child) { return; } actor_->AddChild(child->native()); } void RemoveAllChildren() { if (actor_) { actor_->RemoveAllChildren(); } } std::shared_ptr GetSize() const { return std::make_shared(actor_->GetSize()); } void SetSize(const std::shared_ptr& size) { if (size) { actor_->SetSize(size->toNative()); } } void SetSizeWH(double width, double height) { actor_->SetSize(static_cast(width), static_cast(height)); } std::shared_ptr GetAnchor() const { return std::make_shared(actor_->GetAnchor()); } void SetAnchor(const std::shared_ptr& anchor) { if (anchor) { actor_->SetAnchor(anchor->toNative()); } } void SetAnchorXY(double x, double y) { actor_->SetAnchor(static_cast(x), static_cast(y)); } std::shared_ptr GetParent() const; std::shared_ptr GetTopLeftPosition() const; void SetTopLeftPosition(const std::shared_ptr& position); void SetTopLeftPositionXY(double x, double y); std::shared_ptr LocalToWorld(const std::shared_ptr& point) const; std::shared_ptr WorldToLocal(const std::shared_ptr& point) const; bool ContainsWorldPoint(double x, double y) const; bool IsEventReceiveEnabled() const { return actor_ && actor_->IsEventReceiveEnabled(); } void EnableEventReceive() { if (actor_) { actor_->EnableEventReceive(); } } void DisableEventReceive() { if (actor_) { actor_->DisableEventReceive(); } } uint32 AddEventListener(const std::string& type, std::function callback); bool RemoveEventListener(uint32 listenerId); void ClearEventListeners(); const Ptr& native() const { return actor_; } protected: Ptr actor_; }; } // namespace frostbite2D