|
28 | 28 |
|
29 | 29 | struct UserExample : tvgexam::Example |
30 | 30 | { |
| 31 | + // This example demonstrates two approaches to accessing internal SVG scene nodes. |
31 | 32 | bool content(tvg::Canvas* canvas, uint32_t w, uint32_t h) override |
32 | 33 | { |
33 | | - //load the tvg file |
| 34 | + // SVG Picture |
34 | 35 | auto picture = tvg::Picture::gen(); |
35 | | - auto result = picture->load(EXAMPLE_DIR"/svg/favorite_on.svg"); |
36 | | - if (!tvgexam::verify(result)) return false; |
| 36 | + |
| 37 | + picture->accessible = true; // allow accessing to svg internals, it must be set before svg load call. |
| 38 | + picture->load(EXAMPLE_DIR"/svg/favorite_on.svg"); |
37 | 39 | picture->size(w, h); |
38 | 40 |
|
| 41 | + /* 1. This demonstartes a traversing the internal scene tree of the SVG picture. */ |
39 | 42 | auto accessor = unique_ptr<tvg::Accessor>(tvg::Accessor::gen()); |
40 | 43 |
|
41 | | - //The callback function from lambda expression. |
42 | | - //This function will be called for every paint nodes of the picture tree. |
43 | | - auto f = [](const tvg::Paint* paint, void* data) -> bool |
44 | | - { |
45 | | - if (paint->type() == tvg::Type::Shape) { |
46 | | - auto shape = (tvg::Shape*) paint; |
47 | | - //override color? |
48 | | - uint8_t r, g, b; |
49 | | - shape->fill(&r, &g, &b); |
50 | | - if (r == 255 && g == 180 && b == 0) |
51 | | - shape->fill(0, 0, 255); |
| 44 | + // If picture->accessible is set to true, only ID-accessible nodes are traversed, |
| 45 | + // which improves efficiency. Otherwise, all nodes are considered. |
| 46 | + auto f = [](const tvg::Paint* paint, void* data) -> bool { |
| 47 | + auto accessor = static_cast<tvg::Accessor*>(data); |
52 | 48 |
|
| 49 | + // figure out SVG node with the unique ID "star". |
| 50 | + if (!strcmp(accessor->name(paint->id), "star")) { |
| 51 | + // override color |
| 52 | + auto shape = (tvg::Shape*) paint; |
| 53 | + shape->fill(0, 0, 255); |
53 | 54 | } |
54 | 55 |
|
55 | | - //You can return false, to stop traversing immediately. |
| 56 | + // you can return false, to stop traversing immediately |
56 | 57 | return true; |
57 | 58 | }; |
58 | 59 |
|
59 | | - if (!tvgexam::verify(accessor->set(picture, f, nullptr))) return false; |
| 60 | + if (!tvgexam::verify(accessor->set(picture, f, accessor.get()))) return false; |
60 | 61 |
|
61 | | - // Try to retrieve the shape that corresponds to the SVG node with the unique ID "star". |
| 62 | + /* 2. This demonstrates a direct-access to the shape that corresponds to the SVG node with the unique ID "star". */ |
62 | 63 | if (auto paint = picture->paint(tvg::Accessor::id("star"))) { |
63 | 64 | auto shape = static_cast<tvg::Shape*>(const_cast<tvg::Paint*>(paint)); |
64 | 65 | shape->strokeFill(255, 255, 0); |
|
0 commit comments