SurfaceBoxRendererList.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace SmartCoalApplication.Core
  9. {
  10. public sealed class SurfaceBoxRendererList
  11. {
  12. private SurfaceBoxRenderer[] list;
  13. private SurfaceBoxRenderer[] topList;
  14. private Size sourceSize;
  15. private Size destinationSize;
  16. private ScaleFactor scaleFactor;
  17. private object lockObject = new object();
  18. public object SyncRoot
  19. {
  20. get
  21. {
  22. return this.lockObject;
  23. }
  24. }
  25. public ScaleFactor ScaleFactor
  26. {
  27. get
  28. {
  29. return this.scaleFactor;
  30. }
  31. }
  32. public SurfaceBoxRenderer[][] Renderers
  33. {
  34. get
  35. {
  36. return new SurfaceBoxRenderer[][] { list, topList };
  37. }
  38. }
  39. private void ComputeScaleFactor()
  40. {
  41. scaleFactor = new ScaleFactor(this.DestinationSize.Width, this.SourceSize.Width);
  42. }
  43. public Point SourceToDestination(Point pt)
  44. {
  45. return this.scaleFactor.ScalePoint(pt);
  46. }
  47. public RectangleF SourceToDestination(Rectangle rect)
  48. {
  49. return this.scaleFactor.ScaleRectangle((RectangleF)rect);
  50. }
  51. public Point DestinationToSource(Point pt)
  52. {
  53. return this.scaleFactor.UnscalePoint(pt);
  54. }
  55. public void Add(SurfaceBoxRenderer addMe, bool alwaysOnTop)
  56. {
  57. SurfaceBoxRenderer[] startList = alwaysOnTop ? this.topList : this.list;
  58. SurfaceBoxRenderer[] listPlusOne = new SurfaceBoxRenderer[startList.Length + 1];
  59. for (int i = 0; i < startList.Length; ++i)
  60. {
  61. listPlusOne[i] = startList[i];
  62. }
  63. listPlusOne[listPlusOne.Length - 1] = addMe;
  64. if (alwaysOnTop)
  65. {
  66. this.topList = listPlusOne;
  67. }
  68. else
  69. {
  70. this.list = listPlusOne;
  71. }
  72. Invalidate();
  73. }
  74. public void Remove(SurfaceBoxRenderer removeMe)
  75. {
  76. if (this.list.Length == 0 && this.topList.Length == 0)
  77. {
  78. throw new InvalidOperationException("zero items left, can't remove anything");
  79. }
  80. else
  81. {
  82. bool found = false;
  83. if (this.list.Length > 0)
  84. {
  85. SurfaceBoxRenderer[] listSubOne = new SurfaceBoxRenderer[this.list.Length - 1];
  86. bool foundHere = false;
  87. int dstIndex = 0;
  88. for (int i = 0; i < this.list.Length; ++i)
  89. {
  90. if (this.list[i] == removeMe)
  91. {
  92. if (foundHere)
  93. {
  94. throw new ArgumentException("removeMe appeared multiple times in the list");
  95. }
  96. else
  97. {
  98. foundHere = true;
  99. }
  100. }
  101. else
  102. {
  103. if (dstIndex == this.list.Length - 1)
  104. {
  105. // was not found
  106. }
  107. else
  108. {
  109. listSubOne[dstIndex] = this.list[i];
  110. ++dstIndex;
  111. }
  112. }
  113. }
  114. if (foundHere)
  115. {
  116. this.list = listSubOne;
  117. found = true;
  118. }
  119. }
  120. if (this.topList.Length > 0)
  121. {
  122. SurfaceBoxRenderer[] topListSubOne = new SurfaceBoxRenderer[this.topList.Length - 1];
  123. int topDstIndex = 0;
  124. bool foundHere = false;
  125. for (int i = 0; i < this.topList.Length; ++i)
  126. {
  127. if (this.topList[i] == removeMe)
  128. {
  129. if (found || foundHere)
  130. {
  131. throw new ArgumentException("removeMe appeared multiple times in the list");
  132. }
  133. else
  134. {
  135. foundHere = true;
  136. }
  137. }
  138. else
  139. {
  140. if (topDstIndex == this.topList.Length - 1)
  141. {
  142. // was not found
  143. }
  144. else
  145. {
  146. topListSubOne[topDstIndex] = this.topList[i];
  147. ++topDstIndex;
  148. }
  149. }
  150. }
  151. if (foundHere)
  152. {
  153. this.topList = topListSubOne;
  154. found = true;
  155. }
  156. }
  157. if (!found)
  158. {
  159. throw new ArgumentException("removeMe was not found", "removeMe");
  160. }
  161. Invalidate();
  162. }
  163. }
  164. private void OnDestinationSizeChanged()
  165. {
  166. InvalidateLookups();
  167. if (this.destinationSize.Width != 0 && this.sourceSize.Width != 0)
  168. {
  169. ComputeScaleFactor();
  170. for (int i = 0; i < this.list.Length; ++i)
  171. {
  172. this.list[i].OnDestinationSizeChanged();
  173. }
  174. for (int i = 0; i < this.topList.Length; ++i)
  175. {
  176. this.topList[i].OnDestinationSizeChanged();
  177. }
  178. }
  179. }
  180. public Size DestinationSize
  181. {
  182. get
  183. {
  184. return this.destinationSize;
  185. }
  186. set
  187. {
  188. if (this.destinationSize != value)
  189. {
  190. this.destinationSize = value;
  191. OnDestinationSizeChanged();
  192. }
  193. }
  194. }
  195. private void OnSourceSizeChanged()
  196. {
  197. InvalidateLookups();
  198. if (this.destinationSize.Width != 0 && this.sourceSize.Width != 0)
  199. {
  200. ComputeScaleFactor();
  201. for (int i = 0; i < this.list.Length; ++i)
  202. {
  203. this.list[i].OnSourceSizeChanged();
  204. }
  205. for (int i = 0; i < this.topList.Length; ++i)
  206. {
  207. this.topList[i].OnSourceSizeChanged();
  208. }
  209. }
  210. }
  211. public Size SourceSize
  212. {
  213. get
  214. {
  215. return this.sourceSize;
  216. }
  217. set
  218. {
  219. if (this.sourceSize != value)
  220. {
  221. this.sourceSize = value;
  222. OnSourceSizeChanged();
  223. }
  224. }
  225. }
  226. public int[] Dst2SrcLookupX
  227. {
  228. get
  229. {
  230. lock (this.SyncRoot)
  231. {
  232. CreateD2SLookupX();
  233. }
  234. return this.d2SLookupX;
  235. }
  236. }
  237. private int[] d2SLookupX; // maps from destination->source coordinates
  238. private void CreateD2SLookupX()
  239. {
  240. if (this.d2SLookupX == null || this.d2SLookupX.Length != this.DestinationSize.Width + 1)
  241. {
  242. this.d2SLookupX = new int[this.DestinationSize.Width + 1];
  243. for (int x = 0; x < d2SLookupX.Length; ++x)
  244. {
  245. Point pt = new Point(x, 0);
  246. Point surfacePt = this.DestinationToSource(pt);
  247. // Sometimes the scale factor is slightly different on one axis than
  248. // on another, simply due to accuracy. So we have to clamp this value to
  249. // be within bounds.
  250. d2SLookupX[x] = Utility.Clamp(surfacePt.X, 0, this.SourceSize.Width - 1);
  251. }
  252. }
  253. }
  254. public int[] Dst2SrcLookupY
  255. {
  256. get
  257. {
  258. lock (this.SyncRoot)
  259. {
  260. CreateD2SLookupY();
  261. }
  262. return this.d2SLookupY;
  263. }
  264. }
  265. private int[] d2SLookupY; // maps from destination->source coordinates
  266. private void CreateD2SLookupY()
  267. {
  268. if (this.d2SLookupY == null || this.d2SLookupY.Length != this.DestinationSize.Height + 1)
  269. {
  270. this.d2SLookupY = new int[this.DestinationSize.Height + 1];
  271. for (int y = 0; y < d2SLookupY.Length; ++y)
  272. {
  273. Point pt = new Point(0, y);
  274. Point surfacePt = this.DestinationToSource(pt);
  275. // Sometimes the scale factor is slightly different on one axis than
  276. // on another, simply due to accuracy. So we have to clamp this value to
  277. // be within bounds.
  278. d2SLookupY[y] = Utility.Clamp(surfacePt.Y, 0, this.SourceSize.Height - 1);
  279. }
  280. }
  281. }
  282. public int[] Src2DstLookupX
  283. {
  284. get
  285. {
  286. lock (this.SyncRoot)
  287. {
  288. CreateS2DLookupX();
  289. }
  290. return this.s2DLookupX;
  291. }
  292. }
  293. private int[] s2DLookupX; // maps from source->destination coordinates
  294. private void CreateS2DLookupX()
  295. {
  296. if (this.s2DLookupX == null || this.s2DLookupX.Length != this.SourceSize.Width + 1)
  297. {
  298. this.s2DLookupX = new int[this.SourceSize.Width + 1];
  299. for (int x = 0; x < s2DLookupX.Length; ++x)
  300. {
  301. Point pt = new Point(x, 0);
  302. Point clientPt = this.SourceToDestination(pt);
  303. // Sometimes the scale factor is slightly different on one axis than
  304. // on another, simply due to accuracy. So we have to clamp this value to
  305. // be within bounds.
  306. s2DLookupX[x] = Utility.Clamp(clientPt.X, 0, this.DestinationSize.Width - 1);
  307. }
  308. }
  309. }
  310. public int[] Src2DstLookupY
  311. {
  312. get
  313. {
  314. lock (this.SyncRoot)
  315. {
  316. CreateS2DLookupY();
  317. }
  318. return this.s2DLookupY;
  319. }
  320. }
  321. private int[] s2DLookupY; // maps from source->destination coordinates
  322. private void CreateS2DLookupY()
  323. {
  324. if (this.s2DLookupY == null || this.s2DLookupY.Length != this.SourceSize.Height + 1)
  325. {
  326. this.s2DLookupY = new int[this.SourceSize.Height + 1];
  327. for (int y = 0; y < s2DLookupY.Length; ++y)
  328. {
  329. Point pt = new Point(0, y);
  330. Point clientPt = this.SourceToDestination(pt);
  331. // Sometimes the scale factor is slightly different on one axis than
  332. // on another, simply due to accuracy. So we have to clamp this value to
  333. // be within bounds.
  334. s2DLookupY[y] = Utility.Clamp(clientPt.Y, 0, this.DestinationSize.Height - 1);
  335. }
  336. }
  337. }
  338. public void InvalidateLookups()
  339. {
  340. this.s2DLookupX = null;
  341. this.s2DLookupY = null;
  342. this.d2SLookupX = null;
  343. this.d2SLookupY = null;
  344. }
  345. public void Render(Surface dst, Point offset)
  346. {
  347. foreach (SurfaceBoxRenderer sbr in this.list)
  348. {
  349. if (sbr.Visible)
  350. {
  351. sbr.Render(dst, offset);
  352. }
  353. }
  354. foreach (SurfaceBoxRenderer sbr in this.topList)
  355. {
  356. if (sbr.Visible)
  357. {
  358. sbr.Render(dst, offset);
  359. }
  360. }
  361. }
  362. public event InvalidateEventHandler Invalidated;
  363. public void Invalidate(Rectangle rect)
  364. {
  365. if (Invalidated != null)
  366. {
  367. Invalidated(this, new InvalidateEventArgs(rect));
  368. }
  369. }
  370. public void Invalidate()
  371. {
  372. Invalidate(SurfaceBoxRenderer.MaxBounds);
  373. }
  374. public SurfaceBoxRendererList(Size sourceSize, Size destinationSize)
  375. {
  376. this.list = new SurfaceBoxRenderer[0];
  377. this.topList = new SurfaceBoxRenderer[0];
  378. this.sourceSize = sourceSize;
  379. this.destinationSize = destinationSize;
  380. }
  381. }
  382. }