epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkRegion.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkUtils.h" |
reed@android.com | 64ad966 | 2008-12-19 19:15:15 +0000 | [diff] [blame] | 16 | #include "SkComposeShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "Sk1DPathEffect.h" |
| 18 | #include "SkCornerPathEffect.h" |
| 19 | #include "SkPathMeasure.h" |
| 20 | #include "SkRandom.h" |
| 21 | #include "SkColorPriv.h" |
| 22 | #include "SkColorFilter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkLayerRasterizer.h" |
| 24 | |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 25 | #include "SkParsePath.h" |
| 26 | static void testparse() { |
| 27 | SkRect r; |
| 28 | r.set(0, 0, SkFloatToScalar(10), SkFloatToScalar(10.5)); |
| 29 | SkPath p, p2; |
| 30 | SkString str, str2; |
| 31 | |
| 32 | p.addRect(r); |
| 33 | SkParsePath::ToSVGString(p, &str); |
| 34 | SkParsePath::FromSVGString(str.c_str(), &p2); |
| 35 | SkParsePath::ToSVGString(p2, &str2); |
| 36 | } |
| 37 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 38 | class ArcsView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | public: |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 40 | ArcsView() { |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 41 | testparse(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | fSweep = SkIntToScalar(100); |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 43 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | protected: |
| 47 | // overrides from SkEventSink |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 48 | virtual bool onQuery(SkEvent* evt) { |
| 49 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | SampleCode::TitleR(evt, "Arcs"); |
| 51 | return true; |
| 52 | } |
| 53 | return this->INHERITED::onQuery(evt); |
| 54 | } |
| 55 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 56 | static void drawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | canvas->drawRect(r, p); |
| 58 | canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p); |
| 59 | canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p); |
| 60 | canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p); |
| 61 | canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p); |
| 62 | } |
| 63 | |
| 64 | static void draw_label(SkCanvas* canvas, const SkRect& rect, |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 65 | int start, int sweep) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | SkPaint paint; |
| 67 | |
| 68 | paint.setAntiAlias(true); |
| 69 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 70 | |
| 71 | SkString str; |
| 72 | |
| 73 | str.appendS32(start); |
| 74 | str.append(", "); |
| 75 | str.appendS32(sweep); |
| 76 | canvas->drawText(str.c_str(), str.size(), rect.centerX(), |
| 77 | rect.fBottom + paint.getTextSize() * 5/4, paint); |
| 78 | } |
| 79 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 80 | static void drawArcs(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | SkPaint paint; |
| 82 | SkRect r; |
| 83 | SkScalar w = SkIntToScalar(75); |
| 84 | SkScalar h = SkIntToScalar(50); |
| 85 | |
| 86 | r.set(0, 0, w, h); |
| 87 | paint.setAntiAlias(true); |
| 88 | paint.setStyle(SkPaint::kStroke_Style); |
| 89 | |
| 90 | canvas->save(); |
| 91 | canvas->translate(SkIntToScalar(10), SkIntToScalar(300)); |
| 92 | |
| 93 | paint.setStrokeWidth(SkIntToScalar(1)); |
| 94 | |
| 95 | static const int gAngles[] = { |
| 96 | 0, 360, |
| 97 | 0, 45, |
| 98 | 0, -45, |
| 99 | 720, 135, |
| 100 | -90, 269, |
| 101 | -90, 270, |
| 102 | -90, 271, |
| 103 | -180, -270, |
| 104 | 225, 90 |
| 105 | }; |
| 106 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 107 | for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | paint.setColor(SK_ColorBLACK); |
| 109 | drawRectWithLines(canvas, r, paint); |
| 110 | |
| 111 | paint.setColor(SK_ColorRED); |
| 112 | canvas->drawArc(r, SkIntToScalar(gAngles[i]), |
| 113 | SkIntToScalar(gAngles[i+1]), false, paint); |
| 114 | |
| 115 | draw_label(canvas, r, gAngles[i], gAngles[i+1]); |
| 116 | |
| 117 | canvas->translate(w * 8 / 7, 0); |
| 118 | } |
| 119 | |
| 120 | canvas->restore(); |
| 121 | } |
| 122 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 123 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 124 | fSweep = SampleCode::GetAnimScalar(SkIntToScalar(360)/24, |
| 125 | SkIntToScalar(360)); |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 126 | // fSweep = SkFloatToScalar(359.99f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | |
| 128 | SkRect r; |
| 129 | SkPaint paint; |
| 130 | |
| 131 | paint.setAntiAlias(true); |
| 132 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 133 | paint.setStyle(SkPaint::kStroke_Style); |
| 134 | |
| 135 | r.set(0, 0, SkIntToScalar(200), SkIntToScalar(200)); |
| 136 | r.offset(SkIntToScalar(20), SkIntToScalar(20)); |
| 137 | |
| 138 | if (false) { |
| 139 | const SkScalar d = SkIntToScalar(3); |
| 140 | const SkScalar rad[] = { d, d, d, d, d, d, d, d }; |
| 141 | SkPath path; |
| 142 | path.addRoundRect(r, rad); |
| 143 | canvas->drawPath(path, paint); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | drawRectWithLines(canvas, r, paint); |
| 148 | |
| 149 | // printf("----- sweep %g %X\n", SkScalarToFloat(fSweep), SkDegreesToRadians(fSweep)); |
| 150 | |
| 151 | |
| 152 | paint.setStyle(SkPaint::kFill_Style); |
| 153 | paint.setColor(0x800000FF); |
| 154 | canvas->drawArc(r, 0, fSweep, true, paint); |
| 155 | |
| 156 | paint.setColor(0x800FF000); |
| 157 | canvas->drawArc(r, 0, fSweep, false, paint); |
| 158 | |
| 159 | paint.setStyle(SkPaint::kStroke_Style); |
| 160 | paint.setColor(SK_ColorRED); |
| 161 | canvas->drawArc(r, 0, fSweep, true, paint); |
| 162 | |
| 163 | paint.setStrokeWidth(0); |
| 164 | paint.setColor(SK_ColorBLUE); |
| 165 | canvas->drawArc(r, 0, fSweep, false, paint); |
| 166 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | drawArcs(canvas); |
| 168 | this->inval(NULL); |
| 169 | } |
| 170 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 171 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | // fSweep += SK_Scalar1; |
| 173 | this->inval(NULL); |
| 174 | return this->INHERITED::onFindClickHandler(x, y); |
| 175 | } |
| 176 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 177 | virtual bool onClick(Click* click) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | return this->INHERITED::onClick(click); |
| 179 | } |
| 180 | |
| 181 | private: |
| 182 | SkScalar fSweep; |
| 183 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 184 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | ////////////////////////////////////////////////////////////////////////////// |
| 188 | |
| 189 | static SkView* MyFactory() { return new ArcsView; } |
| 190 | static SkViewRegister reg(MyFactory); |
| 191 | |