Magic_Game/src/kiwano/render/Shape.cpp

173 lines
4.9 KiB
C++
Raw Normal View History

// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +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
//
// 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
//
// 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.
2020-02-05 19:56:22 +08:00
#include <kiwano/render/Shape.h>
2020-02-16 20:14:01 +08:00
#include <kiwano/render/ShapeMaker.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/Renderer.h>
2020-02-16 20:14:01 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
#include <kiwano/render/DirectX/NativePtr.h>
#endif
namespace kiwano
{
2020-02-05 19:56:22 +08:00
Shape::Shape() {}
2019-08-14 23:36:29 +08:00
2020-02-16 20:14:01 +08:00
void Shape::Clear()
{
ResetNativePointer();
}
2020-02-05 19:56:22 +08:00
Rect Shape::GetBoundingBox() const
2020-01-21 10:09:55 +08:00
{
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-01-21 10:09:55 +08:00
Rect bounds;
2020-02-16 20:14:01 +08:00
auto geometry = NativePtr::Get<ID2D1Geometry>(this);
if (geometry)
2020-01-21 10:09:55 +08:00
{
// no matter it failed or not
2020-02-16 20:14:01 +08:00
geometry->GetBounds(nullptr, DX::ConvertToRectF(&bounds));
2020-01-21 10:09:55 +08:00
}
return bounds;
2020-02-14 22:59:29 +08:00
#else
return Rect(); // not supported
#endif
2020-01-21 10:09:55 +08:00
}
2020-02-19 12:09:50 +08:00
Rect Shape::GetBoundingBox(const Matrix3x2& transform) const
2020-01-21 10:09:55 +08:00
{
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-01-21 10:09:55 +08:00
Rect bounds;
2020-02-16 20:14:01 +08:00
auto geometry = NativePtr::Get<ID2D1Geometry>(this);
if (geometry)
2020-01-21 10:09:55 +08:00
{
// no matter it failed or not
2020-02-16 20:14:01 +08:00
geometry->GetBounds(DX::ConvertToMatrix3x2F(transform), DX::ConvertToRectF(&bounds));
2020-01-21 10:09:55 +08:00
}
return bounds;
2020-02-14 22:59:29 +08:00
#else
return Rect(); // not supported
#endif
2020-01-21 10:09:55 +08:00
}
2020-02-05 19:56:22 +08:00
float Shape::GetLength() const
2020-01-21 10:09:55 +08:00
{
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-01-21 10:09:55 +08:00
float length = 0.f;
2020-02-16 20:14:01 +08:00
auto geometry = NativePtr::Get<ID2D1Geometry>(this);
if (geometry)
2020-01-21 10:09:55 +08:00
{
// no matter it failed or not
2020-02-16 20:14:01 +08:00
geometry->ComputeLength(D2D1::Matrix3x2F::Identity(), &length);
2020-01-21 10:09:55 +08:00
}
return length;
2020-02-14 22:59:29 +08:00
#else
return 0.0f; // not supported
#endif
2020-01-21 10:09:55 +08:00
}
2020-02-05 19:56:22 +08:00
bool Shape::ComputePointAtLength(float length, Point& point, Vec2& tangent) const
2020-01-21 10:09:55 +08:00
{
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto geometry = NativePtr::Get<ID2D1Geometry>(this);
if (geometry)
2020-01-21 10:09:55 +08:00
{
2020-02-16 20:14:01 +08:00
HRESULT hr = geometry->ComputePointAtLength(length, D2D1::Matrix3x2F::Identity(), DX::ConvertToPoint2F(&point),
2020-01-21 10:09:55 +08:00
DX::ConvertToPoint2F(&tangent));
return SUCCEEDED(hr);
}
return false;
2020-02-14 22:59:29 +08:00
#else
return false; // not supported
#endif
2020-01-21 10:09:55 +08:00
}
2020-02-05 19:56:22 +08:00
float Shape::ComputeArea() const
2020-01-21 10:09:55 +08:00
{
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto geometry = NativePtr::Get<ID2D1Geometry>(this);
if (geometry)
{
float area = 0.f;
// no matter it failed or not
geometry->ComputeArea(D2D1::Matrix3x2F::Identity(), &area);
}
return 0.f;
2020-02-14 22:59:29 +08:00
#else
return 0.0f; // not supported
#endif
}
2020-01-21 10:09:55 +08:00
2020-02-19 12:09:50 +08:00
bool Shape::ContainsPoint(const Point& point, const Matrix3x2* transform) const
2020-01-21 10:09:55 +08:00
{
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto geometry = NativePtr::Get<ID2D1Geometry>(this);
if (!geometry)
2020-01-21 10:09:55 +08:00
return false;
BOOL ret = 0;
// no matter it failed or not
2020-02-16 20:14:01 +08:00
geometry->FillContainsPoint(DX::ConvertToPoint2F(point), DX::ConvertToMatrix3x2F(transform),
2020-01-21 10:09:55 +08:00
D2D1_DEFAULT_FLATTENING_TOLERANCE, &ret);
return !!ret;
2020-02-14 22:59:29 +08:00
#else
return false; // not supported
#endif
2020-01-21 10:09:55 +08:00
}
2020-02-19 12:09:50 +08:00
ShapePtr Shape::CreateLine(const Point& begin, const Point& end)
2020-01-21 10:09:55 +08:00
{
ShapePtr output = new (autogc) Shape;
2020-02-08 09:59:17 +08:00
Renderer::GetInstance().CreateLineShape(*output, begin, end);
2020-01-21 10:09:55 +08:00
return output;
}
2020-02-19 12:09:50 +08:00
ShapePtr Shape::CreateRect(const Rect& rect)
2020-01-21 10:09:55 +08:00
{
ShapePtr output = new (autogc) Shape;
2020-02-08 09:59:17 +08:00
Renderer::GetInstance().CreateRectShape(*output, rect);
2020-01-21 10:09:55 +08:00
return output;
}
2020-02-19 12:09:50 +08:00
ShapePtr Shape::CreateRoundedRect(const Rect& rect, const Vec2& radius)
2020-01-21 10:09:55 +08:00
{
ShapePtr output = new (autogc) Shape;
2020-02-08 09:59:17 +08:00
Renderer::GetInstance().CreateRoundedRectShape(*output, rect, radius);
2020-01-21 10:09:55 +08:00
return output;
}
2020-02-19 12:09:50 +08:00
ShapePtr Shape::CreateCircle(const Point& center, float radius)
2020-01-21 10:09:55 +08:00
{
ShapePtr output = new (autogc) Shape;
2020-02-08 09:59:17 +08:00
Renderer::GetInstance().CreateEllipseShape(*output, center, Vec2{ radius, radius });
2020-01-21 10:09:55 +08:00
return output;
}
2020-02-19 12:09:50 +08:00
ShapePtr Shape::CreateEllipse(const Point& center, const Vec2& radius)
2020-01-21 10:09:55 +08:00
{
ShapePtr output = new (autogc) Shape;
2020-02-08 09:59:17 +08:00
Renderer::GetInstance().CreateEllipseShape(*output, center, radius);
2020-01-21 10:09:55 +08:00
return output;
}
} // namespace kiwano