载入中...
搜索中...
未找到
DamageNumbersBindings.cpp
浏览该文件的文档.
2
6#include "graphics/Font.h"
7#include "graphics/Graphics.h"
8
9#include <functional>
10#include <memory>
11#include <simplesquirrel/simplesquirrel.hpp>
12
13namespace eve::graphics {
14namespace {
15Result<DamageNumberKind> parseKind(const std::string& kind) {
21 Diagnostic::error(DiagnosticCode::InvalidArgument, "Unknown damage-number kind", "kind"));
22}
24 switch (result) {
25 case DamageNumberAdmission::Spawned: return Value("spawned");
26 case DamageNumberAdmission::Merged: return Value("merged");
27 case DamageNumberAdmission::ReplacedOldest: return Value("replacedOldest");
28 }
29 return Value("spawned");
30}
31} // namespace
32
33void exposeFontAndDamageNumberBindings(ssq::Table& table, ssq::Class& graphicsClass) {
34#ifndef EVENGINE_WEBGPU
35 auto fontCls =
36 table.addClass<Font>("GraphicsFont", std::function<Font*()>([]() -> Font* { return nullptr; }), true);
37 fontCls.addFunc("getHeight", &Font::getHeight);
38 fontCls.addFunc("getAscent", &Font::getAscent);
39 fontCls.addFunc("getBaseline", &Font::getBaseline);
40 fontCls.addFunc("getWidth", &Font::getWidth);
41 fontCls.addFunc("hasGlyph", &Font::hasGlyph);
42#endif
43 const auto vm = table.getHandle();
44 auto style = table.addClass("DamageNumberStyle", ssq::Class::Ctor<DamageNumberStyle()>());
45 style.addVar("lifetime", &DamageNumberStyle::lifetime);
46 style.addVar("velocityY", &DamageNumberStyle::velocityY);
47 style.addVar("gravity", &DamageNumberStyle::gravity);
48 style.addVar("scale", &DamageNumberStyle::scale);
49 style.addVar("pop", &DamageNumberStyle::pop);
50 style.addVar("shake", &DamageNumberStyle::shake);
51 style.addVar("r", &DamageNumberStyle::r);
52 style.addVar("g", &DamageNumberStyle::g);
53 style.addVar("b", &DamageNumberStyle::b);
54 style.addVar("outline", &DamageNumberStyle::outline);
55 style.addVar("fadeIn", &DamageNumberStyle::fadeIn);
56 style.addVar("fadeOut", &DamageNumberStyle::fadeOut);
57 style.addVar("mergeWindow", &DamageNumberStyle::mergeWindow);
58 style.addVar("mergeRadius", &DamageNumberStyle::mergeRadius);
59 style.addVar("decimals", &DamageNumberStyle::decimals);
60 style.addVar("compact", &DamageNumberStyle::compact);
61 auto cls =
62 table.addClass<DamageNumbers>("DamageNumbers", std::function<DamageNumbers*()>([] { return nullptr; }), false);
63 graphicsClass.addFunc("newDamageNumbers", [vm](Graphics*) {
64 auto owned = script::makeOwnedSquirrelInstance(vm, std::make_unique<DamageNumbers>());
65 if (!owned) return script::projectStatusResult(vm, owned.status());
66 auto result = script::projectStatusResult(vm, Status::success(), std::move(owned).takeValue());
67 result.set("ownership", std::string("owned"));
68 return result;
69 });
70 cls.addFunc("spawn", [vm](DamageNumbers* self, float amount, float x, float y, const std::string& kind,
71 const std::string& group, float velocityX) {
72 auto parsed = parseKind(kind);
73 if (!parsed) return script::projectStatusResult(vm, parsed.status());
74 return script::projectResult(vm, self->spawn({amount, x, y, parsed.value(), group, velocityX}), admission);
75 });
76 cls.addFunc("setStyle", [vm](DamageNumbers* self, const std::string& kind, const DamageNumberStyle* value) {
77 auto parsed = parseKind(kind);
78 if (!parsed) return script::projectStatusResult(vm, parsed.status());
79 if (!value)
82 return script::projectResult(vm, self->setStyle(parsed.value(), *value));
83 });
84 cls.addFunc("update",
85 [vm](DamageNumbers* self, float seconds) { return script::projectResult(vm, self->update(seconds)); });
86 cls.addFunc("clear", &DamageNumbers::clear);
87 cls.addFunc("count", [](const DamageNumbers* self) { return static_cast<int>(self->count()); });
88 cls.addFunc("snapshot", [vm](const DamageNumbers* self) {
90 for (const auto& sample : self->snapshot())
91 result.emplace_back(Value::object({{"text", sample.text},
92 {"x", sample.x},
93 {"y", sample.y},
94 {"scale", sample.scale},
95 {"alpha", sample.alpha}}));
97 [](Value value) { return value; });
98 });
99 cls.addFunc("render", [vm](const DamageNumbers* self, Graphics* graphics, Font* font) {
100 if (!graphics || !font)
102 vm, Status::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "Graphics and font are required",
103 "render")));
104 self->render(*graphics, *font);
106 });
107}
108} // namespace eve::graphics
double y
double x
ActiveSource owned
HSQUIRRELVM vm
Definition ECS.cpp:38
HSQOBJECT cls
Definition ECS.cpp:39
Poco::JSON::Object::Ptr result
Object::Ptr value
std::string kind
The single Squirrel projection for common Result, Status and Value.
Explicit Value/Owned/Borrowed semantics at the Squirrel boundary.
AbilityActivationGroup group
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
static Status failure(StatusCode code, Diagnostic diagnostic)
Construct a failed status with one diagnostic.
Definition Status.cpp:28
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::vector< Value > Array
Definition Value.h:33
static Value object(Object value)
Compatibility factory for an object value.
Definition Value.cpp:129
Bounded transient floating-number presentation owned by a game/view. @ownership Owns styles and all l...
std::size_t count() const
Return live popup count.
Result< DamageNumberAdmission > spawn(const DamageNumberRequest &request)
Validate and admit one popup. Failure leaves pool and sequence unchanged.
Result< void > update(double seconds)
Advance injected seconds; nonfinite/negative dt fails without mutation.
Result< void > setStyle(DamageNumberKind kind, const DamageNumberStyle &style)
Validate and replace one future-spawn style; existing entries keep their own copy.
std::vector< DamageNumberSample > snapshot() const
Build an independent owning frame snapshot for observation. @cost Linear allocations/copies in live e...
void clear()
Remove every transient entry; deterministic spawn sequence restarts.
GPU-side font: wraps a decoded font::FontData and rasterizes a fixed set of codepoints into a single ...
Definition Font.h:35
float getAscent() const
Returns the ascent.
Definition Font.cpp:173
bool hasGlyph(int codepoint) const
Whether codepoint was rasterized into this Font's atlas.
Definition Font.cpp:178
float getBaseline() const
Distance from the top of a line to the baseline (== getAscent()).
Definition Font.cpp:174
float getHeight() const
Line height in pixels at the FontData's decoded pixel size.
Definition Font.cpp:172
float getWidth(const std::string &text) const
Pixel width of text (UTF-8), including kerning; delegates to FontData.
Definition Font.cpp:176
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
void exposeFontAndDamageNumberBindings(ssq::Table &table, ssq::Class &graphicsClass)
Register font and popup script types on the VM owner thread; retain no table references.
DamageNumberAdmission
Observable bounded-pool admission result.
ssq::Table projectStatusResult(HSQUIRRELVM vm, const Status &status)
Project a checked native status that carries no payload.
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.
eve::Result< ssq::Object > makeOwnedSquirrelInstance(HSQUIRRELVM vm, Owned< T > object)
Creates a Squirrel instance that owns a C++ wrapper through a release hook.
Owning style copied into each popup at spawn. Coordinates are drawable pixels.