Наследование: IInkStrokeContainer
Пример #1
0
 public static async Task<InkStrokeContainer> LoadInkingFromFile(int pageNumber, StorageFolder dataFolder)
 {
     InkStrokeContainer inkStrokeContainer = new InkStrokeContainer();
     StorageFolder inkingFolder = await getInkingFolder(dataFolder);
     StorageFile inkFile = await inkingFolder.TryGetItemAsync(pageNumber.ToString() + EXT_INKING) as StorageFile;
     if (inkFile != null)
     {
         try
         {
             using (var inkStream = await inkFile.OpenSequentialReadAsync())
             {
                 await inkStrokeContainer.LoadAsync(inkStream);
             }
             AppEventSource.Log.Debug("InAppInking: Inking for page " + pageNumber.ToString() + " loaded.");
         }
         catch (Exception e)
         {
             string errorMsg = "Error when loading inking for page " + pageNumber.ToString() + "\n Exception: " + e.Message;
             AppEventSource.Log.Error("In App Inking: " + errorMsg);
             int userResponse = await App.NotifyUserWithOptions(errorMsg, new string[] { "Remove Inking", "Ignore" });
             switch (userResponse)
             {
                 case 0: // Delete inking file
                     await inkFile.DeleteAsync();
                     AppEventSource.Log.Error("InAppInking: File deleted.");
                     break;
                 default: break;
             }
         }
     }
     return inkStrokeContainer;
 }
Пример #2
0
 public InkStrokeContainer loadInking(int pageNumber)
 {
     // Load inking if exist
     InkStrokeContainer inkStrokeContainer;
     if (!InkDictionary.TryGetValue(pageNumber, out inkStrokeContainer))
         inkStrokeContainer = new InkStrokeContainer();
     return inkStrokeContainer;
 }
Пример #3
0
 public async Task eraseStrokes(int pageNumber, InkStrokeContainer inkStrokeContainer, IReadOnlyList<InkStroke> inkStrokes)
 {
     foreach(InkStroke inkStroke in inkStrokes)
     {
         inAppInkStrokes[pageNumber].Remove(inkStroke);
     }
     await saveInking(pageNumber);
 }
Пример #4
0
 public async Task addStrokes(int pageNumber, InkStrokeContainer inkStrokeContainer, IReadOnlyList<InkStroke> inkStrokes)
 {
     if (!inAppInkStrokes.ContainsKey(pageNumber))
     {
         inAppInkStrokes[pageNumber] = new List<InkStroke>(inkStrokes);
     }
     else inAppInkStrokes[pageNumber].AddRange(inkStrokes);
     await saveInking(pageNumber);
 }
Пример #5
0
 public async Task saveInking(int pageNumber, InkStrokeContainer inkStrokeContainer = null)
 {
     InkStrokeContainer container = new InkStrokeContainer();
     List<InkStroke> inAppStrokes = new List<InkStroke>();
     inAppInkStrokes.TryGetValue(pageNumber, out inAppStrokes);
     foreach (InkStroke inkStroke in inAppStrokes)
     {
         container.AddStroke(inkStroke.Clone());
     }
     await inAppInking.saveInking(pageNumber, container);
     // TODO: Save removed ink strokes
 }
