/// <summary>
        /// Convert a Xamarin Ink drawing attributes to UWP
        /// </summary>
        /// <param name="attributes">Xamarin Ink Drawing attributes</param>
        /// <returns>Xamarin ink drawing attributes</returns>
        public static InkDrawingAttributes ToInkDrawingAttributes(this XInkDrawingAttributes attributes)
        {
            if (attributes == null)
            {
                throw new ArgumentNullException(nameof(attributes));
            }

            InkDrawingAttributes attributes2;

            if (attributes.Kind == XInkDrawingAttributesKind.Pencil)
            {
                attributes2 = InkDrawingAttributes.CreateForPencil();
            }
            else
            {
                attributes2 = new InkDrawingAttributes();
            }

            attributes2.Color          = attributes.Color.ToWindowsColor();
            attributes2.IgnorePressure = attributes.IgnorePressure;
            attributes2.Size           = new Windows.Foundation.Size(Convert.ToDouble(attributes.Size), Convert.ToDouble(attributes.Size));
            attributes2.PenTip         = attributes.PenTip == XPenTipShape.Circle ? PenTipShape.Circle : PenTipShape.Rectangle;

            return(attributes2);
        }
        private void UpdatePencilPreviewInkStroke()
        {
            var currentStrokeSize    = PencilStrokeWidthSlider.Value;
            var inkDrawingAttributes = InkDrawingAttributes.CreateForPencil();

            inkDrawingAttributes.Color = StrokePreviewColor;
            inkDrawingAttributes.Size  = new Size(currentStrokeSize, currentStrokeSize);

            if (PencilPreviewInkStroke == null)
            {
                CreatePencilPreviewStroke();
            }

            PencilPreviewInkStroke.DrawingAttributes = inkDrawingAttributes;

            var strokeContainer = PencilStrokePreviewInkCanvas.InkPresenter.StrokeContainer;

            foreach (var stroke in strokeContainer.GetStrokes())
            {
                stroke.Selected = true;
            }

            strokeContainer.AddStroke(PencilPreviewInkStroke.Clone());
            strokeContainer.DeleteSelected();
        }
