0

我们都看到了鼠标拖放的良好 RX 处理。我想要类似的东西,但将角度的变化输出到屏幕的中心。

就像用户点击鼠标按钮并在屏幕上旋转两次一样,我会得到 0 到 720 度的值。

基本上旋转屏幕上的东西。

如何做到这一点?

4

2 回答 2

1

这将为您提供距对象中心的弧度角。它只会在范围内运行(-Pi, Pi] 但是。如果您旋转 3 次,则不会获得高于单次旋转的值。

var mousedown = from evt in Observable.FromEventPattern<MouseButtonEventArgs>(this, "MouseDown")
        select evt.EventArgs.GetPosition(this);

var mouseup = from evt in Observable.FromEventPattern<MouseEventArgs>(this, "MouseUp")
              select evt.EventArgs.GetPosition(this);

var mousemove = from evt in Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove")
                select evt.EventArgs.GetPosition(this);

Vector center = new Vector(this.Width / 2, this.Height / 2);

var radian = from start in mousedown
        from pos in mousemove.StartWith(start).TakeUntil(mouseup)
        select Math.Atan2((pos - center).Y, (pos -center).X);

编辑

如果您在改变角度之后,以下应该有效:

var angle = from start in mousedown
            from pos in mousemove.StartWith(start).TakeUntil(mouseup)
            select Vector.AngleBetween(pos - center, start - center);
于 2013-10-28T06:09:51.890 回答
0

谢谢你的帮助。我自己想出了最后一点:

        var down = Observable.FromEventPattern<MouseButtonEventArgs>(this, "MouseDown").Select(e=>e.EventArgs.GetPosition(this));

        var move = Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove").Select(e => e.EventArgs.GetPosition(this));

        var up = Observable.FromEventPattern<MouseEventArgs>(this, "MouseUp").Select(e => e.EventArgs.GetPosition(this));

        Vector center = new Vector(this.Width / 2, this.Height / 2);

        var f = from start in down
                from pos in move.StartWith(start).TakeUntil(up).Buffer(2, 1)
                where pos.Count == 2
                select  Vector.AngleBetween(new Vector(pos[0].X, pos[0].Y) - center, new Vector(pos[1].X, pos[1].Y) - center);


        f.ObserveOnDispatcher().Subscribe(p => {
            game.Player.Angle += p;
        });
于 2013-10-29T10:54:24.723 回答