Blame mono/Assistance/Canvas.cs

ebcc4f
using System;
ebcc4f
using System.Collections.Generic;
ebcc4f
ebcc4f
namespace Assistance {
ebcc4f
	public class Canvas {
b1ff53
		public static readonly int initialSize = 100;
b1ff53
		public static readonly double incrementScale = 1.2;
b1ff53
	
b82ef4
		private int offsetX = -initialSize/2;
b82ef4
		private int offsetY = -initialSize/2;
b82ef4
		private Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, initialSize, initialSize);
b1ff53
b82ef4
		public void draw(Cairo.Context context) {
b82ef4
			context.Save();
b82ef4
			context.Translate(offsetX, offsetY);
b82ef4
			context.SetSource(surface);
b82ef4
			context.Paint();
b82ef4
			context.Restore();
ebcc4f
		}
ebcc4f
b1ff53
		public void expand(Rectangle rect) {
b82ef4
			int l = offsetX;
b82ef4
			int t = offsetY;
b82ef4
			int r = l + surface.Width;
b82ef4
			int b = t + surface.Height;
b82ef4
			
b82ef4
			int rl = (int)Math.Floor(rect.x0);
b82ef4
			int rt = (int)Math.Floor(rect.y0);
b82ef4
			int rr = Math.Max(rl, (int)Math.Ceiling(rect.x1));
b82ef4
			int rb = Math.Max(rt, (int)Math.Ceiling(rect.y1));
b1ff53
		    
b82ef4
		    int incX = (int)Math.Ceiling(surface.Width*incrementScale);
b82ef4
		    int incY = (int)Math.Ceiling(surface.Height*incrementScale);
b1ff53
b82ef4
		    if (rl < l) l = rl - incX;
b82ef4
		    if (rt < t) t = rt - incY;
b82ef4
		    if (rr > r) r = rr + incX;
b82ef4
		    if (rb > b) b = rb + incY;
b1ff53
		    
b82ef4
		    int w = r - l;
b82ef4
		    int h = b - t;
b82ef4
		    if (l != offsetX || t != offsetY || w != surface.Width || h != surface.Height) {
b82ef4
				Cairo.ImageSurface newSurface = new Cairo.ImageSurface(Cairo.Format.ARGB32, w, h);
b82ef4
		    	Cairo.Context context = new Cairo.Context(newSurface);
b82ef4
		    	context.Translate(offsetX - l, offsetY - t);
b82ef4
		    	context.SetSource(surface);
b82ef4
		    	context.Paint();
b82ef4
				context.GetTarget().Flush();
b82ef4
				context.Dispose();
b82ef4
				surface.Dispose();
b1ff53
				
b82ef4
				offsetX = l;
b82ef4
				offsetY = t;
b82ef4
				surface = newSurface;
b1ff53
			}
ebcc4f
		}
ebcc4f
b82ef4
		private Cairo.Context getContext() {
b82ef4
			Cairo.Context context = new Cairo.Context(surface);
b82ef4
			context.Antialias = Cairo.Antialias.Gray;
b82ef4
			context.Translate(-offsetX, -offsetY);
b82ef4
			return context;
ebcc4f
		}
ebcc4f
b1ff53
		public void paintTrack(Track track) {
b1ff53
			expand(track.getBounds());
b82ef4
			Cairo.Context context = getContext();
b82ef4
			track.draw(context);
b82ef4
			context.GetTarget().Flush();
b82ef4
			context.Dispose();
ebcc4f
		}
ebcc4f
	}
ebcc4f
}
ebcc4f