1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Windows.Forms;
|
---|
6 | using System.Collections.Generic;
|
---|
7 | namespace Netron.Diagramming.Core
|
---|
8 | {
|
---|
9 | /// <summary>
|
---|
10 | /// Group tool
|
---|
11 | /// </summary>
|
---|
12 | class SendBackwardsTool : AbstractTool
|
---|
13 | {
|
---|
14 |
|
---|
15 | #region Fields
|
---|
16 |
|
---|
17 | #endregion
|
---|
18 |
|
---|
19 | #region Constructor
|
---|
20 | /// <summary>
|
---|
21 | /// Initializes a new instance of the <see cref="T:SendBackwards"/> class.
|
---|
22 | /// </summary>
|
---|
23 | /// <param name="name">The name of the tool.</param>
|
---|
24 | public SendBackwardsTool(string name)
|
---|
25 | : base(name)
|
---|
26 | {
|
---|
27 | }
|
---|
28 | #endregion
|
---|
29 |
|
---|
30 | #region Methods
|
---|
31 |
|
---|
32 | /// <summary>
|
---|
33 | /// Called when the tool is activated.
|
---|
34 | /// </summary>
|
---|
35 | protected override void OnActivateTool()
|
---|
36 | {
|
---|
37 | if(Selection.SelectedItems!=null && Selection.SelectedItems.Count>0)
|
---|
38 | {
|
---|
39 | /*
|
---|
40 | * They should give me a Nobel prize for so much thinking early in the morning...
|
---|
41 | */
|
---|
42 | #region Preparation of the ordering
|
---|
43 | Debug.Assert(Selection.SelectedItems[0] != null, "A selection cannot contain a 'null' entity.");
|
---|
44 | //the items have to be moved in the order of the Paintables; the SortedList automatically orders things for us.
|
---|
45 | SortedList<int, IDiagramEntity> list = new SortedList<int, IDiagramEntity>();
|
---|
46 | //We fetch a flattened selection, which means that if there is a group the constituents will be
|
---|
47 | //returned rather than the group itself.
|
---|
48 | foreach (IDiagramEntity entity in Selection.FlattenedSelectionItems)
|
---|
49 | {
|
---|
50 | //the addition will automatically put the item in increasing order
|
---|
51 | list.Add(this.Controller.Model.Paintables.IndexOf(entity), entity);
|
---|
52 | }
|
---|
53 | //if the lowest z-value is the first one in the paintables we cannot shift anything, so we quit
|
---|
54 | if (list.Keys[0] == 0) return;
|
---|
55 |
|
---|
56 | /*Send them forwards but make sure it's a visible effect!
|
---|
57 | It's not enough to move it only once since the shape(s) above might be of
|
---|
58 | high z-order degree, so we have to find which is the first shape overlapping with
|
---|
59 | the selection and take as many steps as it takes to surpass it.
|
---|
60 | If there is no overlap we'll shift the z-order with just one unit.
|
---|
61 | */
|
---|
62 | int delta = 1;
|
---|
63 | int lowestZInSelection = list.Keys[0];
|
---|
64 | bool found = false;
|
---|
65 | //we can speed up the loop by noticing that the previous shape in the z-stack is necessarily
|
---|
66 | //below the first one of the selection
|
---|
67 | for (int m = lowestZInSelection-1; m>=0 && !found; m--)
|
---|
68 | {
|
---|
69 | //the overlap has to be with an entity, not from the selection
|
---|
70 | if (list.ContainsValue(this.Controller.Model.Paintables[m])) continue;
|
---|
71 | for (int s = 0; s < list.Count; s++)
|
---|
72 | {
|
---|
73 | //if there is an overlap we found the required index
|
---|
74 | if (this.Controller.Model.Paintables[m].Rectangle.IntersectsWith(list.Values[s].Rectangle))
|
---|
75 | {
|
---|
76 | //an additional complication here; if the found shape is part of a group we have
|
---|
77 | //to take the upper z-value of the group...
|
---|
78 | if (this.Controller.Model.Paintables[m].Group != null)
|
---|
79 | {
|
---|
80 | int min = int.MaxValue;
|
---|
81 | CollectionBase<IDiagramEntity> leafs = new CollectionBase<IDiagramEntity>();
|
---|
82 | Utils.TraverseCollect(this.Controller.Model.Paintables[m].Group, ref leafs);
|
---|
83 | foreach (IDiagramEntity groupMember in leafs)
|
---|
84 | {
|
---|
85 | min = Math.Min(min, this.Controller.Model.Paintables.IndexOf(groupMember));
|
---|
86 | }
|
---|
87 | //take the found z-value of the group rather than the one of the group-child
|
---|
88 | m = min;
|
---|
89 | }
|
---|
90 | delta = lowestZInSelection - m ;
|
---|
91 | found = true;
|
---|
92 | break;
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|
96 | }
|
---|
97 | #endregion
|
---|
98 | Debug.Assert(delta >= 1, "The shift cannot be less than one since we checked previous situations earlier.");
|
---|
99 | for (int k = 0; k < list.Count; k++)
|
---|
100 | {
|
---|
101 | this.Controller.Model.SendBackwards(list.Values[k], delta);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | DeactivateTool();
|
---|
105 | }
|
---|
106 |
|
---|
107 | #endregion
|
---|
108 |
|
---|
109 |
|
---|
110 | }
|
---|
111 |
|
---|
112 | }
|
---|