blob: e864528449ad6faf2dbd6d9c91b7d3802cce0b85 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#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.comf2b98d62010-12-20 18:26:13 +000019#include "SkImageDecoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000021class CameraView : public SampleView {
reed@android.comf2b98d62010-12-20 18:26:13 +000022 SkTDArray<SkShader*> fShaders;
23 int fShaderIndex;
24 bool fFrontFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +000025public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000026 CameraView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 fRX = fRY = fRZ = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +000028 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.comae933ce2012-08-23 18:19:56 +000039
reed@google.com261b8e22011-04-14 17:53:24 +000040 SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@android.comf2b98d62010-12-20 18:26:13 +000041 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.org5fd92432011-05-05 01:59:48 +000050 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
reed@android.comf2b98d62010-12-20 18:26:13 +000053 virtual ~CameraView() {
54 fShaders.unrefAll();
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 }
56
57protected:
58 // overrides from SkEventSink
reed@android.comf2b98d62010-12-20 18:26:13 +000059 virtual bool onQuery(SkEvent* evt) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 if (SampleCode::TitleQ(*evt))
61 {
62 SampleCode::TitleR(evt, "Camera");
63 return true;
64 }
65 return this->INHERITED::onQuery(evt);
66 }
reed@android.comf2b98d62010-12-20 18:26:13 +000067
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000068 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 canvas->translate(this->width()/2, this->height()/2);
70
71 Sk3DView view;
reed@android.comf2b98d62010-12-20 18:26:13 +000072 view.rotateX(fRX);
73 view.rotateY(fRY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 view.applyToCanvas(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 SkPaint paint;
reed@android.comf2b98d62010-12-20 18:26:13 +000077 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.comae933ce2012-08-23 18:19:56 +000083
reed@android.comf2b98d62010-12-20 18:26:13 +000084 paint.setAntiAlias(true);
85 paint.setShader(fShaders[fShaderIndex]);
reed@android.comf2b98d62010-12-20 18:26:13 +000086 SkRect r = { -150, -150, 150, 150 };
87 canvas->drawRoundRect(r, 30, 30, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
reed@android.comf2b98d62010-12-20 18:26:13 +000090 fRY += SampleCode::GetAnimSecondsDelta() * 90;
91 if (fRY >= SkIntToScalar(360)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 fRY = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +000093 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 this->inval(NULL);
95 }
96
reed@android.com8a1c16f2008-12-17 15:59:43 +000097private:
reed@android.comf2b98d62010-12-20 18:26:13 +000098 SkScalar fRX, fRY, fRZ;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000099 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100};
101
102//////////////////////////////////////////////////////////////////////////////
103
104static SkView* MyFactory() { return new CameraView; }
105static SkViewRegister reg(MyFactory);