[2768] | 1 | using System;
|
---|
| 2 | using System.Collections;
|
---|
| 3 | using System.Collections.Generic;
|
---|
[4068] | 4 | using System.Diagnostics;
|
---|
| 5 | namespace Netron.Diagramming.Core {
|
---|
| 6 | /// <summary>
|
---|
| 7 | /// Send forward tool which moves the selection up in the z-order.
|
---|
| 8 | /// </summary>
|
---|
| 9 | class SendForwardsTool : AbstractTool {
|
---|
| 10 |
|
---|
| 11 | #region Fields
|
---|
| 12 |
|
---|
| 13 | #endregion
|
---|
| 14 |
|
---|
| 15 | #region Constructor
|
---|
[2768] | 16 | /// <summary>
|
---|
[4068] | 17 | /// Initializes a new instance of the <see cref="T:SendBackwards"/> class.
|
---|
[2768] | 18 | /// </summary>
|
---|
[4068] | 19 | /// <param name="name">The name of the tool.</param>
|
---|
| 20 | public SendForwardsTool(string name)
|
---|
| 21 | : base(name) {
|
---|
| 22 | }
|
---|
| 23 | #endregion
|
---|
[2768] | 24 |
|
---|
[4068] | 25 | #region Methods
|
---|
[2768] | 26 |
|
---|
[4068] | 27 | /// <summary>
|
---|
| 28 | /// Called when the tool is activated.
|
---|
| 29 | /// </summary>
|
---|
| 30 | protected override void OnActivateTool() {
|
---|
| 31 | /*
|
---|
| 32 | * A lot of stuff is calculated here for something which might seem at first sight
|
---|
| 33 | * a really simple action. In general the calculations will be short since a user does not
|
---|
| 34 | * usually shift many shapes at the same time and the overlap with the selection
|
---|
| 35 | * is small.
|
---|
| 36 | */
|
---|
| 37 | if (this.Controller.Model.Selection.SelectedItems != null && this.Controller.Model.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(this.Controller.Model.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 this.Controller.Model.Selection.FlattenedSelectionItems) {
|
---|
| 49 | //the addition will automatically put the item in increasing order
|
---|
| 50 | list.Add(this.Controller.Model.Paintables.IndexOf(entity), entity);
|
---|
[2768] | 51 | }
|
---|
[4068] | 52 | //if the highest z-value is the last one in the paintables we cannot shift anything, so we quit
|
---|
| 53 | if (list.Keys[list.Count - 1] == this.Controller.Model.Paintables.Count - 1) return;
|
---|
[2768] | 54 |
|
---|
[4068] | 55 | /*Send them forwards but make sure it's a visible effect!
|
---|
| 56 | It's not enough to move it only once since the shape(s) above might be of
|
---|
| 57 | high z-order degree, so we have to find which is the first shape overlapping with
|
---|
| 58 | the selection and take as many steps as it takes to surpass it.
|
---|
| 59 | If there is no overlap we'll shift the z-order with just one unit.
|
---|
| 60 | */
|
---|
| 61 | int delta = 1;
|
---|
| 62 | int lowestZInSelection = list.Keys[0];
|
---|
| 63 | bool found = false;
|
---|
| 64 | //we can speed up the loop by noticing that the next shape in the z-stack is necessarily
|
---|
| 65 | //above the first one of the selection
|
---|
| 66 | for (int m = lowestZInSelection + 1; m < this.Controller.Model.Paintables.Count && !found; m++) {
|
---|
| 67 | //the overlap has to be with an entity, not from the selection
|
---|
| 68 | if (list.ContainsValue(this.Controller.Model.Paintables[m])) continue;
|
---|
| 69 | for (int s = 0; s < list.Count; s++) {
|
---|
| 70 | //if there is an overlap we found the required index
|
---|
| 71 | if (this.Controller.Model.Paintables[m].Rectangle.IntersectsWith(list.Values[s].Rectangle)) {
|
---|
| 72 | //an additional complication here; if the found shape is part of a group we have
|
---|
| 73 | //to take the upper z-value of the group...
|
---|
| 74 | if (this.Controller.Model.Paintables[m].Group != null) {
|
---|
| 75 | int max = -1;
|
---|
| 76 | CollectionBase<IDiagramEntity> leafs = new CollectionBase<IDiagramEntity>();
|
---|
| 77 | Utils.TraverseCollect(this.Controller.Model.Paintables[m].Group, ref leafs);
|
---|
| 78 | foreach (IDiagramEntity groupMember in leafs) {
|
---|
| 79 | max = Math.Max(max, this.Controller.Model.Paintables.IndexOf(groupMember));
|
---|
[2768] | 80 | }
|
---|
[4068] | 81 | //take the found z-value of the group rather than the one of the group-child
|
---|
| 82 | m = max;
|
---|
| 83 | }
|
---|
| 84 | delta = m - lowestZInSelection;
|
---|
| 85 | found = true;
|
---|
| 86 | break;
|
---|
| 87 | }
|
---|
[2768] | 88 |
|
---|
[4068] | 89 | }
|
---|
[2768] | 90 | }
|
---|
| 91 | #endregion
|
---|
[4068] | 92 | Debug.Assert(delta >= 1, "The shift cannot be less than one since we checked previous situations earlier.");
|
---|
| 93 | for (int k = 0; k < list.Count; k++) {
|
---|
| 94 | this.Controller.Model.SendForwards(list.Values[k], delta);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | DeactivateTool();
|
---|
| 98 | }
|
---|
[2768] | 99 |
|
---|
[4068] | 100 | #endregion
|
---|
[2768] | 101 |
|
---|
| 102 |
|
---|
[4068] | 103 | }
|
---|
| 104 |
|
---|
[2768] | 105 | }
|
---|