Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Drawing;
- namespace Draw
- {
- public partial class DrawPanel : Control
- {
- IDrawPanelClient client;
- SelectedObject defaultClient = new SelectedObject();
- private DrawDocument doc;
- Point lastMouse;
- public DrawCreator creator;
- public DrawForm mainForm;
- private int savedDocSize = 0;
- Stack<List<DrawObject>> actionStack = new Stack<List<DrawObject>>();
- Stack<List<DrawObject>> redoStack = new Stack<List<DrawObject>>();
- public DrawPanel()
- {
- InitializeComponent();
- client = defaultClient;
- }
- public DrawForm MainForm
- {
- get
- {
- return mainForm;
- }
- set
- {
- mainForm = value;
- }
- }
- public DrawDocument Doc
- {
- get
- {
- return doc;
- }
- set
- {
- doc = value;
- client.Clear();
- Invalidate();
- }
- }
- public DrawPanel(IContainer container)
- {
- container.Add(this);
- InitializeComponent();
- client = defaultClient;
- }
- private void DrawPanel_MouseDown(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- if (creator != null)
- {
- if (creator.canCreate(e.Location, doc.List))
- {
- creator.create(e.Location);
- client.Clear();
- }
- this.Invalidate();
- }
- else if ((Control.ModifierKeys & Keys.Shift) == 0)
- {
- // single select
- DrawObject obj = doc.Find(e.Location);
- if (obj != null)
- {
- if (!client.Contains(obj))
- client.Item = obj;
- }
- else
- client.Clear();
- client.BeginDrag(e.Location);
- this.Invalidate();
- }
- else
- {
- //multiple select
- DrawObject obj = doc.Find(e.Location);
- if (obj != null)
- {
- if (client.Contains(obj))
- client.Remove(obj);
- else
- {
- client.Add(obj);
- }
- }
- client.BeginDrag(e.Location);
- this.Invalidate();
- }
- }
- lastMouse = e.Location;
- }
- private void DrawPanel_MouseMove(object sender, MouseEventArgs e)
- {
- Size delta = new Size(e.Location.X - lastMouse.X, e.Location.Y - lastMouse.Y);
- if (e.Button == System.Windows.Forms.MouseButtons.Left)
- {
- if (creator != null)
- {
- creator.Drag(e.Location, delta, Doc.List);
- Invalidate();
- }
- else
- {
- client.Drag(delta);
- Invalidate();
- }
- }
- else if (creator != null)
- {
- creator.canCreate(e.Location, Doc.List);
- Invalidate();
- }
- lastMouse = e.Location;
- }
- private void DrawPanel_MouseUp(object sender, MouseEventArgs e)
- {
- Size delta = new Size(e.Location.X - lastMouse.X, e.Location.Y - lastMouse.Y);
- if (creator != null)
- {
- if (creator.canFinish(e.Location, Doc.List))
- {
- DrawObject finishedObject = creator.FinishedObject;
- Doc.Add(finishedObject);
- client.Item = finishedObject;
- }
- creator = null;
- Invalidate();
- }
- else
- {
- client.EndDrag(e.Location);
- Invalidate();
- }
- lastMouse = e.Location;
- SaveAction();
- }
- private void DrawPanel_Paint(object sender, PaintEventArgs e)
- {
- if (doc != null)
- {
- doc.Paint(e.Graphics);
- client.Paint(e.Graphics);
- if (creator != null)
- creator.Paint(e.Graphics);
- }
- }
- internal void Group()
- {
- if (client.CanGroup())
- {
- DrawGroup group = new DrawGroup(new Rectangle(0, 0, 0, 0));
- group.Add(client.List);
- doc.Remove(client.List);
- client.Item = group;
- doc.Add(group);
- creator = null;
- Invalidate();
- SaveAction();
- }
- }
- internal void UnGroup()
- {
- if (client.CanUngroup())
- {
- DrawGroup group = (DrawGroup)client.Item;
- doc.Remove(group);
- doc.Add(group.List);
- client.List = group.List;
- creator = null;
- this.Invalidate();
- SaveAction();
- }
- }
- public void Cut()
- {
- doc.Remove(client.List);
- Invalidate();
- client.Cut();
- SaveAction();
- }
- public void Delete()
- {
- doc.Remove(client.List);
- Invalidate();
- client.Clear();
- SaveAction();
- }
- internal void Copy()
- {
- client.Copy();
- }
- internal void Paste()
- {
- if (client.Paste())
- {
- client.Drag(new Size(10, 10));
- doc.Add(client.List);
- Invalidate();
- SaveAction();
- }
- }
- public void SelectAll()
- {
- foreach (DrawObject x in doc.List)
- {
- defaultClient.Add(x);
- }
- Invalidate();
- }
- public void Print()
- {
- }
- public void TestBalls()
- {
- Debug.WriteLine("Client list : " + client.List.Count);
- Debug.WriteLine("Doc list : " + doc.List.Count);
- }
- public void Undo()
- {
- List<DrawObject> gather = new List<DrawObject>();
- if (actionStack.Count == 0)
- Debug.WriteLine("Cannot be done");
- else
- {
- if (doc.List != null && actionStack.Count > 1)
- {
- doc.RemoveAll();
- client.Clear();
- redoStack.Push(actionStack.Pop());
- foreach (DrawObject x in actionStack.Peek())
- {
- doc.Add(x);
- }
- Invalidate();
- }
- }
- }
- public void Redo()
- {
- if (redoStack.Count > 0)
- {
- doc.RemoveAll();
- foreach (DrawObject x in redoStack.Pop())
- {
- doc.Add(x);
- }
- Invalidate();
- SaveAction();
- }
- }
- public void SaveAction()
- {
- List<DrawObject> gather = new List<DrawObject>();
- if (doc.List.Count > 0)
- {
- foreach (DrawObject x in doc.List)
- {
- gather.Add(x);
- }
- actionStack.Push(gather);
- }
- else
- actionStack.Push(gather);
- }
- }
- }
Add Comment
Please, Sign In to add comment