コード例 #1
0
ファイル: RemoveCommand.cs プロジェクト: ymnsh/lab6
        public override void Execute(params string[] parameters)
        {
            try
            {
                var uniqueIndexes = new List <string>();
                var shapeLocators = new List <ShapeLocator>();

                if (parameters.Length < 1)
                {
                    throw new ArgumentException("Отсуствует аргумент");
                }

                foreach (var parameter in parameters)
                {
                    if (uniqueIndexes.IndexOf(parameter) == -1)
                    {
                        uniqueIndexes.Add(parameter);
                        shapeLocators.Add(ShapeLocator.Parse(parameter, picture));
                    }
                }

                foreach (var shapeLocator in shapeLocators)
                {
                    var shape  = shapeLocator.Shape;
                    var parent = shapeLocator.Parent;

                    if (parent != null)
                    {
                        parent.Shapes.Remove(shape);

                        if (parent.Shapes.Count < 2)
                        {
                            var grandParent = shapeLocator.GrandParent;
                            if (grandParent != null)
                            {
                                grandParent.Shapes.Add(parent.Shapes[0]);
                                grandParent.Shapes.Remove(parent);
                            }
                            else
                            {
                                picture.Add(parent.Shapes[0]);
                                picture.Remove(parent);
                            }
                        }
                    }
                    else
                    {
                        SelectionContainer.GetInstance().OnMainRemove(shape);
                        picture.Remove(shape);
                    }
                }

                UpdateHistory();
                SelectionContainer.GetInstance().OnMainRemove(picture);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
        }
コード例 #2
0
        public void Ungroup(string[] indexes)
        {
            var shape = ShapeLocator.Parse(indexes[0], this);

            SelectionContainer.GetInstance().OnUngroup(shape);

            if (shape.Shape is CompoundShape)
            {
                var compoundShape = shape.Shape as CompoundShape;
                if (shape.Parent != null)
                {
                    shape.Parent.Shapes.Remove(shape.Shape);
                    foreach (var currentShape in compoundShape.Shapes)
                    {
                        shape.Parent.Shapes.Add(currentShape);
                    }
                }
                else
                {
                    Remove(shape.Shape);
                    foreach (var currentShape in compoundShape.Shapes)
                    {
                        Add(currentShape);
                    }
                }
            }
            else
            {
                throw new ArgumentException($"Фигура с указанным индексом '{indexes[0]}' не составная");
            }
        }
コード例 #3
0
ファイル: BaseEdit.cs プロジェクト: ymnsh/lab6
        protected IEnumerable <IShape> GetShapes(string[] parameters, int shapeIndex)
        {
            if (parameters.Length < shapeIndex)
            {
                var shapes = SelectionContainer.GetInstance().Shapes;
                if (shapes.Count == 0)
                {
                    throw new ArgumentException("Для команды необходимы аргументы либо выбор с помощью " +
                                                "команды 'select'");
                }

                return(shapes);
            }

            var shape = ShapeLocator.Parse(parameters[shapeIndex - 1], picture).Shape;

            return(new List <IShape> {
                shape
            });
        }
コード例 #4
0
ファイル: ShapeLocator.cs プロジェクト: ymnsh/lab6
        public static ShapeLocator Parse(string identifier, Picture picture)
        {
            var shapeLocator     = new ShapeLocator();
            var identifierDecomp = identifier.Split(':');
            int index;

            if (identifierDecomp.Length > 0 && int.TryParse(identifierDecomp[0], out index))
            {
                try
                {
                    shapeLocator.Shape = picture.ShapeAt(index);
                }
                catch (ArgumentOutOfRangeException)
                {
                    throw new ArgumentException("Фигуры с таким индетефикатором " + identifier + " не существует");
                }
            }
            else
            {
                throw new ArgumentException("Формат идеентификатора '" + identifier + "' не верен");
            }

            if (identifierDecomp.Length > 1)
            {
                int[] underPosition = new int[identifierDecomp.Length - 1];
                for (int i = 0; i < identifierDecomp.Length - 1; i++)
                {
                    if (!int.TryParse(identifierDecomp[i + 1], out underPosition[i]))
                    {
                        throw new ArgumentException("Формат идеентификатора '" + identifier + "' не верен");
                    }
                }
                Search(shapeLocator, underPosition);
            }

            return(shapeLocator);
        }
コード例 #5
0
ファイル: ShapeLocator.cs プロジェクト: ymnsh/lab6
 private static void Search(ShapeLocator shapeLocator, int[] index)
 {
     if (shapeLocator.Shape is CompoundShape)
     {
         var compoundShape = shapeLocator.Shape as CompoundShape;
         if (index.Length != 1)
         {
             if (index[0] >= 0 && index[0] < compoundShape.Shapes.Count)
             {
                 var underPosition = new int[index.Length - 1];
                 Array.Copy(index, 1, underPosition, 0, index.Length - 1);
                 if (index.Length < 3)
                 {
                     shapeLocator.GrandParent = shapeLocator.Parent;
                     shapeLocator.Parent      = compoundShape;
                 }
                 shapeLocator.Shape = compoundShape.Shapes[index[0]];
                 Search(shapeLocator, underPosition);
             }
             else
             {
                 throw new ArgumentException("Элемент с указанным индексом не существует");
             }
         }
         else
         {
             shapeLocator.GrandParent = shapeLocator.Parent;
             shapeLocator.Parent      = compoundShape;
             shapeLocator.Shape       = compoundShape.Shapes[index[0]];
         }
     }
     else
     {
         throw new ArgumentException("Элемент с указанным индексом не существует");
     }
 }
コード例 #6
0
        public void Group(string[] indexes)
        {
            var positions = new List <string>();

            foreach (var index in indexes)
            {
                if (positions.IndexOf(index) == -1)
                {
                    bool correct = true;
                    foreach (var position in positions)
                    {
                        if (index.Length > position.Length && index.StartsWith(position))
                        {
                            correct = false;
                            break;
                        }
                        else if (index.Length < position.Length && position.StartsWith(index))
                        {
                            positions.Remove(position);
                        }
                    }
                    if (correct)
                    {
                        positions.Add(index);
                    }
                }
            }

            if (positions.Count < 2)
            {
                throw new ArgumentException("Для группировки необходимы минимум 2 фигуры");
            }

            lock (lockObject)
            {
                int j      = 0;
                var shapes = new ShapeLocator[positions.Count];
                foreach (var position in positions)
                {
                    shapes[j] = ShapeLocator.Parse(position, this);

                    j++;
                }

                CompoundShape compoundShape = new CompoundShape();
                foreach (var shape in shapes)
                {
                    compoundShape.Shapes.Add(shape.Shape);
                    if (shape.Parent != null)
                    {
                        shape.Parent.Shapes.Remove(shape.Shape);
                    }
                    else
                    {
                        Remove(shape.Shape);
                    }
                }

                foreach (var shape in shapes)
                {
                    if (shape.Parent != null && shape.Parent.Shapes.Count < 2)
                    {
                        if (shape.GrandParent != null)
                        {
                            if (shape.Parent.Shapes.Count == 1)
                            {
                                shape.GrandParent.Shapes.Add(shape.Parent.Shapes[0]);
                            }
                            shape.GrandParent.Shapes.Remove(shape.Parent);
                        }
                        else
                        {
                            if (shape.Parent.Shapes.Count == 1)
                            {
                                Add(shape.Parent.Shapes[0]);
                            }
                            Remove(shape.Parent);
                        }
                    }
                }
                Add(compoundShape);
            }
        }