2019-02-12 16:31:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontMetrics.h"
|
|
|
|
#include "include/core/SkPath.h"
|
2022-02-27 17:35:53 +00:00
|
|
|
#include "include/private/SkTDArray.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "samplecode/Sample.h"
|
2019-02-12 16:31:27 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "modules/sksg/include/SkSGDraw.h"
|
|
|
|
#include "modules/sksg/include/SkSGGroup.h"
|
|
|
|
#include "modules/sksg/include/SkSGPaint.h"
|
|
|
|
#include "modules/sksg/include/SkSGRect.h"
|
|
|
|
#include "modules/sksg/include/SkSGScene.h"
|
2019-02-12 16:31:27 +00:00
|
|
|
|
2019-02-12 20:24:16 +00:00
|
|
|
struct PerNodeInfo {
|
|
|
|
// key
|
|
|
|
sksg::Draw* fDraw;
|
|
|
|
|
|
|
|
// value(s)
|
|
|
|
sksg::GeometryNode* fGeo;
|
|
|
|
sksg::PaintNode* fPaint;
|
|
|
|
};
|
|
|
|
|
2019-02-12 16:31:27 +00:00
|
|
|
class SampleSG : public Sample {
|
2022-02-27 17:35:53 +00:00
|
|
|
// TODO(kjlubick) use a vector instead of our private SkTDArray
|
2019-02-12 20:24:16 +00:00
|
|
|
SkTDArray<PerNodeInfo> fSideCar;
|
2019-02-12 16:31:27 +00:00
|
|
|
sk_sp<sksg::Group> fGroup;
|
|
|
|
std::unique_ptr<sksg::Scene> fScene;
|
2019-02-12 20:24:16 +00:00
|
|
|
|
|
|
|
PerNodeInfo* findInfo(sksg::Draw* key) {
|
|
|
|
for (int i = 0; i < fSideCar.count(); ++i) {
|
|
|
|
if (fSideCar[i].fDraw == key) {
|
|
|
|
return &fSideCar[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void appendNode(sk_sp<sksg::Draw> d, sk_sp<sksg::GeometryNode> g, sk_sp<sksg::PaintNode> p) {
|
|
|
|
fGroup->addChild(d);
|
|
|
|
auto sc = fSideCar.append();
|
|
|
|
sc->fDraw = d.get();
|
|
|
|
sc->fGeo = g.get();
|
|
|
|
sc->fPaint = p.get();
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:31:27 +00:00
|
|
|
public:
|
|
|
|
SampleSG() {
|
|
|
|
fGroup = sksg::Group::Make();
|
|
|
|
|
2020-03-26 19:58:56 +00:00
|
|
|
fScene = sksg::Scene::Make(fGroup);
|
2019-02-12 16:31:27 +00:00
|
|
|
|
|
|
|
auto r = sksg::Rect::Make({20, 20, 400, 300});
|
|
|
|
auto p = sksg::Color::Make(SK_ColorRED);
|
|
|
|
auto d = sksg::Draw::Make(r, p);
|
2019-02-12 20:24:16 +00:00
|
|
|
this->appendNode(d, r, p);
|
2019-02-12 16:31:27 +00:00
|
|
|
|
|
|
|
r = sksg::Rect::Make({60, 70, 300, 400});
|
|
|
|
p = sksg::Color::Make(SK_ColorBLUE);
|
|
|
|
d = sksg::Draw::Make(r, p);
|
2019-02-12 20:24:16 +00:00
|
|
|
this->appendNode(d, r, p);
|
2019-02-12 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("SceneGraph"); }
|
2019-02-12 16:31:27 +00:00
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
fScene->render(canvas);
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
|
2019-02-12 20:24:16 +00:00
|
|
|
if (auto node = fScene->nodeAt({x, y})) {
|
2019-07-12 15:32:43 +00:00
|
|
|
Click* click = new Click();
|
2019-02-12 20:24:16 +00:00
|
|
|
click->fMeta.setPtr("node", (void*)node);
|
|
|
|
return click;
|
|
|
|
}
|
2019-07-12 15:32:43 +00:00
|
|
|
return nullptr;
|
2019-02-12 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool onClick(Click* click) override {
|
2019-02-12 20:24:16 +00:00
|
|
|
sksg::Draw* node = nullptr;
|
|
|
|
if (click->fMeta.findPtr("node", (void**)&node)) {
|
|
|
|
if (auto info = this->findInfo(node)) {
|
|
|
|
auto geo = info->fGeo;
|
|
|
|
sksg::Rect* r = (sksg::Rect*)geo;
|
|
|
|
SkScalar dx = click->fCurr.fX - click->fPrev.fX;
|
|
|
|
SkScalar dy = click->fCurr.fY - click->fPrev.fY;
|
|
|
|
r->setL(r->getL() + dx);
|
|
|
|
r->setR(r->getR() + dx);
|
|
|
|
r->setT(r->getT() + dy);
|
|
|
|
r->setB(r->getB() + dy);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-12 16:31:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Sample;
|
2019-02-12 16:31:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_SAMPLE( return new SampleSG(); )
|