[635] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Drawing;
|
---|
| 4 |
|
---|
[1964] | 5 | namespace HeuristicLab.Visualization.Drawing {
|
---|
[635] | 6 | public class CompositeShape : IShape {
|
---|
[1240] | 7 | private IShape parent;
|
---|
[1559] | 8 | private bool showChildShapes = true;
|
---|
[1240] | 9 |
|
---|
[635] | 10 | protected readonly List<IShape> shapes = new List<IShape>();
|
---|
| 11 | protected RectangleD boundingBox = RectangleD.Empty;
|
---|
| 12 |
|
---|
[1559] | 13 |
|
---|
| 14 |
|
---|
[1240] | 15 | public virtual void Draw(Graphics graphics) {
|
---|
[1559] | 16 | if(!showChildShapes)
|
---|
| 17 | return;
|
---|
[635] | 18 | foreach (IShape shape in shapes) {
|
---|
[1240] | 19 | shape.Draw(graphics);
|
---|
[635] | 20 | }
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | public RectangleD BoundingBox {
|
---|
| 24 | get {
|
---|
| 25 | if (shapes.Count == 0) {
|
---|
| 26 | throw new InvalidOperationException("No shapes, no bounding box.");
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | return boundingBox;
|
---|
| 30 | }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[1240] | 33 | public RectangleD ClippingArea {
|
---|
| 34 | get { return Parent.ClippingArea; }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public Rectangle Viewport {
|
---|
| 38 | get { return Parent.Viewport; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public IShape Parent {
|
---|
| 42 | get { return parent; }
|
---|
| 43 | set { parent = value; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[1559] | 46 | public bool ShowChildShapes {
|
---|
| 47 | get { return showChildShapes; }
|
---|
| 48 | set { showChildShapes = value; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[987] | 51 | public void ClearShapes() {
|
---|
| 52 | shapes.Clear();
|
---|
| 53 | boundingBox = RectangleD.Empty;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[1559] | 56 | public IShape GetShape(int index) {
|
---|
| 57 | return shapes[index];
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[1883] | 60 |
|
---|
| 61 | /// <summary>
|
---|
| 62 | /// Adds a shape to the container
|
---|
| 63 | /// </summary>
|
---|
| 64 | /// <param name="shape"> the Shape to add</param>
|
---|
[635] | 65 | public void AddShape(IShape shape) {
|
---|
[1240] | 66 | shape.Parent = this;
|
---|
| 67 |
|
---|
[635] | 68 | if (shapes.Count == 0) {
|
---|
| 69 | boundingBox = shape.BoundingBox;
|
---|
| 70 | } else {
|
---|
| 71 | boundingBox = new RectangleD(Math.Min(boundingBox.X1, shape.BoundingBox.X1),
|
---|
| 72 | Math.Min(boundingBox.Y1, shape.BoundingBox.Y1),
|
---|
| 73 | Math.Max(boundingBox.X2, shape.BoundingBox.X2),
|
---|
| 74 | Math.Max(boundingBox.Y2, shape.BoundingBox.Y2));
|
---|
| 75 | }
|
---|
| 76 | shapes.Add(shape);
|
---|
| 77 | }
|
---|
[1883] | 78 |
|
---|
| 79 | /// <summary>
|
---|
| 80 | /// Recalculate the bounding box
|
---|
| 81 | /// </summary>
|
---|
| 82 | private void InitBoundingBox() {
|
---|
| 83 | if (shapes.Count > 0)
|
---|
| 84 | boundingBox = shapes[0].BoundingBox;
|
---|
| 85 | foreach (var shape in shapes) {
|
---|
| 86 | boundingBox = new RectangleD(Math.Min(boundingBox.X1, shape.BoundingBox.X1),
|
---|
[1964] | 87 | Math.Min(boundingBox.Y1, shape.BoundingBox.Y1),
|
---|
| 88 | Math.Max(boundingBox.X2, shape.BoundingBox.X2),
|
---|
| 89 | Math.Max(boundingBox.Y2, shape.BoundingBox.Y2));
|
---|
[1883] | 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /// <summary>
|
---|
| 94 | /// Removes a Shape from the container and reinitializes the bounding box
|
---|
| 95 | /// </summary>
|
---|
| 96 | /// <param name="shape">the Shape to remove</param>
|
---|
| 97 | public void RemoveShape(IShape shape) {
|
---|
| 98 | shapes.Remove(shape);
|
---|
| 99 | InitBoundingBox();
|
---|
| 100 | }
|
---|
[635] | 101 | }
|
---|
| 102 | } |
---|