Index: sky/examples/raw/touch_demo.dart |
diff --git a/sky/examples/raw/touch_demo.dart b/sky/examples/raw/touch_demo.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..295f83de40f7a104fd380afade833edeb54d5827 |
--- /dev/null |
+++ b/sky/examples/raw/touch_demo.dart |
@@ -0,0 +1,100 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+import 'dart:math'; |
+import 'dart:sky'; |
+import 'package:sky/framework/layout2.dart'; |
+ |
+// Material design colors. :p |
+List<int> colors = [ |
+ 0xFF009688, |
+ 0xFFFFC107, |
+ 0xFF9C27B0, |
+ 0xFF03A9F4, |
+ 0xFF673AB7, |
+ 0xFFCDDC39, |
+]; |
+ |
+class Dot { |
+ final Paint _paint; |
+ double x = 0.0; |
+ double y = 0.0; |
+ double radius = 0.0; |
+ |
+ Dot({int color}) : _paint = new Paint()..color = color; |
+ |
+ void update(PointerEvent event) { |
+ x = event.x; |
+ y = event.y; |
+ radius = 5 + (95 * event.pressure); |
+ } |
+ |
+ void paint(RenderNodeDisplayList canvas) { |
+ canvas.drawCircle(x, y, radius, _paint); |
+ } |
+} |
+ |
+class RenderTouchDemo extends RenderBox { |
+ Map<int, Dot> dots = new Map(); |
+ |
+ RenderTouchDemo(); |
+ |
+ bool handlePointer(PointerEvent event, { double x: 0.0, double y: 0.0 }) { |
+ 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
|
+ int color = colors[event.pointer.remainder(colors.length)]; |
+ dots[event.pointer] = new Dot(color: color)..update(event); |
+ markNeedsPaint(); |
+ return true; |
+ } |
+ |
+ if (event.type == 'pointerup') { |
+ dots.remove(event.pointer); |
+ markNeedsPaint(); |
eseidel
2015/05/21 17:54:45
You might actually want a dots model, which has ch
|
+ return true; |
+ } |
+ |
+ if (event.type == 'pointercancel') { |
+ dots = new Map(); |
+ markNeedsPaint(); |
+ return true; |
+ } |
+ |
+ if (event.type == 'pointermove') { |
+ dots[event.pointer].update(event); |
+ markNeedsPaint(); |
+ return true; |
+ } |
+ |
+ return false; |
+ } |
+ |
+ void paint(RenderNodeDisplayList canvas) { |
+ dots.forEach((_, Dot dot) { |
+ dot.paint(canvas); |
+ }); |
+ } |
+} |
+ |
+RenderView renderView; |
+ |
+void beginFrame(double timeStamp) { |
+ RenderNode.flushLayout(); |
+ renderView.paintFrame(); |
+} |
+ |
+bool handleEvent(Event event) { |
+ if (event is! PointerEvent) |
+ return false; |
+ return renderView.handlePointer(event, x: event.x, y: event.y); |
+} |
+ |
+void main() { |
+ view.setEventCallback(handleEvent); |
+ view.setBeginFrameCallback(beginFrame); |
+ |
+ renderView = new RenderView(root: new RenderTouchDemo()); |
+ renderView.layout(newWidth: view.width, newHeight: view.height); |
+ |
+ view.scheduleFrame(); |
+} |