flutter/examples/raw/touch_demo.dart
Hixie c09aac3f3f Rationalise hit testing in the new RenderNode world
- makes the event logic not involve a boolean return value (since we ignored it anyway)
- splits the event handling logic into two steps, hit testing and event dispatch
- introduces an App class on the Dart side to factor out the interaction with the C++ side
- ports sector-layout and simple_render_tree to the new App infrastructure
- port simple_render_tree to the new event handling logic
- implement hit testing for the sector-layout demo

R=eseidel@chromium.org

Review URL: https://codereview.chromium.org/1143343004
2015-05-26 12:44:35 -07:00

75 lines
1.6 KiB
Dart

// 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/app.dart';
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();
void handlePointer(PointerEvent event) {
switch (event.type) {
case 'pointerdown':
int color = colors[event.pointer.remainder(colors.length)];
dots[event.pointer] = new Dot(color: color)..update(event);
break;
case 'pointerup':
dots.remove(event.pointer);
break;
case 'pointercancel':
dots = new Map();
break;
case 'pointermove':
dots[event.pointer].update(event);
break;
}
markNeedsPaint();
}
void paint(RenderNodeDisplayList canvas) {
dots.forEach((_, Dot dot) {
dot.paint(canvas);
});
}
}
AppView app;
void main() {
app = new AppView(new RenderTouchDemo());
}