#include "hd2d/editing/Hd2dTarget.h" #include "zeroerr/assert.h" #include "zeroerr/unittest.h" using namespace eve::hd2d_editing; using namespace eve::editing; namespace{SelectionSnapshot select(const Hd2dDocumentTarget&t){SelectionSnapshot s;s.channel="hd2d";s.items.push_back({SelectionDomain::Asset,TargetId(t.targetId()),StableId(t.targetId().value()),"hd2d.asset"});return s;}void apply(Hd2dDocumentTarget&t,EditorResult op){REQUIRE(op.ok());REQUIRE(t.applyDomainOperation(op.value()).ok());}class MissingTexture final:public IHd2dTextureResolver{public:EditorResulttexture(const std::string&)const override{return eve::editing::failed(EditorStatus::NotFound,RuleId("test.texture"),"missing texture");}};} TEST_CASE("editor.hd2d.sprite_preset_is_reversible_and_scrubs_frames"){ Hd2dDocumentTarget target("hero");const auto selection=select(target);apply(target,target.makeSet(selection,PropertyPath("asset.source"),"hero.png",PropertySetMode::Absolute));apply(target,target.makeSet(selection,PropertyPath("sprite.grid"),EditorValue::Array{4.0,2.0},PropertySetMode::Absolute));apply(target,target.makeSet(selection,PropertyPath("sprite.animation"),EditorValue::Array{2.0,5.0},PropertySetMode::Absolute));apply(target,target.makeSet(selection,PropertyPath("sprite.fps"),2.0,PropertySetMode::Absolute));Hd2dFramePreviewService preview;auto p=preview.evaluate(target,1);REQUIRE(p.ok());CHECK_EQ(p.value().frame,4);CHECK_EQ(p.value().uv[0],0.f);CHECK_EQ(p.value().uv[1],.5f); auto flip=target.makeSet(selection,PropertyPath("sprite.flipX"),true,PropertySetMode::Absolute);REQUIRE(flip.ok());REQUIRE(target.applyDomainOperation(flip.value()).ok());DomainOperation undo=flip.value();undo.payload=flip.value().inverse;REQUIRE(target.applyDomainOperation(undo).ok());CHECK(!target.value().flipX); } TEST_CASE("editor.hd2d.rejects_invalid_frame_ranges_and_loads_atomically"){ Hd2dDocumentTarget target("hero");const auto selection=select(target);CHECK_EQ(static_cast(target.makeSet(selection,PropertyPath("sprite.animation"),EditorValue::Array{0.0,2.0},PropertySetMode::Absolute).code()),static_cast(EditorStatus::Rejected));const auto before=target.snapshotValue();EditorValue invalid=before;auto*root=invalid.getIf();auto*content=(*root)["content"].getIf();(*content)["wallUv"]=EditorValue::Array{.8,.8,.2,.2};CHECK_EQ(static_cast(target.loadSnapshot(invalid).code()),static_cast(EditorStatus::Rejected));CHECK_EQ(target.snapshotValue(),before); } TEST_CASE("editor.hd2d.runtime_resolves_assets_before_live_creation"){ Hd2dDocumentTarget target("hero");const auto selection=select(target);apply(target,target.makeSet(selection,PropertyPath("asset.source"),"missing.png",PropertySetMode::Absolute));Hd2dDocumentRuntime runtime;MissingTexture textures;auto*module=reinterpret_cast(std::uintptr_t{1});auto*graphics=reinterpret_cast(std::uintptr_t{1});CHECK_EQ(static_cast(runtime.publishSprite(target,module,graphics,nullptr,textures).code()),static_cast(EditorStatus::NotFound));CHECK_EQ(runtime.revision(),Revision(0)); }