OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 import 'dart:math'; | |
6 import 'dart:sky'; | |
7 import 'package:sky/framework/layout2.dart'; | |
8 | |
9 // Material design colors. :p | |
10 List<int> colors = [ | |
11 0xFF009688, | |
12 0xFFFFC107, | |
13 0xFF9C27B0, | |
14 0xFF03A9F4, | |
15 0xFF673AB7, | |
16 0xFFCDDC39, | |
17 ]; | |
18 | |
19 class Dot { | |
20 final Paint _paint; | |
21 double x = 0.0; | |
22 double y = 0.0; | |
23 double radius = 0.0; | |
24 | |
25 Dot({int color}) : _paint = new Paint()..color = color; | |
26 | |
27 void update(PointerEvent event) { | |
28 x = event.x; | |
29 y = event.y; | |
30 radius = 5 + (95 * event.pressure); | |
31 } | |
32 | |
33 void paint(RenderNodeDisplayList canvas) { | |
34 canvas.drawCircle(x, y, radius, _paint); | |
35 } | |
36 } | |
37 | |
38 class RenderTouchDemo extends RenderBox { | |
39 Map<int, Dot> dots = new Map(); | |
40 | |
41 RenderTouchDemo(); | |
42 | |
43 bool handlePointer(PointerEvent event, { double x: 0.0, double y: 0.0 }) { | |
44 if (event.type == 'pointerdown') { | |
eseidel
2015/05/21 17:54:45
Would be nice to be able to handle these as an enu
abarth-chromium
2015/05/21 19:26:49
Done.
Hixie
2015/05/21 19:47:06
eventually these will be class "is" expressions, w
| |
45 int color = colors[event.pointer.remainder(colors.length)]; | |
46 dots[event.pointer] = new Dot(color: color)..update(event); | |
47 markNeedsPaint(); | |
48 return true; | |
49 } | |
50 | |
51 if (event.type == 'pointerup') { | |
52 dots.remove(event.pointer); | |
53 markNeedsPaint(); | |
eseidel
2015/05/21 17:54:45
You might actually want a dots model, which has ch
| |
54 return true; | |
55 } | |
56 | |
57 if (event.type == 'pointercancel') { | |
58 dots = new Map(); | |
59 markNeedsPaint(); | |
60 return true; | |
61 } | |
62 | |
63 if (event.type == 'pointermove') { | |
64 dots[event.pointer].update(event); | |
65 markNeedsPaint(); | |
66 return true; | |
67 } | |
68 | |
69 return false; | |
70 } | |
71 | |
72 void paint(RenderNodeDisplayList canvas) { | |
73 dots.forEach((_, Dot dot) { | |
74 dot.paint(canvas); | |
75 }); | |
76 } | |
77 } | |
78 | |
79 RenderView renderView; | |
80 | |
81 void beginFrame(double timeStamp) { | |
82 RenderNode.flushLayout(); | |
83 renderView.paintFrame(); | |
84 } | |
85 | |
86 bool handleEvent(Event event) { | |
87 if (event is! PointerEvent) | |
88 return false; | |
89 return renderView.handlePointer(event, x: event.x, y: event.y); | |
90 } | |
91 | |
92 void main() { | |
93 view.setEventCallback(handleEvent); | |
94 view.setBeginFrameCallback(beginFrame); | |
95 | |
96 renderView = new RenderView(root: new RenderTouchDemo()); | |
97 renderView.layout(newWidth: view.width, newHeight: view.height); | |
98 | |
99 view.scheduleFrame(); | |
100 } | |
OLD | NEW |