2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef _TVG_SHAPE_NODE_CPP_
18 #define _TVG_SHAPE_NODE_CPP_
20 #include "tvgCommon.h"
21 #include "tvgShapePath.h"
23 /************************************************************************/
24 /* Internal Class Implementation */
25 /************************************************************************/
43 struct ShapeNode::Impl
45 ShapeTransform *transform = nullptr;
46 ShapeFill *fill = nullptr;
47 ShapeStroke *stroke = nullptr;
48 ShapePath *path = nullptr;
49 uint8_t color[4] = {0, 0, 0, 0}; //r, g, b, a
50 void *edata = nullptr; //engine data
52 Impl() : path(new ShapePath)
58 if (path) delete(path);
59 if (stroke) delete(stroke);
60 if (fill) delete(fill);
61 if (transform) delete(transform);
66 /************************************************************************/
67 /* External Class Implementation */
68 /************************************************************************/
70 ShapeNode :: ShapeNode() : pImpl(make_unique<Impl>())
75 ShapeNode :: ~ShapeNode()
77 cout << "ShapeNode(" << this << ") destroyed!" << endl;
81 unique_ptr<ShapeNode> ShapeNode::gen() noexcept
83 return unique_ptr<ShapeNode>(new ShapeNode);
87 void* ShapeNode::engine() noexcept
89 auto impl = pImpl.get();
95 int ShapeNode::update(RenderMethod* engine) noexcept
97 auto impl = pImpl.get();
100 impl->edata = engine->prepare(*this, impl->edata, RenderMethod::UpdateFlag::All);
101 if (impl->edata) return 0;
106 int ShapeNode::clear() noexcept
108 auto impl = pImpl.get();
111 return impl->path->clear();
115 int ShapeNode::pathCommands(const PathCommand** cmds) const noexcept
117 auto impl = pImpl.get();
118 assert(impl && cmds);
120 *cmds = impl->path->cmds;
122 return impl->path->cmdCnt;
126 int ShapeNode::pathCoords(const Point** pts) const noexcept
128 auto impl = pImpl.get();
131 *pts = impl->path->pts;
133 return impl->path->ptsCnt;
137 int ShapeNode::appendCircle(float cx, float cy, float radius) noexcept
143 int ShapeNode::appendRect(float x, float y, float w, float h, float radius) noexcept
145 auto impl = pImpl.get();
148 //clamping radius by minimum size
149 auto min = (w < h ? w : h) * 0.5f;
150 if (radius > min) radius = min;
154 impl->path->reserve(5, 4);
155 impl->path->moveTo(x, y);
156 impl->path->lineTo(x + w, y);
157 impl->path->lineTo(x + w, y + h);
158 impl->path->lineTo(x, y + h);
161 } else if (w == h && radius * 2 == w) {
162 appendCircle(x + (w * 0.5f), y + (h * 0.5f), radius);
171 int ShapeNode::fill(size_t r, size_t g, size_t b, size_t a) noexcept
173 auto impl = pImpl.get();
185 int ShapeNode::fill(size_t* r, size_t* g, size_t* b, size_t* a) const noexcept
187 auto impl = pImpl.get();
190 if (r) *r = impl->color[0];
191 if (g) *g = impl->color[1];
192 if (b) *b = impl->color[2];
193 if (a) *a = impl->color[3];
198 #endif //_TVG_SHAPE_NODE_CPP_