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; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 28 | r.set(0, 0, 10, 10.5f); |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 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: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 63 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 67 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | paint.setAntiAlias(true); |
| 69 | paint.setTextAlign(SkPaint::kCenter_Align); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 70 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | SkString str; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 72 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 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); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 89 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | canvas->save(); |
| 91 | canvas->translate(SkIntToScalar(10), SkIntToScalar(300)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 92 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | paint.setStrokeWidth(SkIntToScalar(1)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 94 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 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); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 114 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | draw_label(canvas, r, gAngles[i], gAngles[i+1]); |
| 116 | |
| 117 | canvas->translate(w * 8 / 7, 0); |
| 118 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 119 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | canvas->restore(); |
| 121 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 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)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 126 | // fSweep = 359.99f; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | |
| 128 | SkRect r; |
| 129 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 130 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | paint.setAntiAlias(true); |
| 132 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 133 | paint.setStyle(SkPaint::kStroke_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 134 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | r.set(0, 0, SkIntToScalar(200), SkIntToScalar(200)); |
| 136 | r.offset(SkIntToScalar(20), SkIntToScalar(20)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 137 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 148 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | // printf("----- sweep %g %X\n", SkScalarToFloat(fSweep), SkDegreesToRadians(fSweep)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 150 | |
| 151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 162 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | paint.setStrokeWidth(0); |
| 164 | paint.setColor(SK_ColorBLUE); |
| 165 | canvas->drawArc(r, 0, fSweep, false, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 166 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | drawArcs(canvas); |
| 168 | this->inval(NULL); |
| 169 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 170 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 171 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
| 172 | unsigned modi) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | // fSweep += SK_Scalar1; |
| 174 | this->inval(NULL); |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 175 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 177 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 178 | virtual bool onClick(Click* click) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 179 | return this->INHERITED::onClick(click); |
| 180 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 181 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | private: |
| 183 | SkScalar fSweep; |
| 184 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 185 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | ////////////////////////////////////////////////////////////////////////////// |
| 189 | |
| 190 | static SkView* MyFactory() { return new ArcsView; } |
| 191 | static SkViewRegister reg(MyFactory); |