OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "SampleCode.h" | 9 #include "SampleCode.h" |
10 #include "SkView.h" | 10 #include "SkView.h" |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 | 203 |
204 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned m
odi) SK_OVERRIDE { | 204 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned m
odi) SK_OVERRIDE { |
205 fShowHairline = !fShowHairline; | 205 fShowHairline = !fShowHairline; |
206 this->inval(NULL); | 206 this->inval(NULL); |
207 return this->INHERITED::onFindClickHandler(x, y, modi); | 207 return this->INHERITED::onFindClickHandler(x, y, modi); |
208 } | 208 } |
209 | 209 |
210 private: | 210 private: |
211 typedef SampleView INHERITED; | 211 typedef SampleView INHERITED; |
212 }; | 212 }; |
| 213 DEF_SAMPLE( return new PathView; ) |
213 | 214 |
214 ////////////////////////////////////////////////////////////////////////////// | 215 ////////////////////////////////////////////////////////////////////////////// |
215 | 216 |
216 static SkView* MyFactory() { return new PathView; } | 217 #include "SkCornerPathEffect.h" |
217 static SkViewRegister reg(MyFactory); | 218 #include "SkRandom.h" |
| 219 |
| 220 class ArcToView : public SampleView { |
| 221 SkPaint fPtsPaint, fArcPaint, fSkeletonPaint, fCornerPaint; |
| 222 public: |
| 223 enum { |
| 224 N = 4 |
| 225 }; |
| 226 SkPoint fPts[N]; |
| 227 SkScalar fRadius; |
| 228 |
| 229 ArcToView() : fRadius(50) { |
| 230 SkRandom rand; |
| 231 for (int i = 0; i < N; ++i) { |
| 232 fPts[i].fX = 20 + rand.nextUScalar1() * 640; |
| 233 fPts[i].fY = 20 + rand.nextUScalar1() * 480; |
| 234 } |
| 235 |
| 236 fPtsPaint.setAntiAlias(true); |
| 237 fPtsPaint.setStrokeWidth(15); |
| 238 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 239 |
| 240 fArcPaint.setAntiAlias(true); |
| 241 fArcPaint.setStyle(SkPaint::kStroke_Style); |
| 242 fArcPaint.setStrokeWidth(9); |
| 243 fArcPaint.setColor(0x800000FF); |
| 244 |
| 245 fCornerPaint.setAntiAlias(true); |
| 246 fCornerPaint.setStyle(SkPaint::kStroke_Style); |
| 247 fCornerPaint.setStrokeWidth(13); |
| 248 fCornerPaint.setColor(SK_ColorGREEN); |
| 249 fCornerPaint.setPathEffect(SkCornerPathEffect::Create(fRadius*2))->unref
(); |
| 250 |
| 251 fSkeletonPaint.setAntiAlias(true); |
| 252 fSkeletonPaint.setStyle(SkPaint::kStroke_Style); |
| 253 fSkeletonPaint.setColor(SK_ColorRED); |
| 254 } |
| 255 |
| 256 protected: |
| 257 // overrides from SkEventSink |
| 258 bool onQuery(SkEvent* evt) SK_OVERRIDE { |
| 259 if (SampleCode::TitleQ(*evt)) { |
| 260 SampleCode::TitleR(evt, "ArcTo"); |
| 261 return true; |
| 262 } |
| 263 return this->INHERITED::onQuery(evt); |
| 264 } |
| 265 |
| 266 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { |
| 267 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint); |
| 268 |
| 269 SkPath path; |
| 270 |
| 271 path.moveTo(fPts[0]); |
| 272 for (int i = 1; i < N; ++i) { |
| 273 path.lineTo(fPts[i].fX, fPts[i].fY); |
| 274 } |
| 275 canvas->drawPath(path, fCornerPaint); |
| 276 |
| 277 path.reset(); |
| 278 path.moveTo(fPts[0]); |
| 279 for (int i = 1; i < N - 1; ++i) { |
| 280 path.arcTo(fPts[i].fX, fPts[i].fY, fPts[i+1].fX, fPts[i+1].fY, fRadi
us); |
| 281 } |
| 282 path.lineTo(fPts[N - 1]); |
| 283 canvas->drawPath(path, fArcPaint); |
| 284 |
| 285 canvas->drawPoints(SkCanvas::kPolygon_PointMode, N, fPts, fSkeletonPaint
); |
| 286 } |
| 287 |
| 288 bool onClick(Click* click) SK_OVERRIDE { |
| 289 int32_t index; |
| 290 if (click->fMeta.findS32("index", &index)) { |
| 291 SkASSERT((unsigned)index < N); |
| 292 fPts[index] = click->fCurr; |
| 293 this->inval(NULL); |
| 294 return true; |
| 295 } |
| 296 return false; |
| 297 } |
| 298 |
| 299 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_
OVERRIDE { |
| 300 const SkScalar tol = 4; |
| 301 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); |
| 302 for (int i = 0; i < N; ++i) { |
| 303 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { |
| 304 Click* click = new Click(this); |
| 305 click->fMeta.setS32("index", i); |
| 306 return click; |
| 307 } |
| 308 } |
| 309 return this->INHERITED::onFindClickHandler(x, y, modi); |
| 310 } |
| 311 |
| 312 private: |
| 313 typedef SampleView INHERITED; |
| 314 }; |
| 315 DEF_SAMPLE( return new ArcToView; ) |
| 316 |
OLD | NEW |