Exemple #3
0
        internal void OnDeserializedMethod(StreamingContext context)
        {
            var finalPointList = new List <InkPoint>(SerializableFinalPointList.Count);

            foreach (var point in SerializableFinalPointList)
            {
                finalPointList.Add(new InkPoint(point.Position, point.Pressure, point.TiltX, point.TiltY, point.Timestamp));
            }

            FinalPointList = finalPointList;

            if (DrawingAttributes != null && SerializableDrawingAttributesKind.HasValue && SerializableDrawingAttributesKind == (short)InkDrawingAttributesKind.Pencil)
            {
                var pencilAttributes = InkDrawingAttributes.CreateForPencil();
                pencilAttributes.Color = DrawingAttributes.Color;

                // work around argument null exception.
                pencilAttributes.FitToCurve     = DrawingAttributes.FitToCurve;
                pencilAttributes.IgnorePressure = DrawingAttributes.IgnorePressure;
                pencilAttributes.IgnoreTilt     = DrawingAttributes.IgnoreTilt;
                pencilAttributes.Size           = DrawingAttributes.Size;

                if (SerializableDrawingAttributesPencilProperties.HasValue)
                {
                    pencilAttributes.PencilProperties.Opacity = SerializableDrawingAttributesPencilProperties.Value;
                }

                DrawingAttributes = pencilAttributes;
            }

            // Empty unused values
            SerializableDrawingAttributesPencilProperties = null;
            SerializableFinalPointList        = null;
            SerializableDrawingAttributesKind = null;
        }
        internal void OnDeserializedMethod(StreamingContext context)
        {
            var finalPointList = new List <InkPoint>(SerializableFinalPointList.Count);

            foreach (var point in SerializableFinalPointList)
            {
                finalPointList.Add(new InkPoint(point.Position, point.Pressure, point.TiltX, point.TiltY, point.Timestamp));
            }

            FinalPointList = finalPointList;

            InkDrawingAttributes pencilAttributes;

            if (SerializableDrawingAttributesKind.HasValue &&
                SerializableDrawingAttributesKind == (short)InkDrawingAttributesKind.Pencil)
            {
                pencilAttributes = InkDrawingAttributes.CreateForPencil();
            }
            else
            {
                pencilAttributes = new InkDrawingAttributes
                {
                    PenTip            = DrawingAttributes.PenTip,
                    PenTipTransform   = DrawingAttributes.PenTipTransform,
                    DrawAsHighlighter = DrawingAttributes.DrawAsHighlighter
                };
            }

            pencilAttributes.Color          = DrawingAttributes.Color;
            pencilAttributes.FitToCurve     = DrawingAttributes.FitToCurve;
            pencilAttributes.IgnorePressure = DrawingAttributes.IgnorePressure;
            pencilAttributes.IgnoreTilt     = DrawingAttributes.IgnoreTilt;
            pencilAttributes.Size           = DrawingAttributes.Size;

            if (SerializableDrawingAttributesPencilProperties.HasValue)
            {
                pencilAttributes.PencilProperties.Opacity = SerializableDrawingAttributesPencilProperties.Value;
            }

            DrawingAttributesIgnored = pencilAttributes;

            // Empty unused values
            SerializableDrawingAttributesPencilProperties = null;
            SerializableFinalPointList        = null;
            SerializableDrawingAttributesKind = null;
        }
        private async void StrokeChangeBrokerOnStrokeCollected(object sender, StrokeDescription strokeDescription)
        {
            if (idToStrokeMapping.ContainsKey(strokeDescription.Id))
            {
                return;
            }

            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                          () =>
            {
                lock (canvasChangeLock)
                {
                    InkDrawingAttributes inkDrawingAttributes;
                    if (strokeDescription.IsPencil)
                    {
                        inkDrawingAttributes = InkDrawingAttributes.CreateForPencil();
                        inkDrawingAttributes.PencilProperties.Opacity = strokeDescription.Opacity;
                    }
                    else
                    {
                        inkDrawingAttributes = new InkDrawingAttributes();
                        inkDrawingAttributes.DrawAsHighlighter = strokeDescription.DrawAsHighlighter;
                    }

                    inkDrawingAttributes.Color          = Color.FromArgb(strokeDescription.ColorValues[0], strokeDescription.ColorValues[1], strokeDescription.ColorValues[2], strokeDescription.ColorValues[3]);
                    inkDrawingAttributes.FitToCurve     = strokeDescription.FitToCurve;
                    inkDrawingAttributes.IgnorePressure = strokeDescription.IgnorePressure;
                    inkDrawingAttributes.Size           = new Size(strokeDescription.SizeValues[0], strokeDescription.SizeValues[1]);

                    strokeBuilder.SetDefaultDrawingAttributes(inkDrawingAttributes);

                    var points = new List <InkPoint>(strokeDescription.PointXValues.Length);
                    for (var i = 0; i < strokeDescription.PointXValues.Length; i++)
                    {
                        points.Add(new InkPoint(new Point(strokeDescription.PointXValues[i], strokeDescription.PointYValues[i]), strokeDescription.PressureValues[i]));
                    }

                    var newStroke = strokeBuilder.CreateStrokeFromInkPoints(points, Matrix3x2.Identity);
                    idToStrokeMapping[strokeDescription.Id] = newStroke;

                    canvas.InkPresenter.StrokeContainer.AddStroke(newStroke);
                }
            });
        }
        public InkExample()
        {
            this.InitializeComponent();

            inkCanvas.InkPresenter.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Mouse | Windows.UI.Core.CoreInputDeviceTypes.Pen | Windows.UI.Core.CoreInputDeviceTypes.Touch;

            // By default, pen barrel button or right mouse button is processed for inking
            // Set the configuration to instead allow processing these input on the UI thread
            inkCanvas.InkPresenter.InputProcessingConfiguration.RightDragAction = InkInputRightDragAction.LeaveUnprocessed;

            inkCanvas.InkPresenter.UnprocessedInput.PointerPressed  += UnprocessedInput_PointerPressed;
            inkCanvas.InkPresenter.UnprocessedInput.PointerMoved    += UnprocessedInput_PointerMoved;
            inkCanvas.InkPresenter.UnprocessedInput.PointerReleased += UnprocessedInput_PointerReleased;

            inkCanvas.InkPresenter.StrokeInput.StrokeStarted += StrokeInput_StrokeStarted;

            inkCanvas.InkPresenter.StrokesCollected += InkPresenter_StrokesCollected;

            inkSynchronizer = inkCanvas.InkPresenter.ActivateCustomDrying();

            inkAttributes      = inkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
            inkAttributes.Size = new Size(10, inkAttributes.Size.Height);

            if (isPencilSupported)
            {
                pencilAttributes      = InkDrawingAttributes.CreateForPencil();
                pencilAttributes.Size = new Size(10, pencilAttributes.Size.Height);
                pencilAttributes.PencilProperties.Opacity = 2;
            }
            else
            {
                usePencilCheckbox.Visibility = Visibility.Collapsed;
            }

            textFormat = new CanvasTextFormat();

            // Set defaults
            SelectedDryInkRenderingType = DryInkRenderingType.BuiltIn;
            SelectColor(color0);
            showTextLabels = true;

            needToCreateSizeDependentResources = true;
        }
