C++实现CSS布局管理器

30 篇文章 1 订阅
4 篇文章 0 订阅

/*
Yoga 是一个针对 Web 标准的嵌入式布局引擎。
https://www.yogalayout.dev/docs/styling/
https://www.yogalayout.dev/playground

Flex 布局教程:语法篇 - 阮一峰的网络日志
其它类似:
https://github.com/Tencent/Taitank
*/
#include <yoga/Yoga.h>

void applyLayout(YGNodeRef node) {
    //if (!YGNodeHasNewLayout(node)) {
    //    return;
    //}
    if (!YGNodeIsDirty(node))
        return;

    // Reset the flag
    YGNodeSetHasNewLayout(node, false);

    // Do the real work

    for (size_t i = 0; i < YGNodeGetChildCount(node); i++) {
        applyLayout(YGNodeGetChild(node, i));
    }
}


static float _baselineFunc(
    YGNodeConstRef /*node*/,
    const float /*width*/,
    const float height) {
    return height / 2;
}

static float
_baseline(YGNodeConstRef node, const float /*width*/, const float /*height*/) {
    auto* baseline = (float*)YGNodeGetContext(node);
    return *baseline;
}

static YGSize _measure1(
    YGNodeConstRef /*node*/,
    float /*width*/,
    YGMeasureMode /*widthMode*/,
    float /*height*/,
    YGMeasureMode /*heightMode*/) {
    return YGSize{ 42, 50 };
}

static void _dirtied(YGNodeConstRef node) {
    int* dirtiedCount = (int*)YGNodeGetContext(node);
    (*dirtiedCount)++;
}

void test() {
    // 设置
    YGConfigRef config = YGConfigNew();
    YGConfigSetUseWebDefaults(config, true);
    // Setup config...
    YGConfigSetErrata(config, YGErrataClassic); // None,Classic,All
    YGConfigSetPointScaleFactor(config, 2.0f);    // 网格对齐,0禁用

    YGNodeRef root = YGNodeNewWithConfig(config);

    //YGNodeRef root = YGNodeNew();
    YGNodeStyleSetPositionType(root, YGPositionTypeAbsolute);
    YGNodeStyleSetOverflow(root, YGOverflowHidden);
    YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
    YGNodeStyleSetJustifyContent(root, YGJustifyCenter);
    YGNodeStyleSetAlignItems(root, YGAlignCenter);
    YGNodeStyleSetAlignContent(root, YGAlignCenter);
    YGNodeStyleSetFlexWrap(root, YGWrapWrapReverse);
    YGNodeStyleSetFlexGrow(root, 1);
    YGNodeStyleSetFlexShrink(root, 1);
    YGNodeStyleSetFlexBasisPercent(root, 0);
    YGNodeStyleSetMargin(root, YGEdgeLeft, 10);
    YGNodeStyleSetPadding(root, YGEdgeTop, 10);
    YGNodeStyleSetPaddingPercent(root, YGEdgeLeft, 3);
    YGNodeStyleSetBorder(root, YGEdgeBottom, 10);
    YGNodeStyleSetGap(root, YGGutterRow, 10);
    //YGNodeStyleSetGapPercent(root, YGGutterRow, 10);
    YGNodeStyleSetWidth(root, 100.0f);
    YGNodeStyleSetHeight(root, 100.0f);
    YGNodeStyleSetMaxHeight(root, 500);
    YGNodeStyleSetMinHeight(root, 500);
    YGNodeStyleSetMinWidthPercent(root, 10);

    const YGNodeRef root_child0_child0 = YGNodeNewWithConfig(config);
    YGNodeRef child0 = YGNodeNew();
    YGNodeStyleSetPosition(child0, YGEdgeTop, 10);
    YGNodeStyleSetAlignSelf(child0, YGAlignCenter);
    YGNodeStyleSetPositionPercent(child0, YGEdgeTop, 50);
    YGNodeStyleSetWidthPercent(child0, 20);
    YGNodeStyleSetAspectRatio(child0, 1 / 1);    // 宽高比
    YGNodeStyleSetFlexGrow(child0, 1.0f);
    YGNodeStyleSetFlexBasis(child0, 50);
    YGNodeStyleSetMargin(child0, YGEdgeRight, 10.0f);
    YGNodeStyleSetDisplay(child0, YGDisplayNone);
    YGNodeStyleSetMarginAuto(child0, YGEdgeRight);
    YGNodeInsertChild(root, child0, 0.0f);

    float baselineValue = 10;
    YGNodeRef child1 = YGNodeNew();
    YGNodeCopyStyle(child0, child1);
    YGNodeSetContext(child1, &baselineValue);
    YGNodeStyleSetFlexGrow(child1, 1.0f);
    YGNodeSetIsReferenceBaseline(child1, true);
    YGNodeSetBaselineFunc(child1, _baselineFunc);
    YGNodeSetBaselineFunc(child1, _baseline);
    YGNodeSetMeasureFunc(child1, _measure1);
    int dirtiedCount = 0;
    YGNodeSetContext(root, &dirtiedCount);
    YGNodeSetDirtiedFunc(root, _dirtied);
    YGNodeInsertChild(root, child1, 1.0f);

    // 层级单独创建
    YGNodeRef const root_child0 = YGNodeNew();
    YGNodeRef const root_child1 = YGNodeNew();
    YGNodeRef children[] = { root_child0, root_child1 };
    YGNodeSetChildren(root, children, 2);
    root = YGNodeGetOwner(root_child0);

    // 布局
    YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

    // 获取 margin, border, and padding
    float left = YGNodeLayoutGetLeft(child0);
    float height = YGNodeLayoutGetHeight(child0);

    // 设置测量函数  MeasureMode  Exactly(stretch-fit)  Undefined(max-content)  AtMost(fit-content)
    // 通过测量函数委托给不同的布局系统,比如文本排版
    //Widget widget{}; 
    //YGNodeSetContext(node, &w);
    //YGNodeSetMeasureFunc(node, &measureWidget);

    // 添加脏区
    //YGNodeMarkDirty(child1);

    // 设置行flex行间距
    // YGNodeStyleSetGap(node, YGGutterRow, amount);

    // 设置布局后偏移
    // YGNodeStyleSetPosition(node, edge, position);

      // ceil/floor flags
    //ASSERT_FLOAT_EQ(6.0, YGRoundValueToPixelGrid(6.01, 2.0, false, false));

    //YGNodeFree(child0);
    //YGNodeFree(child1);
    //YGNodeRemoveAllChildren(root);
    YGNodeFreeRecursive(root);
    YGConfigFree(config);
}
 


