Guest User

Untitled

a guest
May 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Drawing;
  9.  
  10. namespace Draw
  11. {
  12. public partial class DrawPanel : Control
  13. {
  14. IDrawPanelClient client;
  15. SelectedObject defaultClient = new SelectedObject();
  16. private DrawDocument doc;
  17. Point lastMouse;
  18. public DrawCreator creator;
  19. public DrawForm mainForm;
  20. private int savedDocSize = 0;
  21.  
  22. Stack<List<DrawObject>> actionStack = new Stack<List<DrawObject>>();
  23. Stack<List<DrawObject>> redoStack = new Stack<List<DrawObject>>();
  24.  
  25. public DrawPanel()
  26. {
  27. InitializeComponent();
  28. client = defaultClient;
  29.  
  30. }
  31.  
  32. public DrawForm MainForm
  33. {
  34. get
  35. {
  36. return mainForm;
  37. }
  38. set
  39. {
  40. mainForm = value;
  41. }
  42. }
  43.  
  44. public DrawDocument Doc
  45. {
  46. get
  47. {
  48. return doc;
  49. }
  50. set
  51. {
  52. doc = value;
  53. client.Clear();
  54. Invalidate();
  55.  
  56. }
  57. }
  58.  
  59. public DrawPanel(IContainer container)
  60. {
  61. container.Add(this);
  62. InitializeComponent();
  63. client = defaultClient;
  64. }
  65.  
  66. private void DrawPanel_MouseDown(object sender, MouseEventArgs e)
  67. {
  68. if (e.Button == MouseButtons.Left)
  69. {
  70. if (creator != null)
  71. {
  72. if (creator.canCreate(e.Location, doc.List))
  73. {
  74. creator.create(e.Location);
  75. client.Clear();
  76.  
  77.  
  78.  
  79.  
  80.  
  81. }
  82. this.Invalidate();
  83. }
  84. else if ((Control.ModifierKeys & Keys.Shift) == 0)
  85. {
  86. // single select
  87. DrawObject obj = doc.Find(e.Location);
  88. if (obj != null)
  89. {
  90. if (!client.Contains(obj))
  91. client.Item = obj;
  92. }
  93. else
  94. client.Clear();
  95. client.BeginDrag(e.Location);
  96. this.Invalidate();
  97. }
  98. else
  99. {
  100. //multiple select
  101. DrawObject obj = doc.Find(e.Location);
  102. if (obj != null)
  103. {
  104. if (client.Contains(obj))
  105. client.Remove(obj);
  106. else
  107. {
  108. client.Add(obj);
  109. }
  110. }
  111. client.BeginDrag(e.Location);
  112. this.Invalidate();
  113. }
  114. }
  115. lastMouse = e.Location;
  116. }
  117.  
  118. private void DrawPanel_MouseMove(object sender, MouseEventArgs e)
  119. {
  120. Size delta = new Size(e.Location.X - lastMouse.X, e.Location.Y - lastMouse.Y);
  121. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  122. {
  123. if (creator != null)
  124. {
  125. creator.Drag(e.Location, delta, Doc.List);
  126. Invalidate();
  127. }
  128. else
  129. {
  130. client.Drag(delta);
  131. Invalidate();
  132. }
  133. }
  134. else if (creator != null)
  135. {
  136. creator.canCreate(e.Location, Doc.List);
  137. Invalidate();
  138. }
  139. lastMouse = e.Location;
  140. }
  141.  
  142. private void DrawPanel_MouseUp(object sender, MouseEventArgs e)
  143. {
  144. Size delta = new Size(e.Location.X - lastMouse.X, e.Location.Y - lastMouse.Y);
  145. if (creator != null)
  146. {
  147. if (creator.canFinish(e.Location, Doc.List))
  148. {
  149. DrawObject finishedObject = creator.FinishedObject;
  150. Doc.Add(finishedObject);
  151. client.Item = finishedObject;
  152. }
  153. creator = null;
  154. Invalidate();
  155. }
  156. else
  157. {
  158. client.EndDrag(e.Location);
  159. Invalidate();
  160. }
  161. lastMouse = e.Location;
  162. SaveAction();
  163. }
  164.  
  165. private void DrawPanel_Paint(object sender, PaintEventArgs e)
  166. {
  167. if (doc != null)
  168. {
  169. doc.Paint(e.Graphics);
  170. client.Paint(e.Graphics);
  171. if (creator != null)
  172. creator.Paint(e.Graphics);
  173. }
  174. }
  175.  
  176. internal void Group()
  177. {
  178. if (client.CanGroup())
  179. {
  180. DrawGroup group = new DrawGroup(new Rectangle(0, 0, 0, 0));
  181. group.Add(client.List);
  182. doc.Remove(client.List);
  183. client.Item = group;
  184. doc.Add(group);
  185. creator = null;
  186. Invalidate();
  187. SaveAction();
  188. }
  189.  
  190. }
  191.  
  192. internal void UnGroup()
  193. {
  194. if (client.CanUngroup())
  195. {
  196. DrawGroup group = (DrawGroup)client.Item;
  197. doc.Remove(group);
  198. doc.Add(group.List);
  199. client.List = group.List;
  200. creator = null;
  201. this.Invalidate();
  202. SaveAction();
  203. }
  204. }
  205.  
  206. public void Cut()
  207. {
  208. doc.Remove(client.List);
  209. Invalidate();
  210. client.Cut();
  211. SaveAction();
  212. }
  213.  
  214. public void Delete()
  215. {
  216. doc.Remove(client.List);
  217. Invalidate();
  218. client.Clear();
  219. SaveAction();
  220. }
  221.  
  222. internal void Copy()
  223. {
  224. client.Copy();
  225. }
  226.  
  227. internal void Paste()
  228. {
  229. if (client.Paste())
  230. {
  231. client.Drag(new Size(10, 10));
  232. doc.Add(client.List);
  233. Invalidate();
  234. SaveAction();
  235. }
  236. }
  237.  
  238. public void SelectAll()
  239. {
  240. foreach (DrawObject x in doc.List)
  241. {
  242. defaultClient.Add(x);
  243. }
  244. Invalidate();
  245. }
  246.  
  247. public void Print()
  248. {
  249. }
  250.  
  251. public void TestBalls()
  252. {
  253. Debug.WriteLine("Client list : " + client.List.Count);
  254. Debug.WriteLine("Doc list : " + doc.List.Count);
  255. }
  256.  
  257. public void Undo()
  258. {
  259. List<DrawObject> gather = new List<DrawObject>();
  260. if (actionStack.Count == 0)
  261. Debug.WriteLine("Cannot be done");
  262. else
  263. {
  264. if (doc.List != null && actionStack.Count > 1)
  265. {
  266. doc.RemoveAll();
  267. client.Clear();
  268. redoStack.Push(actionStack.Pop());
  269.  
  270. foreach (DrawObject x in actionStack.Peek())
  271. {
  272. doc.Add(x);
  273. }
  274.  
  275. Invalidate();
  276. }
  277. }
  278.  
  279. }
  280.  
  281. public void Redo()
  282. {
  283.  
  284. if (redoStack.Count > 0)
  285. {
  286. doc.RemoveAll();
  287. foreach (DrawObject x in redoStack.Pop())
  288. {
  289. doc.Add(x);
  290. }
  291. Invalidate();
  292. SaveAction();
  293. }
  294.  
  295. }
  296.  
  297. public void SaveAction()
  298. {
  299.  
  300. List<DrawObject> gather = new List<DrawObject>();
  301.  
  302. if (doc.List.Count > 0)
  303. {
  304. foreach (DrawObject x in doc.List)
  305. {
  306. gather.Add(x);
  307. }
  308. actionStack.Push(gather);
  309. }
  310.  
  311. else
  312. actionStack.Push(gather);
  313. }
  314. }
  315. }
Add Comment
Please, Sign In to add comment