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