Пример #6
0
 public async Task saveInking(int pageNumber, InkStrokeContainer inkStrokeContainer)
 {
     // Remove if there is no strokes.
     if (inkStrokeContainer.GetStrokes().Count == 0)
     {
         InkDictionary.Remove(pageNumber);
     }
     else
     {
         InkDictionary[pageNumber] = inkStrokeContainer;
     }
     // Enqueue the page if it is not already in the queue
     if (!this.inkingChangedPagesQueue.Contains(pageNumber))
         this.inkingChangedPagesQueue.Enqueue(pageNumber);
     // Invoke save inking only if SaveInking is not running.
     // This will prevent running multiple saving instance at the same time.
     if (!this.isSavingInking)
         await SaveInkingQueue();
 }
        /// <summary>
        /// Load the family and their notes from local storage
        /// </summary>
        /// <returns>Null if there was no model to load, otherwise, the deserialized model</returns>
        private async Task<Model> LoadModelAsync()
        {
            Model model = null;

            InkStrokeContainer combinedStrokes = new InkStrokeContainer(); // To avoid managing individual files for every InkCanvas, we will combine all ink stroke information into one container
            List<int> InkStrokesPerCanvas = new List<int>();

            try
            {
                StorageFile modelDataFile = await ApplicationData.Current.LocalFolder.GetFileAsync(NOTES_MODEL_FILE);
                using (IRandomAccessStream randomAccessStream = await modelDataFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    // Load the model which contains the people and the note collection
                    try
                    {
                        DataContractJsonSerializer modelSerializer = new DataContractJsonSerializer(typeof(Model));
                        model = (Model)modelSerializer.ReadObject(randomAccessStream.AsStream());
                    }
                    catch (System.Runtime.Serialization.SerializationException)
                    {
                        System.Diagnostics.Debug.Assert(false, "Failed to load serialized model");
                        return null;
                    }
                }

                // For each sticky note, load the number of inkstrokes it contains
                StorageFile inkDataFile = await ApplicationData.Current.LocalFolder.GetFileAsync(NOTES_INK_FILE);
                using (IInputStream inkStream = await inkDataFile.OpenSequentialReadAsync())
                {
                    bool combinedStrokesExist = false;
                    DataReader reader = new DataReader(inkStream);
                    foreach (StickyNote n in model.StickyNotes)
                    {
                        await reader.LoadAsync(sizeof(int)); // You need to buffer the data before you can read from a DataReader.
                        int numberOfInkStrokes = reader.ReadInt32();
                        InkStrokesPerCanvas.Add(numberOfInkStrokes);
                        combinedStrokesExist |= numberOfInkStrokes > 0;
                    }

                    // Load the ink data
                    if (combinedStrokesExist)
                    {
                        await combinedStrokes.LoadAsync(inkStream);
                    }
                } // using inkStream
            } // try
            catch (FileNotFoundException)
            {
                // No data to load. We'll start with a fresh model
                return null;
            }

            // Factor out the inkstrokes from the big container into each note
            int allStrokesIndex = 0, noteIndex = 0;
            IReadOnlyList<InkStroke> allStrokes = combinedStrokes.GetStrokes();
            foreach (StickyNote n in model.StickyNotes)
            {
                // InkStrokeContainers can't be serialized using the default xml/json serialization.
                // So create a new one and fill it up from the data we restored
                n.Ink = new InkStrokeContainer();
                // pull out the ink strokes that belong to this note
                for (int i = 0; i < InkStrokesPerCanvas[noteIndex]; i++)
                {
                    n.Ink.AddStroke(allStrokes[allStrokesIndex++].Clone());
                }
                ++noteIndex;
            }

            return model;
        }
        /// <summary>
        /// Save the model (family and their notes) to the app's isolated storage
        /// </summary>
        /// <remarks>
        /// The data format for notes data:
        /// 
        ///     Serialized model data (the people and the sticky notes)
        ///     For each sticky note
        ///     {
        ///         int32 number of inkstrokes for the note
        ///     }
        ///     All ink stroke data (for all notes) combined into one container
        /// </remarks>
        /// <returns></returns>
        public async Task SaveModelAsync()
        {
            // Persist the model
            StorageFile notesDataFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(NOTES_MODEL_FILE, CreationCollisionOption.ReplaceExisting);
            using (Stream notesDataStream = await notesDataFile.OpenStreamForWriteAsync())
            {
                // Serialize the model which contains the people and the stickyNote collection
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Model));
                serializer.WriteObject(notesDataStream, Model);
            }

            /* For each sticky note, save the number of inkstrokes it contains.
               The function on the InkStrokeContainer that persists its contents is not designed
               to save persist containers to the one stream. We also don't want to manage one
               backing file per note. So combine the ink strokes into one container and persist that.
               We'll seperate out the ink strokes to the right ink control by keeping track of how
               many ink strokes belongs to each note */

            InkStrokeContainer CombinedStrokes = new InkStrokeContainer();
            StorageFile inkFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(NOTES_INK_FILE, CreationCollisionOption.ReplaceExisting);
            using (var randomAccessStream = await inkFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                using (IOutputStream inkStream = randomAccessStream.GetOutputStreamAt(0)) // DataWriter requires an IOutputStream
                {
                    bool combinedStrokesHasContent = false; // whether we had any ink to save across all of the notes
                    DataWriter writer = new DataWriter(inkStream);
                    foreach (StickyNote Note in Model.StickyNotes)
                    {
                        // Save # strokes for this note
                        if (Note.Ink != null && Note.Ink.GetStrokes().Count > 0)
                        {
                            IReadOnlyList<InkStroke> InkStrokesInNote = Note.Ink.GetStrokes();
                            writer.WriteInt32(InkStrokesInNote.Count);
                            // capture the ink strokes into the combined container which will be saved at the end of the notes data file
                            foreach (InkStroke s in InkStrokesInNote)
                            {
                                CombinedStrokes.AddStroke(s.Clone());
                            }
                            combinedStrokesHasContent = true;
                        }
                        else
                        {
                            writer.WriteInt32(0); // not all notes have ink
                        }
                    }
                    await writer.StoreAsync(); // flush the data in the writer to the inkStream

                    // Persist the ink data
                    if (combinedStrokesHasContent ) 
                    {
                        await CombinedStrokes.SaveAsync(inkStream);
                    }
                }
            }
        }
Пример #9
0
 public Task saveInking(int pageNumber, InkStrokeContainer inkStrokes)
 {
     throw new NotImplementedException();
 }
Пример #10
0
 public Task eraseStrokes(int pageNumber, InkStrokeContainer inkStrokeContainer, IReadOnlyList<InkStroke> inkStrokes)
 {
     throw new NotImplementedException();
 }
