OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 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 "SampleCode.h" |
| 9 #include "SkView.h" |
| 10 #include "SkCanvas.h" |
| 11 #include "SkCanvasDrawable.h" |
| 12 #include "SkInterpolator.h" |
| 13 #include "SkPictureRecorder.h" |
| 14 #include "SkRandom.h" |
| 15 |
| 16 const SkRect gUnitSquare = { -1, -1, 1, 1 }; |
| 17 |
| 18 static void color_to_floats(SkColor c, SkScalar f[4]) { |
| 19 f[0] = SkIntToScalar(SkColorGetA(c)); |
| 20 f[1] = SkIntToScalar(SkColorGetR(c)); |
| 21 f[2] = SkIntToScalar(SkColorGetG(c)); |
| 22 f[3] = SkIntToScalar(SkColorGetB(c)); |
| 23 } |
| 24 |
| 25 static SkColor floats_to_color(const SkScalar f[4]) { |
| 26 return SkColorSetARGB(SkScalarRoundToInt(f[0]), |
| 27 SkScalarRoundToInt(f[1]), |
| 28 SkScalarRoundToInt(f[2]), |
| 29 SkScalarRoundToInt(f[3])); |
| 30 } |
| 31 |
| 32 static bool oval_contains(const SkRect& r, SkScalar x, SkScalar y) { |
| 33 SkMatrix m; |
| 34 m.setRectToRect(r, gUnitSquare, SkMatrix::kFill_ScaleToFit); |
| 35 SkPoint pt; |
| 36 m.mapXY(x, y, &pt); |
| 37 return pt.lengthSqd() <= 1; |
| 38 } |
| 39 |
| 40 static SkColor rand_opaque_color(uint32_t seed) { |
| 41 SkRandom rand(seed); |
| 42 return rand.nextU() | (0xFF << 24); |
| 43 } |
| 44 |
| 45 class HTDrawable : public SkCanvasDrawable { |
| 46 SkRect fR; |
| 47 SkColor fColor; |
| 48 SkInterpolator* fInterp; |
| 49 |
| 50 public: |
| 51 HTDrawable(SkRandom& rand) { |
| 52 fR = SkRect::MakeXYWH(rand.nextRangeF(0, 640), rand.nextRangeF(0, 480), |
| 53 rand.nextRangeF(20, 200), rand.nextRangeF(20, 200)
); |
| 54 fColor = rand_opaque_color(rand.nextU()); |
| 55 fInterp = NULL; |
| 56 } |
| 57 |
| 58 void spawnAnimation() { |
| 59 SkDELETE(fInterp); |
| 60 fInterp = SkNEW_ARGS(SkInterpolator, (5, 3)); |
| 61 SkScalar values[5]; |
| 62 color_to_floats(fColor, values); values[4] = 0; |
| 63 fInterp->setKeyFrame(0, SampleCode::GetAnimTime(), values); |
| 64 values[0] = 0; values[4] = 180; |
| 65 fInterp->setKeyFrame(1, SampleCode::GetAnimTime() + 1000, values); |
| 66 color_to_floats(rand_opaque_color(fColor), values); values[4] = 360; |
| 67 fInterp->setKeyFrame(2, SampleCode::GetAnimTime() + 2000, values); |
| 68 |
| 69 fInterp->setMirror(true); |
| 70 fInterp->setRepeatCount(3); |
| 71 |
| 72 this->notifyDrawingChanged(); |
| 73 } |
| 74 |
| 75 bool hitTest(SkScalar x, SkScalar y) { |
| 76 return oval_contains(fR, x, y); |
| 77 } |
| 78 |
| 79 void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 80 SkAutoCanvasRestore acr(canvas, false); |
| 81 |
| 82 SkPaint paint; |
| 83 paint.setAntiAlias(true); |
| 84 |
| 85 if (fInterp) { |
| 86 SkScalar values[5]; |
| 87 SkInterpolator::Result res = fInterp->timeToValues(SampleCode::GetAn
imTime(), values); |
| 88 fColor = floats_to_color(values); |
| 89 |
| 90 canvas->save(); |
| 91 canvas->translate(fR.centerX(), fR.centerY()); |
| 92 canvas->rotate(values[4]); |
| 93 canvas->translate(-fR.centerX(), -fR.centerY()); |
| 94 |
| 95 switch (res) { |
| 96 case SkInterpolator::kFreezeEnd_Result: |
| 97 SkDELETE(fInterp); |
| 98 fInterp = NULL; |
| 99 break; |
| 100 default: |
| 101 break; |
| 102 } |
| 103 } |
| 104 paint.setColor(fColor); |
| 105 canvas->drawRect(fR, paint); |
| 106 } |
| 107 |
| 108 SkRect onGetBounds() SK_OVERRIDE { return fR; } |
| 109 }; |
| 110 |
| 111 class HTView : public SampleView { |
| 112 public: |
| 113 enum { |
| 114 N = 50, |
| 115 W = 640, |
| 116 H = 480, |
| 117 }; |
| 118 |
| 119 struct Rec { |
| 120 HTDrawable* fDrawable; |
| 121 }; |
| 122 Rec fArray[N]; |
| 123 SkAutoTUnref<SkCanvasDrawable> fRoot; |
| 124 |
| 125 HTView() { |
| 126 SkRandom rand; |
| 127 |
| 128 SkPictureRecorder recorder; |
| 129 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeWH(W, H)); |
| 130 for (int i = 0; i < N; ++i) { |
| 131 fArray[i].fDrawable = new HTDrawable(rand); |
| 132 canvas->EXPERIMENTAL_drawDrawable(fArray[i].fDrawable); |
| 133 fArray[i].fDrawable->unref(); |
| 134 } |
| 135 fRoot.reset(recorder.EXPERIMENTAL_endRecordingAsDrawable()); |
| 136 } |
| 137 |
| 138 protected: |
| 139 bool onQuery(SkEvent* evt) SK_OVERRIDE { |
| 140 if (SampleCode::TitleQ(*evt)) { |
| 141 SampleCode::TitleR(evt, "HT"); |
| 142 return true; |
| 143 } |
| 144 return this->INHERITED::onQuery(evt); |
| 145 } |
| 146 |
| 147 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { |
| 148 canvas->EXPERIMENTAL_drawDrawable(fRoot); |
| 149 this->inval(NULL); |
| 150 } |
| 151 |
| 152 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_
OVERRIDE { |
| 153 // search backwards to find the top-most |
| 154 for (int i = N - 1; i >= 0; --i) { |
| 155 if (fArray[i].fDrawable->hitTest(x, y)) { |
| 156 fArray[i].fDrawable->spawnAnimation(); |
| 157 break; |
| 158 } |
| 159 } |
| 160 this->inval(NULL); |
| 161 return NULL; |
| 162 } |
| 163 |
| 164 private: |
| 165 typedef SampleView INHERITED; |
| 166 }; |
| 167 |
| 168 ////////////////////////////////////////////////////////////////////////////// |
| 169 |
| 170 static SkView* MyFactory() { return new HTView; } |
| 171 static SkViewRegister reg(MyFactory); |
OLD | NEW |