Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1200)

Side by Side Diff: samplecode/SamplePathUtils.cpp

Issue 18552005: Animated the PathUtils Sample to show path contours (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Revised with Leon's comments. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gm/gradients.cpp ('k') | src/utils/SkPathUtils.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9
9 #include "SkCanvas.h" 10 #include "SkCanvas.h"
11 #include "SkCornerPathEffect.h"
12 #include "SkDashPathEffect.h"
10 #include "SkPathUtils.h" 13 #include "SkPathUtils.h"
14 #include "SkRandom.h"
11 #include "SkView.h" 15 #include "SkView.h"
12 //#include "SkPathOps.h" // loads fine here, won't in PathUtils src files 16
13 #include "SkRandom.h" 17 typedef void (*BitsToPath)(SkPath*, const char*, int, int, int);
14 //#include "SkTime.h" 18
19 static const BitsToPath gBitsToPath_fns[] = {
20 SkPathUtils::BitsToPath_Path,
21 SkPathUtils::BitsToPath_Region,
22 };
23
24 // hardcoded bitmap patterns
25 static const uint8_t gBits[][16] = {
26 { 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00,
27 0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 },
28
29 { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0,
30 0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 },
31
32 { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70,
33 0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 }
34 };
35
15 36
16 class samplePathUtils : public SampleView { 37 class samplePathUtils : public SampleView {
djsollen 2013/07/09 12:22:36 capitalize... SamplePathUtils
dierk 2013/07/09 13:56:21 Done.
17 public: 38 public:
39 static const int fNumBits = 3;
40 static const int fH = 8, fW = 12;
41 static const size_t fRowBytes = 2;
42 static const int fNumChars = fH * fRowBytes;
43
44 SkPaint fBmpPaint;
45 SkPaint fOutlinePaint;
46 SkScalar fPhase;
47
18 samplePathUtils() { 48 samplePathUtils() {
19 bmp_paint.setAntiAlias(true); // Black paint for bitmap 49 fBmpPaint.setAntiAlias(true); // Black paint for bitmap
20 bmp_paint.setStyle(SkPaint::kFill_Style); 50 fBmpPaint.setStyle(SkPaint::kFill_Style);
21 bmp_paint.setColor(SK_ColorBLACK); 51
52 fOutlinePaint.setAntiAlias(true); // Black paint for bitmap
53 fOutlinePaint.setStyle(SkPaint::kStroke_Style);
54
55 fPhase = 0.0f;
22 } 56 }
23 57
24 protected: 58 protected:
25 static const int numModes = 3;
26 static const int h=8, w=12, stride=2; // stride is in bytes
27 static const int numChars = h * stride; // number of chars in entire array
28
29 SkPaint bmp_paint;
30
31 // overrides from SkEventSink 59 // overrides from SkEventSink
32 virtual bool onQuery(SkEvent* evt) { 60 virtual bool onQuery(SkEvent* evt) {
33 if (SampleCode::TitleQ(*evt)) { 61 if (SampleCode::TitleQ(*evt)) {
34 SampleCode::TitleR(evt, "PathUtils"); 62 SampleCode::TitleR(evt, "PathUtils");
35 return true; 63 return true;
36 } 64 }
37 return this->INHERITED::onQuery(evt); 65 return this->INHERITED::onQuery(evt);
38 } 66 }
39 67
40 ///////////////////////////////////////////////////////////// 68 /////////////////////////////////////////////////////////////
41 69
42 virtual void onDrawContent(SkCanvas* canvas) { 70 virtual void onDrawContent(SkCanvas* canvas) {
43 // bitmap definitions 71 // creating animated path effect composition (avoiding memory leaks)
44 const uint8_t bits[numModes][numChars] = { 72 SkScalar intervals[8] = {.5, .3, .5, .3, .5, .3, .5, .3};
45 { 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00, 73 SkAutoTUnref<SkDashPathEffect> dash(new SkDashPathEffect(intervals, 2, f Phase) );
46 0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 }, 74 SkAutoTUnref<SkCornerPathEffect> corner(new SkCornerPathEffect(.25f) );
75 fOutlinePaint.setPathEffect(new SkComposePathEffect(dash, corner) )->unr ef();
djsollen 2013/07/09 12:22:36 I know I'm late to the review party, but this seem
dierk 2013/07/09 13:56:21 Done.
47 76
48 { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0, 77 canvas->scale(10.0f, 10.0f); // scales up each bitmap
49 0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 },
50 78
51 { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70, 79 for (int i = 0; i < fNumBits; ++i) {
52 0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 } 80 canvas->save();
53 }; 81 for (size_t j = 0; j < SK_ARRAY_COUNT(gBitsToPath_fns); ++j) {
82 SkPath path;
83 gBitsToPath_fns[j](&path, (char*) &gBits[i], fW, fH, fRowBytes);
54 84
55 static const SkScalar kScale = 10.0f; 85 //draw skPath and outline
86 canvas->drawPath(path, fBmpPaint);
87 canvas->translate(1.5f * fW, 0); // translates past previous bit map
88 canvas->drawPath(path, fOutlinePaint);
89 canvas->translate(1.5f * fW, 0); // translates past previous bit map
90 }
91 canvas->restore();
92 canvas->translate(0, 1.5f * fH); //translate to next row
93 }
56 94
57 for (int i = 0; i < numModes; ++i) { 95 // for animated pathEffect
58 SkPath path; // generate and simplify each path 96 fPhase += .01;
59 SkPathUtils::BitsToPath_Path(&path, (char*) &bits[i], h, w, stride); 97 this->inval(NULL);
60
61 canvas->save(); // DRAWING
62 canvas->scale(kScale, kScale); // scales up each bitmap
63 canvas->translate(0, 1.5f * h * i);
64 canvas->drawPath(path, bmp_paint); // draw bitmap
65 canvas->restore();
66
67 // use the SkRegion method
68 SkPath pathR;
69 SkPathUtils::BitsToPath_Region(&pathR, (char*) &bits[i], h, w, strid e);
70
71 canvas->save();
72
73 canvas->scale(kScale, kScale); // scales up each bitmap
74 canvas->translate(1.5f * w, 1.5f * h * i); // translates past previo us bitmap
75 canvas->drawPath(pathR, bmp_paint); // draw bitmap
76 canvas->restore();
77 }
78 } 98 }
79 99
80 private: 100 private:
81 typedef SkView INHERITED; 101 typedef SkView INHERITED;
82 }; 102 };
83 103
84 ////////////////////////////////////////////////////////////////////////////// 104 //////////////////////////////////////////////////////////////////////////////
85 105
86 static SkView* MyFactory() { return new samplePathUtils; } 106 static SkView* MyFactory() { return new samplePathUtils; }
87 static SkViewRegister reg(MyFactory) 107 static SkViewRegister reg(MyFactory)
88 ; 108 ;
OLDNEW
« no previous file with comments | « gm/gradients.cpp ('k') | src/utils/SkPathUtils.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698