Пример #11
0
        public async Task<Dictionary<int, InkStrokeContainer>> LoadInkDictionary()
        {
            InkDictionary = new Dictionary<int, InkStrokeContainer>();
            foreach (StorageFile inkFile in await InkingFolder.GetFilesAsync())
            {
                int pageNumber = 0;
                try
                {
                    pageNumber = Convert.ToInt32(inkFile.Name.Substring(0, inkFile.Name.Length - 4));
                    InkStrokeContainer inkStrokeContainer = new InkStrokeContainer();
                    using (var inkStream = await inkFile.OpenSequentialReadAsync())
                    {
                        await inkStrokeContainer.LoadAsync(inkStream);
                    }
                    InkDictionary.Add(pageNumber, inkStrokeContainer);

                }
                catch (Exception e)
                {
                    string errorMsg = "Error when loading inking for page " + pageNumber.ToString() + "\n Exception: " + e.Message;
                    AppEventSource.Log.Error("InkManager: " + errorMsg);
                    int userResponse = await App.NotifyUserWithOptions(errorMsg, new string[] { "Remove Inking", "Ignore" });
                    switch (userResponse)
                    {
                        case 0: // Delete inking file
                            await inkFile.DeleteAsync();
                            AppEventSource.Log.Error("InkManager: File deleted.");
                            break;
                        default: break;
                    }
                    continue;
                }
            }
            return InkDictionary;
        }
Пример #12
0
 public async Task eraseStrokes(int pageNumber, InkStrokeContainer inkStrokeContainer, IReadOnlyList<InkStroke> inkStrokes)
 {
     await saveInking(pageNumber, inkStrokeContainer);
 }
 public void SyncStrokeEx(Dictionary<InkStroke,double> strokes, InkStrokeContainer containner, double currentCanvasWidth,bool forcrRefresh = false)
 {
     lock(locker)
     {
         if(strokes.Count > containner.GetStrokes().Count)
         {
             foreach (var item in strokes)
             {
                 bool exist = false;
                 foreach (var itemInner in containner.GetStrokes())
                 {
                     InkStroke strokeCopy = itemInner.Clone();
                     strokeCopy.PointTransform = System.Numerics.Matrix3x2.CreateScale(1f);
                     if (InkHelper.SameInkStroke(item.Key, strokeCopy))
                     {
                         exist = true;
                         break;
                     }
                 }
                 if (!exist)
                 {
                     foreach(var itemMp in StrokeMapping)
                     {
                         if(InkHelper.SameInkStroke(item.Key,itemMp.Key) && item.Value == itemMp.Value)
                         {
                             StrokeMapping.Remove(itemMp.Key);
                             break;
                         }
                     }                           
                 }
             }
         }
         else if(strokes.Count < containner.GetStrokes().Count)
         {
             InkStroke stroke = containner.GetStrokes()[containner.GetStrokes().Count - 1].Clone();
             StrokeMapping.Add(stroke.Clone(),currentCanvasWidth);
         }
         else
         {
             if(strokes.Count == StrokeMapping.Count && !forcrRefresh)
             {
                 return;
             }
         }
         strokes.Clear();
         containner.Clear();
         foreach(var item in StrokeMapping)
         {
             InkStroke stroke = item.Key.Clone();
             strokes.Add(stroke.Clone(), item.Value);
             stroke.PointTransform = System.Numerics.Matrix3x2.CreateScale((float)(currentCanvasWidth / item.Value));
             containner.AddStroke(stroke.Clone());
         }
         //if(read)
         //{
         //    strokes.Clear();
         //    foreach(var item in StrokeMapping)
         //    {
         //        strokes.Add(item.Key.Clone(),item.Value);
         //    }
         //}
         //else
         //{
         //    StrokeMapping.Clear();
         //    foreach(var item in strokes)
         //    {
         //        StrokeMapping.Add(item.Key.Clone(),item.Value);
         //    }
         //}
     }
 }
        public void SyncStroke(InkStrokeContainer containner,double currentCanvasWidth,bool syncAll = true, InkStroke stroke = null)
        {
            lock(locker)
            {
                try
                {
                    if (syncAll)
                    {
                        Strokes.Clear();
                        foreach (var item in containner.GetStrokes())
                        {
                            InkStroke strokeClone = item.Clone();
                            strokeClone.PointTransform = System.Numerics.Matrix3x2.CreateScale((float)(currentCanvasWidth / originalCanvasWidth));
                            Strokes.Add(strokeClone);
                        }
                    }
                    else
                    {
                        if (null != stroke)
                        {
                            InkStroke strokeClone = stroke.Clone();
                            strokeClone.PointTransform = System.Numerics.Matrix3x2.CreateScale((float)(currentCanvasWidth / originalCanvasWidth));
                            Strokes.Add(strokeClone);
                        }
                        else
                        {
                            containner.Clear();
                            foreach (var item in Strokes)
                            {
                                InkStroke strokeClone = item.Clone();
                                strokeClone.PointTransform = System.Numerics.Matrix3x2.CreateScale((float)(currentCanvasWidth / originalCanvasWidth));
                                containner.AddStroke(strokeClone);
                            }
                        }
                    }
                }
                catch(Exception e)
                {
#if DEBUG
                    System.Diagnostics.Debug.WriteLine(e.Message);

#endif
                }                           
            }
        }