PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Tools/MapCruncher/MSR.CVE.BackMaker/UserRegionViewController.cs

#
C# | 261 lines | 261 code | 0 blank | 0 comment | 21 complexity | 7b523683ee8b5c9501609d8b953c74e1 MD5 | raw file
  1. using MSR.CVE.BackMaker.ImagePipeline;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Windows.Forms;
  7. namespace MSR.CVE.BackMaker
  8. {
  9. internal class UserRegionViewController
  10. {
  11. private struct State
  12. {
  13. public LatLonZoom center;
  14. public Size size;
  15. public bool valid;
  16. public override bool Equals(object o2)
  17. {
  18. if (o2 is UserRegionViewController.State)
  19. {
  20. UserRegionViewController.State state = (UserRegionViewController.State)o2;
  21. return this.center == state.center && this.size == state.size && this.valid == state.valid;
  22. }
  23. return false;
  24. }
  25. public override int GetHashCode()
  26. {
  27. return this.center.GetHashCode() ^ this.size.GetHashCode();
  28. }
  29. public override string ToString()
  30. {
  31. return string.Format("{0} {1}", this.center, this.size);
  32. }
  33. public State(UserRegionViewController.State state)
  34. {
  35. this.center = state.center;
  36. this.size = new Size(state.size.Width, state.size.Height);
  37. this.valid = state.valid;
  38. }
  39. }
  40. private class ClickableThing
  41. {
  42. public enum ClickedWhich
  43. {
  44. Vertex,
  45. Segment
  46. }
  47. public TracedScreenPoint vertexLocation;
  48. public GraphicsPath path;
  49. public UserRegionViewController.ClickableThing.ClickedWhich clickedWhich;
  50. public int pointIndex;
  51. }
  52. private class VertexMouseAction : ViewerControl.MouseAction
  53. {
  54. private UserRegionViewController controller;
  55. private int draggedVertexIndex;
  56. public VertexMouseAction(UserRegionViewController controller, int draggedVertexIndex)
  57. {
  58. this.controller = controller;
  59. this.draggedVertexIndex = draggedVertexIndex;
  60. }
  61. public void Dragged(Point diff)
  62. {
  63. this.controller.DragVertex(diff, this.draggedVertexIndex);
  64. }
  65. public Cursor GetCursor(bool dragging)
  66. {
  67. return Cursors.Arrow;
  68. }
  69. public void OnPopup(ContextMenu menu)
  70. {
  71. MenuItem menuItem = menu.MenuItems.Add("Remove corner", new EventHandler(this.RemoveCorner));
  72. menuItem.Enabled = this.controller.RemoveEnabled();
  73. }
  74. public void RemoveCorner(object sender, EventArgs e)
  75. {
  76. this.controller.RemoveCorner(this.draggedVertexIndex);
  77. }
  78. }
  79. private class SegmentMouseAction : ViewerControl.MouseAction
  80. {
  81. private UserRegionViewController controller;
  82. private int originalVertexIndex;
  83. private Point clickedPoint;
  84. private int menuIncarnation;
  85. private static int menuIncarnationCounter;
  86. public SegmentMouseAction(UserRegionViewController controller, int originalVertexIndex, Point clickedPoint)
  87. {
  88. this.controller = controller;
  89. this.originalVertexIndex = originalVertexIndex;
  90. UserRegionViewController.SegmentMouseAction.menuIncarnationCounter++;
  91. this.menuIncarnation = UserRegionViewController.SegmentMouseAction.menuIncarnationCounter;
  92. this.clickedPoint = clickedPoint;
  93. }
  94. public void Dragged(Point diff)
  95. {
  96. }
  97. public Cursor GetCursor(bool dragging)
  98. {
  99. return Cursors.Cross;
  100. }
  101. public void OnPopup(ContextMenu menu)
  102. {
  103. menu.MenuItems.Add("Add corner", new EventHandler(this.AddCorner));
  104. D.Say(0, string.Format("Updating menu from incarnation {0}", this.menuIncarnation));
  105. }
  106. public void AddCorner(object sender, EventArgs e)
  107. {
  108. D.Say(0, string.Format("AddCorner from incarnation {0}", this.menuIncarnation));
  109. this.controller.AddCorner(this.clickedPoint, this.originalVertexIndex);
  110. }
  111. }
  112. private const int vertexRadius = 3;
  113. private const int edgeClickWidth = 4;
  114. private const int vertexClickRadius = 4;
  115. private CoordinateSystemIfc csi;
  116. private SVDisplayParams svdp;
  117. private LatentRegionHolder latentRegionHolder;
  118. private IDisplayableSource displayableSource;
  119. private Brush vertexFillBrush;
  120. private Pen vertexStrokePen;
  121. private Brush segmentFillBrush;
  122. private UserRegionViewController.State lastState;
  123. private UserRegionViewController.ClickableThing[] clickableThings;
  124. public UserRegionViewController(CoordinateSystemIfc csi, SVDisplayParams svdp, LatentRegionHolder latentRegionHolder, IDisplayableSource unwarpedMapTileSource)
  125. {
  126. this.csi = csi;
  127. this.svdp = svdp;
  128. this.latentRegionHolder = latentRegionHolder;
  129. this.displayableSource = unwarpedMapTileSource;
  130. this.vertexFillBrush = new SolidBrush(Color.LightBlue);
  131. this.vertexStrokePen = new Pen(Color.DarkBlue, 1f);
  132. this.segmentFillBrush = new SolidBrush(Color.DarkBlue);
  133. }
  134. private void UpdateState(UserRegionViewController.State state)
  135. {
  136. if (state.Equals(this.lastState))
  137. {
  138. return;
  139. }
  140. TracedScreenPoint[] path = this.GetUserRegion().GetPath(CoordinateSystemUtilities.GetBounds(this.csi, state.center, state.size), state.center.zoom, this.csi);
  141. List<TracedScreenPoint> list = new List<TracedScreenPoint>();
  142. int length = path.GetLength(0);
  143. for (int i = 0; i < length; i++)
  144. {
  145. D.Assert(path[i].originalIndex >= 0);
  146. int num = (i + length - 1) % length;
  147. if (!(path[num].pointf == path[i].pointf))
  148. {
  149. list.Add(path[i]);
  150. }
  151. }
  152. list.ToArray();
  153. int count = list.Count;
  154. List<UserRegionViewController.ClickableThing> list2 = new List<UserRegionViewController.ClickableThing>();
  155. List<UserRegionViewController.ClickableThing> list3 = new List<UserRegionViewController.ClickableThing>();
  156. for (int j = 0; j < count; j++)
  157. {
  158. UserRegionViewController.ClickableThing clickableThing = new UserRegionViewController.ClickableThing();
  159. list2.Add(clickableThing);
  160. clickableThing.vertexLocation = list[j];
  161. clickableThing.path = new GraphicsPath();
  162. clickableThing.path.AddEllipse(list[j].pointf.X - 4f, list[j].pointf.Y - 4f, 8f, 8f);
  163. clickableThing.clickedWhich = UserRegionViewController.ClickableThing.ClickedWhich.Vertex;
  164. clickableThing.pointIndex = j;
  165. UserRegionViewController.ClickableThing clickableThing2 = new UserRegionViewController.ClickableThing();
  166. list3.Add(clickableThing2);
  167. clickableThing2.vertexLocation = list[j];
  168. clickableThing2.path = new GraphicsPath();
  169. clickableThing2.path.AddLine(list[j].pointf, list[(j + 1) % count].pointf);
  170. clickableThing2.path.Widen(new Pen(Color.Black, 4f));
  171. clickableThing2.clickedWhich = UserRegionViewController.ClickableThing.ClickedWhich.Segment;
  172. clickableThing2.pointIndex = j;
  173. }
  174. list2.AddRange(list3);
  175. this.clickableThings = list2.ToArray();
  176. this.lastState = new UserRegionViewController.State(state);
  177. }
  178. internal RenderRegion GetUserRegion()
  179. {
  180. return this.latentRegionHolder.renderRegion;
  181. }
  182. internal void Paint(PaintSpecification e, LatLonZoom center, Size size)
  183. {
  184. if (this.GetUserRegion() == null)
  185. {
  186. return;
  187. }
  188. UserRegionViewController.State state;
  189. state.center = center;
  190. state.size = size;
  191. state.valid = true;
  192. this.UpdateState(state);
  193. UserRegionViewController.ClickableThing[] array = this.clickableThings;
  194. for (int i = 0; i < array.Length; i++)
  195. {
  196. UserRegionViewController.ClickableThing clickableThing = array[i];
  197. e.Graphics.FillPath(this.segmentFillBrush, clickableThing.path);
  198. }
  199. }
  200. internal ViewerControl.MouseAction ImminentAction(MouseEventArgs e)
  201. {
  202. if (this.clickableThings == null)
  203. {
  204. return null;
  205. }
  206. int i = 0;
  207. while (i < this.clickableThings.GetLength(0))
  208. {
  209. UserRegionViewController.ClickableThing clickableThing = this.clickableThings[i];
  210. if (clickableThing.vertexLocation.originalIndex >= 0 && clickableThing.path.IsVisible(e.Location))
  211. {
  212. if (clickableThing.clickedWhich == UserRegionViewController.ClickableThing.ClickedWhich.Segment)
  213. {
  214. return new UserRegionViewController.SegmentMouseAction(this, clickableThing.vertexLocation.originalIndex, e.Location);
  215. }
  216. return new UserRegionViewController.VertexMouseAction(this, clickableThing.vertexLocation.originalIndex);
  217. }
  218. else
  219. {
  220. i++;
  221. }
  222. }
  223. return null;
  224. }
  225. internal void DragVertex(Point diff, int draggedVertexIndex)
  226. {
  227. LatLon point = this.GetUserRegion().GetPoint(draggedVertexIndex);
  228. LatLonZoom center = new LatLonZoom(point.lat, point.lon, this.svdp.MapCenter().zoom);
  229. diff = new Point(-diff.X, -diff.Y);
  230. LatLonZoom translationInLatLon = this.csi.GetTranslationInLatLon(center, diff);
  231. this.GetUserRegion().UpdatePoint(draggedVertexIndex, translationInLatLon.latlon);
  232. this.Invalidate();
  233. }
  234. internal void AddCorner(Point newCornerPoint, int originalVertexIndex)
  235. {
  236. LatLonZoom center = this.svdp.MapCenter();
  237. Point offsetInPixels = new Point(this.svdp.ScreenCenter().X - newCornerPoint.X, this.svdp.ScreenCenter().Y - newCornerPoint.Y);
  238. LatLonZoom translationInLatLon = this.csi.GetTranslationInLatLon(center, offsetInPixels);
  239. D.Say(0, string.Format("newCornerPosition= {0}", translationInLatLon));
  240. this.GetUserRegion().InsertPoint(originalVertexIndex + 1, translationInLatLon.latlon);
  241. this.Invalidate();
  242. }
  243. internal void RemoveCorner(int originalVertexIndex)
  244. {
  245. this.GetUserRegion().RemovePoint(originalVertexIndex);
  246. this.Invalidate();
  247. }
  248. internal bool RemoveEnabled()
  249. {
  250. return this.GetUserRegion().Count > 3;
  251. }
  252. private void Invalidate()
  253. {
  254. AsyncRef asyncRef = (AsyncRef)this.displayableSource.GetUserBounds(this.latentRegionHolder, (FutureFeatures)7).Realize("UserRegionViewController.Invalidate");
  255. asyncRef.ProcessSynchronously();
  256. asyncRef.Dispose();
  257. this.svdp.InvalidateView();
  258. this.lastState.valid = false;
  259. }
  260. }
  261. }