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 "SkAnimTimer.h" |
10 #include "SkView.h" | 11 #include "SkView.h" |
11 #include "SkCanvas.h" | 12 #include "SkCanvas.h" |
12 #include "SkGradientShader.h" | 13 #include "SkGradientShader.h" |
13 #include "SkGraphics.h" | 14 #include "SkGraphics.h" |
14 #include "SkImageDecoder.h" | 15 #include "SkImageDecoder.h" |
15 #include "SkPath.h" | 16 #include "SkPath.h" |
16 #include "SkRegion.h" | 17 #include "SkRegion.h" |
17 #include "SkShader.h" | 18 #include "SkShader.h" |
18 #include "SkUtils.h" | 19 #include "SkUtils.h" |
19 #include "SkXfermode.h" | 20 #include "SkXfermode.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 SkBitmap bitmap; | 76 SkBitmap bitmap; |
76 bitmap.allocN32Pixels(300, 200); | 77 bitmap.allocN32Pixels(300, 200); |
77 | 78 |
78 SkCanvas canvas(bitmap); | 79 SkCanvas canvas(bitmap); |
79 SkPaint paint; | 80 SkPaint paint; |
80 paint.setAntiAlias(true); | 81 paint.setAntiAlias(true); |
81 canvas.drawPath(path, paint); | 82 canvas.drawPath(path, paint); |
82 } | 83 } |
83 | 84 |
84 class PathView : public SampleView { | 85 class PathView : public SampleView { |
| 86 SkScalar fPrevSecs; |
85 public: | 87 public: |
86 int fDStroke, fStroke, fMinStroke, fMaxStroke; | 88 SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke; |
87 SkPath fPath[6]; | 89 SkPath fPath[6]; |
88 bool fShowHairline; | 90 bool fShowHairline; |
89 bool fOnce; | 91 bool fOnce; |
90 | 92 |
91 PathView() { | 93 PathView() { |
| 94 fPrevSecs = 0; |
92 fOnce = false; | 95 fOnce = false; |
93 } | 96 } |
94 | 97 |
95 void init() { | 98 void init() { |
96 if (fOnce) { | 99 if (fOnce) { |
97 return; | 100 return; |
98 } | 101 } |
99 fOnce = true; | 102 fOnce = true; |
100 | 103 |
101 test_cubic(); | 104 test_cubic(); |
102 test_cubic2(); | 105 test_cubic2(); |
103 | 106 |
104 fShowHairline = false; | 107 fShowHairline = false; |
105 | 108 |
106 fDStroke = 1; | 109 fDStroke = 1; |
107 fStroke = 10; | 110 fStroke = 10; |
108 fMinStroke = 10; | 111 fMinStroke = 10; |
109 fMaxStroke = 180; | 112 fMaxStroke = 180; |
110 | 113 |
111 const int V = 85; | 114 const SkScalar V = 85; |
112 | 115 |
113 fPath[0].moveTo(SkIntToScalar(40), SkIntToScalar(70)); | 116 fPath[0].moveTo(40, 70); |
114 fPath[0].lineTo(SkIntToScalar(70), SkIntToScalar(70) + SK_Scalar1/1); | 117 fPath[0].lineTo(70, 70 + SK_ScalarHalf); |
115 fPath[0].lineTo(SkIntToScalar(110), SkIntToScalar(70)); | 118 fPath[0].lineTo(110, 70); |
116 | 119 |
117 fPath[1].moveTo(SkIntToScalar(40), SkIntToScalar(70)); | 120 fPath[1].moveTo(40, 70); |
118 fPath[1].lineTo(SkIntToScalar(70), SkIntToScalar(70) - SK_Scalar1/1); | 121 fPath[1].lineTo(70, 70 - SK_ScalarHalf); |
119 fPath[1].lineTo(SkIntToScalar(110), SkIntToScalar(70)); | 122 fPath[1].lineTo(110, 70); |
120 | 123 |
121 fPath[2].moveTo(SkIntToScalar(V), SkIntToScalar(V)); | 124 fPath[2].moveTo(V, V); |
122 fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(V)); | 125 fPath[2].lineTo(50, V); |
123 fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(50)); | 126 fPath[2].lineTo(50, 50); |
124 | 127 |
125 fPath[3].moveTo(SkIntToScalar(50), SkIntToScalar(50)); | 128 fPath[3].moveTo(50, 50); |
126 fPath[3].lineTo(SkIntToScalar(50), SkIntToScalar(V)); | 129 fPath[3].lineTo(50, V); |
127 fPath[3].lineTo(SkIntToScalar(V), SkIntToScalar(V)); | 130 fPath[3].lineTo(V, V); |
128 | 131 |
129 fPath[4].moveTo(SkIntToScalar(50), SkIntToScalar(50)); | 132 fPath[4].moveTo(50, 50); |
130 fPath[4].lineTo(SkIntToScalar(50), SkIntToScalar(V)); | 133 fPath[4].lineTo(50, V); |
131 fPath[4].lineTo(SkIntToScalar(52), SkIntToScalar(50)); | 134 fPath[4].lineTo(52, 50); |
132 | 135 |
133 fPath[5].moveTo(SkIntToScalar(52), SkIntToScalar(50)); | 136 fPath[5].moveTo(52, 50); |
134 fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(V)); | 137 fPath[5].lineTo(50, V); |
135 fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(50)); | 138 fPath[5].lineTo(50, 50); |
136 | 139 |
137 this->setBGColor(0xFFDDDDDD); | 140 this->setBGColor(0xFFDDDDDD); |
138 } | 141 } |
139 | 142 |
140 void nextStroke() { | |
141 fStroke += fDStroke; | |
142 if (fStroke > fMaxStroke || fStroke < fMinStroke) | |
143 fDStroke = -fDStroke; | |
144 } | |
145 | |
146 protected: | 143 protected: |
147 // overrides from SkEventSink | 144 // overrides from SkEventSink |
148 virtual bool onQuery(SkEvent* evt) { | 145 virtual bool onQuery(SkEvent* evt) { |
149 if (SampleCode::TitleQ(*evt)) { | 146 if (SampleCode::TitleQ(*evt)) { |
150 SampleCode::TitleR(evt, "Paths"); | 147 SampleCode::TitleR(evt, "Paths"); |
151 return true; | 148 return true; |
152 } | 149 } |
153 return this->INHERITED::onQuery(evt); | 150 return this->INHERITED::onQuery(evt); |
154 } | 151 } |
155 | 152 |
156 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) { | 153 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) { |
157 SkPaint paint; | 154 SkPaint paint; |
158 | 155 |
159 paint.setAntiAlias(true); | 156 paint.setAntiAlias(true); |
160 paint.setStyle(SkPaint::kStroke_Style); | 157 paint.setStyle(SkPaint::kStroke_Style); |
161 paint.setStrokeJoin(j); | 158 paint.setStrokeJoin(j); |
162 paint.setStrokeWidth(SkIntToScalar(fStroke)); | 159 paint.setStrokeWidth(fStroke); |
163 | 160 |
164 if (fShowHairline) { | 161 if (fShowHairline) { |
165 SkPath fill; | 162 SkPath fill; |
166 | 163 |
167 paint.getFillPath(path, &fill); | 164 paint.getFillPath(path, &fill); |
168 paint.setStrokeWidth(0); | 165 paint.setStrokeWidth(0); |
169 canvas->drawPath(fill, paint); | 166 canvas->drawPath(fill, paint); |
170 } else { | 167 } else { |
171 canvas->drawPath(path, paint); | 168 canvas->drawPath(path, paint); |
172 } | 169 } |
173 | 170 |
174 paint.setColor(SK_ColorRED); | 171 paint.setColor(SK_ColorRED); |
175 paint.setStrokeWidth(0); | 172 paint.setStrokeWidth(0); |
176 canvas->drawPath(path, paint); | 173 canvas->drawPath(path, paint); |
177 } | 174 } |
178 | 175 |
179 virtual void onDrawContent(SkCanvas* canvas) { | 176 virtual void onDrawContent(SkCanvas* canvas) { |
180 this->init(); | 177 this->init(); |
181 canvas->translate(SkIntToScalar(50), SkIntToScalar(50)); | 178 canvas->translate(50, 50); |
182 | 179 |
183 static const SkPaint::Join gJoins[] = { | 180 static const SkPaint::Join gJoins[] = { |
184 SkPaint::kBevel_Join, | 181 SkPaint::kBevel_Join, |
185 SkPaint::kMiter_Join, | 182 SkPaint::kMiter_Join, |
186 SkPaint::kRound_Join | 183 SkPaint::kRound_Join |
187 }; | 184 }; |
188 | 185 |
189 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) { | 186 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) { |
190 canvas->save(); | 187 canvas->save(); |
191 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) { | 188 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) { |
192 this->drawPath(canvas, fPath[j], gJoins[i]); | 189 this->drawPath(canvas, fPath[j], gJoins[i]); |
193 canvas->translate(SkIntToScalar(200), 0); | 190 canvas->translate(200, 0); |
194 } | 191 } |
195 canvas->restore(); | 192 canvas->restore(); |
196 | 193 |
197 canvas->translate(0, SkIntToScalar(200)); | 194 canvas->translate(0, 200); |
198 } | 195 } |
| 196 } |
| 197 |
| 198 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { |
| 199 SkScalar currSecs = timer.scaled(100); |
| 200 SkScalar delta = currSecs - fPrevSecs; |
| 201 fPrevSecs = currSecs; |
199 | 202 |
200 this->nextStroke(); | 203 fStroke += fDStroke * delta; |
201 this->inval(NULL); | 204 if (fStroke > fMaxStroke || fStroke < fMinStroke) { |
| 205 fDStroke = -fDStroke; |
| 206 } |
| 207 return true; |
202 } | 208 } |
203 | 209 |
204 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_
OVERRIDE { | 210 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_
OVERRIDE { |
205 fShowHairline = !fShowHairline; | 211 fShowHairline = !fShowHairline; |
206 this->inval(NULL); | 212 this->inval(NULL); |
207 return this->INHERITED::onFindClickHandler(x, y, modi); | 213 return this->INHERITED::onFindClickHandler(x, y, modi); |
208 } | 214 } |
209 | 215 |
210 private: | 216 private: |
211 typedef SampleView INHERITED; | 217 typedef SampleView INHERITED; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 } | 338 } |
333 } | 339 } |
334 return this->INHERITED::onFindClickHandler(x, y, modi); | 340 return this->INHERITED::onFindClickHandler(x, y, modi); |
335 } | 341 } |
336 | 342 |
337 private: | 343 private: |
338 typedef SampleView INHERITED; | 344 typedef SampleView INHERITED; |
339 }; | 345 }; |
340 DEF_SAMPLE( return new ArcToView; ) | 346 DEF_SAMPLE( return new ArcToView; ) |
341 | 347 |
OLD | NEW |