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
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkDrawable.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkPathMeasure.h"
|
|
|
|
#include "include/core/SkPictureRecorder.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/effects/Sk1DPathEffect.h"
|
|
|
|
#include "include/effects/SkCornerPathEffect.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "include/utils/SkTextUtils.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkParsePath.h"
|
2009-06-05 16:21:03 +00:00
|
|
|
static void testparse() {
|
|
|
|
SkRect r;
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setLTRB(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
|
|
|
};
|
|
|
|
|
2019-07-17 13:08:11 +00:00
|
|
|
SkRect fRect = {20, 20, 220, 220};
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<MyDrawable> fAnimatingDrawable;
|
|
|
|
sk_sp<SkDrawable> fRootDrawable;
|
2014-11-12 03:36:09 +00:00
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("Arcs"); }
|
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) {
|
2019-01-03 20:45:53 +00:00
|
|
|
SkFont font;
|
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);
|
2019-01-03 20:45:53 +00:00
|
|
|
SkTextUtils::DrawString(canvas, str.c_str(), rect.centerX(),
|
|
|
|
rect.fBottom + font.getSize() * 5/4, font, SkPaint(),
|
2018-10-30 16:53:21 +00:00
|
|
|
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
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setWH(w, h);
|
2008-12-17 15:59:43 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:11:24 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
testparse();
|
|
|
|
this->setBGColor(0xFFDDDDDD);
|
|
|
|
|
|
|
|
fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect);
|
|
|
|
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
|
|
|
|
fRootDrawable = recorder.finishRecordingAsDrawable();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360));
|
2019-04-17 13:44:32 +00:00
|
|
|
if (fAnimatingDrawable) {
|
|
|
|
fAnimatingDrawable->setSweep(angle);
|
|
|
|
}
|
2015-02-02 03:01:04 +00:00
|
|
|
return true;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new ArcsView(); )
|