Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.34 KB | None | 0 0
  1. Index: C:/Users/Frank/Desktop/umtsnew/trunk/src/RCT/RCT/UI/MapControl.cs
  2. ===================================================================
  3. --- C:/Users/Frank/Desktop/umtsnew/trunk/src/RCT/RCT/UI/MapControl.cs (revision 239)
  4. +++ C:/Users/Frank/Desktop/umtsnew/trunk/src/RCT/RCT/UI/MapControl.cs (revision 240)
  5. @@ -22,8 +22,10 @@
  6. SvgPictureBoxWindow svgWindow;
  7. Size svgOriginalSize;
  8.  
  9. - private static Size generalSvgSize = new Size(2000, 2000);
  10. + private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  11.  
  12. + private static Size generalSvgSize = new Size(2000, 2000);
  13. +
  14. public MapControl()
  15. : base() {
  16. this.svgRenderer = new GdiGraphicsRenderer();
  17. @@ -85,7 +87,7 @@
  18. try {
  19. if (tracksvg.Length > 0) {
  20. // Load the source
  21. - this.svgWindow = new SvgPictureBoxWindow(generalSvgSize.Width, generalSvgSize.Height, svgRenderer);
  22. + this.svgWindow = new SvgPictureBoxWindow(generalSvgSize.Width, generalSvgSize.Height, svgRenderer);
  23. var doc = svgWindow.CreateEmptySvgDocument();
  24. doc.LoadXml(Encoding.UTF8.GetString(tracksvg));
  25. svgWindow.Document = doc;
  26. @@ -146,41 +148,42 @@
  27. /// and zoomed in by the scale factor.
  28. /// </summary>
  29. private void DrawCentered(CarStatus cs, Graphics gfx, float scale = 2.0f) {
  30. - gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  31. + gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  32.  
  33. var src = GetClippedSrcRect(_map.Project(GetStatusFor(cs).Position, svgOriginalSize), scale);
  34. - gfx.Clear(Color.Black); // Background filler for the edges
  35. - gfx.DrawImage(svgRenderer.RasterImage, ClientRectangle, src, GraphicsUnit.Pixel);
  36. + gfx.Clear(Color.Black); // Background filler for the edges
  37. + gfx.DrawImage(svgRenderer.RasterImage, ClientRectangle, src, GraphicsUnit.Pixel);
  38. + DrawInitialMap(gfx, src);
  39.  
  40. int r = (int)Math.Round(37 * scale);
  41. double ratio = this.Width / (double)src.Width;
  42.  
  43. - foreach (var carstats in carsOnMap.Keys.OrderBy(x => x.id)) {
  44. - var drawStatus = GetStatusFor(carstats);
  45. - if (drawStatus.Position != null) {
  46. - Point carPos = _map.Project(drawStatus.Position, svgOriginalSize);
  47. - if (src.Contains(carPos)) {
  48. - carPos.Offset((int)(-r / 2), (int)(-r / 2));
  49. + foreach (var carstats in carsOnMap.Keys.OrderBy(x => x.id)) {
  50. + var drawStatus = GetStatusFor(carstats);
  51. + if (drawStatus.Position != null) {
  52. + Point carPos = _map.Project(drawStatus.Position, svgOriginalSize);
  53. + if (src.Contains(carPos)) {
  54. + carPos.Offset((int)(-r / 2), (int)(-r / 2));
  55. Rectangle dst = new Rectangle((int)Math.Round((carPos.X - src.X) * ratio), (int)Math.Round((carPos.Y - src.Y) * ratio), r, r);
  56. - DrawCar(gfx, carstats, ref dst, scale);
  57. - }
  58. - }
  59. - }
  60. + DrawCar(gfx, carstats, ref dst, scale);
  61. + }
  62. + }
  63. + }
  64.  
  65. - InvalidateRect = this.ClientRectangle;
  66. - }
  67. + InvalidateRect = this.ClientRectangle;
  68. + }
  69.  
  70. /// <summary>
  71. /// Returns a rectangle of the original image that is
  72. /// centered around pos. The size is dependant on scale.
  73. /// </summary>
  74. - private Rectangle GetClippedSrcRect(Point pos, float scale) {
  75. - int width = (int)Math.Round(svgOriginalSize.Width / scale);
  76. - int height = (int)Math.Round(svgOriginalSize.Height / scale);
  77. + private Rectangle GetClippedSrcRect(Point pos, float scale) {
  78. + int width = (int)Math.Round(svgOriginalSize.Width / scale);
  79. + int height = (int)Math.Round(svgOriginalSize.Height / scale);
  80.  
  81. Rectangle src = new Rectangle(pos.X - (width / 2), pos.Y - (height / 2), width, height);
  82. return src;
  83. - }
  84. + }
  85.  
  86. /// <summary>
  87. /// Returns last known valid status for cs. Either cached,
  88. @@ -200,10 +203,10 @@
  89. private void DrawCar(Graphics gfx, CarStatus cs, ref Rectangle dest, double scale = 1.0) {
  90. Pen blackpen = new Pen(new SolidBrush(Color.Black), 1);
  91. using (SolidBrush sb = new SolidBrush(cs.Car.Color)) {
  92. - gfx.DrawEllipse(new Pen(sb, (int)(3 * scale)), dest);
  93. - dest.Inflate((int)(2 * scale - 1), (int)(2 * scale - 1));
  94. + gfx.DrawEllipse(new Pen(sb, (int)(3 * scale)), dest);
  95. + dest.Inflate((int)(2 * scale - 1), (int)(2 * scale - 1));
  96. gfx.DrawEllipse(blackpen, dest);
  97. - dest.Inflate((int)(-3 * scale), (int)(-3 * scale));
  98. + dest.Inflate((int)(-3 * scale), (int)(-3 * scale));
  99. gfx.DrawEllipse(blackpen, dest);
  100.  
  101. var quad = GetCarAsQuad(dest, cs.Angle, scale);
  102. @@ -213,7 +216,7 @@
  103.  
  104. string speedLabel = cs.CurrentStatus.StreamStatus != StreamStatus.Valid
  105. ? "?" : cs.Speed.ToString();
  106. - dest.Inflate((int)(10 * scale), (int)(22 * scale));
  107. + dest.Inflate((int)(10 * scale), (int)(22 * scale));
  108. var f = new Font(RCT.UI.RCTPrivateFontCollection.DS_DIGI, (int)(8 + 8 * scale), FontStyle.Bold);
  109. var p = new PointF(dest.X, dest.Y);
  110. var r = new RectangleF(p, gfx.MeasureString(speedLabel, f));
  111. @@ -298,12 +301,18 @@
  112. public void RefreshBaseImage() {
  113. lock (this) {
  114. if (Width > 0 && Height > 0) {
  115. - BaseImage = svgRenderer.RasterImage.Clone() as Bitmap;
  116. -
  117. + Bitmap svgImage = svgRenderer.RasterImage.Clone() as Bitmap;
  118. + if (BaseImage != null && BaseImage.Size != this.Size) {
  119. + BaseImage.Dispose();
  120. + BaseImage = null;
  121. + }
  122. + if (BaseImage == null)
  123. + BaseImage = new Bitmap(this.Width, this.Height);
  124. lock (BaseImage) {
  125. using (Graphics gfx = Graphics.FromImage(BaseImage)) {
  126. - gfx.DrawImage(BaseImage, ClientRectangle, new Rectangle(new Point(0, 0), BaseImage.Size), GraphicsUnit.Pixel);
  127. - DrawInitialMap(gfx);
  128. + var rt = new Rectangle(0, 0, svgImage.Width, svgImage.Height);
  129. + gfx.DrawImage(svgImage, ClientRectangle, rt, GraphicsUnit.Pixel);
  130. + DrawInitialMap(gfx, rt);
  131. }
  132. if (bm != null) bm.Dispose();
  133. bm = BaseImage.Clone() as Bitmap;
  134. @@ -316,30 +325,41 @@
  135. /// Draws initial map with finish lines, sector lines and pitline
  136. /// </summary>
  137. /// <param name="gfx"></param>
  138. - private void DrawInitialMap(Graphics gfx) {
  139. + private void DrawInitialMap(Graphics gfx, Rectangle srcRect) {
  140. gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  141. Pen thickRedPen = new Pen(new SolidBrush(Color.Red), 2);
  142. Pen thickBluePen = new Pen(new SolidBrush(Color.Blue), 2);
  143. +
  144. + double ratio = this.Width / (double)srcRect.Width;
  145. +
  146. + // Finish
  147. PositionVector finishline = _map.FinishLine;
  148. - Point point1 = _map.Project(finishline.Pos1, this.Size);
  149. - Point point2 = _map.Project(finishline.Pos2, this.Size);
  150. + Point point1 = OffsetPoint(_map.Project(finishline.Pos1, svgOriginalSize), ratio, srcRect);
  151. + Point point2 = OffsetPoint(_map.Project(finishline.Pos2, svgOriginalSize), ratio, srcRect);
  152. gfx.DrawLine(thickRedPen, point1, point2);
  153.  
  154. + // Sectors
  155. List<PositionVector> sectors = _map.GetSectors();
  156. foreach (PositionVector sector in sectors) {
  157. - Point point3 = _map.Project(sector.Pos1, this.Size);
  158. - Point point4 = _map.Project(sector.Pos2, this.Size);
  159. + Point point3 = OffsetPoint(_map.Project(sector.Pos1, svgOriginalSize), ratio, srcRect);
  160. + Point point4 = OffsetPoint(_map.Project(sector.Pos2, svgOriginalSize), ratio, srcRect);
  161. gfx.DrawLine(thickRedPen, point3, point4);
  162. }
  163.  
  164. List<PositionVector> pit = _map.GetPit();
  165. foreach (PositionVector p in pit) {
  166. - Point point5 = _map.Project(p.Pos1, this.Size);
  167. - Point point6 = _map.Project(p.Pos2, this.Size);
  168. + Point point5 = OffsetPoint(_map.Project(p.Pos1, svgOriginalSize), ratio, srcRect);
  169. + Point point6 = OffsetPoint(_map.Project(p.Pos2, svgOriginalSize), ratio, srcRect);
  170. gfx.DrawLine(thickBluePen, point5, point6);
  171. }
  172. }
  173.  
  174. + private Point OffsetPoint(Point point, double ratio, Rectangle rt) {
  175. + point.X = (int)((point.X - rt.X) * ratio);
  176. + point.Y = (int)((point.Y - rt.Y) * ratio);
  177. + return point;
  178. + }
  179. +
  180. protected override void OnPaintBackground(PaintEventArgs pevent) {
  181. if (_map == null || DoPaintBackground) base.OnPaintBackground(pevent);
  182. DoPaintBackground = false;
  183. @@ -364,14 +384,14 @@
  184. gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  185.  
  186. lock (carsToUpdate) {
  187. - ClearCarsOnMap(gfx);
  188. + ClearCarsOnMap(gfx);
  189.  
  190. var centerCar = carsToUpdate.FirstOrDefault(x => x.CenterOfMap);
  191. - if (centerCar != null)
  192. - DrawCentered(centerCar, gfx);
  193. - else {
  194. - DrawCarsOnMap(gfx);
  195. - }
  196. + if (centerCar != null)
  197. + DrawCentered(centerCar, gfx);
  198. + else {
  199. + DrawCarsOnMap(gfx);
  200. + }
  201.  
  202. foreach (var cs in carsToUpdate)
  203. carsOnMap[cs] = GetStatusFor(cs);
  204. @@ -379,13 +399,15 @@
  205. carsToUpdate.Clear();
  206. }
  207. }
  208. - Invalidate(InvalidateRect);
  209. + Invalidate(InvalidateRect);
  210. }
  211. Rectangle InvalidateRect; // updated during drawonmap and drawcentered
  212.  
  213. #endregion
  214. - }
  215.  
  216. +
  217. + }
  218. +
  219. /// <summary>
  220. /// Basic Point class with decimal properties.
  221. /// </summary>
  222. Index: C:/Users/Frank/Desktop/umtsnew/trunk/src/RCT/RCT/UI/CarControl.cs
  223. ===================================================================
  224. --- C:/Users/Frank/Desktop/umtsnew/trunk/src/RCT/RCT/UI/CarControl.cs (revision 239)
  225. +++ C:/Users/Frank/Desktop/umtsnew/trunk/src/RCT/RCT/UI/CarControl.cs (revision 240)
  226. @@ -11,6 +11,10 @@
  227. using RCT.Positional;
  228.  
  229. namespace RCT {
  230. +
  231. +
  232. +
  233. +
  234. public class CarControl : Control {
  235. public CarStatus CarStatus { get; private set; }
  236. private Bitmap bm = null;
  237. @@ -45,31 +49,35 @@
  238. }
  239.  
  240. CarControl() {
  241. - /*base.DoubleBuffered = true;
  242. - this.SetStyle(
  243. - ControlStyles.UserPaint |
  244. - ControlStyles.AllPaintingInWmPaint |
  245. - ControlStyles.OptimizedDoubleBuffer, true);*/
  246. _streamStatusCheckTimer.Interval = 100;
  247. _streamStatusCheckTimer.Tick += new EventHandler(_streamStatusCheckTimer_Tick);
  248. _streamStatusCheckTimer.Enabled = true;
  249. this.Click += new EventHandler(CarControl_Click);
  250. + ContextMenu cm = new ContextMenu();
  251. + cm.MenuItems.Add(new MenuItem("Open centered in new window", new EventHandler(popOutCentered)));
  252. + this.ContextMenu = cm;
  253. }
  254.  
  255. + private void popOutCentered(object sender, EventArgs args) {
  256. +
  257. + }
  258. +
  259. private bool expanded = true;
  260. void CarControl_Click(object sender, EventArgs e) {
  261. var me = (MouseEventArgs)e;
  262. - if (me.Y < 40) {
  263. - // collapse / expand
  264. - if (expanded) {
  265. - this.Height = 40;
  266. - }
  267. - else {
  268. - this.Height = 380;
  269. - }
  270. - expanded = !expanded;
  271. - Invalidate();
  272. - }
  273. + if (me.Button == System.Windows.Forms.MouseButtons.Left) {
  274. + if (me.Y < 40) {
  275. + // collapse / expand
  276. + if (expanded) {
  277. + this.Height = 40;
  278. + }
  279. + else {
  280. + this.Height = 380;
  281. + }
  282. + expanded = !expanded;
  283. + Invalidate();
  284. + }
  285. + }
  286. }
  287.  
  288. bool doUpdateLapList = false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement