2

i am trying to use a Anoto-Pen as a TouchDevice with SurfaceInkCanvas.
The pen uses a coordinate-system printed on a sheet of paper to derive its position and this positional data then is send to my application. There i try to transform it to TouchInput by subclassing TouchDevice and convert the send positional data and events to .NET Touch-Events using TouchDevice.ReportDown();, TouchDevice.ReportMove(), etc. Moving ScatterViewItems around and handling button "clicks" works so far.

The problem now is that when I try to write on the InkCanvas only dots are drawn. After observing the events that are fired it seems that the InkCanvas does not receive OnTouchMove events.

I registered event handlers for TouchDown, TouchMove and TouchUp on my SurfaceInkCanvas. TouchDown is never triggered. TouchMove and TouchUp only when I start outside of the SurfaceInkCanvas and then move to a point inside of it.

Here is the code for my TouchDevice:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Text.RegularExpressions;
using System.Windows;
using PaperDisplay.PenInput;
using System.Windows.Media;
using System.Windows.Threading;

namespace TouchApp
{
public class PenTouchDevice : TouchDevice
{
    public Point Position { get; set; }
    public Person Person { get; set; }

    public PenTouchDevice(Person person)
        : base(person.GetHashCode())
    {
        Person = person;
    }

    public override TouchPointCollection GetIntermediateTouchPoints(System.Windows.IInputElement relativeTo)
    {
        return new TouchPointCollection();
    }

    public override TouchPoint GetTouchPoint(System.Windows.IInputElement relativeTo)
    {
        Point point = Position;
        if (relativeTo != null)
        {
            point = this.ActiveSource.RootVisual.TransformToDescendant((Visual)relativeTo).Transform(Position);
        }
        return new TouchPoint(this, point, new Rect(point, new Size(2.0, 2.0)), TouchAction.Move);
    }

    public void PenDown(PenPointInputArgs args, Dispatcher dispatcher)
    {
        dispatcher.BeginInvoke((Action)(() =>
        {
            SetActiveSource(PresentationSource.FromVisual(Person.Window));
            Position = GetPosition(args);
            if (!IsActive)
            {
                Activate();
            }
            ReportDown();
        }));
    }

    public void PenUp(PenPointInputArgs args, Dispatcher dispatcher)
    {
        dispatcher.BeginInvoke((Action)(() =>
        {
            Position = GetPosition(args);
            if (IsActive)
            {
                ReportUp();
                Deactivate();
            }
        }));

    }

    public void PenMove(PenPointInputArgs args, Dispatcher dispatcher)
    {
        dispatcher.BeginInvoke((Action)(() =>
        {
            if (IsActive)
            {
                Position = GetPosition(args);
                ReportMove();
            }
        }));
    }

    public Point GetPosition(PenPointInputArgs args)
    {
        double adaptedX = args.Y - 0.01;
        double adaptedY = (1 - args.X) - 0.005;
        return new Point(adaptedX * Person.Window.ActualWidth, adaptedY * Person.Window.ActualHeight);
    }
}
}

I have the following code in my App.xaml.cs and it is called every time a pen input occurs:

public void HandleEvent(object sender, EventArgs args)
    {
        if (typeof(PointInputArgs).IsAssignableFrom(args.GetType()))
        {
            PenPointInputArgs pointArgs = (PenPointInputArgs)args;
            switch (pointArgs.EventType)
            {
                case InputEvent.Down: touchDevice1.PenDown(pointArgs, this.Dispatcher); break;
                case InputEvent.Up: touchDevice1.PenUp(pointArgs, this.Dispatcher); break;
                case InputEvent.Move:
                case InputEvent.MoveDown: touchDevice1.PenMove(pointArgs, this.Dispatcher); break;
            }

        }
    }

Thank you in advance.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
me.
  • 21
  • 3

1 Answers1

-1

GetIntermediateTouchPoints must not return an empty array, it should contain at least one point

public override TouchPointCollection GetIntermediateTouchPoints(System.Windows.IInputElement relativeTo)
{
    TouchPointCollection refCollection = new TouchPointCollection();
    refCollection.Add(GetTouchPoint(relativeTo));
    return refCollection;
}
Steve Czetty
  • 6,147
  • 9
  • 39
  • 48