Exemple #7
0
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            // load Model
            var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///inkshapes.onnx"));

            model = await Model.CreateModel(file);


            #region setup InkCanvs, sound and timers
            currentShape = shapeLabels[random.Next(shapeLabels.Count)];

            timeTimer          = new DispatcherTimer();
            timeTimer.Interval = TimeSpan.FromMilliseconds(100);
            timeTimer.Tick    += Timer_Tick;
            timeTimer.Start();


            var alarmFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///wakeup.m4a"));

            var stream = await alarmFile.OpenAsync(FileAccessMode.Read);

            mediaElement           = new MediaElement();
            mediaElement.IsLooping = true;
            mediaElement.AutoPlay  = true;
            mediaElement.SetSource(stream, alarmFile.ContentType);

            Inker.InkPresenter.InputDeviceTypes =
                CoreInputDeviceTypes.Pen |
                CoreInputDeviceTypes.Touch |
                CoreInputDeviceTypes.Mouse;

            ida       = InkDrawingAttributes.CreateForPencil();
            ida.Size  = new Size(30, 30);
            ida.Color = Colors.White;
            ida.PencilProperties.Opacity = 1;
            Inker.InkPresenter.UpdateDefaultDrawingAttributes(ida);

            Inker.InkPresenter.StrokesCollected += InkPresenter_StrokesCollectedAsync;

            SubText.Text = $"draw {currentShape} to Snooze";
            #endregion
        }
Exemple #8
0
        public void PenAttributesChanged(InkCanvas PreviewInkStrokeCanvas)
        {
            PreviewInkStrokeCanvas.InkPresenter.StrokeContainer.Clear();
            CreatePreviewInkStroke();
            InkDrawingAttributes da = new InkDrawingAttributes();

            SolidColorBrush scb = (SolidColorBrush)SelectedPen.SelectedBrush;

            Windows.UI.Color cl = new Windows.UI.Color();
            cl = scb.Color;

            da.Color = cl;

            if (SelectedInkingToolIndex == 1)
            {
                da = InkDrawingAttributes.CreateForPencil();
            }

            da.Color          = cl;
            da.Size           = new Size(SelectedPen.SelectedStrokeWidth, SelectedPen.SelectedStrokeWidth);
            da.IgnorePressure = true;
            UpdatePreviewInkStroke(da);
            PreviewInkStrokeCanvas.InkPresenter.StrokeContainer.AddStroke(PreviewInkStroke);
        }
        private void SetupBrush()
        {
            if (null != _inkCanvas)
            {
                InkDrawingAttributes inkDrawingAttributes = InkDrawingAttributes.CreateForPencil();
                inkDrawingAttributes.Color = GetNextColor();

                //inkDrawingAttributes.PenTip = PenTipShape.Circle;
                inkDrawingAttributes.Size = new Size(WINDOW_BOUNDS.Width * 0.01, WINDOW_BOUNDS.Height * 0.01);
                //inkDrawingAttributes.PenTipTransform = System.Numerics.Matrix3x2.CreateRotation((float)(70 * Math.PI / 180));// System.Numerics.Matrix3x2.CreateRotation(.785f);//
                //InkDrawingAttributes inkDrawingAttributes = new InkDrawingAttributes()
                //{
                //    Color = GetNextColor(),
                //    //FitToCurve = true,
                //    DrawAsHighlighter = true,
                //    PenTip = PenTipShape.Circle,
                //    PenTipTransform = System.Numerics.Matrix3x2.CreateRotation((float)(70 * Math.PI / 180)),
                ////FitToCurve = true,
                //    Size = new Size(WINDOW_BOUNDS.Width * 0.01, WINDOW_BOUNDS.Height * 0.01)
                //};

                _inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(inkDrawingAttributes);
            }
        }
        void OnPenTypeChanged(object sender, RoutedEventArgs e)
        {
            if (inkCanvas != null)
            {
                InkDrawingAttributes drawingAttributes = inkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
                string value = ((ComboBoxItem)PenType.SelectedItem).Content.ToString();

                if (value == "Ballpoint")
                {
                    if (drawingAttributes.Kind != InkDrawingAttributesKind.Default)
                    {
                        var newDrawingAttributes = new InkDrawingAttributes();
                        newDrawingAttributes.Color = drawingAttributes.Color;
                        drawingAttributes          = newDrawingAttributes;
                    }
                    drawingAttributes.Size              = new Size(penSize, penSize);
                    drawingAttributes.PenTip            = PenTipShape.Circle;
                    drawingAttributes.DrawAsHighlighter = false;
                    drawingAttributes.PenTipTransform   = System.Numerics.Matrix3x2.Identity;
                }
                else if (value == "Highlighter")
                {
                    if (drawingAttributes.Kind != InkDrawingAttributesKind.Default)
                    {
                        var newDrawingAttributes = new InkDrawingAttributes();
                        newDrawingAttributes.Color = drawingAttributes.Color;
                        drawingAttributes          = newDrawingAttributes;
                    }
                    // Make the pen rectangular for highlighter
                    drawingAttributes.Size              = new Size(penSize, penSize * 2);
                    drawingAttributes.PenTip            = PenTipShape.Rectangle;
                    drawingAttributes.DrawAsHighlighter = true;
                    drawingAttributes.PenTipTransform   = System.Numerics.Matrix3x2.Identity;
                }
                else if (value == "Calligraphy")
                {
                    if (drawingAttributes.Kind != InkDrawingAttributesKind.Default)
                    {
                        var newDrawingAttributes = new InkDrawingAttributes();
                        newDrawingAttributes.Color = drawingAttributes.Color;
                        drawingAttributes          = newDrawingAttributes;
                    }
                    drawingAttributes.Size              = new Size(penSize, penSize * 2);
                    drawingAttributes.PenTip            = PenTipShape.Rectangle;
                    drawingAttributes.DrawAsHighlighter = false;

                    // Set a 45 degree rotation on the pen tip
                    double radians = 45.0 * Math.PI / 180;
                    drawingAttributes.PenTipTransform = System.Numerics.Matrix3x2.CreateRotation((float)radians);
                }
                else if (value == "Windows.UI.Xaml.Controls.TextBlock")
                {
                    if (drawingAttributes.Kind != InkDrawingAttributesKind.Pencil)
                    {
                        var newDrawingAttributes = InkDrawingAttributes.CreateForPencil();
                        newDrawingAttributes.Color = drawingAttributes.Color;
                        newDrawingAttributes.Size  = drawingAttributes.Size;
                        drawingAttributes          = newDrawingAttributes;
                    }
                }
                inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(drawingAttributes);
            }
        }
