blob: 8a04bcdcc4c40dece4c8130d6d71325cb186521b [file] [log] [blame]
mike@reedtribe.orgb5637092012-12-22 20:53:59 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkRandom.h"
12
13static void rotateAbout(SkCanvas* canvas, SkScalar degrees,
14 SkScalar cx, SkScalar cy) {
15 canvas->translate(cx, cy);
16 canvas->rotate(degrees);
17 canvas->translate(-cx, -cy);
18}
19
20class RotateCirclesView : public SampleView {
21public:
22 RotateCirclesView() {
23 this->setBGColor(SK_ColorLTGRAY);
skia.committer@gmail.com15ed90f2012-12-23 02:01:31 +000024
mike@reedtribe.orgb5637092012-12-22 20:53:59 +000025 fAngle = 0;
26 }
27
28protected:
29 // overrides from SkEventSink
30 virtual bool onQuery(SkEvent* evt) {
31 if (SampleCode::TitleQ(*evt)) {
32 SampleCode::TitleR(evt, "RotateCircles");
33 return true;
34 }
35 return this->INHERITED::onQuery(evt);
36 }
37
38 virtual void onDrawContent(SkCanvas* canvas) {
39 SkRandom rand;
40 SkPaint paint;
41 paint.setAntiAlias(true);
42 paint.setStrokeWidth(20);
43
44 SkScalar cx = 240;
45 SkScalar cy = 240;
46 SkScalar DX = 240 * 2;
47 SkColor color = 0;
48
49 float scale = 1;
50 float sign = 0.3f;
51 for (SkScalar rad = 200; rad >= 20; rad -= 15) {
52 sign = -sign;
53 scale += 0.2f;
54
55 paint.setColor(rand.nextU());
56 paint.setAlpha(0xFF);
57 color = ~color;
skia.committer@gmail.com15ed90f2012-12-23 02:01:31 +000058
mike@reedtribe.orgb5637092012-12-22 20:53:59 +000059 paint.setStyle(SkPaint::kFill_Style);
60
61 canvas->save();
62 rotateAbout(canvas, fAngle * scale * sign, cx, cy);
63 canvas->drawCircle(cx, cy, rad, paint);
64 canvas->restore();
65
66 paint.setStyle(SkPaint::kStroke_Style);
67 paint.setStrokeWidth(rad*2);
68
69 canvas->save();
70 rotateAbout(canvas, fAngle * scale * sign, cx + DX, cy);
71 canvas->drawCircle(cx + DX, cy, 10, paint);
72 canvas->restore();
skia.committer@gmail.com15ed90f2012-12-23 02:01:31 +000073
mike@reedtribe.orgb5637092012-12-22 20:53:59 +000074 }
skia.committer@gmail.com15ed90f2012-12-23 02:01:31 +000075
mike@reedtribe.orgb5637092012-12-22 20:53:59 +000076 fAngle = (fAngle + 1) % 360;
77 this->inval(NULL);
78 }
79
80private:
81 int fAngle;
82 typedef SkView INHERITED;
83};
84
85static SkView* F0() { return new RotateCirclesView; }
86static SkViewRegister gR0(F0);
87