-
Notifications
You must be signed in to change notification settings - Fork 116
/
SmilePainter.dart
439 lines (353 loc) · 14.2 KB
/
SmilePainter.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import 'dart:ui';
import 'package:flutter/material.dart';
class SmilePainter extends CustomPainter {
//debugging Paint
final debugPaint = new Paint()
..color = Colors.grey //0xFFF9D976
..strokeWidth = 1.0
..style = PaintingStyle.stroke;
final whitePaint = new Paint()
..color = Colors.white //0xFFF9D976
..style = PaintingStyle.fill;
int slideValue = 200;
ReviewState badReview;
ReviewState ughReview;
ReviewState okReview;
ReviewState goodReview;
double centerCenter;
double leftCenter;
double rightCenter;
double smileHeight;
double halfWidth;
double halfHeight;
double radius;
double diameter;
double startingY;
double startingX;
double endingX;
double endingY;
double oneThirdOfDia;
double oneThirdOfDiaByTwo;
double eyeRadius;
double eyeRadiusbythree;
double eyeRadiusbytwo;
SmilePainter(int slideValue) : slideValue = slideValue;
Size lastSize;
ReviewState currentState;
@override
void paint(Canvas canvas, Size size) {
if (size != lastSize) {
lastSize = size;
smileHeight = size.width / 2;
halfWidth = size.width / 2;
halfHeight = smileHeight / 2;
radius = 0.0;
eyeRadius = 10.0;
if (smileHeight < size.width) {
radius = halfHeight - 16.0;
} else {
radius = halfWidth - 16.0;
}
eyeRadius = radius / 6.5;
eyeRadiusbythree = eyeRadius/3;
eyeRadiusbytwo = eyeRadius/2;
diameter = radius * 2;
//left top corner
startingX = halfWidth - radius;
startingY = halfHeight - radius;
oneThirdOfDia = (diameter / 3);
oneThirdOfDiaByTwo = oneThirdOfDia / 2;
//bottom right corner
endingX = halfWidth + radius;
endingY = halfHeight + radius;
final leftSmileX = startingX + (radius / 2);
badReview = ReviewState(
Offset(leftSmileX, endingY - (oneThirdOfDiaByTwo * 1.5)),
Offset(startingX + oneThirdOfDia,
startingY + radius + (oneThirdOfDiaByTwo)),
Offset(endingX - radius, startingY + radius + (oneThirdOfDiaByTwo)),
Offset(endingX - oneThirdOfDia,
startingY + radius + (oneThirdOfDiaByTwo)),
Offset(endingX - (radius / 2), endingY - (oneThirdOfDiaByTwo * 1.5)),
Color(0xFFfe0944),
Color(0xFFfeae96),
Color(0xFFfe5c6e),
'BAD');
ughReview = ReviewState(
Offset(leftSmileX, endingY - (radius / 2)),
Offset(diameter, endingY - oneThirdOfDia),
Offset(endingX - radius, endingY - oneThirdOfDia),
Offset(endingX - (radius / 2), endingY - oneThirdOfDia),
Offset(endingX - (radius / 2), endingY - oneThirdOfDia),
Color(0xFFF9D976),
Color(0xfff39f86),
Color(0xFFf6bc7e),
'UGH');
okReview = ReviewState(
Offset(leftSmileX, endingY - (oneThirdOfDiaByTwo * 1.5)),
Offset(diameter, endingY - (oneThirdOfDiaByTwo * 1.5)),
Offset(endingX - radius, endingY - (oneThirdOfDiaByTwo * 1.5)),
Offset(startingX + radius, endingY - (oneThirdOfDiaByTwo * 1.5)),
Offset(endingX - (radius / 2), endingY - (oneThirdOfDiaByTwo * 1.5)),
Color(0xFF21e1fa),
Color(0xff3bb8fd),
Color(0xFF28cdfc),
'OK');
goodReview = ReviewState(
Offset(startingX + (radius / 2), endingY - (oneThirdOfDiaByTwo * 2)),
Offset(startingX + oneThirdOfDia,
startingY + (diameter - oneThirdOfDiaByTwo)),
Offset(endingX - radius, startingY + (diameter - oneThirdOfDiaByTwo)),
Offset(endingX - oneThirdOfDia,
startingY + (diameter - oneThirdOfDiaByTwo)),
Offset(endingX - (radius / 2), endingY - (oneThirdOfDiaByTwo * 2)),
Color(0xFF3ee98a),
Color(0xFF41f7c7),
Color(0xFF41f7c6),
'GOOD');
//get max width of text, that is width of GOOD text
TextSpan spanGood = new TextSpan(
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: 52.0,
color: okReview.titleColor),
text: "GOOD");
TextPainter tpGood =
new TextPainter(text: spanGood, textDirection: TextDirection.ltr);
tpGood.layout();
double goodWidth = tpGood.width;
double halfGoodWidth = goodWidth / 2;
//center points of BIG labels
centerCenter = halfWidth;
leftCenter = centerCenter - goodWidth;
rightCenter = centerCenter + goodWidth;
}
if (slideValue <= 100) {
tweenText(badReview, ughReview, slideValue / 100, canvas);
} else if (slideValue <= 200) {
tweenText(ughReview, okReview, (slideValue - 100) / 100, canvas);
} else if (slideValue <= 300) {
tweenText(okReview, goodReview, (slideValue - 200) / 100, canvas);
} else if (slideValue <= 400) {
tweenText(goodReview, badReview, (slideValue - 300) / 100, canvas);
}
//draw the outer circle------------------------------------------
final centerPoint = Offset(halfWidth, halfHeight);
final circlePaint = genGradientPaint(
new Rect.fromCircle(
center: centerPoint,
radius: radius,
),
currentState.startColor,
currentState.endColor,
PaintingStyle.stroke,
)..strokeCap = StrokeCap.round;
canvas.drawCircle(centerPoint, radius, circlePaint);
//---------------------------------------------------------------
//draw smile curve with path ------------------------------------------
canvas.drawPath(getSmilePath(currentState), circlePaint);
//---------------------------------------------------------------
//draw eyes---------------------------------------------------
//ele calc
final leftEyeX = startingX + oneThirdOfDia;
final eyeY = startingY + (oneThirdOfDia + oneThirdOfDiaByTwo / 4);
final rightEyeX = startingX + (oneThirdOfDia * 2);
final leftEyePoint = Offset(leftEyeX, eyeY);
final rightEyePoint = Offset(rightEyeX, eyeY);
final Paint leftEyePaintFill = genGradientPaint(
new Rect.fromCircle(center: leftEyePoint, radius: eyeRadius),
currentState.startColor,
currentState.endColor,
PaintingStyle.fill,
);
final Paint rightEyePaintFill = genGradientPaint(
new Rect.fromCircle(center: rightEyePoint, radius: eyeRadius),
currentState.startColor,
currentState.endColor,
PaintingStyle.fill,
);
canvas.drawCircle(leftEyePoint, eyeRadius, leftEyePaintFill);
canvas.drawCircle(rightEyePoint, eyeRadius, rightEyePaintFill);
//draw the edges of BAD Review
if (slideValue <= 100 || slideValue > 300) {
double diff = -1.0;
double tween = -1.0;
if (slideValue <= 100) {
diff = slideValue / 100;
tween = lerpDouble(eyeY-(eyeRadiusbythree*0.6), eyeY-eyeRadius, diff);
} else if (slideValue > 300) {
diff = (slideValue - 300) / 100;
tween = lerpDouble(eyeY-eyeRadius, eyeY-(eyeRadiusbythree*0.6), diff);
}
List<Offset> polygonPath = List<Offset>();
polygonPath.add(Offset(leftEyeX-eyeRadiusbytwo, eyeY-eyeRadius));
polygonPath.add(Offset(leftEyeX+eyeRadius, tween));
polygonPath.add(Offset(leftEyeX+eyeRadius, eyeY-eyeRadius));
Path clipPath = new Path();
clipPath.addPolygon(polygonPath, true);
canvas.drawPath(clipPath, whitePaint);
List<Offset> polygonPath2 = List<Offset>();
polygonPath2.add(Offset(rightEyeX+eyeRadiusbytwo, eyeY-eyeRadius));
polygonPath2.add(Offset(rightEyeX-eyeRadius, tween));
polygonPath2.add(Offset(rightEyeX-eyeRadius, eyeY-eyeRadius));
Path clipPath2 = new Path();
clipPath2.addPolygon(polygonPath2, true);
canvas.drawPath(clipPath2, whitePaint);
}
//draw the balls of UGH Review
if (slideValue > 0 && slideValue < 200) {
double diff = -1.0;
double leftTweenX = -1.0;
double leftTweenY = -1.0;
double rightTweenX = -1.0;
double rightTweenY = -1.0;
if (slideValue <= 100) {
// bad to ugh
diff = slideValue / 100;
leftTweenX = lerpDouble(leftEyeX-eyeRadius, leftEyeX, diff);
leftTweenY = lerpDouble(eyeY-eyeRadius, eyeY, diff);
rightTweenX = lerpDouble(rightEyeX+eyeRadius, rightEyeX, diff);
rightTweenY = lerpDouble(eyeY, eyeY-(eyeRadius+eyeRadiusbythree), diff);
} else {
// ugh to ok
diff = (slideValue - 100) / 100;
leftTweenX = lerpDouble(leftEyeX, leftEyeX-eyeRadius, diff);
leftTweenY = lerpDouble(eyeY, eyeY-eyeRadius, diff);
rightTweenX = lerpDouble(rightEyeX, rightEyeX+eyeRadius, diff);
rightTweenY = lerpDouble(eyeY-(eyeRadius+eyeRadiusbythree), eyeY, diff);
}
canvas.drawOval(Rect.fromLTRB(leftEyeX-(eyeRadius+eyeRadiusbythree), eyeY-(eyeRadius+eyeRadiusbythree), leftTweenX, leftTweenY), whitePaint);
canvas.drawOval(Rect.fromLTRB(rightTweenX, eyeY, rightEyeX+(eyeRadius+eyeRadiusbythree), eyeY-(eyeRadius+eyeRadiusbythree)), whitePaint);
}
//---------------------------------------------------------------
//drawing stuff for debugging-----------------------------------
// canvas.drawRect(
// Rect.fromLTRB(0.0, 0.0, size.width, smileHeight), debugPaint);
// canvas.drawRect(
// Rect.fromLTRB(startingX, startingY, endingX, endingY), debugPaint);
//
// canvas.drawLine(
// Offset(startingX, startingY), Offset(endingX, endingY), debugPaint);
// canvas.drawLine(
// Offset(endingX, startingY), Offset(startingX, endingY), debugPaint);
// canvas.drawLine(Offset(startingX + radius, startingY),
// Offset(startingX + radius, endingY), debugPaint);
// canvas.drawLine(Offset(startingX, startingY + radius),
// Offset(endingX, startingY + radius), debugPaint);
//
// //horizontal lines
// canvas.drawLine(Offset(startingX, startingY + oneThirdOfDia),
// Offset(endingX, startingY + oneThirdOfDia), debugPaint);
// canvas.drawLine(Offset(startingX, endingY - oneThirdOfDia),
// Offset(endingX, endingY - oneThirdOfDia), debugPaint);
// canvas.drawLine(Offset(startingX, endingY - oneThirdOfDiaByTwo),
// Offset(endingX, endingY - oneThirdOfDiaByTwo), debugPaint);
//
// //vertical lines
// canvas.drawLine(Offset(startingX + oneThirdOfDiaByTwo, startingY),
// Offset(startingX + oneThirdOfDiaByTwo, endingY), debugPaint);
// canvas.drawLine(Offset(startingX + oneThirdOfDia, startingY),
// Offset(startingX + oneThirdOfDia, endingY), debugPaint);
// canvas.drawLine(Offset(endingX - oneThirdOfDia, startingY),
// Offset(endingX - oneThirdOfDia, endingY), debugPaint);
// canvas.drawLine(Offset(endingX - oneThirdOfDiaByTwo, startingY),
// Offset(endingX - oneThirdOfDiaByTwo, endingY), debugPaint);
//--------------------------------------------------------------
}
tweenText(ReviewState centerReview, ReviewState rightReview, double diff,
Canvas canvas) {
currentState = ReviewState.lerp(centerReview, rightReview, diff);
TextSpan spanCenter = new TextSpan(
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: 52.0,
color:
centerReview.titleColor.withAlpha(255 - (255 * diff).round())),
text: centerReview.title);
TextPainter tpCenter =
new TextPainter(text: spanCenter, textDirection: TextDirection.ltr);
TextSpan spanRight = new TextSpan(
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: 52.0,
color: rightReview.titleColor.withAlpha((255 * diff).round())),
text: rightReview.title);
TextPainter tpRight =
new TextPainter(text: spanRight, textDirection: TextDirection.ltr);
tpCenter.layout();
tpRight.layout();
Offset centerOffset =
new Offset(centerCenter - (tpCenter.width / 2), smileHeight);
Offset centerToLeftOffset =
new Offset(leftCenter - (tpCenter.width / 2), smileHeight);
Offset rightOffset =
new Offset(rightCenter - (tpRight.width / 2), smileHeight);
Offset rightToCenterOffset =
new Offset(centerCenter - (tpRight.width / 2), smileHeight);
tpCenter.paint(canvas, Offset.lerp(centerOffset, centerToLeftOffset, diff));
tpRight.paint(canvas, Offset.lerp(rightOffset, rightToCenterOffset, diff));
}
Path getSmilePath(ReviewState state) {
var smilePath = Path();
smilePath.moveTo(state.leftOffset.dx, state.leftOffset.dy);
smilePath.quadraticBezierTo(state.leftHandle.dx, state.leftHandle.dy,
state.centerOffset.dx, state.centerOffset.dy);
smilePath.quadraticBezierTo(state.rightHandle.dx, state.rightHandle.dy,
state.rightOffset.dx, state.rightOffset.dy);
return smilePath;
}
Paint genGradientPaint(
Rect rect, Color startColor, Color endColor, PaintingStyle style) {
final Gradient gradient = new LinearGradient(
colors: <Color>[
startColor,
endColor,
],
);
return new Paint()
..strokeWidth = 10.0
..style = style
..shader = gradient.createShader(rect);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
class ReviewState {
//smile points
Offset leftOffset;
Offset centerOffset;
Offset rightOffset;
Offset leftHandle;
Offset rightHandle;
String title;
Color titleColor;
Color startColor;
Color endColor;
ReviewState(
this.leftOffset,
this.leftHandle,
this.centerOffset,
this.rightHandle,
this.rightOffset,
this.startColor,
this.endColor,
this.titleColor,
this.title);
//create new state between given two states.
static ReviewState lerp(ReviewState start, ReviewState end, double ratio) {
var startColor = Color.lerp(start.startColor, end.startColor, ratio);
var endColor = Color.lerp(start.endColor, end.endColor, ratio);
return ReviewState(
Offset.lerp(start.leftOffset,end.leftOffset, ratio),
Offset.lerp(start.leftHandle, end.leftHandle, ratio),
Offset.lerp(start.centerOffset, end.centerOffset, ratio),
Offset.lerp(start.rightHandle, end.rightHandle, ratio),
Offset.lerp(start.rightOffset, end.rightOffset, ratio),
startColor,
endColor,
start.titleColor,
start.title);
}
}