Blame mono/Assistance/MainWindow.cs

ebcc4f
using System;
ebcc4f
using System.Drawing;
ebcc4f
using System.Drawing.Imaging;
ebcc4f
using System.Windows.Forms;
ebcc4f
using System.Collections.Generic;
ebcc4f
ebcc4f
namespace Assistance {
ebcc4f
    public class MainWindow : Form {
ebcc4f
        static public void Main() { Application.Run(new MainWindow()); }
ebcc4f
ebcc4f
		Bitmap bitmap = new Bitmap(1, 1);
b1ff53
		Workarea workarea = new Workarea();
ebcc4f
		bool dragging = false;
ebcc4f
		ActivePoint activePoint;
ebcc4f
		Point offset;
ebcc4f
		Point cursor;
b1ff53
		
b1ff53
		Track track = null;
ebcc4f
ebcc4f
		public MainWindow() {
ebcc4f
			Paint += onPaint;
ebcc4f
			MouseMove += onMouseMove;
ebcc4f
			MouseDown += onMouseDown;
ebcc4f
			MouseUp += onMouseUp;
ebcc4f
			KeyDown += onKeyDown;
ebcc4f
            WindowState = FormWindowState.Maximized;
ebcc4f
        }
ebcc4f
ebcc4f
		protected override void OnPaintBackground(PaintEventArgs e) { }
ebcc4f
ebcc4f
        public void onPaint(Object sender, PaintEventArgs e) {
ebcc4f
			if (bitmap.Size != ClientSize)
ebcc4f
				bitmap = new Bitmap(ClientSize.Width, ClientSize.Height);
ebcc4f
			Graphics g = Graphics.FromImage(bitmap);
ebcc4f
			g.Clear(Color.White);
ebcc4f
            draw(g);
ebcc4f
			g.Flush();
b1ff53
			e.Graphics.DrawImageUnscaled(bitmap, 0, 0);
ebcc4f
        }
ebcc4f
b1ff53
		public Point windowToWorkarea(Point p) {
ebcc4f
			return new Point(p.x - ClientSize.Width/2.0, p.y - ClientSize.Height/2.0);
ebcc4f
		}
ebcc4f
b1ff53
		public Point workareaToWindow(Point p) {
ebcc4f
			return new Point(p.x + ClientSize.Width/2.0, p.y + ClientSize.Height/2.0);
ebcc4f
		}
ebcc4f
ebcc4f
		private void beginDrag() {
b1ff53
			endDragAndTrack();
ebcc4f
			dragging = true;
ebcc4f
			offset = activePoint.position - cursor;
ebcc4f
			activePoint.bringToFront();
ebcc4f
		}
ebcc4f
b1ff53
		private void beginTrack() {
b1ff53
			endDragAndTrack();
b1ff53
			track = new Track();
b1ff53
		}
b1ff53
b1ff53
		private void endDragAndTrack() {
ebcc4f
			dragging = false;
ebcc4f
			offset = new Point();
b1ff53
			
b1ff53
			if (track != null)
b1ff53
				workarea.paintTrack(track);
b1ff53
			track = null;
ebcc4f
		}
ebcc4f
ebcc4f
		public void onKeyDown(Object sender, KeyEventArgs e) {
ebcc4f
			switch(e.KeyCode) {
ebcc4f
			case Keys.D1:
38ad69
				new AssistantVanishingPoint(workarea, cursor);
ebcc4f
				break;
ebcc4f
			case Keys.D2:
38ad69
				new AssistantGrid(workarea, cursor);
ebcc4f
				break;
ebcc4f
			case Keys.Delete:
ebcc4f
				if (activePoint != null)
ebcc4f
					activePoint.assistant.remove();
b1ff53
				endDragAndTrack();
ebcc4f
				break;
ebcc4f
			}
b1ff53
			endDragAndTrack();
ebcc4f
			Invalidate();
ebcc4f
		}
ebcc4f
ebcc4f
		public void onMouseDown(Object sender, MouseEventArgs e) {
b1ff53
			cursor = windowToWorkarea(new Point(e.Location.X, e.Location.Y));
ebcc4f
			if (e.Button == MouseButtons.Left) {
b1ff53
				activePoint = workarea.findPoint(cursor);
b1ff53
				if (activePoint != null) {
ebcc4f
					beginDrag();
b1ff53
				} else {
b1ff53
					beginTrack();
b1ff53
					track.points.Add(cursor);
b1ff53
				}
ebcc4f
			}
ebcc4f
			Invalidate();
ebcc4f
		}
ebcc4f
ebcc4f
		public void onMouseUp(Object sender, MouseEventArgs e) {
b1ff53
			cursor = windowToWorkarea(new Point(e.X, e.Y));
ebcc4f
			if (e.Button == MouseButtons.Left)
b1ff53
				endDragAndTrack();
b1ff53
			if (!dragging && track == null)
b1ff53
				activePoint = workarea.findPoint(cursor);
ebcc4f
			Invalidate();
ebcc4f
		}
ebcc4f
ebcc4f
		public void onMouseMove(Object sender, MouseEventArgs e) {
b1ff53
			cursor = windowToWorkarea(new Point(e.Location.X, e.Location.Y));
ebcc4f
			if (dragging) {
ebcc4f
				activePoint.assistant.onMovePoint(activePoint, cursor + offset);
b1ff53
			} else
b1ff53
			if (track != null) {
b1ff53
				track.points.Add(cursor);
ebcc4f
			} else {
b1ff53
				activePoint = workarea.findPoint(cursor);
ebcc4f
			}
ebcc4f
			Invalidate();
ebcc4f
        }
ebcc4f
ebcc4f
        public void draw(Graphics g) {
ebcc4f
			g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
b1ff53
			g.TranslateTransform(ClientSize.Width/2, ClientSize.Height/2);
b1ff53
			workarea.draw(g, activePoint, cursor + offset, track);
ebcc4f
        }
ebcc4f
    }
ebcc4f
}