2011-09-20 21 views
4

マウスを動かしたいグリッドがあります。私はマウスを動かすだけですが、私はイベントを発射したいのですが、マウスを押して発射を一時停止する必要があります。一度私はマウスを発射すると、彼らは続けるべきです。マウスボタンが押されている間にmousemoveイベントを防ぐ方法を教えてください。

それは超簡単に聞こえる場合、おそらくそうではありません。しばらくすると、それほどエレガントではない解決策が出てきましたが、何か良いものが存在するかどうかは疑問です。私はあなたのアプローチにハックで影響を与えません。

mouseMove.TakeUntil(mouseDown).Repeat() 

.SkipUntil(mouseUp)の追加機能しない

だから、最初のコードは、左または右TakeUntilにbasicly全く働いてからのコードの上に停止します。

+0

病気になる..私の最後の質問のいずれかが好きではないと思います。 –

答えて

2

どのようにこの件について:

bool mouseIsDown = false; 
Observable.Merge(
    mouseDown.Select(_ => true), 
    mouseUp.Select(_ => false) 
).Subscribe(x => mouseIsDown = x); 

mouseMove.Where(_ => !mouseIsDown); 

技術的に正しい答えはウィンドウのオペレータを必要とするが、これは完全に理解するために同じように良いと簡単です(と私は書くしやすい)

+0

これは、実際に手術室が求めているものの反対です。あなたは、Whereの条件を逆転させるだけです。とにかく+1。 –

+0

修正済みです –

0

これは可能性がありかのうソリューション

// create the observables 
IObservable<Point> mouseMove = Observable.FromEventPattern<MouseEventArgs>(this, "MouseDown") 
    .Select(e=>e.EventArgs.GetPosition(this)); 

IObservable<bool> mouseDown = Observable.FromEventPattern(this, "MouseDown").Select(_ => false); 
IObservable<bool> mouseUp = Observable.FromEventPattern(this, "MouseUp").Select(_ => true); 

var merged = mouseUp.Merge(mouseDown).StartWith(true); 

// sends the moves with the current state of the mouse button 
var all = mouseMove.CombineLatest(merged, (move, take) => new {Take = take, Move = move}); 

// the result is all the points from mouse move where the mouse button isn't pressed 
var result = all.Where(t => t.Take).Select(t => t.Move); 
0

以下は2つの可能な解決策

です0

OR

var du = mousedown.Select(_ => false).Merge(mouseup.Select(_ => true)).Merge(Observable.Return(true)); 
mousemove.CombineLatest(du, (ev, b) => new Tuple<MouseEventArgs, bool>(ev.EventArgs, b)) 
.Where(t => t.Item2) 
.Select(t => t.Item1) 
.Subscribe(....); 
0

これは動作します:

 var mouseMoveWhileUpOnly = 
      mouseUp 
       .Select(mu => 
        mouseMove 
         .TakeUntil(mouseDown)) 
       .Switch(); 

あなたは、観察が実際に手動でマウスアップを行うために必要とせずに開始するために行う必要がある唯一のトリックはこれです:

 var mouseUp = Observable 
      .FromEventPattern<MouseButtonEventHandler, MouseButtonEventArgs>(
       h => this.MouseLeftButtonUp += h, 
       h => this.MouseLeftButtonUp -= h) 
      .Select(ep => Unit.Default) 
      .StartWith(Unit.Default); 

StartWithに注意してください。

その他の場合、mouseDown & mouseMoveの観測値は正常に定義されます。

関連する問題