Magic_Game/src/kiwano/render/ShapeSink.cpp

165 lines
4.5 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/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
ShapeSink::ShapeSink() {}
2020-02-05 19:56:22 +08:00
ShapeSink::~ShapeSink()
2020-01-21 10:09:55 +08:00
{
Close();
}
2020-02-05 19:56:22 +08:00
void ShapeSink::Open()
2020-01-21 10:09:55 +08:00
{
if (!IsOpened())
{
path_geo_.reset();
2020-02-08 09:59:17 +08:00
Renderer::GetInstance().CreateShapeSink(*this);
2020-01-21 10:09:55 +08:00
win32::ThrowIfFailed(path_geo_->Open(&sink_));
}
}
2020-02-05 19:56:22 +08:00
void ShapeSink::Close()
2020-01-21 10:09:55 +08:00
{
if (IsOpened())
{
win32::ThrowIfFailed(sink_->Close());
sink_.reset();
}
2020-02-05 19:56:22 +08:00
shape_ = new Shape;
shape_->SetGeometry(path_geo_);
2020-01-21 10:09:55 +08:00
}
2020-02-05 19:56:22 +08:00
bool ShapeSink::IsOpened() const
2020-01-21 10:09:55 +08:00
{
return sink_ != nullptr;
}
2020-02-05 19:56:22 +08:00
ShapePtr ShapeSink::GetShape()
2020-01-21 10:09:55 +08:00
{
Close();
2020-02-05 19:56:22 +08:00
return shape_;
2020-01-21 10:09:55 +08:00
}
2020-02-05 19:56:22 +08:00
ShapeSink& ShapeSink::AddShape(ShapePtr input, const Matrix3x2* input_matrix)
2020-01-21 10:09:55 +08:00
{
if (!IsOpened())
{
Open();
}
2020-02-05 19:56:22 +08:00
if (input && input->IsValid())
{
ComPtr<ID2D1Geometry> geo = input->GetGeometry();
win32::ThrowIfFailed(
geo->Outline(DX::ConvertToMatrix3x2F(input_matrix), D2D1_DEFAULT_FLATTENING_TOLERANCE, sink_.get()));
}
2020-01-21 10:09:55 +08:00
return (*this);
}
2020-02-05 19:56:22 +08:00
ShapeSink& ShapeSink::BeginPath(Point const& begin_pos)
2020-01-21 10:09:55 +08:00
{
if (!IsOpened())
{
Open();
}
sink_->BeginFigure(DX::ConvertToPoint2F(begin_pos), D2D1_FIGURE_BEGIN_FILLED);
return (*this);
}
2020-02-05 19:56:22 +08:00
ShapeSink& ShapeSink::EndPath(bool closed)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(sink_);
sink_->EndFigure(closed ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN);
return (*this);
}
2020-02-05 19:56:22 +08:00
ShapeSink& ShapeSink::AddLine(Point const& point)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(sink_);
sink_->AddLine(DX::ConvertToPoint2F(point));
return (*this);
}
2020-02-05 19:56:22 +08:00
ShapeSink& ShapeSink::AddLines(Vector<Point> const& points)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(sink_);
sink_->AddLines(reinterpret_cast<const D2D_POINT_2F*>(&points[0]), static_cast<uint32_t>(points.size()));
return (*this);
}
2020-02-05 19:56:22 +08:00
ShapeSink& kiwano::ShapeSink::AddLines(const Point* points, size_t count)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(sink_);
sink_->AddLines(reinterpret_cast<const D2D_POINT_2F*>(points), UINT32(count));
return (*this);
}
2020-02-05 19:56:22 +08:00
ShapeSink& ShapeSink::AddBezier(Point const& point1, Point const& point2, Point const& point3)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(sink_);
sink_->AddBezier(
D2D1::BezierSegment(DX::ConvertToPoint2F(point1), DX::ConvertToPoint2F(point2), DX::ConvertToPoint2F(point3)));
return (*this);
}
2020-02-05 19:56:22 +08:00
ShapeSink& ShapeSink::AddArc(Point const& point, Size const& radius, float rotation, bool clockwise, bool is_small)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(sink_);
sink_->AddArc(D2D1::ArcSegment(DX::ConvertToPoint2F(point), DX::ConvertToSizeF(radius), rotation,
clockwise ? D2D1_SWEEP_DIRECTION_CLOCKWISE : D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE,
is_small ? D2D1_ARC_SIZE_SMALL : D2D1_ARC_SIZE_LARGE));
return (*this);
}
2020-02-06 16:54:47 +08:00
ShapeSink& ShapeSink::Combine(ShapePtr shape_a, ShapePtr shape_b, CombineMode mode, const Matrix3x2* matrix)
2020-01-21 10:09:55 +08:00
{
if (!IsOpened())
{
Open();
}
2020-02-06 16:54:47 +08:00
if (shape_a && shape_b)
{
ComPtr<ID2D1Geometry> geo_a_raw = shape_a->geo_;
ComPtr<ID2D1Geometry> geo_b_raw = shape_b->geo_;
win32::ThrowIfFailed(geo_a_raw->CombineWithGeometry(geo_b_raw.get(), D2D1_COMBINE_MODE(mode),
DX::ConvertToMatrix3x2F(matrix), sink_.get()));
}
2020-01-21 10:09:55 +08:00
return (*this);
}
2020-02-05 19:56:22 +08:00
void ShapeSink::Clear()
2020-01-21 10:09:55 +08:00
{
Close();
path_geo_.reset();
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano