Example #1
0
 private void ConnectVisuals()
 {
     if (SelectedShapes == null || SelectedShapes.Count() != 2)
     {
         return;
     }
     AddConnection(SelectedShapes.First(), SelectedShapes.Last());
 }
Example #2
0
        public MainWindowVM(INavigationService navigationService = null)
        {
            this.navigationService = navigationService ?? Locator.CurrentMutable.GetService <INavigationService>();

            Visuals = new ReactiveList <IVisual>
            {
                ChangeTrackingEnabled = true
            };
            SelectedVisuals = new ReactiveList <IVisual>
            {
                ChangeTrackingEnabled = true
            };
            shapes              = Visuals.CountChanged.Select(_ => Visuals.Where(x => x is IShape).Cast <IShape>()).ToProperty(this, x => x.Shapes);
            connections         = Visuals.CountChanged.Select(_ => Visuals.Where(x => x is IConnection).Cast <IConnection>()).ToProperty(this, x => x.Connections);
            selectedShapes      = SelectedVisuals.CountChanged.Select(_ => SelectedVisuals.Where(x => x is IShape).Cast <IShape>()).ToProperty(this, x => x.SelectedShapes);
            selectedConnections = SelectedVisuals.CountChanged.Select(_ => SelectedVisuals.Where(x => x is IConnection).Cast <IConnection>()).ToProperty(this, x => x.SelectedConnections);

            var shp1 = AddCircle();
            var shp2 = AddTriangle();
            var shp3 = AddSquare();

            AddConnection(shp1, shp2);
            AddConnection(shp2, shp3);

            AddCircleCommand = ReactiveCommand.Create(AddCircle);

            //Only allow Squares when there are at least 2 Circles
            var canCreateSquares = Visuals
                                   .Changed
                                   .ToUnit()
                                   .Select(_ => Visuals.Count(m => m is CircleVM) >= 2);

            AddSquareCommand = ReactiveCommand.Create(AddSquare, canCreateSquares);

            //Limit Triangles to Number of Squares / 2
            var canCreateTriangles = Visuals
                                     .Changed
                                     .ToUnit()
                                     .Select(_ => Visuals.Count(m => m is TriangleVM) < Visuals.Count(m => m is SquareVM) / 2)
                                     .StartWith(false);

            AddTriangleCommand = ReactiveCommand.Create(AddTriangle, canCreateTriangles);

            var oneSelected = SelectedVisuals.CountChanged.Select(c => c == 1);

            EditCommand = ReactiveCommand.Create <IVisual>(x => EditVisual(x), oneSelected);
            //Some commands only make sense when something is selected
            var anythingSelected = SelectedVisuals.CountChanged.Select(c => c > 0);

            DeselectAllCommand       = ReactiveCommand.Create(DeselectAll, anythingSelected);
            RotateSelectedCommand    = ReactiveCommand.Create(RotateSelected, anythingSelected);
            DuplicateSelectedCommand = ReactiveCommand.Create(DuplicateSelected, anythingSelected);
            DeleteSelectedCommand    = ReactiveCommand.Create(DeleteSelected, anythingSelected);
            ToggleSpawnerCommand     = ReactiveCommand.Create(ToggleSpawner);

            var canConnect = SelectedVisuals.CountChanged.Select(_ => SelectedShapes != null && SelectedShapes.Count() == 2).StartWith(false);

            ConnectCommand = ReactiveCommand.Create(ConnectVisuals, canConnect);

            //When one item is moved manually move all the other selected movables too
            Visuals.ItemChanged
            .Where(x => x.Sender is IShape && (x.Sender as IShape).IsMoving)
            .Select(x => new { Sender = x.Sender as IShape, Pos = new Point(x.Sender.Left, x.Sender.Top) })
            .Buffer(2, 1)
            .Where(x => x[0].Sender == x[1].Sender)
            .Subscribe(x => MovableMoved(x[0].Sender, x[0].Pos, x[1].Pos));
        }