SurfaceBoxRendererList.cs 13 KB

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