2011-07-28 18:26:00 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2008-12-17 18:59:43 +03:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkCamera.h"
|
|
|
|
#include "SkEmbossMaskFilter.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkRandom.h"
|
2010-12-20 21:26:13 +03:00
|
|
|
#include "SkImageDecoder.h"
|
2008-12-17 18:59:43 +03:00
|
|
|
|
2011-05-05 05:59:48 +04:00
|
|
|
class CameraView : public SampleView {
|
2010-12-20 21:26:13 +03:00
|
|
|
SkTDArray<SkShader*> fShaders;
|
|
|
|
int fShaderIndex;
|
|
|
|
bool fFrontFace;
|
2008-12-17 18:59:43 +03:00
|
|
|
public:
|
2010-12-20 21:26:13 +03:00
|
|
|
CameraView() {
|
2008-12-17 18:59:43 +03:00
|
|
|
fRX = fRY = fRZ = 0;
|
2010-12-20 21:26:13 +03:00
|
|
|
fShaderIndex = 0;
|
|
|
|
fFrontFace = false;
|
|
|
|
|
|
|
|
for (int i = 0;; i++) {
|
|
|
|
SkString str;
|
|
|
|
str.printf("/skimages/elephant%d.jpeg", i);
|
|
|
|
SkBitmap bm;
|
|
|
|
if (SkImageDecoder::DecodeFile(str.c_str(), &bm)) {
|
|
|
|
SkShader* s = SkShader::CreateBitmapShader(bm,
|
|
|
|
SkShader::kClamp_TileMode,
|
|
|
|
SkShader::kClamp_TileMode);
|
|
|
|
|
2011-04-14 21:53:24 +04:00
|
|
|
SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
|
2010-12-20 21:26:13 +03:00
|
|
|
SkRect dst = { -150, -150, 150, 150 };
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
|
|
|
|
s->setLocalMatrix(matrix);
|
|
|
|
*fShaders.append() = s;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-05-05 05:59:48 +04:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2008-12-17 18:59:43 +03:00
|
|
|
}
|
|
|
|
|
2010-12-20 21:26:13 +03:00
|
|
|
virtual ~CameraView() {
|
|
|
|
fShaders.unrefAll();
|
2008-12-17 18:59:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2010-12-20 21:26:13 +03:00
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
2008-12-17 18:59:43 +03:00
|
|
|
if (SampleCode::TitleQ(*evt))
|
|
|
|
{
|
|
|
|
SampleCode::TitleR(evt, "Camera");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2010-12-20 21:26:13 +03:00
|
|
|
|
2011-05-05 05:59:48 +04:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->translate(this->width()/2, this->height()/2);
|
|
|
|
|
|
|
|
Sk3DView view;
|
2010-12-20 21:26:13 +03:00
|
|
|
view.rotateX(fRX);
|
|
|
|
view.rotateY(fRY);
|
2008-12-17 18:59:43 +03:00
|
|
|
view.applyToCanvas(canvas);
|
|
|
|
|
|
|
|
SkPaint paint;
|
2010-12-20 21:26:13 +03:00
|
|
|
if (fShaders.count() > 0) {
|
|
|
|
bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0;
|
|
|
|
if (frontFace != fFrontFace) {
|
|
|
|
fFrontFace = frontFace;
|
|
|
|
fShaderIndex = (fShaderIndex + 1) % fShaders.count();
|
|
|
|
}
|
2008-12-17 18:59:43 +03:00
|
|
|
|
2010-12-20 21:26:13 +03:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setShader(fShaders[fShaderIndex]);
|
|
|
|
SkRect r = { -150, -150, 150, 150 };
|
|
|
|
canvas->drawRoundRect(r, 30, 30, paint);
|
|
|
|
}
|
2008-12-17 18:59:43 +03:00
|
|
|
|
2010-12-20 21:26:13 +03:00
|
|
|
fRY += SampleCode::GetAnimSecondsDelta() * 90;
|
|
|
|
if (fRY >= SkIntToScalar(360)) {
|
2008-12-17 18:59:43 +03:00
|
|
|
fRY = 0;
|
2010-12-20 21:26:13 +03:00
|
|
|
}
|
2008-12-17 18:59:43 +03:00
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-12-20 21:26:13 +03:00
|
|
|
SkScalar fRX, fRY, fRZ;
|
2011-05-05 05:59:48 +04:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new CameraView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|
|
|
|
|