创作不易,小小的支持一下吧!

  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C员工管理系统界面使用MySQL数据库进行数据存储和管理。MySQL是一款开源的关系型数据库管理系统,被广泛应用于Web应用程序开发中。 C员工管理系统的界面设计主要分为前端和后端两部分。前端使用HTML、CSS和JavaScript等技术进行页面布局和交互设计,后端使用PHP、Python或其他语言来处理数据的增删改查操作。 在C员工管理系统中,MySQL数据库用于存储员工相关的信息,如员工姓名、部门、职位、入职时间、工资等。员工数据在数据库中以表的形式存在,每个表对应一个数据实体。通过SQL语言可以对员工数据进行查询、插入、更新和删除操作。 系统界面中可能包括以下功能: 1. 员工信息查询:用户可以通过输入员工姓名或其他关键字来查询员工信息,系统会根据用户输入的条件在数据库中检索相关的数据并显示在界面上。 2. 员工信息录入:用户可以通过界面提供的表单来录入新员工的信息,包括姓名、部门、职位等,并将这些数据保存到MySQL数据库中。 3. 员工信息修改:用户可以选择某个员工的信息进行修改,如修改工资、调整部门等,修改后的数据会同步更新到数据库中。 4. 员工信息删除:用户可以选择某个员工的信息进行删除操作,删除后的数据会从数据库中删除。 5. 数据统计与分析:系统可以根据数据库中的员工数据进行各种统计和分析,如计算平均工资、部门人员分布等,并通过可视化图表展示在界面上。 通过MySQL数据库,C员工管理系统界面可以方便地对员工数据进行操作和管理,使员工管理更加高效和便捷。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码力码力我爱你

创作不易,小小的支持一下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值