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
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2015-02-02 20:55:02 +00:00
|
|
|
#include "SkAnimTimer.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkCanvas.h"
|
2017-12-01 15:52:28 +00:00
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkCornerPathEffect.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"
|
2017-12-01 15:52:28 +00:00
|
|
|
#include "SkPathMeasure.h"
|
|
|
|
#include "SkPictureRecorder.h"
|
|
|
|
#include "SkRandom.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
2017-12-01 15:52:28 +00:00
|
|
|
#include "SkString.h"
|
2018-10-26 11:28:30 +00:00
|
|
|
#include "SkTextUtils.h"
|
2018-08-21 15:45:46 +00:00
|
|
|
#include "SkUTF.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "Sk1DPathEffect.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);
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class ArcsView : public Sample {
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-11-12 03:36:09 +00:00
|
|
|
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
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkRect onGetBounds() override {
|
2014-11-18 19:08:05 +00:00
|
|
|
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;
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<MyDrawable> fAnimatingDrawable;
|
|
|
|
sk_sp<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));
|
2016-03-18 14:25:55 +00:00
|
|
|
fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect);
|
2014-11-24 22:41:51 +00:00
|
|
|
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
|
2016-03-18 14:25:55 +00:00
|
|
|
fRootDrawable = recorder.finishRecordingAsDrawable();
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "Arcs");
|
2008-12-17 15:59:43 +00:00
|
|
|
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;
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkString str;
|
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);
|
2018-10-26 11:28:30 +00:00
|
|
|
SkTextUtils::DrawString(canvas, str, rect.centerX(),
|
2018-10-30 16:53:21 +00:00
|
|
|
rect.fBottom + paint.getTextSize() * 5/4, paint,
|
|
|
|
SkTextUtils::kCenter_Align);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
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
|
|
|
|
2016-03-18 14:25:55 +00:00
|
|
|
canvas->drawDrawable(fAnimatingDrawable.get());
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2014-11-24 22:41:51 +00:00
|
|
|
DrawArcs(canvas);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2016-03-18 14:25:55 +00:00
|
|
|
canvas->drawDrawable(fRootDrawable.get());
|
2015-02-02 03:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onAnimate(const SkAnimTimer& timer) override {
|
2015-02-02 20:55:02 +00:00
|
|
|
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
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkScalar fSweep;
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new ArcsView(); )
|