[2768] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[4068] | 3 | using System.ComponentModel;
|
---|
[2768] | 4 | using System.Drawing;
|
---|
[4068] | 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
|
---|
[2768] | 14 |
|
---|
[4068] | 15 | private const double speed = 0.1D;
|
---|
[2768] | 16 |
|
---|
[4068] | 17 | private Random rnd;
|
---|
| 18 | double vectorx;
|
---|
| 19 | double vectory;
|
---|
[2768] | 20 |
|
---|
[4068] | 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
|
---|
[2768] | 29 |
|
---|
[4068] | 30 | #region Constructor
|
---|
| 31 | ///<summary>
|
---|
| 32 | ///Default constructor
|
---|
| 33 | ///</summary>
|
---|
| 34 | public RandomLayout(IController controller)
|
---|
| 35 | : base("Random layout", controller) {
|
---|
| 36 | }
|
---|
| 37 | #endregion
|
---|
[2768] | 38 |
|
---|
[4068] | 39 | #region Methods
|
---|
[2768] | 40 |
|
---|
| 41 |
|
---|
| 42 |
|
---|
[4068] | 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);
|
---|
[2768] | 50 |
|
---|
| 51 |
|
---|
[4068] | 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) {
|
---|
[2768] | 69 |
|
---|
[4068] | 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));
|
---|
[2768] | 74 | }
|
---|
[4068] | 75 | }
|
---|
[2768] | 76 |
|
---|
[4068] | 77 | this.time = time;
|
---|
| 78 | worker = new BackgroundWorker();
|
---|
| 79 | worker.DoWork += new DoWorkEventHandler(worker_DoWork);
|
---|
| 80 | worker.RunWorkerAsync(time);
|
---|
[2768] | 81 |
|
---|
[4068] | 82 | }
|
---|
[2768] | 83 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 89 |
|
---|
[4068] | 90 | }
|
---|
[2768] | 91 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 108 | }
|
---|
[4068] | 109 | }
|
---|
[2768] | 110 |
|
---|
| 111 |
|
---|
[4068] | 112 |
|
---|
| 113 | #endregion
|
---|
| 114 | }
|
---|
[2768] | 115 | }
|
---|