Exemple #11
0
        private InkStroke ParseFromJSON(JObject stroke)
        {
            List <InkPoint> inkPoints = new List <InkPoint>();

            foreach (var point in stroke.Value <JArray>("points"))
            {
                JObject  o  = (JObject)point;
                Point    p  = new Point(o.Value <float>("x"), o.Value <float>("y"));
                InkPoint ip = new InkPoint(p, o.Value <float>("p"));
                inkPoints.Add(ip);

                _ = Windows.ApplicationModel.Core.CoreApplication.MainView.Dispatcher.
                    RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    if (o.Value <float>("y") >= inkCanvas.Height - EXPAND_MARGIN)
                    {
                        expandBoard(o.Value <float>("y"));
                    }
                    else if (o.Value <float>("x") >= inkCanvas.Width - EXPAND_MARGIN)
                    {
                        expandBoard(o.Value <float>("x"));
                    }
                });
            }

            InkStrokeBuilder     b  = new InkStrokeBuilder();
            InkDrawingAttributes da = new InkDrawingAttributes();

            // Pressure
            JObject toolInfo = stroke.Value <JObject>("tool");

            if (toolInfo != null)
            {
                if (toolInfo.Value <Boolean>("pencil"))
                {
                    da = InkDrawingAttributes.CreateForPencil();
                }
                else
                {
                    da.DrawAsHighlighter = toolInfo.Value <Boolean>("marker");
                }

                da.Size = new Size((double)toolInfo.Value <JObject>("size").GetValue("w"),
                                   (double)toolInfo.Value <JObject>("size").GetValue("h"));
            }

            // Color
            da.Color = Windows.UI.ColorHelper.FromArgb(
                (byte)stroke.Value <JObject>("color").GetValue("A"),
                (byte)stroke.Value <JObject>("color").GetValue("R"),
                (byte)stroke.Value <JObject>("color").GetValue("G"),
                (byte)stroke.Value <JObject>("color").GetValue("B")
                );
            da.IgnorePressure = false;
            da.FitToCurve     = true;

            b.SetDefaultDrawingAttributes(da);
            InkStroke c = b.CreateStrokeFromInkPoints(inkPoints, Matrix3x2.Identity);

            return(c);
        }