ink_well.dart 2.1 KB
Newer Older
A
Adam Barth 已提交
1 2 3 4 5 6
// 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:sky' as sky;
import 'package:sky/framework/app.dart';
7
import 'package:sky/framework/rendering/box.dart';
H
Hixie 已提交
8
import 'package:sky/framework/rendering/object.dart';
A
Adam Barth 已提交
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
import 'package:sky/framework/animation/animated_value.dart';
import 'package:sky/framework/animation/curves.dart';

const double _kInitialSize = 0.0;
const double _kTargetSize = 100.0;
const double _kSplashDuration = 500.0;
const int _kInitialOpacity = 0x80;

class InkSplash {
  final InkWell inkWell;
  final sky.Paint _paint = new sky.Paint();
  final sky.Point position;
  AnimatedValue radius;

  InkSplash({ this.position, this.inkWell }) {
    radius = new AnimatedValue(_kInitialSize, onChange: _handleRadiusChange);
    radius.animateTo(_kTargetSize, _kSplashDuration, curve: easeOut);
  }

  void _handleRadiusChange() {
    if (radius.value == _kTargetSize)
      inkWell._splashes.remove(this);
    inkWell.markNeedsPaint();
  }

H
Hixie 已提交
34
  void paint(RenderObjectDisplayList canvas) {
A
Adam Barth 已提交
35
    int opacity = (_kInitialOpacity * (1.0 - (radius.value / _kTargetSize))).floor();
36
    _paint.color = new sky.Color(opacity << 24);
A
Adam Barth 已提交
37 38 39 40 41 42 43
    canvas.drawCircle(position.x, position.y, radius.value, _paint);
  }
}

class InkWell extends RenderBox {
  final List<InkSplash> _splashes = new List<InkSplash>();

A
Adam Barth 已提交
44
  void handleEvent(sky.Event event) {
A
Adam Barth 已提交
45 46 47 48 49 50 51 52 53 54
    switch (event.type) {
      case 'pointerdown':
        _splashes.add(new InkSplash(position: new sky.Point(event.x, event.y),
                                    inkWell: this));
        break;
    }
    markNeedsPaint();
  }

  void performLayout() {
55
    size = constraints.constrain(sky.Size.infinite);
A
Adam Barth 已提交
56 57
  }

H
Hixie 已提交
58
  void paint(RenderObjectDisplayList canvas) {
A
Adam Barth 已提交
59
    canvas.drawRect(new sky.Rect.fromLTRB(0.0, 0.0, size.width, size.height),
60
                    new sky.Paint()..color = const sky.Color(0xFFCCCCCC));
A
Adam Barth 已提交
61 62 63 64 65 66 67 68 69 70
    for (InkSplash splash in _splashes)
      splash.paint(canvas);
  }
}

AppView app;

void main() {
  app = new AppView(new InkWell());
}