Exemple #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (objectType != typeof(TrackViewModel))
            {
                throw new NotImplementedException();
            }

            var typedValue = (TrackViewModel)existingValue;

            var jobject = JObject.Load(reader);

            var json = jobject["LayoutJson"].ToObject <string>();

            using (var _ = typedValue.SuppressChangeNotifications()) {
                typedValue.TrackPiecesJson = json;
            }
            var repos             = TrackRepository.FromJson(json);
            var boundaryLocations = jobject["BoundaryLocations"].ToObject <Dictionary <int, TrackSectionBoundaryModel> >();
            var sectionLocations  = jobject["SectionLocations"]
                                    .Skip(1)
                                    .Cast <JProperty>()
                                    .ToDictionary(x => int.Parse(x.Name), x => new IntermediaryType2 {
                ControlPoint1 = new Point(x.Value["ControlPoint1"]["X"].ToObject <int>(), x.Value["ControlPoint1"]["Y"].ToObject <int>()),
                ControlPoint2 = new Point(x.Value["ControlPoint1"]["X"].ToObject <int>(), x.Value["ControlPoint2"]["Y"].ToObject <int>())
            });

            typedValue.TrackSections = new ObservableCollection <TrackSectionViewModel>(repos.Sections.Select(x => {
                if (!sectionLocations.TryGetValue(x.Id, out var section))
                {
                    section = new IntermediaryType2 {
                        ControlPoint1 = GetDefaultPoint(), ControlPoint2 = GetDefaultPoint()
                    };
                }

                return(new TrackSectionViewModel {
                    TrackSectionModel = new TrackSectionModel {
                        TrackSection = x,
                        ControlPoint1 = section.ControlPoint1,
                        ControlPoint2 = section.ControlPoint2,
                        Boundary1 = GetBoundaryLocation(0, x, boundaryLocations),
                        Boundary2 = GetBoundaryLocation(1, x, boundaryLocations)
                    }
                });
            }));

            return(typedValue);
        }
        private void LoadTrackPieces(string json)
        {
            var repos      = TrackRepository.FromJson(json);
            var random     = new Random();
            var boundaries = repos.Boundaries.ToDictionary(x => x.Id, x => new TrackSectionBoundaryModel {
                Id = x.Id, Location = new Point(random.Next(100, 200), random.Next(100, 200))
            });

            using var _ = this.DelayChangeNotifications();

            Focus         = null;
            TrackSections = new ObservableCollection <TrackSectionViewModel>(repos.Sections.Select(x => new TrackSectionViewModel {
                TrackSectionModel = new TrackSectionModel {
                    Boundary1     = boundaries[x.ConnectedBoundaries[0].Id],
                    Boundary2     = boundaries[x.ConnectedBoundaries[1].Id],
                    ControlPoint1 = new Point(random.Next(100, 200), random.Next(100, 200)),
                    ControlPoint2 = new Point(random.Next(100, 200), random.Next(100, 200)),
                    TrackSection  = x
                }
            }));
        }