243 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			C++
		
	
	
	
			
		
		
	
	
			243 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			C++
		
	
	
	
// Copyright (c) 2016-2018 Kiwano - 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.
 | 
						|
 | 
						|
#pragma once
 | 
						|
#include "Node.h"
 | 
						|
#include "Font.hpp"
 | 
						|
#include "TextStyle.hpp"
 | 
						|
#include "../renderer/TextRenderer.h"
 | 
						|
 | 
						|
#undef DrawText
 | 
						|
 | 
						|
namespace kiwano
 | 
						|
{
 | 
						|
	// »²¼
 | 
						|
	class KGE_API Canvas
 | 
						|
		: public VisualNode
 | 
						|
	{
 | 
						|
	public:
 | 
						|
		Canvas();
 | 
						|
 | 
						|
		Canvas(
 | 
						|
			Size const& size
 | 
						|
		);
 | 
						|
 | 
						|
		Canvas(
 | 
						|
			float width,
 | 
						|
			float height
 | 
						|
		);
 | 
						|
 | 
						|
		virtual ~Canvas();
 | 
						|
 | 
						|
		// ¿ªÊ¼»æÍ¼
 | 
						|
		void BeginDraw();
 | 
						|
 | 
						|
		// ½áÊø»æÍ¼
 | 
						|
		void EndDraw();
 | 
						|
 | 
						|
		// »Ö±Ïß
 | 
						|
		void DrawLine(
 | 
						|
			const Point& begin,
 | 
						|
			const Point& end
 | 
						|
		);
 | 
						|
 | 
						|
		// »Ô²Ðα߿ò
 | 
						|
		void DrawCircle(
 | 
						|
			const Point& center,
 | 
						|
			float radius
 | 
						|
		);
 | 
						|
 | 
						|
		// »ÍÖÔ²Ðα߿ò
 | 
						|
		void DrawEllipse(
 | 
						|
			const Point& center,
 | 
						|
			float radius_x,
 | 
						|
			float radius_y
 | 
						|
		);
 | 
						|
 | 
						|
		// »¾ØÐα߿ò
 | 
						|
		void DrawRect(
 | 
						|
			const Rect& rect
 | 
						|
		);
 | 
						|
 | 
						|
		// »Ô²½Ç¾ØÐα߿ò
 | 
						|
		void DrawRoundedRect(
 | 
						|
			const Rect& rect,
 | 
						|
			float radius_x,
 | 
						|
			float radius_y
 | 
						|
		);
 | 
						|
 | 
						|
		// »Í¼Æ¬
 | 
						|
		void DrawImage(
 | 
						|
			ImagePtr image,
 | 
						|
			float opacity = 1.f
 | 
						|
		);
 | 
						|
 | 
						|
		// »ÎÄ×Ö
 | 
						|
		void DrawText(
 | 
						|
			String const& text,		/* ÎÄ×Ö */
 | 
						|
			Point const& point		/* ÎÄ×ÖλÖà */
 | 
						|
		);
 | 
						|
 | 
						|
		// »¼¸ºÎͼÐα߿ò
 | 
						|
		void DrawGeometry(
 | 
						|
			GeometryPtr geo
 | 
						|
		);
 | 
						|
 | 
						|
		// Ìî³äÔ²ÐÎ
 | 
						|
		void FillCircle(
 | 
						|
			const Point& center,
 | 
						|
			float radius
 | 
						|
		);
 | 
						|
 | 
						|
		// Ìî³äÍÖÔ²ÐÎ
 | 
						|
		void FillEllipse(
 | 
						|
			const Point& center,
 | 
						|
			float radius_x,
 | 
						|
			float radius_y
 | 
						|
		);
 | 
						|
 | 
						|
		// Ìî³ä¾ØÐÎ
 | 
						|
		void FillRect(
 | 
						|
			const Rect& rect
 | 
						|
		);
 | 
						|
 | 
						|
		// Ìî³äÔ²½Ç¾ØÐÎ
 | 
						|
		void FillRoundedRect(
 | 
						|
			const Rect& rect,
 | 
						|
			float radius_x,
 | 
						|
			float radius_y
 | 
						|
		);
 | 
						|
 | 
						|
		// Ìî³ä¼¸ºÎͼÐÎ
 | 
						|
		void FillGeometry(
 | 
						|
			GeometryPtr geo
 | 
						|
		);
 | 
						|
 | 
						|
		// ¿ªÊ¼»æÖÆÂ·¾¶
 | 
						|
		void BeginPath(
 | 
						|
			Point const& begin_pos	/* ·¾¶Æðʼµã */
 | 
						|
		);
 | 
						|
 | 
						|
		// ½áÊøÂ·¾¶
 | 
						|
		void EndPath(
 | 
						|
			bool closed = true		/* ·¾¶ÊÇ·ñ±ÕºÏ */
 | 
						|
		);
 | 
						|
 | 
						|
		// Ìí¼ÓÒ»ÌõÏß¶Î
 | 
						|
		void AddLine(
 | 
						|
			Point const& point		/* ¶Ëµã */
 | 
						|
		);
 | 
						|
 | 
						|
		// Ìí¼Ó¶àÌõÏß¶Î
 | 
						|
		void AddLines(
 | 
						|
			Array<Point> const& points
 | 
						|
		);
 | 
						|
 | 
						|
		// Ìí¼ÓÒ»ÌõÈý´Î·½±´Èû¶ûÇúÏß
 | 
						|
		void AddBezier(
 | 
						|
			Point const& point1,	/* ±´Èû¶ûÇúÏߵĵÚÒ»¸ö¿ØÖƵã */
 | 
						|
			Point const& point2,	/* ±´Èû¶ûÇúÏߵĵڶþ¸ö¿ØÖƵã */
 | 
						|
			Point const& point3		/* ±´Èû¶ûÇúÏßµÄÖÕµã */
 | 
						|
		);
 | 
						|
 | 
						|
		// Ìí¼Ó»¡Ïß
 | 
						|
		void AddArc(
 | 
						|
			Point const& point,		/* ÖÕµã */
 | 
						|
			Point const& radius,	/* ÍÖÔ²°ë¾¶ */
 | 
						|
			float rotation,			/* ÍÖÔ²Ðýת½Ç¶È */
 | 
						|
			bool clockwise = true,	/* ˳ʱÕë or ÄæÊ±Õë */
 | 
						|
			bool is_small = true	/* ÊÇ·ñȡСÓÚ 180¡ã µÄ»¡ */
 | 
						|
		);
 | 
						|
 | 
						|
		// ·¾¶Ãè±ß
 | 
						|
		void StrokePath();
 | 
						|
 | 
						|
		// ·¾¶Ìî³ä
 | 
						|
		void FillPath();
 | 
						|
 | 
						|
		// Çå¿Õ»²¼
 | 
						|
		void Clear();
 | 
						|
 | 
						|
		// ÉèÖÃÌî³äÑÕÉ«
 | 
						|
		void SetFillColor(
 | 
						|
			const Color& color
 | 
						|
		);
 | 
						|
 | 
						|
		// ÉèÖÃÏßÌõÑÕÉ«
 | 
						|
		void SetStrokeColor(
 | 
						|
			const Color& color
 | 
						|
		);
 | 
						|
 | 
						|
		// ÉèÖÃÏßÌõ¿í¶È
 | 
						|
		void SetStrokeWidth(
 | 
						|
			float width
 | 
						|
		);
 | 
						|
 | 
						|
		// ÉèÖÃÏßÌõÏཻÑùʽ
 | 
						|
		void SetOutlineJoinStyle(
 | 
						|
			StrokeStyle outline_join
 | 
						|
		);
 | 
						|
 | 
						|
		// ÉèÖÃÎÄ×ֻˢÑùʽ
 | 
						|
		void SetTextStyle(
 | 
						|
			Font const& font,
 | 
						|
			TextStyle const& text_style
 | 
						|
		);
 | 
						|
 | 
						|
		// »ñÈ¡Ìî³äÑÕÉ«
 | 
						|
		Color GetFillColor() const;
 | 
						|
 | 
						|
		// »ñÈ¡ÏßÌõÑÕÉ«
 | 
						|
		Color GetStrokeColor() const;
 | 
						|
 | 
						|
		// »ñÈ¡ÏßÌõ¿í¶È
 | 
						|
		float GetStrokeWidth() const;
 | 
						|
 | 
						|
		// ±ä»»»±Ê
 | 
						|
		void SetBrushTransform(
 | 
						|
			Matrix const& transform
 | 
						|
		);
 | 
						|
 | 
						|
		// µ¼³öΪͼƬ
 | 
						|
		ImagePtr ExportToImage() const;
 | 
						|
 | 
						|
		void OnRender() override;
 | 
						|
 | 
						|
	protected:
 | 
						|
		ComPtr<ID2D1Bitmap> const& GetBitmap() const;
 | 
						|
 | 
						|
	protected:
 | 
						|
		float						stroke_width_;
 | 
						|
		Font						text_font_;
 | 
						|
		TextStyle					text_style_;
 | 
						|
 | 
						|
		ComPtr<ID2D1PathGeometry>		current_geometry_;
 | 
						|
		ComPtr<ID2D1GeometrySink>		current_sink_;
 | 
						|
		ComPtr<ID2D1StrokeStyle>		outline_join_style_;
 | 
						|
		ComPtr<ID2D1SolidColorBrush>	fill_brush_;
 | 
						|
		ComPtr<ID2D1SolidColorBrush>	stroke_brush_;
 | 
						|
		ComPtr<IDWriteTextFormat>		text_format_;
 | 
						|
		ComPtr<ITextRenderer>			text_renderer_;
 | 
						|
		ComPtr<ID2D1BitmapRenderTarget>	render_target_;
 | 
						|
 | 
						|
		mutable bool					cache_expired_;
 | 
						|
		mutable ComPtr<ID2D1Bitmap>		bitmap_cached_;
 | 
						|
	};
 | 
						|
} |