Magic_Game/src/kiwano/render/Shape.cpp

144 lines
3.8 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>
#include <kiwano/render/ShapeSink.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/Renderer.h>
namespace kiwano
{
2020-02-05 19:56:22 +08:00
Shape::Shape() {}
2019-08-14 23:36:29 +08:00
2020-02-05 19:56:22 +08:00
bool Shape::IsValid() const
2020-01-21 10:09:55 +08:00
{
return geo_ != nullptr;
}
2020-02-05 19:56:22 +08:00
Rect Shape::GetBoundingBox() const
2020-01-21 10:09:55 +08:00
{
Rect bounds;
if (geo_)
{
// no matter it failed or not
geo_->GetBounds(nullptr, DX::ConvertToRectF(&bounds));
}
return bounds;
}
2020-02-05 19:56:22 +08:00
Rect Shape::GetBoundingBox(Matrix3x2 const& transform) const
2020-01-21 10:09:55 +08:00
{
Rect bounds;
if (geo_)
{
// no matter it failed or not
geo_->GetBounds(DX::ConvertToMatrix3x2F(transform), DX::ConvertToRectF(&bounds));
}
return bounds;
}
2020-02-05 19:56:22 +08:00
float Shape::GetLength() const
2020-01-21 10:09:55 +08:00
{
float length = 0.f;
if (geo_)
{
// no matter it failed or not
geo_->ComputeLength(D2D1::Matrix3x2F::Identity(), &length);
}
return length;
}
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
{
if (geo_)
{
HRESULT hr = geo_->ComputePointAtLength(length, D2D1::Matrix3x2F::Identity(), DX::ConvertToPoint2F(&point),
DX::ConvertToPoint2F(&tangent));
return SUCCEEDED(hr);
}
return false;
}
2020-02-05 19:56:22 +08:00
void Shape::Clear()
2020-01-21 10:09:55 +08:00
{
geo_.reset();
}
2020-02-05 19:56:22 +08:00
float Shape::ComputeArea() const
2020-01-21 10:09:55 +08:00
{
if (!geo_)
return 0.f;
float area = 0.f;
// no matter it failed or not
geo_->ComputeArea(D2D1::Matrix3x2F::Identity(), &area);
return area;
}
2020-01-21 10:09:55 +08:00
2020-02-05 19:56:22 +08:00
bool Shape::ContainsPoint(Point const& point, const Matrix3x2* transform) const
2020-01-21 10:09:55 +08:00
{
if (!geo_)
return false;
BOOL ret = 0;
// no matter it failed or not
geo_->FillContainsPoint(DX::ConvertToPoint2F(point), DX::ConvertToMatrix3x2F(transform),
D2D1_DEFAULT_FLATTENING_TOLERANCE, &ret);
return !!ret;
}
2020-02-05 19:56:22 +08:00
ShapePtr Shape::CreateLine(Point const& begin, Point const& end)
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
ShapePtr output = new Shape;
Renderer::Instance().CreateLineShape(*output, begin, end);
2020-01-21 10:09:55 +08:00
return output;
}
2020-02-05 19:56:22 +08:00
ShapePtr Shape::CreateRect(Rect const& rect)
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
ShapePtr output = new Shape;
Renderer::Instance().CreateRectShape(*output, rect);
2020-01-21 10:09:55 +08:00
return output;
}
2020-02-05 19:56:22 +08:00
ShapePtr Shape::CreateRoundedRect(Rect const& rect, Vec2 const& radius)
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
ShapePtr output = new Shape;
Renderer::Instance().CreateRoundedRectShape(*output, rect, radius);
2020-01-21 10:09:55 +08:00
return output;
}
2020-02-05 19:56:22 +08:00
ShapePtr Shape::CreateCircle(Point const& center, float radius)
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
ShapePtr output = new Shape;
Renderer::Instance().CreateEllipseShape(*output, center, Vec2{ radius, radius });
2020-01-21 10:09:55 +08:00
return output;
}
2020-02-05 19:56:22 +08:00
ShapePtr Shape::CreateEllipse(Point const& center, Vec2 const& radius)
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
ShapePtr output = new Shape;
Renderer::Instance().CreateEllipseShape(*output, center, radius);
2020-01-21 10:09:55 +08:00
return output;
}
} // namespace kiwano