reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGradientShader.h" |
| 5 | #include "SkGraphics.h" |
| 6 | #include "SkImageDecoder.h" |
| 7 | #include "SkPath.h" |
| 8 | #include "SkRegion.h" |
| 9 | #include "SkShader.h" |
| 10 | #include "SkUtils.h" |
| 11 | #include "SkXfermode.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkColorFilter.h" |
| 14 | #include "SkTime.h" |
| 15 | #include "SkRandom.h" |
| 16 | #include "SkLineClipper.h" |
| 17 | |
| 18 | enum { |
| 19 | W = 640/4, |
| 20 | H = 480/4 |
| 21 | }; |
| 22 | |
| 23 | class LineClipperView : public SkView { |
| 24 | SkRect fClip; |
| 25 | SkRandom fRand; |
| 26 | SkPoint fPts[2]; |
| 27 | |
| 28 | void randPts() { |
| 29 | fPts[0].set(fRand.nextUScalar1() * 640, fRand.nextUScalar1() * 480); |
| 30 | fPts[1].set(fRand.nextUScalar1() * 640, fRand.nextUScalar1() * 480); |
| 31 | } |
| 32 | |
| 33 | public: |
| 34 | LineClipperView() { |
| 35 | int x = (640 - W)/2; |
| 36 | int y = (480 - H)/2; |
| 37 | fClip.set(x, y, x + W, y + H); |
| 38 | this->randPts(); |
| 39 | } |
| 40 | |
| 41 | protected: |
| 42 | // overrides from SkEventSink |
| 43 | virtual bool onQuery(SkEvent* evt) { |
| 44 | if (SampleCode::TitleQ(*evt)) { |
| 45 | SampleCode::TitleR(evt, "LineClipper"); |
| 46 | return true; |
| 47 | } |
| 48 | return this->INHERITED::onQuery(evt); |
| 49 | } |
| 50 | |
| 51 | void drawBG(SkCanvas* canvas) { |
| 52 | canvas->drawColor(SK_ColorWHITE); |
| 53 | } |
| 54 | |
| 55 | static void drawVLine(SkCanvas* canvas, SkScalar x, const SkPaint& paint) { |
| 56 | canvas->drawLine(x, -999, x, 999, paint); |
| 57 | } |
| 58 | |
| 59 | static void drawHLine(SkCanvas* canvas, SkScalar y, const SkPaint& paint) { |
| 60 | canvas->drawLine(-999, y, 999, y, paint); |
| 61 | } |
| 62 | |
| 63 | static void check_lineclipper(int count, const SkPoint pts[], |
| 64 | const SkRect& clip) { |
| 65 | if (count > 0) { |
| 66 | for (int i = 0; i <= count; i++) { |
| 67 | SkASSERT(pts[i].fX >= clip.fLeft); |
| 68 | SkASSERT(pts[i].fX <= clip.fRight); |
| 69 | SkASSERT(pts[i].fY >= clip.fTop); |
| 70 | SkASSERT(pts[i].fY <= clip.fBottom); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | virtual void onDraw(SkCanvas* canvas) { |
| 76 | this->drawBG(canvas); |
| 77 | |
| 78 | SkPaint paint; |
| 79 | |
| 80 | drawVLine(canvas, fClip.fLeft + SK_ScalarHalf, paint); |
| 81 | drawVLine(canvas, fClip.fRight - SK_ScalarHalf, paint); |
| 82 | drawHLine(canvas, fClip.fTop + SK_ScalarHalf, paint); |
| 83 | drawHLine(canvas, fClip.fBottom - SK_ScalarHalf, paint); |
| 84 | |
| 85 | paint.setColor(SK_ColorLTGRAY); |
| 86 | canvas->drawRect(fClip, paint); |
| 87 | |
| 88 | paint.setAntiAlias(true); |
| 89 | paint.setColor(SK_ColorBLUE); |
| 90 | paint.setStrokeWidth(SkIntToScalar(3)); |
| 91 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 92 | SkPoint pts[SkLineClipper::kMaxPoints]; |
| 93 | int count = SkLineClipper::ClipLine(fPts, fClip, pts); |
| 94 | check_lineclipper(count, pts, fClip); |
| 95 | for (int i = 0; i < count; i++) { |
| 96 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, &pts[i], paint); |
| 97 | } |
| 98 | |
| 99 | paint.setColor(SK_ColorRED); |
| 100 | paint.setStrokeWidth(0); |
| 101 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, paint); |
| 102 | |
| 103 | if (true) { |
| 104 | this->randPts(); |
| 105 | this->inval(NULL); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 110 | this->randPts(); |
| 111 | this->inval(NULL); |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | virtual bool onClick(Click* click) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | typedef SkView INHERITED; |
| 121 | }; |
| 122 | |
| 123 | ////////////////////////////////////////////////////////////////////////////// |
| 124 | |
| 125 | static SkView* MyFactory() { return new LineClipperView; } |
| 126 | static SkViewRegister reg(MyFactory); |
| 127 | |