private T CreateButton <T>(ref ButtonRect btnRect, int tabIndex, string text, MouseEventHandler clickFunc, int shapeIndex = -1) where T : Control, new() { Control button = new T() { Location = new Point(btnRect.x, btnRect.y), Size = new Size(btnRect.width, btnRect.height), TabIndex = tabIndex, Text = text, }; // Only set type info when button is a shaoe button if (typeof(T) == typeof(ShapeButton)) { ((ShapeButton)button).Text = ShapeTypeHelper.GetShapeName((ShapeType)shapeIndex); ((ShapeButton)button).Checked = (Value == (ShapeType)shapeIndex); ((ShapeButton)button).Type = (ShapeType)shapeIndex; } button.MouseClick += clickFunc; Controls.Add(button); btnRect.x += btnRect.width + btnRect.xSpacing; return((T)button); }
/// <summary> /// Creates a shape with the data, adds it to the layer, and returns it. /// </summary> /// <param name="type"></param> /// <returns></returns> public Shape AddNewShape(Point location, Size size, Color color, ShapeType type, int index = -1, bool stretchTriangle = true) { int x = location.X, y = location.Y, w = size.Width, h = size.Height; // Move to center of cursor, while still snapping to grid x = Grid.SnapToGrid(x - (w / 2)); y = Grid.SnapToGrid(y - (h / 2)); Shape shape = ShapeTypeHelper.CreateNewShape( x, y, w, h, color, type, stretchTriangle ); // Add to list and return if (index == -1 || index >= shapes.Count) { Add(shape); } else { shapes.Insert(index, shape); } return(shape); }