private void Apply()
        {
            string actionText = null;
            var ac = new ActionCollection();
            var editAction = GetEditEntityDataAction();
            var visgroupAction = GetUpdateVisgroupsAction();

            if (editAction != null)
            {
                // The entity change is more important to show
                actionText = "Edit entity data";
                ac.Add(editAction);
            }

            if (visgroupAction != null)
            {
                // Visgroup change shows if entity data not changed
                if (actionText == null) actionText = "Edit object visgroups";
                ac.Add(visgroupAction);
            }

            if (!ac.IsEmpty())
            {
                // Run if either action shows changes
                Document.PerformAction(actionText, ac);
            }

            Class.BackColor = Color.White;
        }
Пример #2
0
        public override void MouseDown(ViewportBase viewport, ViewportEvent e)
        {
            var vp = viewport as Viewport3D;
            if (vp == null || (e.Button != MouseButtons.Left && e.Button != MouseButtons.Right)) return;

            var behaviour = e.Button == MouseButtons.Left
                                ? _form.GetLeftClickBehaviour(KeyboardState.Ctrl, KeyboardState.Shift, KeyboardState.Alt)
                                : _form.GetRightClickBehaviour(KeyboardState.Ctrl, KeyboardState.Shift, KeyboardState.Alt);

            var ray = vp.CastRayFromScreen(e.X, e.Y);
            var hits = Document.Map.WorldSpawn.GetAllNodesIntersectingWith(ray).OfType<Solid>();
            var clickedFace = hits.SelectMany(f => f.Faces)
                .Select(x => new {Item = x, Intersection = x.GetIntersectionPoint(ray)})
                .Where(x => x.Intersection != null)
                .OrderBy(x => (x.Intersection - ray.Start).VectorMagnitude())
                .Select(x => x.Item)
                .FirstOrDefault();

            if (clickedFace == null) return;

            var faces = new List<Face>();
            if (KeyboardState.Shift) faces.AddRange(clickedFace.Parent.Faces);
            else faces.Add(clickedFace);

            var firstSelected = Document.Selection.GetSelectedFaces().FirstOrDefault();
            var firstClicked = faces.FirstOrDefault(face => !String.IsNullOrWhiteSpace(face.Texture.Name));

            var ac = new ActionCollection();

            var select = new ChangeFaceSelection(
                KeyboardState.Ctrl ? faces.Where(x => !x.IsSelected) : faces,
                KeyboardState.Ctrl ? faces.Where(x => x.IsSelected) : Document.Selection.GetSelectedFaces().Where(x => !faces.Contains(x)));

            Action lift = () =>
            {
                if (firstClicked == null) return;
                var itemToSelect = Document.TextureCollection.GetItem(firstClicked.Texture.Name)
                                   ?? new TextureItem(null, firstClicked.Texture.Name, TextureFlags.Missing, 64, 64);
                Mediator.Publish(EditorMediator.TextureSelected, itemToSelect);
            };

            switch (behaviour)
            {
                case SelectBehaviour.Select:
                    ac.Add(select);
                    break;
                case SelectBehaviour.LiftSelect:
                    lift();
                    ac.Add(select);
                    break;
                case SelectBehaviour.Lift:
                    lift();
                    break;
                case SelectBehaviour.Apply:
                case SelectBehaviour.ApplyWithValues:
                    var item = _form.GetFirstSelectedTexture();
                    if (item != null)
                    {
                        var texture = item.GetTexture();
                        ac.Add(new EditFace(faces, (document, face) =>
                                                        {
                                                            face.Texture.Name = item.Name;
                                                            face.Texture.Texture = texture;
                                                            if (behaviour == SelectBehaviour.ApplyWithValues && firstSelected != null)
                                                            {
                                                                // Calculates the texture coordinates
                                                                face.AlignTextureWithFace(firstSelected);
                                                            }
                                                            else if (behaviour == SelectBehaviour.ApplyWithValues)
                                                            {
                                                                face.Texture.XScale = _form.CurrentProperties.XScale;
                                                                face.Texture.YScale = _form.CurrentProperties.YScale;
                                                                face.Texture.XShift = _form.CurrentProperties.XShift;
                                                                face.Texture.YShift = _form.CurrentProperties.YShift;
                                                                face.SetTextureRotation(_form.CurrentProperties.Rotation);
                                                            }
                                                            else
                                                            {
                                                                face.CalculateTextureCoordinates(true);
                                                            }
                                                        }, true));
                    }
                    break;
                case SelectBehaviour.AlignToView:
                    var right = vp.Camera.GetRight();
                    var up = vp.Camera.GetUp();
                    var loc = vp.Camera.Location;
                    var point = new Coordinate((decimal)loc.X, (decimal)loc.Y, (decimal)loc.Z);
                    var uaxis = new Coordinate((decimal) right.X, (decimal) right.Y, (decimal) right.Z);
                    var vaxis = new Coordinate((decimal) up.X, (decimal) up.Y, (decimal) up.Z);
                    ac.Add(new EditFace(faces, (document, face) =>
                                                    {
                                                        face.Texture.XScale = 1;
                                                        face.Texture.YScale = 1;
                                                        face.Texture.UAxis = uaxis;
                                                        face.Texture.VAxis = vaxis;
                                                        face.Texture.XShift = face.Texture.UAxis.Dot(point);
                                                        face.Texture.YShift = face.Texture.VAxis.Dot(point);
                                                        face.Texture.Rotation = 0;
                                                        face.CalculateTextureCoordinates(true);
                                                    }, false));
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            if (!ac.IsEmpty())
            {
                Document.PerformAction("Texture selection", ac);
            }
        }