SelectionRenderer.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. using PaintDotNet.SystemLayer;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6. namespace PaintDotNet.Measurement
  7. {
  8. public class SelectionRenderer
  9. : SurfaceBoxGraphicsRenderer
  10. {
  11. private const int dancingAntsInterval = 60;
  12. private const double maxCpuTime = 0.2; // max 20% CPU time
  13. private Color tintColor = Color.FromArgb(32, 32, 32, 255);
  14. private static Pen outlinePen1 = null;
  15. private static Pen outlinePen2 = null;
  16. private UserControl2 ownerControl;
  17. private System.Windows.Forms.Timer selectionTimer;
  18. private bool enableOutlineAnimation = true;
  19. private System.ComponentModel.IContainer components = null;
  20. private bool invertedTinting = false;
  21. private bool render = true; // when false, we do not Render()
  22. private PdnGraphicsPath selectedPath;
  23. private Selection selection;
  24. private PdnGraphicsPath zoomInSelectedPath;
  25. private int dancingAntsT = 0;
  26. private int whiteOpacity = 255;
  27. private int lastTickMod = 0;
  28. private Rectangle[] simplifiedRegionForTimer = null;
  29. private double coolOffTimeTickCount = 0.0;
  30. /// <summary>
  31. /// This variable is used to accumulate an invalidation region. It is initialized
  32. /// upon responding to the SelectedPathChanging event that is raised by the
  33. /// DocumentEnvironment. Then, when the SelectedPathChanged event is raised, the
  34. /// full region that needs to be redrawn is accounted for.
  35. /// </summary>
  36. private PdnRegion selectionRedrawInterior = PdnRegion.CreateEmpty();
  37. private PdnGraphicsPath selectionRedrawOutline = new PdnGraphicsPath();
  38. private DateTime lastFullInvalidate = DateTime.Now;
  39. protected override void OnVisibleChanged()
  40. {
  41. this.selectionTimer.Enabled = this.Visible;
  42. if (this.selection != null)
  43. {
  44. Rectangle rect = this.selection.GetBounds();
  45. Invalidate(rect);
  46. }
  47. }
  48. public override void OnDestinationSizeChanged()
  49. {
  50. lock (SyncRoot)
  51. {
  52. this.simplifiedRegionForTimer = null;
  53. }
  54. base.OnDestinationSizeChanged();
  55. }
  56. public override void OnSourceSizeChanged()
  57. {
  58. lock (SyncRoot)
  59. {
  60. this.simplifiedRegionForTimer = null;
  61. }
  62. base.OnSourceSizeChanged();
  63. }
  64. public bool EnableOutlineAnimation
  65. {
  66. get
  67. {
  68. return this.enableOutlineAnimation;
  69. }
  70. set
  71. {
  72. if (this.enableOutlineAnimation != value)
  73. {
  74. this.enableOutlineAnimation = value;
  75. Invalidate();
  76. }
  77. }
  78. }
  79. public bool InvertedTinting
  80. {
  81. get
  82. {
  83. return this.invertedTinting;
  84. }
  85. set
  86. {
  87. if (this.invertedTinting != value)
  88. {
  89. this.invertedTinting = value;
  90. Invalidate();
  91. }
  92. }
  93. }
  94. public Color TintColor
  95. {
  96. get
  97. {
  98. return this.tintColor;
  99. }
  100. set
  101. {
  102. if (value != this.tintColor)
  103. {
  104. this.tintColor = value;
  105. if (this.interiorBrush != null)
  106. {
  107. this.interiorBrush.Dispose();
  108. this.interiorBrush = null;
  109. }
  110. Invalidate();
  111. }
  112. }
  113. }
  114. private void OnSelectionChanging(object sender, EventArgs e)
  115. {
  116. this.render = false;
  117. if (!this.selectionTimer.Enabled)
  118. {
  119. this.selectionTimer.Enabled = true;
  120. }
  121. }
  122. private void OnSelectionChanged(object sender, EventArgs e)
  123. {
  124. this.render = true;
  125. PdnGraphicsPath path = this.selection.CreatePath(); //this.selection.GetPathReadOnly();
  126. if (this.selectedPath == null)
  127. {
  128. Invalidate();
  129. }
  130. else
  131. {
  132. this.selectedPath.Dispose(); //
  133. this.selectedPath = null;
  134. }
  135. bool fullInvalidate = false;
  136. this.selectedPath = path;
  137. // HACK: Sometimes the selection leaves behind artifacts. So do a full invalidate
  138. // every 1 second.
  139. if (this.selectedPath.PointCount > 10 && (DateTime.Now - lastFullInvalidate > new TimeSpan(0, 0, 0, 1, 0)))
  140. {
  141. fullInvalidate = true;
  142. }
  143. // if we're moving to a simpler selection region ...
  144. if (this.selectedPath == null)// || this.selectedPath.PointCount == 0)
  145. {
  146. // then invalidate everything
  147. fullInvalidate = true;
  148. }
  149. else
  150. {
  151. // otherwise, be intelligent about it and only redraw the 'new' area
  152. PdnRegion xorMe = new PdnRegion(this.selectedPath);
  153. selectionRedrawInterior.Xor(xorMe);
  154. xorMe.Dispose();
  155. }
  156. float ratio = 1.0f / (float)OwnerList.ScaleFactor.Ratio;
  157. int ratioInt = (int)Math.Ceiling(ratio);
  158. if (this.Visible && (this.EnableSelectionOutline || this.EnableSelectionTinting))
  159. {
  160. using (PdnRegion simplified = Utility.SimplifyAndInflateRegion(selectionRedrawInterior, Utility.DefaultSimplificationFactor, 2 * ratioInt))
  161. {
  162. Invalidate(simplified);
  163. }
  164. }
  165. if (fullInvalidate)
  166. {
  167. Rectangle rect = Rectangle.Inflate(Rectangle.Truncate(selectionRedrawOutline.GetBounds2()), 1, 1);
  168. Invalidate(rect);
  169. lastFullInvalidate = DateTime.Now;
  170. }
  171. this.selectionRedrawInterior.Dispose();
  172. this.selectionRedrawInterior = null;
  173. if (this.zoomInSelectedPath != null)
  174. {
  175. this.zoomInSelectedPath.Dispose();
  176. this.zoomInSelectedPath = null;
  177. }
  178. this.simplifiedRegionForTimer = null;
  179. // prepare for next call
  180. if (this.selectedPath != null && !this.selectedPath.IsEmpty)
  181. {
  182. this.selectionRedrawOutline = (PdnGraphicsPath)this.selectedPath.Clone();
  183. this.selectionRedrawInterior = new PdnRegion(this.selectedPath);
  184. }
  185. else
  186. {
  187. if (invertedTinting)
  188. {
  189. this.selectionRedrawInterior = new PdnRegion(new Rectangle(0, 0, this.SourceSize.Width, this.SourceSize.Height));
  190. }
  191. else
  192. {
  193. this.selectionRedrawInterior = new PdnRegion();
  194. this.selectionRedrawInterior.MakeEmpty();
  195. }
  196. Invalidate();
  197. this.selectionRedrawOutline = new PdnGraphicsPath();
  198. }
  199. }
  200. /// <summary>
  201. /// When we zoom in, we want to "stair-step" the selected path.
  202. /// </summary>
  203. /// <returns></returns>
  204. private PdnGraphicsPath GetZoomInPath()
  205. {
  206. lock (this.SyncRoot)
  207. {
  208. if (this.zoomInSelectedPath == null)
  209. {
  210. if (this.selectedPath == null)
  211. {
  212. this.zoomInSelectedPath = new PdnGraphicsPath();
  213. }
  214. else
  215. {
  216. this.zoomInSelectedPath = this.selection.CreatePixelatedPath();
  217. }
  218. }
  219. return this.zoomInSelectedPath;
  220. }
  221. }
  222. private PdnGraphicsPath GetAppropriateRenderPath()
  223. {
  224. if (OwnerList.ScaleFactor.Ratio >= 1.01)
  225. {
  226. return GetZoomInPath();
  227. }
  228. else
  229. {
  230. return this.selectedPath;
  231. }
  232. }
  233. private Timing timer = new Timing();
  234. private double renderTime = 0.0;
  235. public override bool ShouldRender()
  236. {
  237. return (this.render && (this.EnableSelectionOutline || this.EnableSelectionTinting));
  238. }
  239. public override void RenderToGraphics(Graphics g, Point offset)
  240. {
  241. double start = timer.GetTickCountDouble();
  242. lock (SyncRoot)
  243. {
  244. PdnGraphicsPath path = GetAppropriateRenderPath();
  245. if (path == null || path.IsEmpty)
  246. {
  247. this.render = false; // will be reset next time selection changes
  248. }
  249. else
  250. {
  251. g.TranslateTransform(-offset.X, -offset.Y);
  252. //System.Console.WriteLine(path.GetLastPoint().X);
  253. DrawSelection(g, path);
  254. }
  255. double end = timer.GetTickCountDouble();
  256. this.renderTime += (end - start);
  257. }
  258. }
  259. public SelectionRenderer(SurfaceBoxRendererList ownerList, Selection selection)
  260. : this(ownerList, selection, null)
  261. {
  262. }
  263. public SelectionRenderer(SurfaceBoxRendererList ownerList, Selection selection, UserControl2 ownerControl)
  264. : base(ownerList)
  265. {
  266. this.ownerControl = ownerControl;
  267. this.selection = selection;
  268. this.selection.Changing += new EventHandler(OnSelectionChanging);
  269. this.selection.Changed += new EventHandler(OnSelectionChanged);
  270. this.components = new System.ComponentModel.Container();
  271. this.selectionTimer = new System.Windows.Forms.Timer(this.components);
  272. this.selectionTimer.Enabled = true;
  273. this.selectionTimer.Interval = dancingAntsInterval / 2;
  274. this.selectionTimer.Tick += new System.EventHandler(this.SelectionTimer_Tick);
  275. }
  276. protected override void Dispose(bool disposing)
  277. {
  278. if (disposing)
  279. {
  280. if (this.components != null)
  281. {
  282. this.components.Dispose();
  283. this.components = null;
  284. }
  285. if (this.selectionTimer != null)
  286. {
  287. this.selectionTimer.Dispose();
  288. this.selectionTimer = null;
  289. }
  290. if (this.zoomInSelectedPath != null)
  291. {
  292. this.zoomInSelectedPath.Dispose();
  293. this.zoomInSelectedPath = null;
  294. }
  295. }
  296. base.Dispose(disposing);
  297. }
  298. private Brush interiorBrush;
  299. private Brush InteriorBrush
  300. {
  301. get
  302. {
  303. if (interiorBrush == null)
  304. {
  305. interiorBrush = new SolidBrush(tintColor);
  306. }
  307. return interiorBrush;
  308. }
  309. }
  310. private bool enableSelectionOutline = true;
  311. public bool EnableSelectionOutline
  312. {
  313. get
  314. {
  315. return enableSelectionOutline;
  316. }
  317. set
  318. {
  319. if (this.enableSelectionOutline != value)
  320. {
  321. enableSelectionOutline = value;
  322. Invalidate();
  323. }
  324. }
  325. }
  326. private bool enableSelectionTinting = true;
  327. public bool EnableSelectionTinting
  328. {
  329. get
  330. {
  331. return enableSelectionTinting;
  332. }
  333. set
  334. {
  335. if (enableSelectionTinting != value)
  336. {
  337. enableSelectionTinting = value;
  338. Invalidate();
  339. }
  340. }
  341. }
  342. /// <summary>
  343. /// This is a silly function name.
  344. /// </summary>
  345. public void ResetOutlineWhiteOpacity()
  346. {
  347. if (this.whiteOpacity > 0)
  348. {
  349. Invalidate();
  350. }
  351. this.whiteOpacity = 0;
  352. }
  353. private void DrawSelectionOutline(Graphics g, PdnGraphicsPath outline)
  354. {
  355. if (outline == null)
  356. {
  357. return;
  358. }
  359. if (outlinePen1 == null)
  360. {
  361. outlinePen1 = new Pen(Color.FromArgb(160, Color.Black), 1.0f);
  362. outlinePen1.Alignment = PenAlignment.Outset;
  363. outlinePen1.LineJoin = LineJoin.Bevel;
  364. outlinePen1.Width = -1;
  365. }
  366. if (outlinePen2 == null)
  367. {
  368. outlinePen2 = new Pen(Color.White, 1.0f);
  369. outlinePen2.Alignment = PenAlignment.Outset;
  370. outlinePen2.LineJoin = LineJoin.Bevel;
  371. outlinePen2.MiterLimit = 2;
  372. outlinePen2.Width = -1;
  373. outlinePen2.DashStyle = DashStyle.Dash;
  374. outlinePen2.DashPattern = new float[] { 4, 4 };
  375. outlinePen2.Color = Color.White;
  376. outlinePen2.DashOffset = 4.0f;
  377. }
  378. PixelOffsetMode oldPOM = g.PixelOffsetMode;
  379. g.PixelOffsetMode = PixelOffsetMode.None;
  380. SmoothingMode oldSM = g.SmoothingMode;
  381. g.SmoothingMode = SmoothingMode.AntiAlias;
  382. outline.Draw(g, outlinePen1);
  383. float offset = (float)((double)dancingAntsT / OwnerList.ScaleFactor.Ratio);
  384. outlinePen2.DashOffset += offset;
  385. if (whiteOpacity != 0)
  386. {
  387. outlinePen2.Color = Color.FromArgb(whiteOpacity, Color.White);
  388. outline.Draw(g, outlinePen2);
  389. }
  390. outlinePen2.DashOffset -= offset;
  391. g.SmoothingMode = oldSM;
  392. g.PixelOffsetMode = oldPOM;
  393. }
  394. private void DrawSelectionTinting(Graphics g, PdnGraphicsPath outline)
  395. {
  396. if (outline == null)
  397. {
  398. return;
  399. }
  400. CompositingMode oldCM = g.CompositingMode;
  401. g.CompositingMode = CompositingMode.SourceOver;
  402. SmoothingMode oldSM = g.SmoothingMode;
  403. g.SmoothingMode = SmoothingMode.AntiAlias;
  404. PixelOffsetMode oldPOM = g.PixelOffsetMode;
  405. g.PixelOffsetMode = PixelOffsetMode.None;
  406. Region oldClipRegion = null;
  407. RectangleF outlineBounds = outline.GetBounds();
  408. if (outlineBounds.Left < 0 ||
  409. outlineBounds.Top < 0 ||
  410. outlineBounds.Right >= this.SourceSize.Width ||
  411. outlineBounds.Bottom >= this.SourceSize.Height)
  412. {
  413. oldClipRegion = g.Clip;
  414. Region newClipRegion = oldClipRegion.Clone();
  415. newClipRegion.Intersect(new Rectangle(0, 0, this.SourceSize.Width, this.SourceSize.Height));
  416. g.Clip = newClipRegion;
  417. newClipRegion.Dispose();
  418. }
  419. g.FillPath(InteriorBrush, outline);
  420. if (oldClipRegion != null)
  421. {
  422. g.Clip = oldClipRegion;
  423. oldClipRegion.Dispose();
  424. }
  425. g.PixelOffsetMode = oldPOM;
  426. g.SmoothingMode = oldSM;
  427. g.CompositingMode = oldCM;
  428. }
  429. private void DrawSelection(Graphics gdiG, PdnGraphicsPath outline)
  430. {
  431. if (outline == null)
  432. {
  433. return;
  434. }
  435. float ratio = (float)OwnerList.ScaleFactor.Ratio;
  436. gdiG.ScaleTransform(ratio, ratio);
  437. if (EnableSelectionTinting)
  438. {
  439. PdnGraphicsPath outline2;
  440. if (invertedTinting)
  441. {
  442. outline2 = (PdnGraphicsPath)outline.Clone();
  443. outline2.AddRectangle(new Rectangle(-1, -1, this.SourceSize.Width + 1, this.SourceSize.Height + 1));
  444. outline2.CloseAllFigures();
  445. }
  446. else
  447. {
  448. outline2 = outline;
  449. }
  450. DrawSelectionTinting(gdiG, outline2);
  451. if (invertedTinting)
  452. {
  453. outline2.Dispose();
  454. }
  455. }
  456. if (EnableSelectionOutline)
  457. {
  458. DrawSelectionOutline(gdiG, outline);
  459. }
  460. gdiG.ScaleTransform(1 / ratio, 1 / ratio);
  461. }
  462. private void SelectionTimer_Tick(object sender, System.EventArgs e)
  463. {
  464. if (this.IsDisposed || this.ownerControl.IsDisposed)
  465. {
  466. return;
  467. }
  468. if (this.selectedPath == null || this.selectedPath.IsEmpty)
  469. {
  470. this.selectionTimer.Enabled = false;
  471. return;
  472. }
  473. if (!this.enableOutlineAnimation)
  474. {
  475. return;
  476. }
  477. if (this.timer.GetTickCountDouble() < this.coolOffTimeTickCount)
  478. {
  479. return;
  480. }
  481. if (this.ownerControl != null && this.ownerControl.IsMouseCaptured())
  482. {
  483. return;
  484. }
  485. Form form = this.ownerControl.FindForm();
  486. if (form != null && form.WindowState == FormWindowState.Minimized)
  487. {
  488. return;
  489. }
  490. int presentTickMod = (int)((Utility.GetTimeMs() / dancingAntsInterval) % 2);
  491. if (presentTickMod != lastTickMod)
  492. {
  493. lastTickMod = presentTickMod;
  494. dancingAntsT = unchecked(dancingAntsT + 1);
  495. if (this.simplifiedRegionForTimer == null)
  496. {
  497. using (PdnGraphicsPath invalidPath = (PdnGraphicsPath)selectedPath.Clone())
  498. {
  499. invalidPath.CloseAllFigures();
  500. float ratio = 1.0f / (float)OwnerList.ScaleFactor.Ratio;
  501. int inflateAmount = (int)Math.Ceiling(ratio);
  502. this.simplifiedRegionForTimer = Utility.SimplifyTrace(invalidPath, 50);
  503. Utility.InflateRectanglesInPlace(this.simplifiedRegionForTimer, inflateAmount);
  504. }
  505. }
  506. try
  507. {
  508. foreach (Rectangle rect in this.simplifiedRegionForTimer)
  509. {
  510. Invalidate(rect);
  511. }
  512. }
  513. catch (ObjectDisposedException)
  514. {
  515. try
  516. {
  517. this.selectionTimer.Enabled = false;
  518. }
  519. catch (Exception)
  520. {
  521. // Ignore error
  522. }
  523. }
  524. if (this.ownerControl == null || (this.ownerControl != null && !this.ownerControl.IsMouseCaptured()))
  525. {
  526. whiteOpacity = Math.Min(whiteOpacity + 16, 255);
  527. }
  528. }
  529. // If it takes "too long" to render the dancing ants, then we institute
  530. // a cooling-off period during which we will not render the ants.
  531. // This will curb the CPU usage by a few percent, which will avoid us
  532. // monopolizing the CPU.
  533. double maxRenderTime = (double)dancingAntsInterval * maxCpuTime;
  534. if (renderTime > maxRenderTime)
  535. {
  536. double coolOffTime = renderTime / maxRenderTime;
  537. this.coolOffTimeTickCount = timer.GetTickCountDouble() + coolOffTime;
  538. }
  539. this.renderTime = 0.0;
  540. }
  541. }
  542. }