reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2011 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 "SkColorPriv.h" |
| 12 | #include "SkDevice.h" |
| 13 | #include "SkPaint.h" |
| 14 | #include "SkRandom.h" |
| 15 | |
| 16 | #define W 270 |
| 17 | #define H 200 |
| 18 | |
| 19 | static void show_text(SkCanvas* canvas) { |
| 20 | SkRandom rand; |
| 21 | SkPaint paint; |
| 22 | paint.setAntiAlias(true); |
| 23 | paint.setTextSize(SkIntToScalar(20)); |
| 24 | |
| 25 | for (int i = 0; i < 300; ++i) { |
| 26 | paint.setColor((SK_A32_MASK << SK_A32_SHIFT) | rand.nextU()); |
| 27 | canvas->drawText("Hamburgefons", 12, |
| 28 | rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 29 | paint); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | static void show_geo(SkCanvas* canvas) { |
| 34 | SkRandom rand; |
| 35 | SkPaint paint; |
| 36 | paint.setAntiAlias(true); |
| 37 | |
| 38 | for (int i = 0; i < 30; ++i) { |
| 39 | SkRect r; |
| 40 | SkPath p; |
| 41 | |
| 42 | r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 43 | rand.nextUScalar1() * W, rand.nextUScalar1() * H); |
| 44 | paint.setStyle(SkPaint::kFill_Style); |
| 45 | paint.setColor(rand.nextU()); |
| 46 | canvas->drawRect(r, paint); |
| 47 | |
| 48 | r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 49 | rand.nextUScalar1() * W, rand.nextUScalar1() * H); |
| 50 | paint.setStyle(SkPaint::kStroke_Style); |
| 51 | paint.setColor(rand.nextU()); |
| 52 | canvas->drawRect(r, paint); |
| 53 | |
| 54 | r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 55 | rand.nextUScalar1() * W, rand.nextUScalar1() * H); |
| 56 | paint.setStyle(SkPaint::kFill_Style); |
| 57 | paint.setColor(rand.nextU()); |
| 58 | p.addOval(r); |
| 59 | canvas->drawPath(p, paint); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | typedef void (*CanvasProc)(SkCanvas*); |
| 64 | |
| 65 | class ClipView : public SampleView { |
| 66 | public: |
| 67 | ClipView() { |
| 68 | } |
| 69 | |
| 70 | virtual ~ClipView() { |
| 71 | } |
| 72 | |
| 73 | protected: |
| 74 | // overrides from SkEventSink |
| 75 | virtual bool onQuery(SkEvent* evt) { |
| 76 | if (SampleCode::TitleQ(*evt)) { |
| 77 | SampleCode::TitleR(evt, "Clip"); |
| 78 | return true; |
| 79 | } |
| 80 | return this->INHERITED::onQuery(evt); |
| 81 | } |
| 82 | |
| 83 | virtual void onDrawContent(SkCanvas* canvas) { |
| 84 | canvas->drawColor(SK_ColorLTGRAY); |
| 85 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 86 | |
| 87 | static const CanvasProc gProc[] = { |
| 88 | show_text, show_geo |
| 89 | }; |
| 90 | |
| 91 | SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) }; |
| 92 | SkPath clipPath; |
| 93 | r.inset(SK_Scalar1 / 4, SK_Scalar1 / 4); |
| 94 | clipPath.addRoundRect(r, SkIntToScalar(16), SkIntToScalar(16)); |
| 95 | |
| 96 | for (int aa = 0; aa <= 1; ++aa) { |
| 97 | canvas->save(); |
| 98 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); ++i) { |
| 99 | canvas->save(); |
| 100 | canvas->clipPath(clipPath); |
| 101 | canvas->drawColor(SK_ColorWHITE); |
| 102 | gProc[i](canvas); |
| 103 | canvas->restore(); |
| 104 | canvas->translate(W * SK_Scalar1 * 8 / 7, 0); |
| 105 | } |
| 106 | canvas->restore(); |
| 107 | canvas->translate(0, H * SK_Scalar1 * 8 / 7); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | typedef SampleView INHERITED; |
| 113 | }; |
| 114 | |
| 115 | ////////////////////////////////////////////////////////////////////////////// |
| 116 | |
| 117 | static SkView* MyFactory() { return new ClipView; } |
| 118 | static SkViewRegister reg(MyFactory); |
| 119 | |