Skip to content

Commit 3932576

Browse files
committed
Adding coercion of list of 2 to SkV2 struct in SkRuntimeEffectBuilder.setUniform
SkV2 maps naturally to (x,y) co-ordinates for RuntimeEffect, and probably should have been added earlier. Part of kyamagu#320
1 parent ac01599 commit 3932576

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/skia/RuntimeEffect.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdexcept>
22
#include "common.h"
33
#include <include/effects/SkRuntimeEffect.h>
4-
//#include <include/core/SkM44.h> // defines SkV3, SkV4 ; M44 used in Matrix/Canvas ; Revisit.
4+
//#include <include/core/SkM44.h> // defines SkV2, SkV3, SkV4 ; M44 used in Matrix/Canvas ; Revisit.
55
#include <pybind11/stl_bind.h>
66

77
PYBIND11_MAKE_OPAQUE(std::vector<SkRuntimeEffect::ChildPtr>)
@@ -16,6 +16,22 @@ py::class_<SkSpan<const SkRuntimeEffect::ChildPtr>> span_runtime_effect_childptr
1616

1717
py::class_<SkRuntimeEffectBuilder> runtime_effect_builder(m, "RuntimeEffectBuilder");
1818

19+
py::class_<SkV2>(m, "V2")
20+
.def(py::init(
21+
[] (float x, float y) {
22+
return SkV2{x, y};
23+
}))
24+
.def(py::init(
25+
[] (py::tuple v2) {
26+
if (v2.size() != 2)
27+
throw py::value_error("V2 must have exactly two elements.");
28+
return SkV2{v2[0].cast<float>(), v2[1].cast<float>()};
29+
}),
30+
py::arg("v2"))
31+
;
32+
33+
py::implicitly_convertible<py::tuple, SkV2>();
34+
1935
py::class_<SkV3>(m, "V3")
2036
.def(py::init(
2137
[] (float x, float y, float z) {
@@ -197,9 +213,11 @@ runtime_effect_builder
197213
py::arg("name"), py::arg("uniform"))
198214
.def("setUniform",
199215
[] (SkRuntimeEffectBuilder& builder, std::string_view name, py::list vN) {
200-
if (vN.size() != 3 && vN.size() != 4)
201-
throw py::value_error("Input must have exactly three or four elements.");
216+
if (vN.size() != 2 && vN.size() != 3 && vN.size() != 4)
217+
throw py::value_error("Input must have exactly two, three or four elements.");
202218
auto v = builder.uniform(name);
219+
if (vN.size() == 2)
220+
v = SkV2{vN[0].cast<float>(), vN[1].cast<float>()};
203221
if (vN.size() == 3)
204222
v = SkV3{vN[0].cast<float>(), vN[1].cast<float>(), vN[2].cast<float>()};
205223
if (vN.size() == 4)

0 commit comments

Comments
 (0)