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-08-05 20:57:49 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPaint.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
// ensure that we don't accidentally screw up the bounds when the oval is
|
|
|
|
// fractional, and the impl computes the center and radii, and uses them to
|
|
|
|
// reconstruct the edges of the circle.
|
|
|
|
// see bug# 1504910
|
2013-03-12 14:36:57 +00:00
|
|
|
static void test_circlebounds(SkCanvas*) {
|
2011-04-14 17:53:24 +00:00
|
|
|
SkRect r = { 1.39999998f, 1, 21.3999996f, 21 };
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPath p;
|
|
|
|
p.addOval(r);
|
2009-04-01 18:31:44 +00:00
|
|
|
SkASSERT(r == p.getBounds());
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class CircleView : public Sample {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2010-12-20 18:26:13 +00:00
|
|
|
static const SkScalar ANIM_DX;
|
|
|
|
static const SkScalar ANIM_DY;
|
|
|
|
static const SkScalar ANIM_RAD;
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar fDX, fDY, fRAD;
|
|
|
|
|
|
|
|
CircleView() {
|
|
|
|
fDX = fDY = fRAD = 0;
|
2009-07-16 01:13:14 +00:00
|
|
|
fN = 3;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
virtual bool onQuery(Sample::Event* evt) {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "Circles");
|
2008-12-17 15:59:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void circle(SkCanvas* canvas, int width, bool aa) {
|
|
|
|
SkPaint paint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setAntiAlias(aa);
|
|
|
|
if (width < 0) {
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
} else {
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(width));
|
|
|
|
}
|
|
|
|
canvas->drawCircle(0, 0, SkIntToScalar(9) + fRAD, paint);
|
2012-06-06 12:09:51 +00:00
|
|
|
if (false) { // avoid bit rot, suppress warning
|
|
|
|
test_circlebounds(canvas);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
|
|
|
|
for (int width = -1; width <= 1; width++) {
|
|
|
|
canvas->save();
|
|
|
|
circle(canvas, width, false);
|
|
|
|
canvas->translate(0, dy);
|
|
|
|
circle(canvas, width, true);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-07-16 01:13:14 +00:00
|
|
|
static void make_poly(SkPath* path, int n) {
|
|
|
|
if (n <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
path->incReserve(n + 1);
|
|
|
|
path->moveTo(SK_Scalar1, 0);
|
|
|
|
SkScalar step = SK_ScalarPI * 2 / n;
|
|
|
|
SkScalar angle = 0;
|
|
|
|
for (int i = 1; i < n; i++) {
|
|
|
|
angle += step;
|
|
|
|
SkScalar c, s = SkScalarSinCos(angle, &c);
|
|
|
|
path->lineTo(c, s);
|
|
|
|
}
|
|
|
|
path->close();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2009-07-16 01:13:14 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setScale(SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200));
|
|
|
|
canvas->concat(matrix);
|
|
|
|
for (int n = 3; n < 20; n++) {
|
|
|
|
SkPath path;
|
|
|
|
make_poly(&path, n);
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->rotate(SkIntToScalar(10) * (n - 3));
|
|
|
|
canvas->translate(-SK_Scalar1, 0);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
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:
|
2009-07-16 01:13:14 +00:00
|
|
|
int fN;
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
const SkScalar CircleView::ANIM_DX(SK_Scalar1 / 67);
|
|
|
|
const SkScalar CircleView::ANIM_DY(SK_Scalar1 / 29);
|
|
|
|
const SkScalar CircleView::ANIM_RAD(SK_Scalar1 / 19);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new CircleView(); )
|