310 lines
7.0 KiB
C++
310 lines
7.0 KiB
C++
/**
|
||
* @file dl_align.h
|
||
* @brief 方向和对齐定义
|
||
*
|
||
*
|
||
* @version 1.0
|
||
* @author lveyou
|
||
* @date 22-08-28
|
||
*
|
||
* @note
|
||
*/
|
||
#pragma once
|
||
|
||
#include <array>
|
||
#include <string_view>
|
||
|
||
#include "base/dl_concept.h"
|
||
#include "io/dl_log.h"
|
||
#include "base/dl_string.h"
|
||
|
||
namespace dl::Align
|
||
{
|
||
enum class Direction : int
|
||
{
|
||
BACK = -1,
|
||
ZERO = 0,
|
||
FRONT = 1,
|
||
FARTHEST = std::numeric_limits<int>::lowest()
|
||
};
|
||
|
||
using Direction2 = std::array<Direction, 2>;
|
||
|
||
constexpr Direction2 LT = { Direction::BACK, Direction::BACK };
|
||
constexpr Direction2 LC = { Direction::BACK, Direction::ZERO };
|
||
constexpr Direction2 LB = { Direction::BACK, Direction::FRONT };
|
||
|
||
constexpr Direction2 CT = { Direction::ZERO, Direction::BACK };
|
||
constexpr Direction2 CC = { Direction::ZERO, Direction::ZERO };
|
||
constexpr Direction2 CB = { Direction::ZERO, Direction::FRONT };
|
||
|
||
constexpr Direction2 RT = { Direction::FRONT, Direction::BACK };
|
||
constexpr Direction2 RC = { Direction::FRONT, Direction::ZERO };
|
||
constexpr Direction2 RB = { Direction::FRONT, Direction::FRONT };
|
||
|
||
enum class Dir4
|
||
{
|
||
RIGHT = 0,
|
||
BOTTOM,
|
||
LEFT,
|
||
TOP
|
||
};
|
||
|
||
enum class Dir8
|
||
{
|
||
RIGHT = 0,
|
||
RB,
|
||
BOTTOM,
|
||
LB,
|
||
LEFT,
|
||
LT,
|
||
TOP,
|
||
RT
|
||
};
|
||
|
||
//! 9方向定义(包含0为中心),以x正轴,朝y轴旋转
|
||
//enum class Direction
|
||
//{
|
||
// CENTER = 0,
|
||
// RIGHT = 1,
|
||
// RB = 2,
|
||
// BUTTOM = 3,
|
||
// LB = 4,
|
||
// LEFT = 5,
|
||
// LT = 6,
|
||
// TOP = 7,
|
||
// RT = 8
|
||
//};
|
||
|
||
//! 左上右下(2个点,rect用)
|
||
enum class LTRB
|
||
{
|
||
LEFT = 0,
|
||
TOP = 1,
|
||
RIGHT = 2,
|
||
BOTTOM = 3
|
||
};
|
||
|
||
//! 水平位置
|
||
//enum class Horizontal
|
||
//{
|
||
// RIGHT = 1,
|
||
// CENTER = 0,
|
||
// LEFT = -1
|
||
//};
|
||
|
||
//! 斜角位置(4个点,quad用)
|
||
enum class Corner
|
||
{
|
||
LT = 0,
|
||
RT = 1,
|
||
RB = 2,
|
||
LB = 3,
|
||
};
|
||
|
||
//! 垂直位置
|
||
//enum class Vertical
|
||
//{
|
||
// BUTTOM = 1,
|
||
// CENTER = 0,
|
||
// TOP = -1
|
||
//};
|
||
|
||
//
|
||
//struct HV
|
||
//{
|
||
// Horizontal _h;
|
||
// Vertical _v;
|
||
// HV():
|
||
// _h{ Horizontal::LEFT },
|
||
// _v{ Vertical::TOP }
|
||
// {}
|
||
// HV(Horizontal h, Vertical v) :
|
||
// _h{ h },
|
||
// _v{ v }
|
||
// {}
|
||
//
|
||
// static const HV LT;
|
||
// static const HV LC;
|
||
// static const HV BUTTOM;
|
||
// static const HV RT;
|
||
//};
|
||
|
||
//const HV HV::LT = { Horizontal::LEFT, Vertical::TOP };
|
||
//const HV HV::LC = { Horizontal::LEFT, Vertical::CENTER };
|
||
//const HV HV::BUTTOM = { Horizontal::LEFT, Vertical::CENTER };
|
||
//const HV HV::RT = { Horizontal::RIGHT, Vertical::TOP };
|
||
//
|
||
//HV Direction2HV(const Direction& dir)
|
||
//{
|
||
// if (dir == Direction::CENTER)
|
||
// return { Horizontal::CENTER, Vertical::CENTER };
|
||
// if (dir == Direction::RIGHT)
|
||
// return { Horizontal::RIGHT, Vertical::CENTER };
|
||
// if (dir == Direction::RB)
|
||
// return { Horizontal::RIGHT, Vertical::BUTTOM };
|
||
// if (dir == Direction::BUTTOM)
|
||
// return { Horizontal::CENTER, Vertical::BUTTOM };
|
||
// if (dir == Direction::LB)
|
||
// return { Horizontal::LEFT, Vertical::BUTTOM };
|
||
// if (dir == Direction::LEFT)
|
||
// return { Horizontal::LEFT, Vertical::CENTER };
|
||
// if (dir == Direction::LT)
|
||
// return { Horizontal::LEFT, Vertical::TOP };
|
||
// if (dir == Direction::TOP)
|
||
// return { Horizontal::CENTER, Vertical::TOP };
|
||
// if (dir == Direction::RT)
|
||
// return { Horizontal::RIGHT, Vertical::TOP };
|
||
// log(fmt::format("错误的Direction:{}", (int)dir));
|
||
// return {};
|
||
//}
|
||
/**
|
||
* @brief 根据大小和对齐,计算锚点
|
||
* @note 例如{32, 24}居中对齐返回的是{16, 12}
|
||
*/
|
||
template<IsSigned T, std::size_t N>
|
||
constexpr std::array<T, N> Size2Anchor(const std::array<T, N>& size, const std::array<Direction, N>& dir)
|
||
{
|
||
std::array<T, N> ret;
|
||
for (std::size_t i = 0; i < N; ++i)
|
||
{
|
||
ret[i] = (size[i] / static_cast<T>(2)) * ((int)dir[i] + 1);
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
/**
|
||
* @brief 根据大小和对齐,计算锚点
|
||
* @note 例如{32, 24}居中对齐返回的是{16, 12}
|
||
*/
|
||
template<IsSigned T, std::size_t N>
|
||
constexpr std::array<T, N> Size2Anchor(
|
||
const std::array<T, N>& size,
|
||
const std::array<T, N>& size_self,
|
||
const std::array<Direction, N>& dir)
|
||
{
|
||
std::array<T, N> ret;
|
||
for (std::size_t i = 0; i < N; ++i)
|
||
{
|
||
ret[i] = ((size[i] - size_self[i]) / static_cast<T>(2)) * ((int)dir[i] + 1);
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
/**
|
||
* @brief 锚点(字符串)返回 对齐
|
||
* @param[in] anchor 九个方位center、lt、rt、rb、lb、left、right、top、buttom
|
||
* @note 为空时返回左上角
|
||
*/
|
||
Direction2 Anchor2Direction(std::string_view anchor);
|
||
|
||
/**
|
||
* @brief 锚点(字符串)返回 偏移
|
||
* @param[in] anchor 九个方位center、lt、rt、rb、lb、left、right、top、buttom
|
||
* @ 也可以是 "12, 45",注意逗号后有个空格,不过也可以没有空格
|
||
* @note 例如{32, 24}居中对齐返回的是{16, 12}
|
||
*/
|
||
Point Anchor2Point(const Size& size, std::string_view anchor);
|
||
|
||
//-----------------------获取Rect斜角方位-----------------------------
|
||
template<typename T>
|
||
constexpr std::array<T, 2> GetLT(const std::array<T, 4>& rect)
|
||
{
|
||
return { rect[(size_t)LTRB::LEFT], rect[(size_t)LTRB::TOP] };
|
||
}
|
||
template<typename T>
|
||
constexpr std::array<T, 2> GetRT(const std::array<T, 4>& rect)
|
||
{
|
||
return { rect[(size_t)LTRB::RIGHT], rect[(size_t)LTRB::TOP] };
|
||
}
|
||
template<typename T>
|
||
constexpr std::array<T, 2> GetRB(const std::array<T, 4>& rect)
|
||
{
|
||
return { rect[(size_t)LTRB::RIGHT], rect[(size_t)LTRB::BOTTOM] };
|
||
}
|
||
template<typename T>
|
||
constexpr std::array<T, 2> GetLB(const std::array<T, 4>& rect)
|
||
{
|
||
return { rect[(size_t)LTRB::LEFT], rect[(size_t)LTRB::BOTTOM] };
|
||
}
|
||
//-----------------------获取Rect水平垂直方位-----------------------------
|
||
template<typename T>
|
||
constexpr T& GetLeft(std::array<T, 4>& rect)
|
||
{
|
||
return rect[(size_t)LTRB::LEFT];
|
||
}
|
||
template<typename T>
|
||
constexpr T& GetTop(std::array<T, 4>& rect)
|
||
{
|
||
return rect[(size_t)LTRB::TOP];
|
||
}
|
||
template<typename T>
|
||
constexpr T& GetRight(std::array<T, 4>& rect)
|
||
{
|
||
return rect[(size_t)LTRB::RIGHT];
|
||
}
|
||
template<typename T>
|
||
constexpr T& GetButtom(std::array<T, 4>& rect)
|
||
{
|
||
return rect[(size_t)LTRB::BOTTOM];
|
||
}
|
||
|
||
//-------------------------------隐藏含义获取--------------------------------
|
||
/**
|
||
* @brief 计算返回 矩形 右下角坐标y 减去 左上角坐标y
|
||
* @param[in] rect 矩形
|
||
*/
|
||
template<typename T>
|
||
constexpr T GetHeight(const std::array<T, 4>& rect)
|
||
{
|
||
return rect[3] - rect[1];
|
||
}
|
||
|
||
/**
|
||
* @brief 计算返回 矩形 右下角坐标x 减去 左上角坐标x
|
||
* @param[in] rect 矩形
|
||
*/
|
||
template<typename T>
|
||
constexpr T GetWidth(const std::array<T, 4>& rect)
|
||
{
|
||
return rect[2] - rect[0];
|
||
}
|
||
|
||
/**
|
||
* @brief 计算返回 矩形 宽和高
|
||
* @param[in] rect 矩形
|
||
*/
|
||
template<typename T>
|
||
constexpr std::array<T, 2> GetSize(const std::array<T, 4>& rect)
|
||
{
|
||
return { GetWidth(rect), GetHeight(rect) };
|
||
}
|
||
|
||
|
||
//----------------Quad和Rect的操作不同,Quad需要加名字-------
|
||
/**
|
||
* @brief 返回 Quad 的大小
|
||
*/
|
||
template<typename T>
|
||
constexpr T GetQuadSize(const std::array<T, 4>& quad)
|
||
{
|
||
return quad[(size_t)Corner::RB] - quad[(size_t)Corner::LT];
|
||
}
|
||
|
||
/**
|
||
* @brief 返回 Quad的三角形
|
||
* @note 顺时针绕序,0为右上方
|
||
*/
|
||
template<typename T>
|
||
constexpr std::array<T, 3> GetQuadTriangle0(const std::array<T, 4>& quad)
|
||
{
|
||
return { quad[(size_t)Corner::LT], quad[(size_t)Corner::RT], quad[(size_t)Corner::RB] };
|
||
}
|
||
template<typename T>
|
||
constexpr std::array<T, 3> GetQuadTriangle1(const std::array<T, 4>& quad)
|
||
{
|
||
return { quad[(size_t)Corner::LT], quad[(size_t)Corner::RB], quad[(size_t)Corner::LB] };
|
||
}
|
||
|
||
}
|