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