2019-08-14 21:38:37 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-08-14 21:38:37 +08:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-08-14 21:38:37 +08:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-08-14 21:38:37 +08:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2020-02-05 19:56:22 +08:00
|
|
|
#include <kiwano/core/ObjectBase.h>
|
2020-01-21 11:05:28 +08:00
|
|
|
#include <kiwano/render/DirectX/D2DDeviceResources.h>
|
2019-08-14 21:38:37 +08:00
|
|
|
|
|
|
|
|
namespace kiwano
|
|
|
|
|
{
|
2020-02-05 19:56:22 +08:00
|
|
|
class ShapeSink;
|
|
|
|
|
|
|
|
|
|
KGE_DECLARE_SMART_PTR(Shape);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \addtogroup Render
|
|
|
|
|
* @{
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
* @brief 形状
|
2020-01-21 10:09:55 +08:00
|
|
|
*/
|
2020-02-05 19:56:22 +08:00
|
|
|
class KGE_API Shape : public virtual ObjectBase
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-05 19:56:22 +08:00
|
|
|
friend class ShapeSink;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
public:
|
2020-02-06 16:54:47 +08:00
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 创建线段形状
|
|
|
|
|
/// @param begin 线段起点
|
|
|
|
|
/// @param end 线段终点
|
2020-02-06 16:54:47 +08:00
|
|
|
static ShapePtr CreateLine(Point const& begin, Point const& end);
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 创建矩形
|
|
|
|
|
/// @param rect 矩形
|
2020-02-06 16:54:47 +08:00
|
|
|
static ShapePtr CreateRect(Rect const& rect);
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 创建圆角矩形
|
|
|
|
|
/// @param rect 矩形
|
|
|
|
|
/// @param radius 矩形圆角半径
|
2020-02-06 16:54:47 +08:00
|
|
|
static ShapePtr CreateRoundedRect(Rect const& rect, Vec2 const& radius);
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 创建圆形
|
|
|
|
|
/// @param center 圆形原点
|
|
|
|
|
/// @param radius 圆形半径
|
2020-02-06 16:54:47 +08:00
|
|
|
static ShapePtr CreateCircle(Point const& center, float radius);
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 创建椭圆形
|
|
|
|
|
/// @param center 椭圆原点
|
|
|
|
|
/// @param radius 椭圆半径
|
2020-02-06 16:54:47 +08:00
|
|
|
static ShapePtr CreateEllipse(Point const& center, Vec2 const& radius);
|
|
|
|
|
|
2020-02-05 19:56:22 +08:00
|
|
|
Shape();
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 是否有效
|
2020-01-21 10:09:55 +08:00
|
|
|
bool IsValid() const;
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 获取外切包围盒
|
2020-01-21 10:09:55 +08:00
|
|
|
Rect GetBoundingBox() const;
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 获取外切包围盒
|
|
|
|
|
/// @param transform 二维变换
|
2020-01-21 10:09:55 +08:00
|
|
|
Rect GetBoundingBox(Matrix3x2 const& transform) const;
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 判断图形是否包含点
|
|
|
|
|
/// @param point 点
|
|
|
|
|
/// @param transform 应用到点上的二维变换
|
2020-01-21 10:09:55 +08:00
|
|
|
bool ContainsPoint(Point const& point, const Matrix3x2* transform = nullptr) const;
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 获取图形展开成一条直线的长度
|
2020-01-21 10:09:55 +08:00
|
|
|
float GetLength() const;
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 计算图形面积
|
2020-01-21 10:09:55 +08:00
|
|
|
float ComputeArea() const;
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 计算图形上点的位置和切线向量
|
|
|
|
|
/// @param[in] length 点在图形上的位置,范围 [0.0 - 1.0]
|
|
|
|
|
/// @param[out] point 点的位置
|
|
|
|
|
/// @param[out] tangent 点的切线向量
|
2020-01-21 10:09:55 +08:00
|
|
|
bool ComputePointAtLength(float length, Point& point, Vec2& tangent) const;
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
/// @brief 清除形状
|
2020-01-21 10:09:55 +08:00
|
|
|
void Clear();
|
|
|
|
|
|
2020-02-14 22:59:29 +08:00
|
|
|
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
|
2020-02-09 18:41:59 +08:00
|
|
|
public:
|
2020-01-21 10:09:55 +08:00
|
|
|
ComPtr<ID2D1Geometry> GetGeometry() const;
|
|
|
|
|
|
2020-02-05 19:56:22 +08:00
|
|
|
void SetGeometry(ComPtr<ID2D1Geometry> shape);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ComPtr<ID2D1Geometry> geo_;
|
2020-02-07 20:50:27 +08:00
|
|
|
#endif
|
2020-01-21 10:09:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
2020-02-14 22:59:29 +08:00
|
|
|
inline bool Shape::IsValid() const
|
|
|
|
|
{
|
|
|
|
|
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
|
|
|
|
|
return geo_ != nullptr;
|
|
|
|
|
#else
|
|
|
|
|
return false; // not supported
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
|
2020-02-05 19:56:22 +08:00
|
|
|
inline ComPtr<ID2D1Geometry> Shape::GetGeometry() const
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
return geo_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 19:56:22 +08:00
|
|
|
inline void Shape::SetGeometry(ComPtr<ID2D1Geometry> shape)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-05 19:56:22 +08:00
|
|
|
geo_ = shape;
|
2019-08-14 21:38:37 +08:00
|
|
|
}
|
2020-02-07 20:50:27 +08:00
|
|
|
#endif
|
2020-02-05 19:56:22 +08:00
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
} // namespace kiwano
|