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