blob: c71cc4377e24458d3fe6d9ba5408b3a16968d43d [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#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.comf2b98d62010-12-20 18:26:13 +000012#include "SkImageDecoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
14class CameraView : public SkView {
reed@android.comf2b98d62010-12-20 18:26:13 +000015 SkTDArray<SkShader*> fShaders;
16 int fShaderIndex;
17 bool fFrontFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +000018public:
reed@android.comf2b98d62010-12-20 18:26:13 +000019 CameraView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 fRX = fRY = fRZ = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +000021 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.com8a1c16f2008-12-17 15:59:43 +000043 }
44
reed@android.comf2b98d62010-12-20 18:26:13 +000045 virtual ~CameraView() {
46 fShaders.unrefAll();
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 }
48
49protected:
50 // overrides from SkEventSink
reed@android.comf2b98d62010-12-20 18:26:13 +000051 virtual bool onQuery(SkEvent* evt) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 if (SampleCode::TitleQ(*evt))
53 {
54 SampleCode::TitleR(evt, "Camera");
55 return true;
56 }
57 return this->INHERITED::onQuery(evt);
58 }
reed@android.comf2b98d62010-12-20 18:26:13 +000059
60 void drawBG(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 canvas->drawColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 }
63
reed@android.comf2b98d62010-12-20 18:26:13 +000064 virtual void onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 this->drawBG(canvas);
66
67 canvas->translate(this->width()/2, this->height()/2);
68
69 Sk3DView view;
reed@android.comf2b98d62010-12-20 18:26:13 +000070 view.rotateX(fRX);
71 view.rotateY(fRY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 view.applyToCanvas(canvas);
73
74 SkPaint paint;
75 SkScalar rad = SkIntToScalar(50);
76 SkScalar dim = rad*2;
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 }
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.com8a1c16f2008-12-17 15:59:43 +000096 }
97
reed@android.comf2b98d62010-12-20 18:26:13 +000098 fRY += SampleCode::GetAnimSecondsDelta() * 90;
99 if (fRY >= SkIntToScalar(360)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 fRY = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +0000101 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 this->inval(NULL);
103 }
104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105private:
reed@android.comf2b98d62010-12-20 18:26:13 +0000106 SkScalar fRX, fRY, fRZ;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 typedef SkView INHERITED;
108};
109
110//////////////////////////////////////////////////////////////////////////////
111
112static SkView* MyFactory() { return new CameraView; }
113static SkViewRegister reg(MyFactory);
114