Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(392)

Side by Side Diff: samplecode/SampleHT.cpp

Issue 888283002: allow GMs to animate (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: switch all existing animations to use animatePulse Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « samplecode/SampleDegenerateTwoPtRadials.cpp ('k') | samplecode/SampleHairline.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9 #include "SkView.h" 9 #include "SkView.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
(...skipping 28 matching lines...) Expand all
39 39
40 static SkColor rand_opaque_color(uint32_t seed) { 40 static SkColor rand_opaque_color(uint32_t seed) {
41 SkRandom rand(seed); 41 SkRandom rand(seed);
42 return rand.nextU() | (0xFF << 24); 42 return rand.nextU() | (0xFF << 24);
43 } 43 }
44 44
45 class HTDrawable : public SkCanvasDrawable { 45 class HTDrawable : public SkCanvasDrawable {
46 SkRect fR; 46 SkRect fR;
47 SkColor fColor; 47 SkColor fColor;
48 SkInterpolator* fInterp; 48 SkInterpolator* fInterp;
49 SkMSec fTime;
49 50
50 public: 51 public:
51 HTDrawable(SkRandom& rand) { 52 HTDrawable(SkRandom& rand) {
52 fR = SkRect::MakeXYWH(rand.nextRangeF(0, 640), rand.nextRangeF(0, 480), 53 fR = SkRect::MakeXYWH(rand.nextRangeF(0, 640), rand.nextRangeF(0, 480),
53 rand.nextRangeF(20, 200), rand.nextRangeF(20, 200) ); 54 rand.nextRangeF(20, 200), rand.nextRangeF(20, 200) );
54 fColor = rand_opaque_color(rand.nextU()); 55 fColor = rand_opaque_color(rand.nextU());
55 fInterp = NULL; 56 fInterp = NULL;
57 fTime = 0;
56 } 58 }
57 59
58 void spawnAnimation() { 60 void spawnAnimation(SkMSec now) {
61 this->setTime(now);
62
59 SkDELETE(fInterp); 63 SkDELETE(fInterp);
60 fInterp = SkNEW_ARGS(SkInterpolator, (5, 3)); 64 fInterp = SkNEW_ARGS(SkInterpolator, (5, 3));
61 SkScalar values[5]; 65 SkScalar values[5];
62 color_to_floats(fColor, values); values[4] = 0; 66 color_to_floats(fColor, values); values[4] = 0;
63 fInterp->setKeyFrame(0, SampleCode::GetAnimTime(), values); 67 fInterp->setKeyFrame(0, now, values);
64 values[0] = 0; values[4] = 180; 68 values[0] = 0; values[4] = 180;
65 fInterp->setKeyFrame(1, SampleCode::GetAnimTime() + 1000, values); 69 fInterp->setKeyFrame(1, now + 1000, values);
66 color_to_floats(rand_opaque_color(fColor), values); values[4] = 360; 70 color_to_floats(rand_opaque_color(fColor), values); values[4] = 360;
67 fInterp->setKeyFrame(2, SampleCode::GetAnimTime() + 2000, values); 71 fInterp->setKeyFrame(2, now + 2000, values);
68 72
69 fInterp->setMirror(true); 73 fInterp->setMirror(true);
70 fInterp->setRepeatCount(3); 74 fInterp->setRepeatCount(3);
71 75
72 this->notifyDrawingChanged(); 76 this->notifyDrawingChanged();
73 } 77 }
74 78
75 bool hitTest(SkScalar x, SkScalar y) { 79 bool hitTest(SkScalar x, SkScalar y) {
76 return oval_contains(fR, x, y); 80 return oval_contains(fR, x, y);
77 } 81 }
78 82
83 void setTime(SkMSec time) { fTime = time; }
84
79 void onDraw(SkCanvas* canvas) SK_OVERRIDE { 85 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
80 SkAutoCanvasRestore acr(canvas, false); 86 SkAutoCanvasRestore acr(canvas, false);
81 87
82 SkPaint paint; 88 SkPaint paint;
83 paint.setAntiAlias(true); 89 paint.setAntiAlias(true);
84 90
85 if (fInterp) { 91 if (fInterp) {
86 SkScalar values[5]; 92 SkScalar values[5];
87 SkInterpolator::Result res = fInterp->timeToValues(SampleCode::GetAn imTime(), values); 93 SkInterpolator::Result res = fInterp->timeToValues(fTime, values);
88 fColor = floats_to_color(values); 94 fColor = floats_to_color(values);
89 95
90 canvas->save(); 96 canvas->save();
91 canvas->translate(fR.centerX(), fR.centerY()); 97 canvas->translate(fR.centerX(), fR.centerY());
92 canvas->rotate(values[4]); 98 canvas->rotate(values[4]);
93 canvas->translate(-fR.centerX(), -fR.centerY()); 99 canvas->translate(-fR.centerX(), -fR.centerY());
94 100
95 switch (res) { 101 switch (res) {
96 case SkInterpolator::kFreezeEnd_Result: 102 case SkInterpolator::kFreezeEnd_Result:
97 SkDELETE(fInterp); 103 SkDELETE(fInterp);
98 fInterp = NULL; 104 fInterp = NULL;
99 break; 105 break;
100 default: 106 default:
101 break; 107 break;
102 } 108 }
103 } 109 }
104 paint.setColor(fColor); 110 paint.setColor(fColor);
105 canvas->drawRect(fR, paint); 111 canvas->drawRect(fR, paint);
106 } 112 }
107 113
108 SkRect onGetBounds() SK_OVERRIDE { return fR; } 114 SkRect onGetBounds() SK_OVERRIDE { return fR; }
109 }; 115 };
110 116
111 class HTView : public SampleView { 117 class HTView : public SampleView {
112 public: 118 public:
113 enum { 119 enum {
114 N = 50, 120 N = 50,
115 W = 640, 121 W = 640,
116 H = 480, 122 H = 480,
117 }; 123 };
118 124
119 struct Rec { 125 struct Rec {
120 HTDrawable* fDrawable; 126 HTDrawable* fDrawable;
121 }; 127 };
122 Rec fArray[N]; 128 Rec fArray[N];
123 SkAutoTUnref<SkCanvasDrawable> fRoot; 129 SkAutoTUnref<SkCanvasDrawable> fRoot;
130 SkMSec fTime;
124 131
125 HTView() { 132 HTView() {
126 SkRandom rand; 133 SkRandom rand;
127 134
128 SkPictureRecorder recorder; 135 SkPictureRecorder recorder;
129 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeWH(W, H)); 136 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeWH(W, H));
130 for (int i = 0; i < N; ++i) { 137 for (int i = 0; i < N; ++i) {
131 fArray[i].fDrawable = new HTDrawable(rand); 138 fArray[i].fDrawable = new HTDrawable(rand);
132 canvas->EXPERIMENTAL_drawDrawable(fArray[i].fDrawable); 139 canvas->EXPERIMENTAL_drawDrawable(fArray[i].fDrawable);
133 fArray[i].fDrawable->unref(); 140 fArray[i].fDrawable->unref();
134 } 141 }
135 fRoot.reset(recorder.EXPERIMENTAL_endRecordingAsDrawable()); 142 fRoot.reset(recorder.EXPERIMENTAL_endRecordingAsDrawable());
136 } 143 }
137 144
138 protected: 145 protected:
139 bool onQuery(SkEvent* evt) SK_OVERRIDE { 146 bool onQuery(SkEvent* evt) SK_OVERRIDE {
140 if (SampleCode::TitleQ(*evt)) { 147 if (SampleCode::TitleQ(*evt)) {
141 SampleCode::TitleR(evt, "HT"); 148 SampleCode::TitleR(evt, "HT");
142 return true; 149 return true;
143 } 150 }
144 return this->INHERITED::onQuery(evt); 151 return this->INHERITED::onQuery(evt);
145 } 152 }
146 153
147 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { 154 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
148 canvas->EXPERIMENTAL_drawDrawable(fRoot); 155 canvas->EXPERIMENTAL_drawDrawable(fRoot);
149 this->inval(NULL); 156 }
157
158 bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE {
159 fTime = curr;
160 for (int i = 0; i < N; ++i) {
161 fArray[i].fDrawable->setTime(fTime);
162 }
163 return true;
150 } 164 }
151 165
152 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_ OVERRIDE { 166 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_ OVERRIDE {
153 // search backwards to find the top-most 167 // search backwards to find the top-most
154 for (int i = N - 1; i >= 0; --i) { 168 for (int i = N - 1; i >= 0; --i) {
155 if (fArray[i].fDrawable->hitTest(x, y)) { 169 if (fArray[i].fDrawable->hitTest(x, y)) {
156 fArray[i].fDrawable->spawnAnimation(); 170 fArray[i].fDrawable->spawnAnimation(fTime);
157 break; 171 break;
158 } 172 }
159 } 173 }
160 this->inval(NULL); 174 this->inval(NULL);
161 return NULL; 175 return NULL;
162 } 176 }
163 177
164 private: 178 private:
165 typedef SampleView INHERITED; 179 typedef SampleView INHERITED;
166 }; 180 };
167 181
168 ////////////////////////////////////////////////////////////////////////////// 182 //////////////////////////////////////////////////////////////////////////////
169 183
170 static SkView* MyFactory() { return new HTView; } 184 static SkView* MyFactory() { return new HTView; }
171 static SkViewRegister reg(MyFactory); 185 static SkViewRegister reg(MyFactory);
OLDNEW
« no previous file with comments | « samplecode/SampleDegenerateTwoPtRadials.cpp ('k') | samplecode/SampleHairline.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698