SnapManager.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. using SmartCoalApplication.Base;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Globalization;
  7. using System.Windows.Forms;
  8. namespace SmartCoalApplication
  9. {
  10. public sealed class SnapManager
  11. {
  12. private Dictionary<SnapObstacle, SnapDescription> obstacles =
  13. new Dictionary<SnapObstacle, SnapDescription>();
  14. private const string isSnappedValueName = "IsSnapped";
  15. private const string leftValueName = "Left";
  16. private const string topValueName = "Top";
  17. private const string widthValueName = "Width";
  18. private const string heightValueName = "Height";
  19. private const string nullName = "";
  20. private const string snappedToValueName = "SnappedTo";
  21. private const string horizontalEdgeValueName = "HorizontalEdge";
  22. private const string verticalEdgeValueName = "VerticalEdge";
  23. private const string xOffsetValueName = "XOffset";
  24. private const string yOffsetValueName = "YOffset";
  25. public void SaveSnapObstacleData(ISimpleCollection<string, string> saveTo, SnapObstacle so)
  26. {
  27. string prefix = so.Name + ".";
  28. SnapDescription sd = this.obstacles[so];
  29. bool isSnappedValue = (sd != null);
  30. saveTo.Set(prefix + isSnappedValueName, isSnappedValue.ToString(CultureInfo.InvariantCulture));
  31. if (isSnappedValue)
  32. {
  33. saveTo.Set(prefix + snappedToValueName, sd.SnappedTo.Name);
  34. saveTo.Set(prefix + horizontalEdgeValueName, sd.HorizontalEdge.ToString());
  35. saveTo.Set(prefix + verticalEdgeValueName, sd.VerticalEdge.ToString());
  36. saveTo.Set(prefix + xOffsetValueName, sd.XOffset.ToString(CultureInfo.InvariantCulture));
  37. saveTo.Set(prefix + yOffsetValueName, sd.YOffset.ToString(CultureInfo.InvariantCulture));
  38. }
  39. saveTo.Set(prefix + leftValueName, so.Bounds.Left.ToString(CultureInfo.InvariantCulture));
  40. saveTo.Set(prefix + topValueName, so.Bounds.Top.ToString(CultureInfo.InvariantCulture));
  41. saveTo.Set(prefix + widthValueName, so.Bounds.Width.ToString(CultureInfo.InvariantCulture));
  42. saveTo.Set(prefix + heightValueName, so.Bounds.Height.ToString(CultureInfo.InvariantCulture));
  43. }
  44. public void LoadSnapObstacleData(ISimpleCollection<string, string> loadFrom, SnapObstacle so)
  45. {
  46. string prefix = so.Name + ".";
  47. SnapDescription sd;
  48. string isSnappedString = loadFrom.Get(prefix + isSnappedValueName);
  49. bool isSnapped = bool.Parse(isSnappedString);
  50. if (isSnapped)
  51. {
  52. string snappedToString = loadFrom.Get(prefix + snappedToValueName);
  53. SnapObstacle snappedTo = FindObstacle(snappedToString);
  54. string horizontalEdgeString = loadFrom.Get(prefix + horizontalEdgeValueName);
  55. HorizontalSnapEdge horizontalEdge = (HorizontalSnapEdge)Enum.Parse(typeof(HorizontalSnapEdge), horizontalEdgeString, true);
  56. string verticalEdgeString = loadFrom.Get(prefix + verticalEdgeValueName);
  57. VerticalSnapEdge verticalEdge = (VerticalSnapEdge)Enum.Parse(typeof(VerticalSnapEdge), verticalEdgeString, true);
  58. string xOffsetString = loadFrom.Get(prefix + xOffsetValueName);
  59. int xOffset = int.Parse(xOffsetString, CultureInfo.InvariantCulture);
  60. string yOffsetString = loadFrom.Get(prefix + yOffsetValueName);
  61. int yOffset = int.Parse(yOffsetString, CultureInfo.InvariantCulture);
  62. sd = new SnapDescription(snappedTo, horizontalEdge, verticalEdge, xOffset, yOffset);
  63. }
  64. else
  65. {
  66. sd = null;
  67. }
  68. this.obstacles[so] = sd;
  69. string leftString = loadFrom.Get(prefix + leftValueName);
  70. int left = int.Parse(leftString, CultureInfo.InvariantCulture);
  71. string topString = loadFrom.Get(prefix + topValueName);
  72. int top = int.Parse(topString, CultureInfo.InvariantCulture);
  73. string widthString = loadFrom.Get(prefix + widthValueName);
  74. int width = int.Parse(widthString, CultureInfo.InvariantCulture);
  75. string heightString = loadFrom.Get(prefix + heightValueName);
  76. int height = int.Parse(heightString, CultureInfo.InvariantCulture);
  77. Rectangle newBounds = new Rectangle(left, top, width, height);
  78. so.RequestBoundsChange(newBounds);
  79. if (sd != null)
  80. {
  81. ParkObstacle(so, sd);
  82. }
  83. }
  84. // Requires that all SnapObstacles are already placed in this.obstacles
  85. public void Save(ISimpleCollection<string, string> saveTo)
  86. {
  87. foreach (SnapObstacle obstacle in this.obstacles.Keys)
  88. {
  89. if (obstacle.EnableSave)
  90. {
  91. SaveSnapObstacleData(saveTo, obstacle);
  92. }
  93. }
  94. }
  95. public void Load(ISimpleCollection<string, string> loadFrom)
  96. {
  97. SnapObstacle[] newObstacles = new SnapObstacle[this.obstacles.Count];
  98. this.obstacles.Keys.CopyTo(newObstacles, 0);
  99. foreach (SnapObstacle obstacle in newObstacles)
  100. {
  101. if (obstacle.EnableSave)
  102. {
  103. LoadSnapObstacleData(loadFrom, obstacle);
  104. }
  105. }
  106. }
  107. public void ParkObstacle(ISnapObstacleHost obstacle, ISnapObstacleHost snappedTo, HorizontalSnapEdge hEdge, VerticalSnapEdge vEdge)
  108. {
  109. ParkObstacle(obstacle.SnapObstacle, snappedTo.SnapObstacle, hEdge, vEdge);
  110. }
  111. public void ParkObstacle(SnapObstacle obstacle, SnapObstacle snappedTo, HorizontalSnapEdge hEdge, VerticalSnapEdge vEdge)
  112. {
  113. SnapDescription sd = new SnapDescription(snappedTo, hEdge, vEdge, obstacle.SnapDistance, obstacle.SnapDistance);
  114. this.obstacles[obstacle] = sd;
  115. ParkObstacle(obstacle, sd);
  116. }
  117. public void ReparkObstacle(ISnapObstacleHost obstacle)
  118. {
  119. ReparkObstacle(obstacle.SnapObstacle);
  120. }
  121. public void ReparkObstacle(SnapObstacle obstacle)
  122. {
  123. if (this.obstacles.ContainsKey(obstacle))
  124. {
  125. SnapDescription sd = this.obstacles[obstacle];
  126. if (sd != null)
  127. {
  128. ParkObstacle(obstacle, sd);
  129. }
  130. }
  131. }
  132. public void AddSnapObstacle(ISnapObstacleHost snapObstacleHost)
  133. {
  134. AddSnapObstacle(snapObstacleHost.SnapObstacle);
  135. }
  136. public void AddSnapObstacle(SnapObstacle snapObstacle)
  137. {
  138. if (!this.obstacles.ContainsKey(snapObstacle))
  139. {
  140. this.obstacles.Add(snapObstacle, null);
  141. if (snapObstacle.StickyEdges)
  142. {
  143. snapObstacle.BoundsChanging += SnapObstacle_BoundsChanging;
  144. snapObstacle.BoundsChanged += SnapObstacle_BoundsChanged;
  145. }
  146. }
  147. }
  148. private void SnapObstacle_BoundsChanging(object sender, EventArgs<Rectangle> e)
  149. {
  150. }
  151. private void SnapObstacle_BoundsChanged(object sender, EventArgs<Rectangle> e)
  152. {
  153. SnapObstacle senderSO = (SnapObstacle)sender;
  154. Rectangle fromRect = e.Data;
  155. Rectangle toRect = senderSO.Bounds;
  156. UpdateDependentObstacles(senderSO, fromRect, toRect);
  157. }
  158. private void UpdateDependentObstacles(SnapObstacle senderSO, Rectangle fromRect, Rectangle toRect)
  159. {
  160. int leftDelta = toRect.Left - fromRect.Left;
  161. int topDelta = toRect.Top - fromRect.Top;
  162. int rightDelta = toRect.Right - fromRect.Right;
  163. int bottomDelta = toRect.Bottom - fromRect.Bottom;
  164. foreach (SnapObstacle obstacle in this.obstacles.Keys)
  165. {
  166. if (!object.ReferenceEquals(senderSO, obstacle))
  167. {
  168. SnapDescription sd = this.obstacles[obstacle];
  169. if (sd != null && object.ReferenceEquals(sd.SnappedTo, senderSO))
  170. {
  171. int deltaX;
  172. if (sd.VerticalEdge == VerticalSnapEdge.Right)
  173. {
  174. deltaX = rightDelta;
  175. }
  176. else
  177. {
  178. deltaX = leftDelta;
  179. }
  180. int deltaY;
  181. if (sd.HorizontalEdge == HorizontalSnapEdge.Bottom)
  182. {
  183. deltaY = bottomDelta;
  184. }
  185. else
  186. {
  187. deltaY = topDelta;
  188. }
  189. Rectangle oldBounds = obstacle.Bounds;
  190. Point newLocation1 = new Point(oldBounds.Left + deltaX, oldBounds.Top + deltaY);
  191. Point newLocation2 = AdjustNewLocation(obstacle, newLocation1, sd);
  192. Rectangle newBounds = new Rectangle(newLocation2, oldBounds.Size);
  193. obstacle.RequestBoundsChange(newBounds);
  194. // Recursively update anything snapped to this obstacle
  195. UpdateDependentObstacles(obstacle, oldBounds, newBounds);
  196. }
  197. }
  198. }
  199. }
  200. public void RemoveSnapObstacle(ISnapObstacleHost snapObstacleHost)
  201. {
  202. RemoveSnapObstacle(snapObstacleHost.SnapObstacle);
  203. }
  204. public void RemoveSnapObstacle(SnapObstacle snapObstacle)
  205. {
  206. if (this.obstacles.ContainsKey(snapObstacle))
  207. {
  208. this.obstacles.Remove(snapObstacle);
  209. if (snapObstacle.StickyEdges)
  210. {
  211. snapObstacle.BoundsChanging -= SnapObstacle_BoundsChanging;
  212. snapObstacle.BoundsChanged -= SnapObstacle_BoundsChanged;
  213. }
  214. }
  215. }
  216. public bool ContainsSnapObstacle(ISnapObstacleHost snapObstacleHost)
  217. {
  218. return ContainsSnapObstacle(snapObstacleHost.SnapObstacle);
  219. }
  220. public bool ContainsSnapObstacle(SnapObstacle snapObstacle)
  221. {
  222. return this.obstacles.ContainsKey(snapObstacle);
  223. }
  224. private static bool AreEdgesClose(int l1, int r1, int l2, int r2)
  225. {
  226. if (r1 < l2)
  227. {
  228. return false;
  229. }
  230. else if (r2 < l1)
  231. {
  232. return false;
  233. }
  234. else if (l1 <= l2 && l2 <= r1 && r1 <= r2)
  235. {
  236. return true;
  237. }
  238. else if (l2 <= l1 && l1 <= r2 && r2 <= r1)
  239. {
  240. return true;
  241. }
  242. else if (l1 <= l2 && r2 <= r1)
  243. {
  244. return true;
  245. }
  246. else if (l2 <= l1 && l1 <= r2)
  247. {
  248. return true;
  249. }
  250. throw new InvalidOperationException();
  251. }
  252. private SnapDescription DetermineNewSnapDescription(
  253. SnapObstacle avoider,
  254. Point newLocation,
  255. SnapObstacle avoidee,
  256. SnapDescription currentSnapDescription)
  257. {
  258. int ourSnapProximity;
  259. if (currentSnapDescription != null &&
  260. (currentSnapDescription.HorizontalEdge != HorizontalSnapEdge.Neither ||
  261. currentSnapDescription.VerticalEdge != VerticalSnapEdge.Neither))
  262. {
  263. // the avoider is already snapped to the avoidee -- make it more difficult to un-snap
  264. ourSnapProximity = avoidee.SnapProximity * 2;
  265. }
  266. else
  267. {
  268. ourSnapProximity = avoidee.SnapProximity;
  269. }
  270. Rectangle avoiderRect = avoider.Bounds;
  271. avoiderRect.Location = newLocation;
  272. Rectangle avoideeRect = avoidee.Bounds;
  273. // Are the vertical edges close enough for snapping?
  274. bool vertProximity = AreEdgesClose(avoiderRect.Top, avoiderRect.Bottom, avoideeRect.Top, avoideeRect.Bottom);
  275. // Are the horizontal edges close enough for snapping?
  276. bool horizProximity = AreEdgesClose(avoiderRect.Left, avoiderRect.Right, avoideeRect.Left, avoideeRect.Right);
  277. // Compute distances from pertinent edges
  278. // (e.g. if SnapRegion.Interior, figure out distance from avoider's right edge to avoidee's right edge,
  279. // if SnapRegion.Exterior, figure out distance from avoider's right edge to avoidee's left edge)
  280. int leftDistance;
  281. int rightDistance;
  282. int topDistance;
  283. int bottomDistance;
  284. switch (avoidee.SnapRegion)
  285. {
  286. case SnapRegion.Interior:
  287. leftDistance = Math.Abs(avoiderRect.Left - avoideeRect.Left);
  288. rightDistance = Math.Abs(avoiderRect.Right - avoideeRect.Right);
  289. topDistance = Math.Abs(avoiderRect.Top - avoideeRect.Top);
  290. bottomDistance = Math.Abs(avoiderRect.Bottom - avoideeRect.Bottom);
  291. break;
  292. case SnapRegion.Exterior:
  293. leftDistance = Math.Abs(avoiderRect.Left - avoideeRect.Right);
  294. rightDistance = Math.Abs(avoiderRect.Right - avoideeRect.Left);
  295. topDistance = Math.Abs(avoiderRect.Top - avoideeRect.Bottom);
  296. bottomDistance = Math.Abs(avoiderRect.Bottom - avoideeRect.Top);
  297. break;
  298. default:
  299. throw new InvalidEnumArgumentException("avoidee.SnapRegion");
  300. }
  301. bool leftClose = (leftDistance < ourSnapProximity);
  302. bool rightClose = (rightDistance < ourSnapProximity);
  303. bool topClose = (topDistance < ourSnapProximity);
  304. bool bottomClose = (bottomDistance < ourSnapProximity);
  305. VerticalSnapEdge vEdge = VerticalSnapEdge.Neither;
  306. if (vertProximity)
  307. {
  308. if ((leftClose && avoidee.SnapRegion == SnapRegion.Exterior) ||
  309. (rightClose && avoidee.SnapRegion == SnapRegion.Interior))
  310. {
  311. vEdge = VerticalSnapEdge.Right;
  312. }
  313. else if ((rightClose && avoidee.SnapRegion == SnapRegion.Exterior) ||
  314. (leftClose && avoidee.SnapRegion == SnapRegion.Interior))
  315. {
  316. vEdge = VerticalSnapEdge.Left;
  317. }
  318. }
  319. HorizontalSnapEdge hEdge = HorizontalSnapEdge.Neither;
  320. if (horizProximity)
  321. {
  322. if ((topClose && avoidee.SnapRegion == SnapRegion.Exterior) ||
  323. (bottomClose && avoidee.SnapRegion == SnapRegion.Interior))
  324. {
  325. hEdge = HorizontalSnapEdge.Bottom;
  326. }
  327. else if ((bottomClose && avoidee.SnapRegion == SnapRegion.Exterior) ||
  328. (topClose && avoidee.SnapRegion == SnapRegion.Interior))
  329. {
  330. hEdge = HorizontalSnapEdge.Top;
  331. }
  332. }
  333. SnapDescription sd;
  334. if (hEdge != HorizontalSnapEdge.Neither || vEdge != VerticalSnapEdge.Neither)
  335. {
  336. int xOffset = avoider.SnapDistance;
  337. int yOffset = avoider.SnapDistance;
  338. if (hEdge == HorizontalSnapEdge.Neither)
  339. {
  340. if (avoidee.SnapRegion == SnapRegion.Interior)
  341. {
  342. yOffset = avoiderRect.Top - avoideeRect.Top;
  343. hEdge = HorizontalSnapEdge.Top;
  344. }
  345. }
  346. if (vEdge == VerticalSnapEdge.Neither)
  347. {
  348. if (avoidee.SnapRegion == SnapRegion.Interior)
  349. {
  350. xOffset = avoiderRect.Left - avoideeRect.Left;
  351. vEdge = VerticalSnapEdge.Left;
  352. }
  353. }
  354. sd = new SnapDescription(avoidee, hEdge, vEdge, xOffset, yOffset);
  355. }
  356. else
  357. {
  358. sd = null;
  359. }
  360. return sd;
  361. }
  362. private static void ParkObstacle(SnapObstacle avoider, SnapDescription snapDescription)
  363. {
  364. Point newLocation = avoider.Bounds.Location;
  365. Point adjustedLocation = AdjustNewLocation(avoider, newLocation, snapDescription);
  366. Rectangle newBounds = new Rectangle(adjustedLocation, avoider.Bounds.Size);
  367. avoider.RequestBoundsChange(newBounds);
  368. }
  369. private static Point AdjustNewLocation(SnapObstacle obstacle, Point newLocation, SnapDescription snapDescription)
  370. {
  371. if (snapDescription == null ||
  372. (snapDescription.HorizontalEdge == HorizontalSnapEdge.Neither &&
  373. snapDescription.VerticalEdge == VerticalSnapEdge.Neither))
  374. {
  375. return obstacle.Bounds.Location;
  376. }
  377. Rectangle obstacleRect = new Rectangle(newLocation, obstacle.Bounds.Size);
  378. Rectangle snappedToRect = snapDescription.SnappedTo.Bounds;
  379. HorizontalSnapEdge hEdge = snapDescription.HorizontalEdge;
  380. VerticalSnapEdge vEdge = snapDescription.VerticalEdge;
  381. SnapRegion region = snapDescription.SnappedTo.SnapRegion;
  382. int deltaY = 0;
  383. if (hEdge == HorizontalSnapEdge.Top && region == SnapRegion.Exterior)
  384. {
  385. int newBottomEdge = snappedToRect.Top - snapDescription.YOffset;
  386. deltaY = obstacleRect.Bottom - newBottomEdge;
  387. }
  388. else if (hEdge == HorizontalSnapEdge.Bottom && region == SnapRegion.Exterior)
  389. {
  390. int newTopEdge = snappedToRect.Bottom + snapDescription.YOffset;
  391. deltaY = obstacleRect.Top - newTopEdge;
  392. }
  393. else if (hEdge == HorizontalSnapEdge.Top && region == SnapRegion.Interior)
  394. {
  395. int newTopEdge = Math.Min(snappedToRect.Bottom, snappedToRect.Top + snapDescription.YOffset);
  396. deltaY = obstacleRect.Top - newTopEdge;
  397. }
  398. else if (hEdge == HorizontalSnapEdge.Bottom && region == SnapRegion.Interior)
  399. {
  400. int newBottomEdge = Math.Max(snappedToRect.Top, snappedToRect.Bottom - snapDescription.YOffset);
  401. deltaY = obstacleRect.Bottom - newBottomEdge;
  402. }
  403. int deltaX = 0;
  404. if (vEdge == VerticalSnapEdge.Left && region == SnapRegion.Exterior)
  405. {
  406. int newRightEdge = snappedToRect.Left - snapDescription.XOffset;
  407. deltaX = obstacleRect.Right - newRightEdge;
  408. }
  409. else if (vEdge == VerticalSnapEdge.Right && region == SnapRegion.Exterior)
  410. {
  411. int newLeftEdge = snappedToRect.Right + snapDescription.XOffset;
  412. deltaX = obstacleRect.Left - newLeftEdge;
  413. }
  414. else if (vEdge == VerticalSnapEdge.Left && region == SnapRegion.Interior)
  415. {
  416. int newLeftEdge = Math.Min(snappedToRect.Right, snappedToRect.Left + snapDescription.XOffset);
  417. deltaX = obstacleRect.Left - newLeftEdge;
  418. }
  419. else if (vEdge == VerticalSnapEdge.Right && region == SnapRegion.Interior)
  420. {
  421. int newRightEdge = Math.Max(snappedToRect.Left, snappedToRect.Right - snapDescription.XOffset);
  422. deltaX = obstacleRect.Right - newRightEdge;
  423. }
  424. Point adjustedLocation = new Point(obstacleRect.Left - deltaX, obstacleRect.Top - deltaY);
  425. return adjustedLocation;
  426. }
  427. /// <summary>
  428. /// Given an obstacle and its attempted destination, determines the correct landing
  429. /// spot for an obstacle.
  430. /// </summary>
  431. /// <param name="movingObstacle">The obstacle that is moving.</param>
  432. /// <param name="newLocation">The upper-left coordinate of the obstacle's original intended destination.</param>
  433. /// <returns>
  434. /// A Point that determines where the obstacle should be placed instead. If there are no adjustments
  435. /// required to the obstacle's desintation, then the return value will be equal to newLocation.
  436. /// </returns>
  437. /// <remarks>
  438. /// movingObstacle's SnapDescription will also be updated. The caller of this method is required
  439. /// to update the SnapObstacle with the new, adjusted location.
  440. /// </remarks>
  441. public Point AdjustObstacleDestination(SnapObstacle movingObstacle, Point newLocation)
  442. {
  443. Point adjusted1 = AdjustObstacleDestination(movingObstacle, newLocation, false);
  444. Point adjusted2 = AdjustObstacleDestination(movingObstacle, adjusted1, true);
  445. return adjusted2;
  446. }
  447. public Point AdjustObstacleDestination(SnapObstacle movingObstacle, Point newLocation, bool considerStickies)
  448. {
  449. Point adjustedLocation = newLocation;
  450. SnapDescription sd = this.obstacles[movingObstacle];
  451. SnapDescription newSD = null;
  452. foreach (SnapObstacle avoidee in this.obstacles.Keys)
  453. {
  454. if (avoidee.StickyEdges != considerStickies)
  455. {
  456. continue;
  457. }
  458. if (avoidee.Enabled && !object.ReferenceEquals(avoidee, movingObstacle))
  459. {
  460. SnapDescription newSD2 = DetermineNewSnapDescription(movingObstacle, adjustedLocation, avoidee, newSD);
  461. if (newSD2 != null)
  462. {
  463. Point adjustedLocation2 = AdjustNewLocation(movingObstacle, adjustedLocation, newSD2);
  464. newSD = newSD2;
  465. adjustedLocation = adjustedLocation2;
  466. Rectangle newBounds = new Rectangle(adjustedLocation, movingObstacle.Bounds.Size);
  467. }
  468. }
  469. }
  470. if (sd == null || !sd.SnappedTo.StickyEdges || newSD == null || newSD.SnappedTo.StickyEdges)
  471. {
  472. this.obstacles[movingObstacle] = newSD;
  473. }
  474. return adjustedLocation;
  475. }
  476. public SnapObstacle FindObstacle(string name)
  477. {
  478. foreach (SnapObstacle so in this.obstacles.Keys)
  479. {
  480. if (string.Compare(so.Name, name, true) == 0)
  481. {
  482. return so;
  483. }
  484. }
  485. return null;
  486. }
  487. public static SnapManager FindMySnapManager(Control me)
  488. {
  489. if (!(me is ISnapObstacleHost))
  490. {
  491. throw new ArgumentException("must be called with a Control that implements ISnapObstacleHost");
  492. }
  493. ISnapManagerHost ismh;
  494. ismh = me as ISnapManagerHost;
  495. if (ismh == null)
  496. {
  497. ismh = me.FindForm() as ISnapManagerHost;
  498. }
  499. SnapManager sm;
  500. if (ismh != null)
  501. {
  502. sm = ismh.SnapManager;
  503. }
  504. else
  505. {
  506. sm = null;
  507. }
  508. return sm;
  509. }
  510. public SnapManager()
  511. {
  512. }
  513. }
  514. }