Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Layout/RandomLayout.cs
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Layout/RandomLayout.cs
r2861 r4068 1 1 using System; 2 2 using System.Collections.Generic; 3 using System. Text;3 using System.ComponentModel; 4 4 using System.Drawing; 5 using System.Threading; 6 using System.ComponentModel; 7 namespace Netron.Diagramming.Core 8 { 9 /// <summary> 10 /// This layout places nodes inside the visible area at a random location. The animation generated by this layout 11 /// allows you to see how nodes move from one location to another. Although it seems this layout does not 12 /// generate a clean organization is does help to perceive information in different perspectives and can be at times 13 /// really useful. 14 /// </summary> 15 class RandomLayout : LayoutBase 16 { 17 #region Fields 18 19 private const double speed = 0.1D; 5 namespace Netron.Diagramming.Core { 6 /// <summary> 7 /// This layout places nodes inside the visible area at a random location. The animation generated by this layout 8 /// allows you to see how nodes move from one location to another. Although it seems this layout does not 9 /// generate a clean organization is does help to perceive information in different perspectives and can be at times 10 /// really useful. 11 /// </summary> 12 class RandomLayout : LayoutBase { 13 #region Fields 20 14 21 private Random rnd; 22 double vectorx; 23 double vectory; 15 private const double speed = 0.1D; 24 16 25 CollectionBase<IDiagramEntity> entities; 26 Dictionary<IDiagramEntity, SpeedVector> speeds; 27 int width; 28 int height; 29 int x, y; 30 int time; 31 BackgroundWorker worker; 32 #endregion 17 private Random rnd; 18 double vectorx; 19 double vectory; 33 20 34 #region Constructor 35 ///<summary> 36 ///Default constructor 37 ///</summary> 38 public RandomLayout(IController controller) 39 : base("Random layout", controller) 40 { 41 } 42 #endregion 21 CollectionBase<IDiagramEntity> entities; 22 Dictionary<IDiagramEntity, SpeedVector> speeds; 23 int width; 24 int height; 25 int x, y; 26 int time; 27 BackgroundWorker worker; 28 #endregion 43 29 44 #region Methods 30 #region Constructor 31 ///<summary> 32 ///Default constructor 33 ///</summary> 34 public RandomLayout(IController controller) 35 : base("Random layout", controller) { 36 } 37 #endregion 45 38 46 47 48 /// <summary> 49 /// Runs this instance. 50 /// </summary> 51 public override void Run() 52 { 53 width = this.Bounds.Width; 54 height = this.Bounds.Height; 55 Run(DefaultRunSpan); 39 #region Methods 56 40 57 41 42 43 /// <summary> 44 /// Runs this instance. 45 /// </summary> 46 public override void Run() { 47 width = this.Bounds.Width; 48 height = this.Bounds.Height; 49 Run(DefaultRunSpan); 50 51 52 } 53 /// <summary> 54 /// Stops this instance. 55 /// </summary> 56 public override void Stop() { 57 if (worker != null && worker.IsBusy) 58 worker.CancelAsync(); 59 } 60 /// <summary> 61 /// Runs the layout for a specified time. 62 /// </summary> 63 /// <param name="time">The time.</param> 64 public override void Run(int time) { 65 rnd = new Random(); 66 entities = this.Model.CurrentPage.DefaultLayer.Entities; 67 speeds = new Dictionary<IDiagramEntity, SpeedVector>(); 68 foreach (IDiagramEntity entity in entities) { 69 70 if (entity is IShape) { 71 vectorx = -20 + 40 * rnd.NextDouble(); 72 vectory = -20 + 40 * rnd.NextDouble(); 73 speeds.Add(entity, new SpeedVector(vectorx, vectory)); 58 74 } 59 /// <summary> 60 /// Stops this instance. 61 /// </summary> 62 public override void Stop() 63 { 64 if (worker != null && worker.IsBusy) 65 worker.CancelAsync(); 75 } 76 77 this.time = time; 78 worker = new BackgroundWorker(); 79 worker.DoWork += new DoWorkEventHandler(worker_DoWork); 80 worker.RunWorkerAsync(time); 81 82 } 83 84 void worker_DoWork(object sender, DoWorkEventArgs e) { 85 DateTime start = DateTime.Now; 86 while (DateTime.Now < start.AddMilliseconds((int)e.Argument)) { 87 RunStep(); 88 } 89 90 } 91 92 /// <summary> 93 /// Runs a single step. 94 /// </summary> 95 private void RunStep() { 96 lock (entities) 97 lock (speeds) { 98 foreach (IDiagramEntity entity in entities) { 99 100 if (entity is IShape) { 101 102 x = Convert.ToInt32(speed * speeds[entity].X); 103 y = Convert.ToInt32(speed * speeds[entity].Y); 104 if (entity.Rectangle.X + x < width - entity.Rectangle.Width - 10 && entity.Rectangle.X + x > 10 && entity.Rectangle.Y + y < height - entity.Rectangle.Height - 10 && entity.Rectangle.Y + y > 10) 105 entity.MoveBy(new Point(x, y)); 106 } 107 } 66 108 } 67 /// <summary> 68 /// Runs the layout for a specified time. 69 /// </summary> 70 /// <param name="time">The time.</param> 71 public override void Run(int time) 72 { 73 rnd = new Random(); 74 entities = this.Model.CurrentPage.DefaultLayer.Entities; 75 speeds = new Dictionary<IDiagramEntity, SpeedVector>(); 76 foreach (IDiagramEntity entity in entities) 77 { 109 } 78 110 79 if (entity is IShape)80 {81 vectorx = -20 + 40 * rnd.NextDouble();82 vectory = -20 + 40 * rnd.NextDouble();83 speeds.Add(entity, new SpeedVector(vectorx, vectory));84 }85 }86 111 87 this.time = time;88 worker = new BackgroundWorker();89 worker.DoWork += new DoWorkEventHandler(worker_DoWork);90 worker.RunWorkerAsync(time);91 112 92 } 93 94 void worker_DoWork(object sender, DoWorkEventArgs e) 95 { 96 DateTime start = DateTime.Now; 97 while (DateTime.Now < start.AddMilliseconds((int)e.Argument)) 98 { 99 RunStep(); 100 } 101 102 } 103 104 /// <summary> 105 /// Runs a single step. 106 /// </summary> 107 private void RunStep() 108 { 109 lock (entities) 110 lock (speeds) 111 { 112 foreach (IDiagramEntity entity in entities) 113 { 114 115 if (entity is IShape) 116 { 117 118 x = Convert.ToInt32(speed * speeds[entity].X); 119 y = Convert.ToInt32(speed * speeds[entity].Y); 120 if (entity.Rectangle.X + x < width - entity.Rectangle.Width - 10 && entity.Rectangle.X + x > 10 && entity.Rectangle.Y + y < height - entity.Rectangle.Height - 10 && entity.Rectangle.Y + y > 10) 121 entity.MoveBy(new Point(x, y)); 122 } 123 } 124 } 125 } 126 127 128 129 #endregion 130 } 113 #endregion 114 } 131 115 }
Note: See TracChangeset
for help on using the changeset viewer.