1 2 /* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 #include "SampleCode.h" 9 #include "SkView.h" 10 #include "SkCanvas.h" 11 #include "SkGradientShader.h" 12 #include "SkGraphics.h" 13 #include "SkImageDecoder.h" 14 #include "SkPath.h" 15 #include "SkRegion.h" 16 #include "SkShader.h" 17 #include "SkUtils.h" 18 #include "SkXfermode.h" 19 #include "SkColorPriv.h" 20 #include "SkColorFilter.h" 21 #include "SkTime.h" 22 #include "SkTypeface.h" 23 24 #include "SkOSFile.h" 25 #include "SkStream.h" 26 27 #define INT_SIZE 64 28 #define SCALAR_SIZE SkIntToScalar(INT_SIZE) 29 30 static void make_bitmap(SkBitmap* bitmap) { 31 bitmap->allocN32Pixels(INT_SIZE, INT_SIZE); 32 SkCanvas canvas(*bitmap); 33 34 canvas.drawColor(SK_ColorRED); 35 SkPaint paint; 36 paint.setAntiAlias(true); 37 const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } }; 38 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE }; 39 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, 40 SkShader::kClamp_TileMode))->unref(); 41 canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint); 42 } 43 44 static SkPoint unit_vec(int degrees) { 45 SkScalar rad = SkDegreesToRadians(SkIntToScalar(degrees)); 46 SkScalar s, c; 47 s = SkScalarSinCos(rad, &c); 48 return SkPoint::Make(c, s); 49 } 50 51 static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) { 52 *value += *delta; 53 if (*value < min) { 54 *value = min; 55 *delta = - *delta; 56 } else if (*value > max) { 57 *value = max; 58 *delta = - *delta; 59 } 60 } 61 62 static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) { 63 bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight); 64 bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom); 65 } 66 67 class BitmapRectView : public SampleView { 68 SkPoint fSrcPts[2]; 69 SkPoint fSrcVec[2]; 70 SkRect fSrcLimit; 71 SkRect fDstR[2]; 72 73 void bounce() { 74 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit); 75 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit); 76 } 77 78 public: 79 BitmapRectView() { 80 this->setBGColor(SK_ColorGRAY); 81 82 fSrcPts[0].set(0, 0); 83 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE); 84 85 fSrcVec[0] = unit_vec(30); 86 fSrcVec[1] = unit_vec(107); 87 88 fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4, 89 SCALAR_SIZE*5/4, SCALAR_SIZE*5/4); 90 91 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100), 92 SkIntToScalar(250), SkIntToScalar(300)); 93 fDstR[1] = fDstR[0]; 94 fDstR[1].offset(fDstR[0].width() * 5/4, 0); 95 96 fSrcPts[0].set(32, 32); 97 fSrcPts[1].set(90, 90); 98 } 99 100 protected: 101 // overrides from SkEventSink 102 virtual bool onQuery(SkEvent* evt) { 103 if (SampleCode::TitleQ(*evt)) { 104 SampleCode::TitleR(evt, "BitmapRect"); 105 return true; 106 } 107 return this->INHERITED::onQuery(evt); 108 } 109 110 virtual void onDrawContent(SkCanvas* canvas) { 111 SkRect srcR; 112 srcR.set(fSrcPts[0], fSrcPts[1]); 113 srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32); 114 srcR.offset(-srcR.width()/2, -srcR.height()/2); 115 116 SkPaint paint; 117 paint.setStyle(SkPaint::kStroke_Style); 118 paint.setColor(SK_ColorYELLOW); 119 120 SkBitmap bitmap; 121 make_bitmap(&bitmap); 122 123 canvas->translate(20, 20); 124 125 canvas->drawBitmap(bitmap, 0, 0, &paint); 126 canvas->drawRect(srcR, paint); 127 128 for (int i = 0; i < 2; ++i) { 129 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); 130 canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint); 131 canvas->drawRect(fDstR[i], paint); 132 } 133 134 this->bounce(); 135 this->inval(NULL); 136 } 137 138 private: 139 typedef SkView INHERITED; 140 }; 141 142 ////////////////////////////////////////////////////////////////////////////// 143 144 static void make_big_bitmap(SkBitmap* bm) { 145 static const char gText[] = 146 "We the people, in order to form a more perfect union, establish justice," 147 " ensure domestic tranquility, provide for the common defense, promote the" 148 " general welfare and ensure the blessings of liberty to ourselves and our" 149 " posterity, do ordain and establish this constitution for the United" 150 " States of America."; 151 152 const int BIG_H = 120; 153 154 SkPaint paint; 155 paint.setAntiAlias(true); 156 paint.setTextSize(SkIntToScalar(BIG_H)); 157 158 const int BIG_W = SkScalarRoundToInt(paint.measureText(gText, strlen(gText))); 159 160 bm->allocN32Pixels(BIG_W, BIG_H); 161 bm->eraseColor(SK_ColorWHITE); 162 163 SkCanvas canvas(*bm); 164 165 canvas.drawText(gText, strlen(gText), 0, paint.getTextSize()*4/5, paint); 166 } 167 168 class BitmapRectView2 : public SampleView { 169 SkBitmap fBitmap; 170 171 SkRect fSrcR; 172 SkRect fLimitR; 173 SkScalar fDX; 174 SkRect fDstR[2]; 175 176 void bounceMe() { 177 SkScalar width = fSrcR.width(); 178 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width); 179 fSrcR.fRight = fSrcR.fLeft + width; 180 } 181 182 public: 183 BitmapRectView2() { 184 make_big_bitmap(&fBitmap); 185 186 this->setBGColor(SK_ColorGRAY); 187 188 fSrcR.fLeft = 0; 189 fSrcR.fTop = 0; 190 fSrcR.fRight = SkIntToScalar(fBitmap.height()) * 3; 191 fSrcR.fBottom = SkIntToScalar(fBitmap.height()); 192 193 fLimitR.set(0, 0, 194 SkIntToScalar(fBitmap.width()), 195 SkIntToScalar(fBitmap.height())); 196 197 fDX = SK_Scalar1; 198 199 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20), 200 SkIntToScalar(600), SkIntToScalar(200)); 201 fDstR[1] = fDstR[0]; 202 fDstR[1].offset(0, fDstR[0].height() * 5/4); 203 } 204 205 protected: 206 // overrides from SkEventSink 207 virtual bool onQuery(SkEvent* evt) { 208 if (SampleCode::TitleQ(*evt)) { 209 SampleCode::TitleR(evt, "BigBitmapRect"); 210 return true; 211 } 212 return this->INHERITED::onQuery(evt); 213 } 214 215 virtual void onDrawContent(SkCanvas* canvas) { 216 SkPaint paint; 217 paint.setStyle(SkPaint::kStroke_Style); 218 paint.setColor(SK_ColorYELLOW); 219 220 for (int i = 0; i < 2; ++i) { 221 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); 222 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint); 223 canvas->drawRect(fDstR[i], paint); 224 } 225 226 this->bounceMe(); 227 this->inval(NULL); 228 } 229 230 private: 231 typedef SkView INHERITED; 232 }; 233 234 ////////////////////////////////////////////////////////////////////////////// 235 236 static SkView* F0() { return new BitmapRectView; } 237 static SkView* F1() { return new BitmapRectView2; } 238 static SkViewRegister gR0(F0); 239 static SkViewRegister gR1(F1); 240