ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
11_animations/main.cpp

Example from examples/11_animations/.

Example from examples/11_animations/.

#include "ExampleHost.hpp"
#include <memory>
#include <string>
using namespace ZenitUI;
static constexpr const char *kStyle = R"zs(
:root {
font: inter;
font-size: 14px;
letter-spacing: 0px;
color: #E5E7EB;
}
@keyframes pulse {
0% {
scale: 1;
opacity: 1;
border-color: #2563EB;
}
50% {
scale: 1.06;
opacity: 0.78;
border-color: #38BDF8;
}
100% {
scale: 1;
opacity: 1;
border-color: #2563EB;
}
}
@keyframes float-y {
0% {
translate-y: 0px;
}
100% {
translate-y: -10px;
}
}
@keyframes spin-badge {
0% {
rotation: 0;
}
100% {
rotation: 360;
}
}
.page {
width: 100%;
height: 100%;
background: #0D1017;
padding: 24px;
overflow-x: hidden;
overflow-y: auto;
}
.shell {
width: 100%;
max-width: 860px;
margin: auto;
gap: 18px;
}
.title {
font: inter-bold;
font-size: 24px;
color: #F8FAFC;
}
.section-title {
font: inter-semibold;
font-size: 15px;
color: #F1F5F9;
}
.muted {
font-size: 13px;
color: #94A3B8;
}
.card {
width: 100%;
background: #151924;
border-width: 1px;
border-color: #262E42;
radius: 12px;
padding: 18px;
gap: 14px;
}
.row {
width: 100%;
gap: 12px;
wrap: true;
align-items: center;
}
/* 1. CSS Transitions */
.lift-tile {
flex: 1;
min-width: 170px;
padding: 16px;
radius: 10px;
background: #111624;
border-width: 1px;
border-color: #2A344D;
gap: 4px;
cursor: pointer;
transition-time: 0.22;
ease: ease-out-quad;
}
.lift-tile:hover {
background: #1B2742;
border-color: #38BDF8;
translate-y: -6px;
}
.press-tile {
flex: 1;
min-width: 170px;
padding: 16px;
radius: 10px;
background: #111624;
border-width: 1px;
border-color: #2A344D;
gap: 4px;
cursor: pointer;
transition-time: 0.14;
ease: ease-out-quad;
}
.press-tile:pressed {
background: #1E3A8A;
border-color: #60A5FA;
scale: 0.93;
}
.check-tile {
flex: 1;
min-width: 200px;
padding: 12px;
gap: 10px;
align-items: center;
}
.check-label {
flex: 1;
min-width: 0px;
}
/* ::part transitions on Checkbox */
Checkbox.fancy::box {
background: #1F2937;
radius: 6px;
transition: background 0.18s ease-out-quad, radius 0.18s ease-out-quad;
}
Checkbox.fancy:checked::box {
background: #2563EB;
radius: 10px;
}
Checkbox.fancy::mark {
color: #F8FAFC;
}
/* 2. @keyframes showcase */
.kf-box {
flex: 1;
min-width: 180px;
height: 92px;
padding: 14px;
radius: 10px;
background: #101522;
border-width: 2px;
border-color: #2A344D;
justify: center;
align-items: center;
gap: 6px;
}
.anim-pulse {
animation: pulse 1.6s ease-in-out-quad infinite;
}
.anim-float {
animation: float-y 1.2s ease-in-out-quad infinite alternate;
}
.spinner {
width: 26px;
height: 26px;
radius: 6px;
background: #38BDF8;
}
.anim-spin .spinner {
animation: spin-badge 2.2s linear infinite;
}
/* 3. Imperative animation stage */
.stage {
width: 100%;
height: 110px;
background: #0F131D;
border-width: 1px;
border-color: #252D40;
radius: 10px;
padding: 16px;
align-items: center;
overflow: hidden;
}
.stage-card {
width: 150px;
height: 68px;
radius: 10px;
background: #2563EB;
border-width: 1px;
border-color: #60A5FA;
justify: center;
align-items: center;
gap: 2px;
}
Button.btn {
padding: 8px 14px;
radius: 8px;
background: #2563EB;
color: #FFFFFF;
cursor: pointer;
transition-time: 0.12;
}
Button.btn:hover {
background: #3B82F6;
}
Button.btn-ghost {
padding: 8px 14px;
radius: 8px;
background: #1F2638;
border-width: 1px;
border-color: #334155;
color: #E2E8F0;
cursor: pointer;
transition-time: 0.12;
}
Button.btn-ghost:hover {
background: #2A344D;
border-color: #38BDF8;
}
.status {
width: 100%;
padding: 10px 14px;
radius: 8px;
background: #10141F;
border-left: 3px;
border-color: #38BDF8;
color: #CBD5E1;
font-size: 13px;
}
)zs";
static constexpr const char *kMarkup = R"zm(
ScrollView.page {
VStack.shell {
VStack width="100%" gap="4px" {
Text.title wrap="true" "Animations — Transitions, @keyframes & Imperative Tracks"
Text.muted wrap="true" "Opt-in CSS transitions, ::part styling, looping @keyframes, and chained C++ UIAnimation tracks."
}
VStack.card {
Text.section-title "1. State Transitions & ::part Pseudo-Elements"
Text.muted wrap="true" "Hover or press any card to trigger translate-y, scale, and color easing; toggle the Checkbox for ::box transitions."
HStack.row {
VStack.lift-tile {
Text.section-title "Hover Only"
Text.muted wrap="true" "Lifts -6px and highlights on :hover"
}
VStack.press-tile {
Text.section-title "Press Only"
Text.muted wrap="true" "Squashes to scale 0.93 on :pressed"
}
HStack.check-tile {
Checkbox.fancy checked="true"
Text.check-label wrap="true" "Checkbox::box transitions color & radius"
}
}
}
VStack.card {
HStack width="100%" justify="space-between" align-items="center" wrap="true" gap="8px" {
Text.section-title "2. Declarative @keyframes"
Button.btn-ghost@toggleKf "Pause @keyframes"
}
HStack.row {
VStack.kf-box.anim-pulse@kfPulse {
Text.section-title "pulse"
Text.muted "scale + opacity + border"
}
VStack.kf-box.anim-float@kfFloat {
Text.section-title "float-y"
Text.muted "infinite alternate"
}
VStack.kf-box.anim-spin@kfSpin {
Stack.spinner {}
Text.muted "rotation: 0 -> 360"
}
}
}
VStack.card {
Text.section-title "3. Imperative UIAnimation (Multi-Track + Chaining)"
Text.muted wrap="true" "Drives translateX, rotation, scale, and background color directly from C++ tracks."
HStack.stage@stage {
VStack.stage-card@stageCard {
Text.section-title "UIAnimation"
Text.muted "4 tracks"
}
}
HStack.row {
Button.btn@playFwd "Play Forward"
Button.btn-ghost@playRev "Play Reverse"
Button.btn-ghost@playChain "Play 2-Step Chain"
}
Text.status@status wrap="true" "Ready — click any playback button above."
}
}
}
)zm";
namespace
{
TransitionFunction resolveEase(std::string_view name)
{
TransitionFunction tf = TransitionFunction::Linear;
parseEasing(name, tf);
return tf;
}
} // namespace
int main()
{
cfg.title = "ZenitUI 11 — Animations";
cfg.width = 940;
cfg.height = 720;
cfg.minWidth = 460;
cfg.minHeight = 480;
return ZenitExample::runApp(cfg, [](UIContext &ctx, IBackend &) -> std::shared_ptr<Layout> {
ZenitExample::loadDefaultFonts(ctx);
auto ui = ZMarkup::build(kMarkup, ctx);
ZMarkup::WeakUINode uiWeak = ui.weak();
auto setStatus = [uiWeak](const std::string &msg) {
if (auto locked = uiWeak.lock())
locked->setTextRef("status", msg);
};
// 1. Toggle @keyframes classes on/off.
auto kfRunning = std::make_shared<bool>(true);
ui.onClickRef("toggleKf", [kfRunning, uiWeak]() {
*kfRunning = !(*kfRunning);
if (auto locked = uiWeak.lock())
{
locked->toggleClassRef("kfPulse", "anim-pulse", *kfRunning);
locked->toggleClassRef("kfFloat", "anim-float", *kfRunning);
locked->toggleClassRef("kfSpin", "anim-spin", *kfRunning);
locked->setButtonTextRef("toggleKf", *kfRunning ? "Pause @keyframes" : "Resume @keyframes");
}
});
// 2. Build imperative UIAnimation tracks on @stageCard.
auto stageNode = ui.ref("stage");
auto stageCard = ui.ref("stageCard");
std::weak_ptr<Layout> stageW = stageNode;
std::weak_ptr<Layout> cardW = stageCard;
auto computeTravel = [stageW, cardW]() -> float {
auto st = stageW.lock();
auto cd = cardW.lock();
if (!st || !cd)
return 120.0f;
// .stage has 16px left + 16px right padding (32px total)
const float avail = st->getRect().width - 32.0f - cd->getRect().width;
return std::max(24.0f, avail);
};
auto makeSlideAnim = [](float fromX, float toX,
float fromDeg, float toDeg,
float fromScale, float toScale,
Color fromCol, Color toCol,
float dur) {
const auto easeOut = resolveEase("ease-out-cubic");
const auto easeInOut = resolveEase("ease-in-out-quad");
auto anim = std::make_shared<UIAnimation>(dur);
anim->blocksInput = false;
anim->addTrack<Value>(Px(fromX), Px(toX),
[](Layout *n, Value v) { n->getInlineBase().translateX = v; },
easeOut);
anim->addTrack<float>(fromDeg, toDeg,
[](Layout *n, float deg) { n->getInlineBase().rotation = deg; },
easeInOut);
anim->addTrack<float>(fromScale, toScale,
[](Layout *n, float s) { n->getInlineBase().scale = s; },
easeOut);
anim->addTrack<Color>(fromCol, toCol,
[](Layout *n, Color c) { n->getInlineBase().background = c; },
easeInOut);
return anim;
};
ui.onClickRef("playFwd", [cardW, computeTravel, makeSlideAnim, setStatus]() {
if (auto c = cardW.lock())
{
const float travel = computeTravel();
auto anim = makeSlideAnim(0.0f, travel, 0.0f, 6.0f, 1.0f, 1.05f,
Color{37, 99, 235, 255}, Color{16, 185, 129, 255}, 0.65f);
anim->onFinished = [setStatus]() { setStatus("Imperative animation 'slide' finished."); };
c->addAnimation("slide", anim);
c->playAnimation("slide", false);
setStatus("Playing forward (travel " + std::to_string(static_cast<int>(travel)) + "px)...");
}
});
ui.onClickRef("playRev", [cardW, computeTravel, makeSlideAnim, setStatus]() {
if (auto c = cardW.lock())
{
const float travel = computeTravel();
auto anim = makeSlideAnim(0.0f, travel, 0.0f, 6.0f, 1.0f, 1.05f,
Color{37, 99, 235, 255}, Color{16, 185, 129, 255}, 0.65f);
anim->onFinished = [setStatus]() { setStatus("Reverse animation finished."); };
c->addAnimation("slide", anim);
c->playAnimation("slide", true);
setStatus("Playing reverse...");
}
});
ui.onClickRef("playChain", [cardW, computeTravel, makeSlideAnim, setStatus]() {
if (auto c = cardW.lock())
{
const float travel = computeTravel();
auto step2 = makeSlideAnim(travel, 0.0f, 6.0f, 0.0f, 1.05f, 1.0f,
Color{16, 185, 129, 255}, Color{37, 99, 235, 255}, 0.45f);
step2->onFinished = [setStatus]() { setStatus("2-step chain complete (slide -> settle)."); };
auto step1 = makeSlideAnim(0.0f, travel, 0.0f, 6.0f, 1.0f, 1.05f,
Color{37, 99, 235, 255}, Color{16, 185, 129, 255}, 0.55f);
step1->onFinished = [cardW, setStatus]() {
setStatus("Step 1 finished -> launching Step 2 ('settle')...");
if (auto card = cardW.lock())
card->playAnimation("settle", false);
};
c->addAnimation("settle", step2);
c->addAnimation("chain1", step1);
c->playAnimation("chain1", false);
setStatus("Playing 2-step chain...");
}
});
return ui.root();
});
}
Unified window, frame-loop, and subsystem host for interchangeable backends.
Definition Backend.hpp:49
The base node type: tree, style, layout, input, scroll, render.
Definition Layout.hpp:154
Non-owning handle to a UINode, safe to capture inside child callbacks.
Definition ZMarkup.hpp:757
bool parseEasing(std::string_view name, TransitionFunction &out)
Parse an easing name (CSS-like) into a TransitionFunction.
Definition Easing.hpp:121
TransitionFunction
Timing function applied between two keyframe values.
Definition Easing.hpp:28
bool loadStyleString(std::string_view src, Theme &theme, const std::string &fileName="<string>", DiagnosticSink *sink=nullptr, bool strict=false, const Registry *registry=nullptr)
Parse src and apply it to theme.
8-bit-per-channel RGBA color, non-premultiplied.
Definition CoreTypes.hpp:78
Per-instance UI context: backends, theme, text engine, frame state, and routing.
Definition UIContext.hpp:725
Theme theme
Per-context stylesheet (rules, keyframes, and :root style).
Definition UIContext.hpp:911
8-byte tagged length expression:
Definition Unit.hpp:58
Backend-agnostic window and graphics-context creation parameters.
Definition Backend.hpp:31