Kiwano Engine v1.3.x
ShapeActor.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/2d/Actor.h>
23#include <kiwano/render/Brush.h>
24#include <kiwano/render/Shape.h>
25#include <kiwano/render/ShapeMaker.h>
26#include <kiwano/render/StrokeStyle.h>
27
28namespace kiwano
29{
30
40class KGE_API ShapeActor : public Actor
41{
42public:
43 ShapeActor();
44
49
55 ShapeActor(RefPtr<Shape> shape, const Color& fill_color, const Color& stroke_color);
56
62 ShapeActor(RefPtr<Shape> shape, RefPtr<Brush> fill_brush, RefPtr<Brush> stroke_brush);
63
64 virtual ~ShapeActor();
65
68 RefPtr<Brush> GetFillBrush() const;
69
72 RefPtr<Brush> GetStrokeBrush() const;
73
76 RefPtr<StrokeStyle> GetStrokeStyle() const;
77
80 RefPtr<Shape> GetShape() const;
81
84 Rect GetBounds() const override;
85
88 Rect GetBoundingBox() const override;
89
92 bool ContainsPoint(const Point& point) const override;
93
97 void SetFillColor(const Color& color);
98
102 void SetFillBrush(RefPtr<Brush> brush);
103
107 void SetStrokeColor(const Color& color);
108
112 void SetStrokeBrush(RefPtr<Brush> brush);
113
116 void SetStrokeStyle(RefPtr<StrokeStyle> stroke_style);
117
120 void SetShape(RefPtr<Shape> shape);
121
122 void OnRender(RenderContext& ctx) override;
123
124protected:
125 bool CheckVisibility(RenderContext& ctx) const override;
126
127private:
128 RefPtr<Brush> fill_brush_;
129 RefPtr<Brush> stroke_brush_;
130 RefPtr<StrokeStyle> stroke_style_;
131 Rect bounds_;
132 RefPtr<Shape> shape_;
133};
134
137class KGE_API LineActor : public ShapeActor
138{
139public:
140 LineActor();
141
146 LineActor(const Point& begin, const Point& end);
147
148 virtual ~LineActor();
149
152 const Point& GetBeginPoint() const;
153
156 const Point& GetEndPoint() const;
157
161 void SetBeginPoint(const Point& begin);
162
166 void SetEndPoint(const Point& end);
167
172 void SetLine(const Point& begin, const Point& end);
173
174private:
175 Point begin_;
176 Point end_;
177};
178
181class KGE_API RectActor : public ShapeActor
182{
183public:
184 RectActor();
185
189 RectActor(const Size& size);
190
191 virtual ~RectActor();
192
195 const Size& GetRectSize() const;
196
200 void SetRectSize(const Size& size);
201
202private:
203 Size rect_size_;
204};
205
208class KGE_API RoundedRectActor : public ShapeActor
209{
210public:
212
217 RoundedRectActor(const Size& size, const Vec2& radius);
218
219 virtual ~RoundedRectActor();
220
223 Vec2 GetRadius() const;
224
227 Size GetRectSize() const;
228
232 void SetRadius(const Vec2& radius);
233
237 void SetRectSize(const Size& size);
238
243 void SetRoundedRect(const Size& size, const Vec2& radius);
244
245private:
246 Size rect_size_;
247 Vec2 radius_;
248};
249
252class KGE_API CircleActor : public ShapeActor
253{
254public:
255 CircleActor();
256
260 CircleActor(float radius);
261
262 virtual ~CircleActor();
263
266 float GetRadius() const;
267
271 void SetRadius(float radius);
272
273private:
274 float radius_;
275};
276
279class KGE_API EllipseActor : public ShapeActor
280{
281public:
282 EllipseActor();
283
287 EllipseActor(const Vec2& radius);
288
289 virtual ~EllipseActor();
290
293 Vec2 GetRadius() const;
294
298 void SetRadius(const Vec2& radius);
299
300private:
301 Vec2 radius_;
302};
303
306class KGE_API PolygonActor : public ShapeActor
307{
308public:
309 PolygonActor();
310
314 PolygonActor(const Vector<Point>& vertices);
315
316 virtual ~PolygonActor();
317
321 void SetVertices(const Vector<Point>& vertices);
322};
323
326inline void ShapeActor::SetStrokeColor(const Color& color)
327{
328 if (!stroke_brush_)
329 {
330 stroke_brush_ = MakePtr<Brush>();
331 }
332 stroke_brush_->SetColor(color);
333}
334
335inline void ShapeActor::SetFillColor(const Color& color)
336{
337 if (!fill_brush_)
338 {
339 fill_brush_ = MakePtr<Brush>();
340 }
341 fill_brush_->SetColor(color);
342}
343
345{
346 fill_brush_ = brush;
347}
348
350{
351 stroke_brush_ = brush;
352}
353
355{
356 return fill_brush_;
357}
358
360{
361 return stroke_brush_;
362}
363
365{
366 return stroke_style_;
367}
368
370{
371 return shape_;
372}
373
375{
376 stroke_style_ = stroke_style;
377}
378
379inline const Point& LineActor::GetBeginPoint() const
380{
381 return begin_;
382}
383
384inline const Point& LineActor::GetEndPoint() const
385{
386 return end_;
387}
388
389inline void LineActor::SetBeginPoint(const Point& begin)
390{
391 SetLine(begin, end_);
392}
393
394inline void LineActor::SetEndPoint(const Point& end)
395{
396 SetLine(begin_, end);
397}
398
399inline const Size& RectActor::GetRectSize() const
400{
401 return rect_size_;
402}
403
405{
406 return radius_;
407}
408
410{
411 return GetSize();
412}
413
414inline float CircleActor::GetRadius() const
415{
416 return radius_;
417}
418
420{
421 return radius_;
422}
423
424} // namespace kiwano
角色
Definition: Actor.h:63
virtual Size GetSize() const
获取大小
Definition: Actor.h:608
圆形角色
Definition: ShapeActor.h:253
float GetRadius() const
获取圆形半径
Definition: ShapeActor.h:414
Definition: Color.h:41
椭圆角色
Definition: ShapeActor.h:280
Vec2 GetRadius() const
获取椭圆半径
Definition: ShapeActor.h:419
线段角色
Definition: ShapeActor.h:138
void SetBeginPoint(const Point &begin)
设置线段起点
Definition: ShapeActor.h:389
void SetEndPoint(const Point &end)
设置线段终点
Definition: ShapeActor.h:394
const Point & GetEndPoint() const
获取线段终点
Definition: ShapeActor.h:384
const Point & GetBeginPoint() const
获取线段起点
Definition: ShapeActor.h:379
void SetLine(const Point &begin, const Point &end)
设置线段起点和终点
Definition: ShapeActor.cpp:124
多边形角色
Definition: ShapeActor.h:307
矩形角色
Definition: ShapeActor.h:182
const Size & GetRectSize() const
获取矩形大小
Definition: ShapeActor.h:399
引用计数智能指针
Definition: RefBasePtr.hpp:35
渲染上下文
Definition: RenderContext.h:62
圆角矩形角色
Definition: ShapeActor.h:209
Size GetRectSize() const
获取圆角矩形大小
Definition: ShapeActor.h:409
Vec2 GetRadius() const
获取圆角半径
Definition: ShapeActor.h:404
形状角色
Definition: ShapeActor.h:41
RefPtr< Shape > GetShape() const
获取形状
Definition: ShapeActor.h:369
RefPtr< StrokeStyle > GetStrokeStyle() const
获取线条样式
Definition: ShapeActor.h:364
RefPtr< Brush > GetStrokeBrush() const
获取轮廓画刷
Definition: ShapeActor.h:359
void SetStrokeColor(const Color &color)
设置轮廓颜色
Definition: ShapeActor.h:326
void SetFillColor(const Color &color)
设置填充颜色
Definition: ShapeActor.h:335
void SetStrokeBrush(RefPtr< Brush > brush)
设置轮廓画刷
Definition: ShapeActor.h:349
RefPtr< Brush > GetFillBrush() const
获取填充画刷
Definition: ShapeActor.h:354
void SetStrokeStyle(RefPtr< StrokeStyle > stroke_style)
设置线条样式
Definition: ShapeActor.h:374
void SetFillBrush(RefPtr< Brush > brush)
设置填充画刷
Definition: ShapeActor.h:344