2018-10-03 22:02:46 +08:00
|
|
|
// Copyright (c) 2016-2018 Easy2D - Nomango
|
|
|
|
|
//
|
|
|
|
|
// 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:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
|
2018-09-06 23:26:32 +08:00
|
|
|
#include "..\e2dobject.h"
|
2018-09-05 13:17:07 +08:00
|
|
|
#include "..\e2dmodule.h"
|
2018-08-23 00:03:26 +08:00
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::Canvas::Canvas(float width, float height)
|
2018-09-04 22:42:34 +08:00
|
|
|
: render_target_(nullptr)
|
|
|
|
|
, fill_brush_(nullptr)
|
|
|
|
|
, line_brush_(nullptr)
|
|
|
|
|
, stroke_style_(nullptr)
|
|
|
|
|
, stroke_width_(1.0f)
|
|
|
|
|
, stroke_(Stroke::Miter)
|
2018-08-23 00:03:26 +08:00
|
|
|
{
|
2018-10-03 18:04:04 +08:00
|
|
|
render_target_ = Device::GetGraphics()->GetRenderTarget();
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->AddRef();
|
2018-08-23 00:03:26 +08:00
|
|
|
|
|
|
|
|
ThrowIfFailed(
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->CreateSolidColorBrush(
|
2018-08-23 00:03:26 +08:00
|
|
|
D2D1::ColorF(D2D1::ColorF::White),
|
2018-09-04 22:42:34 +08:00
|
|
|
&fill_brush_
|
2018-08-23 00:03:26 +08:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ThrowIfFailed(
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->CreateSolidColorBrush(
|
2018-08-23 00:03:26 +08:00
|
|
|
D2D1::ColorF(D2D1::ColorF::White),
|
2018-09-04 22:42:34 +08:00
|
|
|
&line_brush_
|
2018-08-23 00:03:26 +08:00
|
|
|
)
|
|
|
|
|
);
|
2018-08-23 12:25:31 +08:00
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
this->SetClipEnabled(true);
|
|
|
|
|
this->SetWidth(width);
|
|
|
|
|
this->SetHeight(height);
|
|
|
|
|
this->SetStrokeStyle(stroke_);
|
2018-08-23 00:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::Canvas::~Canvas()
|
2018-08-23 00:03:26 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
SafeRelease(line_brush_);
|
|
|
|
|
SafeRelease(fill_brush_);
|
|
|
|
|
SafeRelease(render_target_);
|
2018-08-23 00:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::SetLineColor(const Color & color)
|
2018-08-23 00:03:26 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
line_brush_->SetColor(D2D_COLOR_F(color));
|
2018-08-23 00:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::SetFillColor(const Color & color)
|
2018-08-23 00:03:26 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
fill_brush_->SetColor(D2D_COLOR_F(color));
|
2018-08-23 00:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::SetStrokeWidth(float width)
|
2018-08-23 00:03:26 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
stroke_width_ = std::max(width, 0.f);
|
2018-08-23 00:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::SetStrokeStyle(Stroke strokeStyle)
|
2018-08-23 00:03:26 +08:00
|
|
|
{
|
|
|
|
|
switch (strokeStyle)
|
|
|
|
|
{
|
2018-10-16 14:13:15 +08:00
|
|
|
case easy2d::Stroke::Miter:
|
2018-10-03 18:04:04 +08:00
|
|
|
stroke_style_ = Device::GetGraphics()->GetMiterStrokeStyle();
|
2018-08-23 00:03:26 +08:00
|
|
|
break;
|
2018-10-16 14:13:15 +08:00
|
|
|
case easy2d::Stroke::Bevel:
|
2018-10-03 18:04:04 +08:00
|
|
|
stroke_style_ = Device::GetGraphics()->GetBevelStrokeStyle();
|
2018-08-23 00:03:26 +08:00
|
|
|
break;
|
2018-10-16 14:13:15 +08:00
|
|
|
case easy2d::Stroke::Round:
|
2018-10-03 18:04:04 +08:00
|
|
|
stroke_style_ = Device::GetGraphics()->GetRoundStrokeStyle();
|
2018-08-23 00:03:26 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::Color easy2d::Canvas::GetLineColor() const
|
2018-08-23 12:25:31 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return line_brush_->GetColor();
|
2018-08-23 12:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::Color easy2d::Canvas::GetFillColor() const
|
2018-08-23 12:25:31 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return fill_brush_->GetColor();
|
2018-08-23 12:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
float easy2d::Canvas::GetStrokeWidth() const
|
2018-08-23 12:25:31 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return stroke_width_;
|
2018-08-23 12:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::Stroke easy2d::Canvas::GetStrokeStyle() const
|
2018-08-23 12:25:31 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return stroke_;
|
2018-08-23 12:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::DrawLine(const Point & begin, const Point & end)
|
2018-08-23 00:03:26 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->DrawLine(
|
2018-08-23 00:03:26 +08:00
|
|
|
D2D1::Point2F(begin.x, begin.y),
|
|
|
|
|
D2D1::Point2F(end.x, end.y),
|
2018-09-04 22:42:34 +08:00
|
|
|
line_brush_,
|
|
|
|
|
stroke_width_,
|
|
|
|
|
stroke_style_
|
2018-08-23 00:03:26 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::DrawCircle(const Point & center, float radius)
|
2018-08-23 12:25:31 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->DrawEllipse(
|
2018-08-23 12:25:31 +08:00
|
|
|
D2D1::Ellipse(
|
|
|
|
|
D2D1::Point2F(
|
|
|
|
|
center.x,
|
|
|
|
|
center.y
|
|
|
|
|
),
|
|
|
|
|
radius,
|
|
|
|
|
radius
|
|
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
line_brush_,
|
|
|
|
|
stroke_width_,
|
|
|
|
|
stroke_style_
|
2018-08-23 12:25:31 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::DrawEllipse(const Point & center, float radius_x, float radius_y)
|
2018-08-23 12:25:31 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->DrawEllipse(
|
2018-08-23 12:25:31 +08:00
|
|
|
D2D1::Ellipse(
|
|
|
|
|
D2D1::Point2F(
|
|
|
|
|
center.x,
|
|
|
|
|
center.y
|
|
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
radius_x,
|
|
|
|
|
radius_y
|
2018-08-23 12:25:31 +08:00
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
line_brush_,
|
|
|
|
|
stroke_width_,
|
|
|
|
|
stroke_style_
|
2018-08-23 12:25:31 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::DrawRect(const Rect & rect)
|
2018-08-23 00:03:26 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->DrawRectangle(
|
2018-08-23 00:03:26 +08:00
|
|
|
D2D1::RectF(
|
|
|
|
|
rect.origin.x,
|
|
|
|
|
rect.origin.y,
|
|
|
|
|
rect.origin.x + rect.size.width,
|
|
|
|
|
rect.origin.y + rect.size.height
|
|
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
line_brush_,
|
|
|
|
|
stroke_width_,
|
|
|
|
|
stroke_style_
|
2018-08-23 00:03:26 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::DrawRoundedRect(const Rect & rect, float radius_x, float radius_y)
|
2018-08-23 00:03:26 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->DrawRoundedRectangle(
|
2018-08-23 00:03:26 +08:00
|
|
|
D2D1::RoundedRect(
|
|
|
|
|
D2D1::RectF(
|
|
|
|
|
rect.origin.x,
|
|
|
|
|
rect.origin.y,
|
|
|
|
|
rect.origin.x + rect.size.width,
|
|
|
|
|
rect.origin.y + rect.size.height
|
|
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
radius_x,
|
|
|
|
|
radius_y
|
2018-08-23 00:03:26 +08:00
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
line_brush_,
|
|
|
|
|
stroke_width_,
|
|
|
|
|
stroke_style_
|
2018-08-23 00:03:26 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::FillCircle(const Point & center, float radius)
|
2018-08-23 12:25:31 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->FillEllipse(
|
2018-08-23 12:25:31 +08:00
|
|
|
D2D1::Ellipse(
|
|
|
|
|
D2D1::Point2F(
|
|
|
|
|
center.x,
|
|
|
|
|
center.y
|
|
|
|
|
),
|
|
|
|
|
radius,
|
|
|
|
|
radius
|
|
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
fill_brush_
|
2018-08-23 12:25:31 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::FillEllipse(const Point & center, float radius_x, float radius_y)
|
2018-08-23 12:25:31 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->FillEllipse(
|
2018-08-23 12:25:31 +08:00
|
|
|
D2D1::Ellipse(
|
|
|
|
|
D2D1::Point2F(
|
|
|
|
|
center.x,
|
|
|
|
|
center.y
|
|
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
radius_x,
|
|
|
|
|
radius_y
|
2018-08-23 12:25:31 +08:00
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
fill_brush_
|
2018-08-23 12:25:31 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::FillRect(const Rect & rect)
|
2018-08-23 00:03:26 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->FillRectangle(
|
2018-08-23 00:03:26 +08:00
|
|
|
D2D1::RectF(
|
|
|
|
|
rect.origin.x,
|
|
|
|
|
rect.origin.y,
|
|
|
|
|
rect.origin.x + rect.size.width,
|
|
|
|
|
rect.origin.y + rect.size.height
|
|
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
fill_brush_
|
2018-08-23 00:03:26 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::Canvas::FillRoundedRect(const Rect & rect, float radius_x, float radius_y)
|
2018-08-23 00:03:26 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
render_target_->FillRoundedRectangle(
|
2018-08-23 00:03:26 +08:00
|
|
|
D2D1::RoundedRect(
|
|
|
|
|
D2D1::RectF(
|
|
|
|
|
rect.origin.x,
|
|
|
|
|
rect.origin.y,
|
|
|
|
|
rect.origin.x + rect.size.width,
|
|
|
|
|
rect.origin.y + rect.size.height
|
|
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
radius_x,
|
|
|
|
|
radius_y
|
2018-08-23 00:03:26 +08:00
|
|
|
),
|
2018-09-04 22:42:34 +08:00
|
|
|
fill_brush_
|
2018-08-23 00:03:26 +08:00
|
|
|
);
|
|
|
|
|
}
|