/
030-ReactiveExtensions.cs
63 lines (52 loc) · 2.09 KB
/
030-ReactiveExtensions.cs
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
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Mime;
using System.Windows.Forms;
using NUnit.Framework;
using System;
using Void.Linq;
namespace Tobii
{
[TestFixture]
public class ReactiveExtensions
{
[Test, Ignore("requires gui interaction")]
public void DemoEventFilteringAndSplitting()
{
var form = new Form{ Visible = true, TopMost = true, Width = 800, Height = 600 };
var layOut = new FlowLayoutPanel();
var positionLabel = new Label {Text = "No position"};
var lastFixatedPosition = new Label { Text = "No fixated position" };
layOut.Controls.Add(positionLabel);
layOut.Controls.Add(lastFixatedPosition);
form.Controls.Add(layOut);
var allPoints = Observable
.FromEvent<MouseEventArgs>(form, "MouseMove")
.Select(args => args.EventArgs.Location);
allPoints.Subscribe(point => positionLabel.Text = point.ToString());
allPoints.Where(IsStabilized()).Subscribe(point => lastFixatedPosition.Text = point.ToString());
Application.Run(form);
}
protected bool ChunkIsStable(IEnumerable<Point> points)
{
const int maxVariance = 50;
var xVariance = points.Max(p => p.X) - points.Min(p => p.Y);
var yVariance = points.Max(p => p.X) - points.Min(p => p.Y);
return xVariance < maxVariance && yVariance < maxVariance;
}
static Func<Point, bool> IsStabilized()
{
const int maxVariance = 50;
var last10 = new LinkedList<Point>(new Point(0, 0).Repeat(50));
return currentPosition =>
{
last10.RemoveLast();
last10.AddFirst(currentPosition);
var xVariance = last10.Max(p => p.X) - last10.Min(p => p.X);
var yVariance = last10.Max(p => p.Y) - last10.Min(p => p.Y);
return xVariance < maxVariance && yVariance < maxVariance;
};
}
}
}