Mike Reed | ec80d90 | 2019-02-12 11:31:27 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "Sample.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkFont.h" |
| 11 | #include "SkFontMetrics.h" |
| 12 | #include "SkPath.h" |
| 13 | |
| 14 | #include "SkSGDraw.h" |
| 15 | #include "SkSGColor.h" |
| 16 | #include "SkSGGroup.h" |
| 17 | #include "SkSGRect.h" |
| 18 | #include "SkSGScene.h" |
| 19 | |
Mike Reed | be89130 | 2019-02-12 15:24:16 -0500 | [diff] [blame] | 20 | struct PerNodeInfo { |
| 21 | // key |
| 22 | sksg::Draw* fDraw; |
| 23 | |
| 24 | // value(s) |
| 25 | sksg::GeometryNode* fGeo; |
| 26 | sksg::PaintNode* fPaint; |
| 27 | }; |
| 28 | |
Mike Reed | ec80d90 | 2019-02-12 11:31:27 -0500 | [diff] [blame] | 29 | class SampleSG : public Sample { |
Mike Reed | be89130 | 2019-02-12 15:24:16 -0500 | [diff] [blame] | 30 | SkTDArray<PerNodeInfo> fSideCar; |
Mike Reed | ec80d90 | 2019-02-12 11:31:27 -0500 | [diff] [blame] | 31 | sk_sp<sksg::Group> fGroup; |
| 32 | std::unique_ptr<sksg::Scene> fScene; |
Mike Reed | be89130 | 2019-02-12 15:24:16 -0500 | [diff] [blame] | 33 | |
| 34 | PerNodeInfo* findInfo(sksg::Draw* key) { |
| 35 | for (int i = 0; i < fSideCar.count(); ++i) { |
| 36 | if (fSideCar[i].fDraw == key) { |
| 37 | return &fSideCar[i]; |
| 38 | } |
| 39 | } |
| 40 | return nullptr; |
| 41 | } |
| 42 | |
| 43 | void appendNode(sk_sp<sksg::Draw> d, sk_sp<sksg::GeometryNode> g, sk_sp<sksg::PaintNode> p) { |
| 44 | fGroup->addChild(d); |
| 45 | auto sc = fSideCar.append(); |
| 46 | sc->fDraw = d.get(); |
| 47 | sc->fGeo = g.get(); |
| 48 | sc->fPaint = p.get(); |
| 49 | } |
| 50 | |
Mike Reed | ec80d90 | 2019-02-12 11:31:27 -0500 | [diff] [blame] | 51 | public: |
| 52 | SampleSG() { |
| 53 | fGroup = sksg::Group::Make(); |
| 54 | |
| 55 | fScene = sksg::Scene::Make(fGroup, sksg::AnimatorList()); |
| 56 | |
| 57 | auto r = sksg::Rect::Make({20, 20, 400, 300}); |
| 58 | auto p = sksg::Color::Make(SK_ColorRED); |
| 59 | auto d = sksg::Draw::Make(r, p); |
Mike Reed | be89130 | 2019-02-12 15:24:16 -0500 | [diff] [blame] | 60 | this->appendNode(d, r, p); |
Mike Reed | ec80d90 | 2019-02-12 11:31:27 -0500 | [diff] [blame] | 61 | |
| 62 | r = sksg::Rect::Make({60, 70, 300, 400}); |
| 63 | p = sksg::Color::Make(SK_ColorBLUE); |
| 64 | d = sksg::Draw::Make(r, p); |
Mike Reed | be89130 | 2019-02-12 15:24:16 -0500 | [diff] [blame] | 65 | this->appendNode(d, r, p); |
Mike Reed | ec80d90 | 2019-02-12 11:31:27 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | protected: |
| 69 | bool onQuery(Sample::Event* evt) override { |
| 70 | if (Sample::TitleQ(*evt)) { |
| 71 | Sample::TitleR(evt, "SceneGraph"); |
| 72 | return true; |
| 73 | } |
| 74 | return this->INHERITED::onQuery(evt); |
| 75 | } |
| 76 | |
| 77 | void onDrawContent(SkCanvas* canvas) override { |
| 78 | fScene->render(canvas); |
| 79 | } |
| 80 | |
Mike Reed | be89130 | 2019-02-12 15:24:16 -0500 | [diff] [blame] | 81 | Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
| 82 | if (auto node = fScene->nodeAt({x, y})) { |
| 83 | Click* click = new Click(this); |
| 84 | click->fMeta.setPtr("node", (void*)node); |
| 85 | return click; |
| 86 | } |
Mike Reed | ec80d90 | 2019-02-12 11:31:27 -0500 | [diff] [blame] | 87 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 88 | } |
| 89 | |
| 90 | bool onClick(Click* click) override { |
Mike Reed | be89130 | 2019-02-12 15:24:16 -0500 | [diff] [blame] | 91 | sksg::Draw* node = nullptr; |
| 92 | if (click->fMeta.findPtr("node", (void**)&node)) { |
| 93 | if (auto info = this->findInfo(node)) { |
| 94 | auto geo = info->fGeo; |
| 95 | sksg::Rect* r = (sksg::Rect*)geo; |
| 96 | SkScalar dx = click->fCurr.fX - click->fPrev.fX; |
| 97 | SkScalar dy = click->fCurr.fY - click->fPrev.fY; |
| 98 | r->setL(r->getL() + dx); |
| 99 | r->setR(r->getR() + dx); |
| 100 | r->setT(r->getT() + dy); |
| 101 | r->setB(r->getB() + dy); |
| 102 | } |
| 103 | return true; |
| 104 | } |
Mike Reed | ec80d90 | 2019-02-12 11:31:27 -0500 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | |
| 110 | typedef Sample INHERITED; |
| 111 | }; |
| 112 | |
| 113 | ////////////////////////////////////////////////////////////////////////////// |
| 114 | |
| 115 | DEF_SAMPLE( return new SampleSG(); ) |