1 #include "SampleCode.h" 2 #include "SkView.h" 3 #include "SkCanvas.h" 4 #include "SkDevice.h" 5 #include "SkPaint.h" 6 #include "SkUnitMappers.h" 7 #include "SkCubicInterval.h" 8 9 #include "SkWidgetViews.h" 10 11 static SkStaticTextView* make_textview(SkView* parent, 12 const SkRect& bounds, 13 const SkPaint& paint) { 14 SkStaticTextView* view = new SkStaticTextView; 15 view->setMode(SkStaticTextView::kFixedSize_Mode); 16 view->setPaint(paint); 17 view->setVisibleP(true); 18 view->setSize(bounds.width(), bounds.height()); 19 view->setLoc(bounds.fLeft, bounds.fTop); 20 parent->attachChildToFront(view)->unref(); 21 return view; 22 } 23 24 static void set_scalar(SkStaticTextView* view, SkScalar value) { 25 SkString str; 26 str.appendScalar(value); 27 view->setText(str); 28 } 29 30 class UnitMapperView : public SampleView { 31 SkPoint fPts[4]; 32 SkMatrix fMatrix; 33 SkStaticTextView* fViews[4]; 34 35 void setViews() { 36 set_scalar(fViews[0], fPts[1].fX); 37 set_scalar(fViews[1], fPts[1].fY); 38 set_scalar(fViews[2], fPts[2].fX); 39 set_scalar(fViews[3], fPts[2].fY); 40 } 41 42 public: 43 UnitMapperView() { 44 fPts[0].set(0, 0); 45 fPts[1].set(SK_Scalar1 / 3, SK_Scalar1 / 3); 46 fPts[2].set(SK_Scalar1 * 2 / 3, SK_Scalar1 * 2 / 3); 47 fPts[3].set(SK_Scalar1, SK_Scalar1); 48 49 fMatrix.setScale(SK_Scalar1 * 200, -SK_Scalar1 * 200); 50 fMatrix.postTranslate(SkIntToScalar(100), SkIntToScalar(300)); 51 52 SkRect r = { 53 SkIntToScalar(350), SkIntToScalar(100), 54 SkIntToScalar(500), SkIntToScalar(130) 55 }; 56 SkPaint paint; 57 paint.setAntiAlias(true); 58 paint.setTextSize(SkIntToScalar(25)); 59 for (int i = 0; i < 4; i++) { 60 fViews[i] = make_textview(this, r, paint); 61 r.offset(0, r.height()); 62 } 63 this->setViews(); 64 } 65 66 protected: 67 // overrides from SkEventSink 68 virtual bool onQuery(SkEvent* evt) { 69 if (SampleCode::TitleQ(*evt)) { 70 SampleCode::TitleR(evt, "UnitMapper"); 71 return true; 72 } 73 return this->INHERITED::onQuery(evt); 74 } 75 76 virtual void onDrawContent(SkCanvas* canvas) { 77 SkPaint paint; 78 paint.setAntiAlias(true); 79 paint.setColor(0xFF8888FF); 80 81 SkRect r = { 0, 0, SK_Scalar1, SK_Scalar1 }; 82 83 canvas->concat(fMatrix); 84 canvas->drawRect(r, paint); 85 86 paint.setColor(SK_ColorBLACK); 87 paint.setStyle(SkPaint::kStroke_Style); 88 paint.setStrokeWidth(0); 89 paint.setStrokeCap(SkPaint::kRound_Cap); 90 91 SkPath path; 92 path.moveTo(fPts[0]); 93 path.cubicTo(fPts[1], fPts[2], fPts[3]); 94 canvas->drawPath(path, paint); 95 96 paint.setColor(SK_ColorRED); 97 paint.setStrokeWidth(0); 98 canvas->drawLine(0, 0, SK_Scalar1, SK_Scalar1, paint); 99 100 paint.setColor(SK_ColorBLUE); 101 paint.setStrokeWidth(SK_Scalar1 / 60); 102 for (int i = 0; i < 50; i++) { 103 SkScalar x = i * SK_Scalar1 / 49; 104 canvas->drawPoint(x, SkEvalCubicInterval(&fPts[1], x), paint); 105 } 106 107 paint.setStrokeWidth(SK_Scalar1 / 20); 108 paint.setColor(SK_ColorGREEN); 109 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, &fPts[1], paint); 110 } 111 112 SkPoint invertPt(SkScalar x, SkScalar y) { 113 SkPoint pt; 114 SkMatrix m; 115 fMatrix.invert(&m); 116 m.mapXY(x, y, &pt); 117 return pt; 118 } 119 120 int hittest(SkScalar x, SkScalar y) { 121 SkPoint target = { x, y }; 122 SkPoint pts[2] = { fPts[1], fPts[2] }; 123 fMatrix.mapPoints(pts, 2); 124 for (int i = 0; i < 2; i++) { 125 if (SkPoint::Distance(pts[i], target) < SkIntToScalar(4)) { 126 return i + 1; 127 } 128 } 129 return -1; 130 } 131 132 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { 133 fDragIndex = hittest(x, y); 134 return fDragIndex >= 0 ? new Click(this) : NULL; 135 } 136 137 virtual bool onClick(Click* click) { 138 if (fDragIndex >= 0) { 139 fPts[fDragIndex] = invertPt(click->fCurr.fX, click->fCurr.fY); 140 this->setViews(); 141 this->inval(NULL); 142 return true; 143 } 144 return false; 145 } 146 147 private: 148 int fDragIndex; 149 150 typedef SampleView INHERITED; 151 }; 152 153 ////////////////////////////////////////////////////////////////////////////// 154 155 static SkView* MyFactory() { return new UnitMapperView; } 156 static SkViewRegister reg(MyFactory); 157 158