Blame mono/Assistance/ActivePoint.cs

ebcc4f
using System;
ebcc4f
using System.Collections.Generic;
b82ef4
using Assistance.Drawing;
ebcc4f
ebcc4f
namespace Assistance {
ebcc4f
	public class ActivePoint {
ebcc4f
		public enum Type {
ebcc4f
			Circle,
ebcc4f
			CircleFill,
ebcc4f
			CircleCross,
ebcc4f
		};
ebcc4f
72b17c
		public class Owner {
726e8a
			public readonly Document document;
72b17c
			public readonly List<activepoint> points = new List<activepoint>();</activepoint></activepoint>
72b17c
			
726e8a
			public Owner(Document document) { this.document = document; }
72b17c
			public virtual void onMovePoint(ActivePoint point, Point position) { point.position = position; }
72b17c
			public virtual void bringToFront() { }
726e8a
			public virtual void remove() { foreach(ActivePoint point in points) document.points.Remove(point); }
72b17c
		}
72b17c
ebcc4f
		public static readonly double radius = 10.0;
ebcc4f
		public static readonly double crossSize = 1.2*radius;
b82ef4
		public static readonly Pen pen = new Pen("Gray");
b82ef4
		public static readonly Brush brush = new Brush("Light Gray");
b82ef4
		public static readonly Pen penActive = new Pen("Blue");
b82ef4
		public static readonly Brush brushActive = new Brush("Light Blue");
ebcc4f
726e8a
		public readonly Document document;
72b17c
		public readonly Owner owner;
ebcc4f
		public readonly Type type;
ebcc4f
		public Point position;
ebcc4f
72b17c
		public ActivePoint(Owner owner, Type type, Point position = new Point()) {
726e8a
			this.document = owner.document;
72b17c
			this.owner = owner;
ebcc4f
			this.type = type;
ebcc4f
			this.position = position;
726e8a
			document.points.Add(this);
72b17c
			owner.points.Add(this);
ebcc4f
		}
ebcc4f
ebcc4f
		public bool isInside(Point p) {
ebcc4f
			return (position - p).lenSqr() <= radius*radius;
ebcc4f
		}
ebcc4f
ebcc4f
		public void bringToFront() {
72b17c
			owner.bringToFront();
72b17c
			owner.points.Remove(this);
72b17c
			owner.points.Add(this);
726e8a
			document.points.Remove(this);
726e8a
			document.points.Add(this);
ebcc4f
		}
ebcc4f
38ad69
		private Pen getPen(bool active) {
38ad69
			return active ? penActive : pen;
38ad69
		}
38ad69
38ad69
		private Brush getBrush(bool active) {
38ad69
			return active ? brushActive : brush;
38ad69
		}
38ad69
b82ef4
		private void drawCircle(Cairo.Context context, bool active) {
b82ef4
			context.Save();
b82ef4
			getPen(active).apply(context);
b82ef4
			context.Arc(position.x, position.y, radius, 0.0, 2.0*Math.PI);
b82ef4
			context.Stroke();
b82ef4
			context.Restore();
ebcc4f
		}
ebcc4f
b82ef4
		private void fillCircle(Cairo.Context context, bool active) {
b82ef4
			context.Save();
b82ef4
			getBrush(active).apply(context);
b82ef4
			context.Arc(position.x, position.y, radius, 0.0, 2.0*Math.PI);
b82ef4
			context.Fill();
b82ef4
			context.Restore();
ebcc4f
		}
ebcc4f
b82ef4
		private void drawCross(Cairo.Context context, bool active) {
b82ef4
			context.Save();
b82ef4
			getPen(active).apply(context);
b82ef4
			context.MoveTo(position.x - crossSize, position.y);
b82ef4
			context.LineTo(position.x + crossSize, position.y);
b82ef4
			context.Stroke();
b82ef4
			context.MoveTo(position.x, position.y - crossSize);
b82ef4
			context.LineTo(position.x, position.y + crossSize);
b82ef4
			context.Stroke();
b82ef4
			context.Restore();
ebcc4f
		}
ebcc4f
b82ef4
		public void draw(Cairo.Context context, bool active = false) {
ebcc4f
			switch(type) {
ebcc4f
			case Type.Circle:
b82ef4
				drawCircle(context, active);
ebcc4f
				break;
ebcc4f
			case Type.CircleFill:
b82ef4
				fillCircle(context, active);
b82ef4
				drawCircle(context, active);
ebcc4f
				break;
ebcc4f
			case Type.CircleCross:
b82ef4
				drawCircle(context, active);
b82ef4
				drawCross(context, active);
ebcc4f
				break;
ebcc4f
			}
ebcc4f
		}
ebcc4f
	}
ebcc4f
}
ebcc4f