9 changed files with 190 additions and 158 deletions
@ -1,59 +0,0 @@ |
|||||
// -----------------------------------------------------------------------
|
|
||||
// <copyright file="MouseDevice.cs" company="Steven Kirk">
|
|
||||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|
||||
// </copyright>
|
|
||||
// -----------------------------------------------------------------------
|
|
||||
|
|
||||
namespace Perspex.Windows.Input |
|
||||
{ |
|
||||
using System; |
|
||||
using System.Reactive.Disposables; |
|
||||
using Perspex.Input; |
|
||||
using Perspex.Input.Raw; |
|
||||
using Splat; |
|
||||
|
|
||||
public class MouseDevice : IMouseDevice |
|
||||
{ |
|
||||
private static MouseDevice instance = new MouseDevice(); |
|
||||
|
|
||||
public static MouseDevice Instance |
|
||||
{ |
|
||||
get { return instance; } |
|
||||
} |
|
||||
|
|
||||
public Interactive Captured |
|
||||
{ |
|
||||
get; |
|
||||
private set; |
|
||||
} |
|
||||
|
|
||||
public Window CurrentWindow |
|
||||
{ |
|
||||
get; |
|
||||
set; |
|
||||
} |
|
||||
|
|
||||
public Point Position |
|
||||
{ |
|
||||
get; |
|
||||
set; |
|
||||
} |
|
||||
|
|
||||
public void Capture(Interactive visual) |
|
||||
{ |
|
||||
this.Captured = visual; |
|
||||
|
|
||||
if (visual == null) |
|
||||
{ |
|
||||
RawMouseEventArgs e = new RawMouseEventArgs( |
|
||||
this, |
|
||||
this.CurrentWindow, |
|
||||
RawMouseEventType.Move, |
|
||||
this.Position); |
|
||||
|
|
||||
IInputManager inputManager = Locator.Current.GetService<IInputManager>(); |
|
||||
inputManager.Process(e); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,34 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="MouseDevice.cs" company="Steven Kirk">
|
||||
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Windows.Input |
||||
|
{ |
||||
|
using Perspex.Input; |
||||
|
using Perspex.Input.Raw; |
||||
|
using Splat; |
||||
|
|
||||
|
public class WindowsMouseDevice : MouseDevice |
||||
|
{ |
||||
|
private static WindowsMouseDevice instance = new WindowsMouseDevice(); |
||||
|
|
||||
|
public static WindowsMouseDevice Instance |
||||
|
{ |
||||
|
get { return instance; } |
||||
|
} |
||||
|
|
||||
|
public Window CurrentWindow |
||||
|
{ |
||||
|
get; |
||||
|
set; |
||||
|
} |
||||
|
|
||||
|
public new Point Position |
||||
|
{ |
||||
|
get { return base.Position; } |
||||
|
internal set { base.Position = value; } |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,135 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="MouseDevice.cs" company="Tricycle">
|
||||
|
// Copyright 2014 Tricycle. All rights reserved.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Input |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using System.Reactive.Linq; |
||||
|
using Perspex.Controls; |
||||
|
using Perspex.Input.Raw; |
||||
|
using Splat; |
||||
|
|
||||
|
public abstract class MouseDevice : IMouseDevice |
||||
|
{ |
||||
|
public MouseDevice() |
||||
|
{ |
||||
|
this.InputManager.RawEventReceived |
||||
|
.OfType<RawMouseEventArgs>() |
||||
|
.Where(x => x.Device == this) |
||||
|
.Subscribe(this.ProcessMouse); |
||||
|
} |
||||
|
|
||||
|
public Interactive Captured |
||||
|
{ |
||||
|
get; |
||||
|
protected set; |
||||
|
} |
||||
|
|
||||
|
public IInputManager InputManager |
||||
|
{ |
||||
|
get { return Locator.Current.GetService<IInputManager>(); } |
||||
|
} |
||||
|
|
||||
|
public Point Position |
||||
|
{ |
||||
|
get; |
||||
|
protected set; |
||||
|
} |
||||
|
|
||||
|
public virtual void Capture(Interactive visual) |
||||
|
{ |
||||
|
this.Captured = visual; |
||||
|
} |
||||
|
|
||||
|
private void ProcessMouse(RawMouseEventArgs e) |
||||
|
{ |
||||
|
switch (e.Type) |
||||
|
{ |
||||
|
case RawMouseEventType.Move: |
||||
|
this.MouseMove((IMouseDevice)e.Device, (IVisual)e.Root, e.Position); |
||||
|
break; |
||||
|
case RawMouseEventType.LeftButtonDown: |
||||
|
this.MouseDown((IMouseDevice)e.Device, (IVisual)e.Root, e.Position); |
||||
|
break; |
||||
|
case RawMouseEventType.LeftButtonUp: |
||||
|
this.MouseUp((IMouseDevice)e.Device, (IVisual)e.Root, e.Position); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void MouseMove(IMouseDevice device, IVisual visual, Point p) |
||||
|
{ |
||||
|
if (this.Captured == null) |
||||
|
{ |
||||
|
this.InputManager.SetPointerOver(this, visual, p); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
Point offset = new Point(); |
||||
|
|
||||
|
foreach (IVisual ancestor in this.Captured.GetVisualAncestors()) |
||||
|
{ |
||||
|
offset += ancestor.Bounds.Position; |
||||
|
} |
||||
|
|
||||
|
this.InputManager.SetPointerOver(this, this.Captured, p - offset); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void MouseDown(IMouseDevice device, IVisual visual, Point p) |
||||
|
{ |
||||
|
IVisual hit = visual.GetVisualAt(p); |
||||
|
|
||||
|
if (hit != null) |
||||
|
{ |
||||
|
Interactive interactive = this.Captured ?? (hit as Interactive) ?? hit.GetVisualAncestor<Interactive>(); |
||||
|
IFocusable focusable = |
||||
|
this.Captured as IFocusable ?? |
||||
|
hit.GetVisualAncestorsAndSelf() |
||||
|
.OfType<IFocusable>() |
||||
|
.FirstOrDefault(x => x.Focusable); |
||||
|
|
||||
|
if (interactive != null) |
||||
|
{ |
||||
|
interactive.RaiseEvent(new PointerEventArgs |
||||
|
{ |
||||
|
Device = this, |
||||
|
RoutedEvent = Control.PointerPressedEvent, |
||||
|
OriginalSource = interactive, |
||||
|
Source = interactive, |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
if (focusable != null && focusable.Focusable) |
||||
|
{ |
||||
|
focusable.Focus(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void MouseUp(IMouseDevice device, IVisual visual, Point p) |
||||
|
{ |
||||
|
IVisual hit = visual.GetVisualAt(p); |
||||
|
|
||||
|
if (hit != null) |
||||
|
{ |
||||
|
Interactive source = this.Captured ?? (hit as Interactive) ?? hit.GetVisualAncestor<Interactive>(); |
||||
|
|
||||
|
if (source != null) |
||||
|
{ |
||||
|
source.RaiseEvent(new PointerEventArgs |
||||
|
{ |
||||
|
Device = this, |
||||
|
RoutedEvent = Control.PointerReleasedEvent, |
||||
|
OriginalSource = source, |
||||
|
Source = source, |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue