61 lines
1.6 KiB
C++
61 lines
1.6 KiB
C++
#include "dl_align.h"
|
||
|
||
#include "base/dl_type_convert.h"
|
||
#include "base/dl_string.h"
|
||
|
||
|
||
namespace dl::Align
|
||
{
|
||
|
||
dl::Align::Direction2 Anchor2Direction(std::string_view anchor)
|
||
{
|
||
if (anchor.empty())
|
||
return { Direction::BACK, Direction::BACK };
|
||
if (anchor == "center")
|
||
return { Direction::ZERO, Direction::ZERO };
|
||
if (anchor == "right")
|
||
return { Direction::FRONT, Direction::ZERO };
|
||
if (anchor == "rb")
|
||
return { Direction::FRONT, Direction::FRONT };
|
||
if (anchor == "buttom")
|
||
return { Direction::ZERO, Direction::FRONT };
|
||
if (anchor == "lb")
|
||
return { Direction::BACK, Direction::FRONT };
|
||
if (anchor == "left")
|
||
return { Direction::BACK, Direction::FRONT };
|
||
if (anchor == "lt")
|
||
return { Direction::BACK, Direction::FRONT };
|
||
if (anchor == "top")
|
||
return { Direction::BACK, Direction::FRONT };
|
||
if (anchor == "rt")
|
||
return { Direction::BACK, Direction::FRONT };
|
||
log_err("错误的anchor:{}", anchor);
|
||
return { Direction::ZERO, Direction::ZERO };
|
||
}
|
||
|
||
dl::Point Anchor2Point(const Size& size, std::string_view anchor)
|
||
{
|
||
if (anchor.empty())
|
||
return {};
|
||
if (isdigit(anchor[0]))
|
||
{//数字直接返回
|
||
return String::toPoint2(anchor);
|
||
}
|
||
//center等,通过计算返回
|
||
Direction2 dir = Anchor2Direction(anchor);
|
||
return Size2Anchor(toPoint(size), dir);
|
||
}
|
||
|
||
Position Anchor2Position(const Position &size, std::string_view anchor)
|
||
{
|
||
if (anchor.empty())
|
||
return {};
|
||
if (isdigit(anchor[0]) || anchor[0] == '-' || anchor[0] == '+')
|
||
{//数字直接返回
|
||
return String::toPosition2(anchor);
|
||
}
|
||
//center等,通过计算返回
|
||
Direction2 dir = Anchor2Direction(anchor);
|
||
return Size2Anchor(size, dir);
|
||
}
|
||
} |