blob: db9af09f38f22d6d0f418efce62115c3e6db7dbc [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com161b00b2009-11-03 15:33:14 +00008#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.com562ea922010-02-08 21:45:03 +000016#include "SkWidgetViews.h"
17
18static 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
31static void set_scalar(SkStaticTextView* view, SkScalar value) {
32 SkString str;
33 str.appendScalar(value);
34 view->setText(str);
35}
36
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000037class UnitMapperView : public SampleView {
reed@android.com161b00b2009-11-03 15:33:14 +000038 SkPoint fPts[4];
39 SkMatrix fMatrix;
reed@android.com562ea922010-02-08 21:45:03 +000040 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.com161b00b2009-11-03 15:33:14 +000049public:
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.com562ea922010-02-08 21:45:03 +000058
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.com161b00b2009-11-03 15:33:14 +000071 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed@android.com161b00b2009-11-03 15:33:14 +000073protected:
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.comae933ce2012-08-23 18:19:56 +000082
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000083 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com161b00b2009-11-03 15:33:14 +000084 SkPaint paint;
85 paint.setAntiAlias(true);
86 paint.setColor(0xFF8888FF);
87
88 SkRect r = { 0, 0, SK_Scalar1, SK_Scalar1 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
reed@android.com161b00b2009-11-03 15:33:14 +000090 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.comae933ce2012-08-23 18:19:56 +000097
reed@android.com161b00b2009-11-03 15:33:14 +000098 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.com07ef9112012-06-04 13:22:14 +0000119 bool invertPt(SkScalar x, SkScalar y, SkPoint* result) {
120 if (NULL == result)
121 return true;
122
reed@android.com161b00b2009-11-03 15:33:14 +0000123 SkMatrix m;
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000124 if (!fMatrix.invert(&m)) {
125 return false;
126 }
127
128 m.mapXY(x, y, result);
129 return true;
reed@android.com161b00b2009-11-03 15:33:14 +0000130 }
131
reed@android.com562ea922010-02-08 21:45:03 +0000132 int hittest(SkScalar x, SkScalar y) {
reed@android.com161b00b2009-11-03 15:33:14 +0000133 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.com562ea922010-02-08 21:45:03 +0000138 return i + 1;
reed@android.com161b00b2009-11-03 15:33:14 +0000139 }
140 }
reed@android.com562ea922010-02-08 21:45:03 +0000141 return -1;
reed@android.com161b00b2009-11-03 15:33:14 +0000142 }
143
sugoi@google.com9c55f802013-03-07 20:52:59 +0000144 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
reed@android.com562ea922010-02-08 21:45:03 +0000145 fDragIndex = hittest(x, y);
146 return fDragIndex >= 0 ? new Click(this) : NULL;
reed@android.com161b00b2009-11-03 15:33:14 +0000147 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reed@android.com161b00b2009-11-03 15:33:14 +0000149 virtual bool onClick(Click* click) {
reed@android.com562ea922010-02-08 21:45:03 +0000150 if (fDragIndex >= 0) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000151 if (!invertPt(click->fCurr.fX, click->fCurr.fY,
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000152 &fPts[fDragIndex])) {
153 return false;
154 }
155
reed@android.com562ea922010-02-08 21:45:03 +0000156 this->setViews();
reed@android.com161b00b2009-11-03 15:33:14 +0000157 this->inval(NULL);
158 return true;
159 }
160 return false;
161 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000162
reed@android.com161b00b2009-11-03 15:33:14 +0000163private:
reed@android.com562ea922010-02-08 21:45:03 +0000164 int fDragIndex;
reed@android.com161b00b2009-11-03 15:33:14 +0000165
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000166 typedef SampleView INHERITED;
reed@android.com161b00b2009-11-03 15:33:14 +0000167};
168
169//////////////////////////////////////////////////////////////////////////////
170
171static SkView* MyFactory() { return new UnitMapperView; }
172static SkViewRegister reg(MyFactory);