2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SampleCode.h"
9 #include "SkAnimTimer.h"
12 #include "SkGradientShader.h"
13 #include "SkGraphics.h"
14 #include "SkImageDecoder.h"
19 #include "SkXfermode.h"
20 #include "SkColorPriv.h"
21 #include "SkColorFilter.h"
23 #include "SkTypeface.h"
29 #define SCALAR_SIZE SkIntToScalar(INT_SIZE)
31 static void make_bitmap(SkBitmap* bitmap) {
32 bitmap->allocN32Pixels(INT_SIZE, INT_SIZE);
33 SkCanvas canvas(*bitmap);
35 canvas.drawColor(SK_ColorRED);
37 paint.setAntiAlias(true);
38 const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
39 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
40 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
41 SkShader::kClamp_TileMode))->unref();
42 canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
45 static SkPoint unit_vec(int degrees) {
46 SkScalar rad = SkDegreesToRadians(SkIntToScalar(degrees));
48 s = SkScalarSinCos(rad, &c);
49 return SkPoint::Make(c, s);
52 static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
57 } else if (*value > max) {
63 static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
64 bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
65 bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
68 class BitmapRectView : public SampleView {
75 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
76 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
81 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
83 fSrcVec[0] = unit_vec(30);
84 fSrcVec[1] = unit_vec(107);
89 this->setBGColor(SK_ColorGRAY);
93 fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
94 SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
96 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100),
97 SkIntToScalar(250), SkIntToScalar(300));
99 fDstR[1].offset(fDstR[0].width() * 5/4, 0);
101 fSrcPts[0].set(32, 32);
102 fSrcPts[1].set(90, 90);
106 bool onQuery(SkEvent* evt) SK_OVERRIDE {
107 if (SampleCode::TitleQ(*evt)) {
108 SampleCode::TitleR(evt, "BitmapRect");
111 return this->INHERITED::onQuery(evt);
114 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
116 srcR.set(fSrcPts[0], fSrcPts[1]);
117 srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
118 srcR.offset(-srcR.width()/2, -srcR.height()/2);
121 paint.setStyle(SkPaint::kStroke_Style);
122 paint.setColor(SK_ColorYELLOW);
125 make_bitmap(&bitmap);
127 canvas->translate(20, 20);
129 canvas->drawBitmap(bitmap, 0, 0, &paint);
130 canvas->drawRect(srcR, paint);
132 for (int i = 0; i < 2; ++i) {
133 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
134 canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint);
135 canvas->drawRect(fDstR[i], paint);
139 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
140 if (timer.isStopped()) {
142 } else if (timer.isRunning()) {
149 typedef SampleView INHERITED;
152 //////////////////////////////////////////////////////////////////////////////
154 static void make_big_bitmap(SkBitmap* bm) {
155 static const char gText[] =
156 "We the people, in order to form a more perfect union, establish justice,"
157 " ensure domestic tranquility, provide for the common defense, promote the"
158 " general welfare and ensure the blessings of liberty to ourselves and our"
159 " posterity, do ordain and establish this constitution for the United"
160 " States of America.";
162 const int BIG_H = 120;
165 paint.setAntiAlias(true);
166 paint.setTextSize(SkIntToScalar(BIG_H));
168 const int BIG_W = SkScalarRoundToInt(paint.measureText(gText, strlen(gText)));
170 bm->allocN32Pixels(BIG_W, BIG_H);
171 bm->eraseColor(SK_ColorWHITE);
173 SkCanvas canvas(*bm);
175 canvas.drawText(gText, strlen(gText), 0, paint.getTextSize()*4/5, paint);
178 class BitmapRectView2 : public SampleView {
187 SkScalar width = fSrcR.width();
188 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
189 fSrcR.fRight = fSrcR.fLeft + width;
193 fSrcR.iset(0, 0, fBitmap.height() * 3, fBitmap.height());
199 make_big_bitmap(&fBitmap);
201 this->setBGColor(SK_ColorGRAY);
205 fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height());
207 fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200);
209 fDstR[1].offset(0, fDstR[0].height() * 5/4);
213 bool onQuery(SkEvent* evt) SK_OVERRIDE {
214 if (SampleCode::TitleQ(*evt)) {
215 SampleCode::TitleR(evt, "BigBitmapRect");
218 return this->INHERITED::onQuery(evt);
221 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
223 paint.setStyle(SkPaint::kStroke_Style);
224 paint.setColor(SK_ColorYELLOW);
226 for (int i = 0; i < 2; ++i) {
227 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
228 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint);
229 canvas->drawRect(fDstR[i], paint);
233 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
234 if (timer.isStopped()) {
236 } else if (timer.isRunning()) {
243 typedef SampleView INHERITED;
246 //////////////////////////////////////////////////////////////////////////////
248 static SkView* F0() { return new BitmapRectView; }
249 static SkView* F1() { return new BitmapRectView2; }
250 static SkViewRegister gR0(F0);
251 static SkViewRegister gR1(F1);