2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 virtual ~StrategyMethod(){}
31 virtual bool dispose(RenderMethod& renderer) = 0;
32 virtual bool update(RenderMethod& renderer, const RenderTransform* transform, RenderUpdateFlag pFlag) = 0;
33 virtual bool render(RenderMethod& renderer) = 0;
34 virtual bool bounds(float* x, float* y, float* w, float* h) const = 0;
35 virtual unique_ptr<Paint> duplicate() = 0;
40 StrategyMethod* smethod = nullptr;
41 RenderTransform *rTransform = nullptr;
42 uint32_t flag = RenderUpdateFlag::None;
45 if (smethod) delete(smethod);
46 if (rTransform) delete(rTransform);
49 void method(StrategyMethod* method)
54 bool rotate(float degree)
57 if (fabsf(degree - rTransform->degree) <= FLT_EPSILON) return true;
59 if (fabsf(degree) <= FLT_EPSILON) return true;
60 rTransform = new RenderTransform();
61 if (!rTransform) return false;
63 rTransform->degree = degree;
64 if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
69 bool scale(float factor)
72 if (fabsf(factor - rTransform->scale) <= FLT_EPSILON) return true;
74 if (fabsf(factor) <= FLT_EPSILON) return true;
75 rTransform = new RenderTransform();
76 if (!rTransform) return false;
78 rTransform->scale = factor;
79 if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
84 bool translate(float x, float y)
87 if (fabsf(x - rTransform->x) <= FLT_EPSILON && fabsf(y - rTransform->y) <= FLT_EPSILON) return true;
89 if (fabsf(x) <= FLT_EPSILON && fabsf(y) <= FLT_EPSILON) return true;
90 rTransform = new RenderTransform();
91 if (!rTransform) return false;
95 if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
100 bool transform(const Matrix& m)
103 rTransform = new RenderTransform();
104 if (!rTransform) return false;
106 rTransform->override(m);
107 flag |= RenderUpdateFlag::Transform;
112 bool bounds(float* x, float* y, float* w, float* h) const
114 return smethod->bounds(x, y, w, h);
117 bool dispose(RenderMethod& renderer)
119 return smethod->dispose(renderer);
122 bool update(RenderMethod& renderer, const RenderTransform* pTransform, uint32_t pFlag)
124 if (flag & RenderUpdateFlag::Transform) {
125 if (!rTransform) return false;
126 if (!rTransform->update()) {
128 rTransform = nullptr;
132 auto newFlag = static_cast<RenderUpdateFlag>(pFlag | flag);
133 flag = RenderUpdateFlag::None;
135 if (rTransform && pTransform) {
136 RenderTransform outTransform(pTransform, rTransform);
137 return smethod->update(renderer, &outTransform, newFlag);
139 auto outTransform = pTransform ? pTransform : rTransform;
140 return smethod->update(renderer, outTransform, newFlag);
144 bool render(RenderMethod& renderer)
146 return smethod->render(renderer);
149 unique_ptr<Paint> duplicate()
151 return smethod->duplicate();
157 struct PaintMethod : StrategyMethod
161 PaintMethod(T* _inst) : inst(_inst) {}
164 bool bounds(float* x, float* y, float* w, float* h) const override
166 return inst->bounds(x, y, w, h);
169 bool dispose(RenderMethod& renderer) override
171 return inst->dispose(renderer);
174 bool update(RenderMethod& renderer, const RenderTransform* transform, RenderUpdateFlag flag) override
176 return inst->update(renderer, transform, flag);
179 bool render(RenderMethod& renderer) override
181 return inst->render(renderer);
184 unique_ptr<Paint> duplicate() override
186 return inst->duplicate();
191 #endif //_TVG_PAINT_H_