2011-07-28 14:26:00 +00: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.
|
|
|
|
*/
|
2015-02-02 20:55:02 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
2015-02-02 20:55:02 +00:00
|
|
|
#include "SkAnimTimer.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
2015-02-06 16:36:15 +00:00
|
|
|
#include "SkDrawable.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
2008-12-19 19:15:15 +00:00
|
|
|
#include "SkComposeShader.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "Sk1DPathEffect.h"
|
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkPathMeasure.h"
|
2014-11-24 22:41:51 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkLayerRasterizer.h"
|
|
|
|
|
2009-06-05 16:21:03 +00:00
|
|
|
#include "SkParsePath.h"
|
|
|
|
static void testparse() {
|
|
|
|
SkRect r;
|
2013-11-25 19:44:07 +00:00
|
|
|
r.set(0, 0, 10, 10.5f);
|
2009-06-05 16:21:03 +00:00
|
|
|
SkPath p, p2;
|
|
|
|
SkString str, str2;
|
|
|
|
|
|
|
|
p.addRect(r);
|
|
|
|
SkParsePath::ToSVGString(p, &str);
|
|
|
|
SkParsePath::FromSVGString(str.c_str(), &p2);
|
|
|
|
SkParsePath::ToSVGString(p2, &str2);
|
|
|
|
}
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
class ArcsView : public SampleView {
|
2015-02-06 16:36:15 +00:00
|
|
|
class MyDrawable : public SkDrawable {
|
2014-11-12 03:36:09 +00:00
|
|
|
SkRect fR;
|
|
|
|
SkScalar fSweep;
|
|
|
|
public:
|
2014-11-24 22:41:51 +00:00
|
|
|
MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
|
2014-11-12 03:36:09 +00:00
|
|
|
|
|
|
|
void setSweep(SkScalar sweep) {
|
|
|
|
if (fSweep != sweep) {
|
|
|
|
fSweep = sweep;
|
|
|
|
this->notifyDrawingChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(2));
|
|
|
|
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setColor(0x800000FF);
|
|
|
|
canvas->drawArc(fR, 0, fSweep, true, paint);
|
|
|
|
|
|
|
|
paint.setColor(0x800FF000);
|
|
|
|
canvas->drawArc(fR, 0, fSweep, false, paint);
|
|
|
|
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawArc(fR, 0, fSweep, true, paint);
|
|
|
|
|
|
|
|
paint.setStrokeWidth(0);
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
canvas->drawArc(fR, 0, fSweep, false, paint);
|
|
|
|
}
|
2014-11-18 19:08:05 +00:00
|
|
|
|
|
|
|
SkRect onGetBounds() SK_OVERRIDE {
|
|
|
|
SkRect r(fR);
|
|
|
|
r.outset(2, 2);
|
|
|
|
return r;
|
|
|
|
}
|
2014-11-12 03:36:09 +00:00
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2014-11-12 03:36:09 +00:00
|
|
|
SkRect fRect;
|
2014-11-24 22:41:51 +00:00
|
|
|
MyDrawable* fAnimatingDrawable;
|
2015-02-06 16:36:15 +00:00
|
|
|
SkDrawable* fRootDrawable;
|
2014-11-12 03:36:09 +00:00
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
ArcsView() {
|
2009-06-05 16:21:03 +00:00
|
|
|
testparse();
|
2008-12-17 15:59:43 +00:00
|
|
|
fSweep = SkIntToScalar(100);
|
2011-05-05 14:03:48 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2014-11-12 03:36:09 +00:00
|
|
|
|
|
|
|
fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
|
|
|
|
fRect.offset(SkIntToScalar(20), SkIntToScalar(20));
|
2014-11-24 22:41:51 +00:00
|
|
|
fAnimatingDrawable = SkNEW_ARGS(MyDrawable, (fRect));
|
|
|
|
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
|
2015-02-06 16:36:15 +00:00
|
|
|
fRootDrawable = recorder.endRecordingAsDrawable();
|
2014-11-12 03:36:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
~ArcsView() SK_OVERRIDE {
|
2014-11-24 22:41:51 +00:00
|
|
|
fAnimatingDrawable->unref();
|
|
|
|
fRootDrawable->unref();
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2014-11-24 22:41:51 +00:00
|
|
|
bool onQuery(SkEvent* evt) SK_OVERRIDE {
|
2011-05-05 14:03:48 +00:00
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SampleCode::TitleR(evt, "Arcs");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2014-11-24 22:41:51 +00:00
|
|
|
static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawRect(r, p);
|
|
|
|
canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
|
|
|
|
canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
|
|
|
|
canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
|
|
|
|
canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-09 21:01:05 +00:00
|
|
|
static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextAlign(SkPaint::kCenter_Align);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkString str;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-09 21:01:05 +00:00
|
|
|
str.appendScalar(start);
|
2008-12-17 15:59:43 +00:00
|
|
|
str.append(", ");
|
2015-02-09 21:01:05 +00:00
|
|
|
str.appendScalar(sweep);
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawText(str.c_str(), str.size(), rect.centerX(),
|
|
|
|
rect.fBottom + paint.getTextSize() * 5/4, paint);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2014-11-24 22:41:51 +00:00
|
|
|
static void DrawArcs(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
SkRect r;
|
2015-02-09 21:01:05 +00:00
|
|
|
SkScalar w = 75;
|
|
|
|
SkScalar h = 50;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
r.set(0, 0, w, h);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setStrokeWidth(SkIntToScalar(1));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-09 21:01:05 +00:00
|
|
|
static const SkScalar gAngles[] = {
|
2008-12-17 15:59:43 +00:00
|
|
|
0, 360,
|
|
|
|
0, 45,
|
|
|
|
0, -45,
|
|
|
|
720, 135,
|
|
|
|
-90, 269,
|
|
|
|
-90, 270,
|
|
|
|
-90, 271,
|
|
|
|
-180, -270,
|
|
|
|
225, 90
|
|
|
|
};
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
2014-11-24 22:41:51 +00:00
|
|
|
DrawRectWithLines(canvas, r, paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
2015-02-09 21:01:05 +00:00
|
|
|
canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2014-11-24 22:41:51 +00:00
|
|
|
DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
canvas->translate(w * 8 / 7, 0);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2014-11-24 22:41:51 +00:00
|
|
|
void drawRoot(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(2));
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2014-11-24 22:41:51 +00:00
|
|
|
DrawRectWithLines(canvas, fRect, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-06 16:36:15 +00:00
|
|
|
canvas->drawDrawable(fAnimatingDrawable);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2014-11-24 22:41:51 +00:00
|
|
|
DrawArcs(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
|
2015-02-06 16:36:15 +00:00
|
|
|
canvas->drawDrawable(fRootDrawable);
|
2015-02-02 03:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 20:55:02 +00:00
|
|
|
bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
|
|
|
|
SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
|
2015-02-02 03:01:04 +00:00
|
|
|
fAnimatingDrawable->setSweep(angle);
|
|
|
|
return true;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2014-11-24 22:41:51 +00:00
|
|
|
SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
|
2008-12-17 15:59:43 +00:00
|
|
|
// fSweep += SK_Scalar1;
|
|
|
|
this->inval(NULL);
|
2013-01-08 16:17:50 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkScalar fSweep;
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new ArcsView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|