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

Side by Side Diff: samplecode/SampleBitmapRect.cpp

Issue 894083003: add SkAnimTimer, SPACE = pause/resume, ESP = stop (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 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
OLDNEW
1
2 /* 1 /*
3 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
4 * 3 *
5 * 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
6 * found in the LICENSE file. 5 * found in the LICENSE file.
7 */ 6 */
7
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9 #include "SkAnimTimer.h"
9 #include "SkView.h" 10 #include "SkView.h"
10 #include "SkCanvas.h" 11 #include "SkCanvas.h"
11 #include "SkGradientShader.h" 12 #include "SkGradientShader.h"
12 #include "SkGraphics.h" 13 #include "SkGraphics.h"
13 #include "SkImageDecoder.h" 14 #include "SkImageDecoder.h"
14 #include "SkPath.h" 15 #include "SkPath.h"
15 #include "SkRegion.h" 16 #include "SkRegion.h"
16 #include "SkShader.h" 17 #include "SkShader.h"
17 #include "SkUtils.h" 18 #include "SkUtils.h"
18 #include "SkXfermode.h" 19 #include "SkXfermode.h"
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 SkPoint fSrcPts[2]; 69 SkPoint fSrcPts[2];
69 SkPoint fSrcVec[2]; 70 SkPoint fSrcVec[2];
70 SkRect fSrcLimit; 71 SkRect fSrcLimit;
71 SkRect fDstR[2]; 72 SkRect fDstR[2];
72 73
73 void bounce() { 74 void bounce() {
74 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit); 75 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
75 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit); 76 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
76 } 77 }
77 78
79 void resetBounce() {
80 fSrcPts[0].set(0, 0);
81 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
82
83 fSrcVec[0] = unit_vec(30);
84 fSrcVec[1] = unit_vec(107);
85 }
86
78 public: 87 public:
79 BitmapRectView() { 88 BitmapRectView() {
80 this->setBGColor(SK_ColorGRAY); 89 this->setBGColor(SK_ColorGRAY);
81 90
82 fSrcPts[0].set(0, 0); 91 this->resetBounce();
83 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
84
85 fSrcVec[0] = unit_vec(30);
86 fSrcVec[1] = unit_vec(107);
87 92
88 fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4, 93 fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
89 SCALAR_SIZE*5/4, SCALAR_SIZE*5/4); 94 SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
90 95
91 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100), 96 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100),
92 SkIntToScalar(250), SkIntToScalar(300)); 97 SkIntToScalar(250), SkIntToScalar(300));
93 fDstR[1] = fDstR[0]; 98 fDstR[1] = fDstR[0];
94 fDstR[1].offset(fDstR[0].width() * 5/4, 0); 99 fDstR[1].offset(fDstR[0].width() * 5/4, 0);
95 100
96 fSrcPts[0].set(32, 32); 101 fSrcPts[0].set(32, 32);
97 fSrcPts[1].set(90, 90); 102 fSrcPts[1].set(90, 90);
98 } 103 }
99 104
100 protected: 105 protected:
101 // overrides from SkEventSink 106 bool onQuery(SkEvent* evt) SK_OVERRIDE {
102 virtual bool onQuery(SkEvent* evt) {
103 if (SampleCode::TitleQ(*evt)) { 107 if (SampleCode::TitleQ(*evt)) {
104 SampleCode::TitleR(evt, "BitmapRect"); 108 SampleCode::TitleR(evt, "BitmapRect");
105 return true; 109 return true;
106 } 110 }
107 return this->INHERITED::onQuery(evt); 111 return this->INHERITED::onQuery(evt);
108 } 112 }
109 113
110 virtual void onDrawContent(SkCanvas* canvas) { 114 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
111 SkRect srcR; 115 SkRect srcR;
112 srcR.set(fSrcPts[0], fSrcPts[1]); 116 srcR.set(fSrcPts[0], fSrcPts[1]);
113 srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32); 117 srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
114 srcR.offset(-srcR.width()/2, -srcR.height()/2); 118 srcR.offset(-srcR.width()/2, -srcR.height()/2);
115 119
116 SkPaint paint; 120 SkPaint paint;
117 paint.setStyle(SkPaint::kStroke_Style); 121 paint.setStyle(SkPaint::kStroke_Style);
118 paint.setColor(SK_ColorYELLOW); 122 paint.setColor(SK_ColorYELLOW);
119 123
120 SkBitmap bitmap; 124 SkBitmap bitmap;
121 make_bitmap(&bitmap); 125 make_bitmap(&bitmap);
122 126
123 canvas->translate(20, 20); 127 canvas->translate(20, 20);
124 128
125 canvas->drawBitmap(bitmap, 0, 0, &paint); 129 canvas->drawBitmap(bitmap, 0, 0, &paint);
126 canvas->drawRect(srcR, paint); 130 canvas->drawRect(srcR, paint);
127 131
128 for (int i = 0; i < 2; ++i) { 132 for (int i = 0; i < 2; ++i) {
129 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::k None_FilterLevel); 133 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::k None_FilterLevel);
130 canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint); 134 canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint);
131 canvas->drawRect(fDstR[i], paint); 135 canvas->drawRect(fDstR[i], paint);
132 } 136 }
137 }
133 138
134 this->bounce(); 139 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
135 this->inval(NULL); 140 if (timer.isStopped()) {
141 this->resetBounce();
142 } else if (timer.isRunning()) {
143 this->bounce();
144 }
145 return true;
136 } 146 }
137 147
138 private: 148 private:
139 typedef SkView INHERITED; 149 typedef SampleView INHERITED;
140 }; 150 };
141 151
142 ////////////////////////////////////////////////////////////////////////////// 152 //////////////////////////////////////////////////////////////////////////////
143 153
144 static void make_big_bitmap(SkBitmap* bm) { 154 static void make_big_bitmap(SkBitmap* bm) {
145 static const char gText[] = 155 static const char gText[] =
146 "We the people, in order to form a more perfect union, establish justice ," 156 "We the people, in order to form a more perfect union, establish justice ,"
147 " ensure domestic tranquility, provide for the common defense, promote t he" 157 " ensure domestic tranquility, provide for the common defense, promote t he"
148 " general welfare and ensure the blessings of liberty to ourselves and o ur" 158 " general welfare and ensure the blessings of liberty to ourselves and o ur"
149 " posterity, do ordain and establish this constitution for the United" 159 " posterity, do ordain and establish this constitution for the United"
(...skipping 22 matching lines...) Expand all
172 SkRect fLimitR; 182 SkRect fLimitR;
173 SkScalar fDX; 183 SkScalar fDX;
174 SkRect fDstR[2]; 184 SkRect fDstR[2];
175 185
176 void bounceMe() { 186 void bounceMe() {
177 SkScalar width = fSrcR.width(); 187 SkScalar width = fSrcR.width();
178 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width); 188 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
179 fSrcR.fRight = fSrcR.fLeft + width; 189 fSrcR.fRight = fSrcR.fLeft + width;
180 } 190 }
181 191
192 void resetBounce() {
193 fSrcR.iset(0, 0, fBitmap.height() * 3, fBitmap.height());
194 fDX = SK_Scalar1;
195 }
196
182 public: 197 public:
183 BitmapRectView2() { 198 BitmapRectView2() {
184 make_big_bitmap(&fBitmap); 199 make_big_bitmap(&fBitmap);
185 200
186 this->setBGColor(SK_ColorGRAY); 201 this->setBGColor(SK_ColorGRAY);
187 202
188 fSrcR.fLeft = 0; 203 this->resetBounce();
189 fSrcR.fTop = 0;
190 fSrcR.fRight = SkIntToScalar(fBitmap.height()) * 3;
191 fSrcR.fBottom = SkIntToScalar(fBitmap.height());
192 204
193 fLimitR.set(0, 0, 205 fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height());
194 SkIntToScalar(fBitmap.width()),
195 SkIntToScalar(fBitmap.height()));
196 206
197 fDX = SK_Scalar1; 207 fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200);
198
199 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20),
200 SkIntToScalar(600), SkIntToScalar(200));
201 fDstR[1] = fDstR[0]; 208 fDstR[1] = fDstR[0];
202 fDstR[1].offset(0, fDstR[0].height() * 5/4); 209 fDstR[1].offset(0, fDstR[0].height() * 5/4);
203 } 210 }
204 211
205 protected: 212 protected:
206 // overrides from SkEventSink 213 bool onQuery(SkEvent* evt) SK_OVERRIDE {
207 virtual bool onQuery(SkEvent* evt) {
208 if (SampleCode::TitleQ(*evt)) { 214 if (SampleCode::TitleQ(*evt)) {
209 SampleCode::TitleR(evt, "BigBitmapRect"); 215 SampleCode::TitleR(evt, "BigBitmapRect");
210 return true; 216 return true;
211 } 217 }
212 return this->INHERITED::onQuery(evt); 218 return this->INHERITED::onQuery(evt);
213 } 219 }
214 220
215 virtual void onDrawContent(SkCanvas* canvas) { 221 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
216 SkPaint paint; 222 SkPaint paint;
217 paint.setStyle(SkPaint::kStroke_Style); 223 paint.setStyle(SkPaint::kStroke_Style);
218 paint.setColor(SK_ColorYELLOW); 224 paint.setColor(SK_ColorYELLOW);
219 225
220 for (int i = 0; i < 2; ++i) { 226 for (int i = 0; i < 2; ++i) {
221 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::k None_FilterLevel); 227 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::k None_FilterLevel);
222 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint); 228 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint);
223 canvas->drawRect(fDstR[i], paint); 229 canvas->drawRect(fDstR[i], paint);
224 } 230 }
231 }
225 232
226 this->bounceMe(); 233 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
227 this->inval(NULL); 234 if (timer.isStopped()) {
235 this->resetBounce();
236 } else if (timer.isRunning()) {
237 this->bounceMe();
238 }
239 return true;
228 } 240 }
229 241
230 private: 242 private:
231 typedef SkView INHERITED; 243 typedef SampleView INHERITED;
232 }; 244 };
233 245
234 ////////////////////////////////////////////////////////////////////////////// 246 //////////////////////////////////////////////////////////////////////////////
235 247
236 static SkView* F0() { return new BitmapRectView; } 248 static SkView* F0() { return new BitmapRectView; }
237 static SkView* F1() { return new BitmapRectView2; } 249 static SkView* F1() { return new BitmapRectView2; }
238 static SkViewRegister gR0(F0); 250 static SkViewRegister gR0(F0);
239 static SkViewRegister gR1(F1); 251 static SkViewRegister gR1(F1);
OLDNEW
« gm/SkAnimTimer.h ('K') | « samplecode/SampleArc.cpp ('k') | samplecode/SampleCamera.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698