reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkCamera.h" |
| 5 | #include "SkEmbossMaskFilter.h" |
| 6 | #include "SkGradientShader.h" |
| 7 | #include "SkPath.h" |
| 8 | #include "SkRegion.h" |
| 9 | #include "SkShader.h" |
| 10 | #include "SkUtils.h" |
| 11 | #include "SkRandom.h" |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 12 | #include "SkImageDecoder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | |
| 14 | class CameraView : public SkView { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 15 | SkTDArray<SkShader*> fShaders; |
| 16 | int fShaderIndex; |
| 17 | bool fFrontFace; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | public: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 19 | CameraView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | fRX = fRY = fRZ = 0; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 21 | fShaderIndex = 0; |
| 22 | fFrontFace = false; |
| 23 | |
| 24 | for (int i = 0;; i++) { |
| 25 | SkString str; |
| 26 | str.printf("/skimages/elephant%d.jpeg", i); |
| 27 | SkBitmap bm; |
| 28 | if (SkImageDecoder::DecodeFile(str.c_str(), &bm)) { |
| 29 | SkShader* s = SkShader::CreateBitmapShader(bm, |
| 30 | SkShader::kClamp_TileMode, |
| 31 | SkShader::kClamp_TileMode); |
| 32 | |
| 33 | SkRect src = { 0, 0, bm.width(), bm.height() }; |
| 34 | SkRect dst = { -150, -150, 150, 150 }; |
| 35 | SkMatrix matrix; |
| 36 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 37 | s->setLocalMatrix(matrix); |
| 38 | *fShaders.append() = s; |
| 39 | } else { |
| 40 | break; |
| 41 | } |
| 42 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | } |
| 44 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 45 | virtual ~CameraView() { |
| 46 | fShaders.unrefAll(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | protected: |
| 50 | // overrides from SkEventSink |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 51 | virtual bool onQuery(SkEvent* evt) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | if (SampleCode::TitleQ(*evt)) |
| 53 | { |
| 54 | SampleCode::TitleR(evt, "Camera"); |
| 55 | return true; |
| 56 | } |
| 57 | return this->INHERITED::onQuery(evt); |
| 58 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 59 | |
| 60 | void drawBG(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | canvas->drawColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | } |
| 63 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 64 | virtual void onDraw(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | this->drawBG(canvas); |
| 66 | |
| 67 | canvas->translate(this->width()/2, this->height()/2); |
| 68 | |
| 69 | Sk3DView view; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 70 | view.rotateX(fRX); |
| 71 | view.rotateY(fRY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | view.applyToCanvas(canvas); |
| 73 | |
| 74 | SkPaint paint; |
| 75 | SkScalar rad = SkIntToScalar(50); |
| 76 | SkScalar dim = rad*2; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 77 | if (fShaders.count() > 0) { |
| 78 | bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0; |
| 79 | if (frontFace != fFrontFace) { |
| 80 | fFrontFace = frontFace; |
| 81 | fShaderIndex = (fShaderIndex + 1) % fShaders.count(); |
| 82 | } |
| 83 | |
| 84 | paint.setAntiAlias(true); |
| 85 | paint.setShader(fShaders[fShaderIndex]); |
| 86 | #if 0 |
| 87 | canvas->drawCircle(0, 0, rad, paint); |
| 88 | canvas->drawCircle(-dim, -dim, rad, paint); |
| 89 | canvas->drawCircle(-dim, dim, rad, paint); |
| 90 | canvas->drawCircle( dim, -dim, rad, paint); |
| 91 | canvas->drawCircle( dim, dim, rad, paint); |
| 92 | #else |
| 93 | SkRect r = { -150, -150, 150, 150 }; |
| 94 | canvas->drawRoundRect(r, 30, 30, paint); |
| 95 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | } |
| 97 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 98 | fRY += SampleCode::GetAnimSecondsDelta() * 90; |
| 99 | if (fRY >= SkIntToScalar(360)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | fRY = 0; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 101 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | this->inval(NULL); |
| 103 | } |
| 104 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | private: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 106 | SkScalar fRX, fRY, fRZ; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | typedef SkView INHERITED; |
| 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | |
| 112 | static SkView* MyFactory() { return new CameraView; } |
| 113 | static SkViewRegister reg(MyFactory); |
| 114 | |