Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

GraphicsCopy.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.senocular.display {
	
	import flash.display.Graphics;
	import flash.utils.flash_proxy;
	import flash.utils.Proxy;
	
	/**
	 * a graphics property substitute for DisplayObject instances
	 * that can be duplicated
	 */
	public class GraphicsCopy extends Proxy {
		
		private var _graphics:Graphics;
		private var history:Array = new Array();
		
		/**
		 * graphics instance to recieve
		 * drawing commands given to this GraphicsCopy instance
		 */
		public function get graphics():Graphics {
			return _graphics;
		}
		public function set graphics(g:Graphics):void {
			_graphics = g;
			
			// apply the graphics history to Graphics instance
			copy(this);
		}
		
		/**
		 * constructor
		 * @param graphics Optional Graphics instance to recieve
		 * drawing commands given to this GraphicsCopy instance
		 */
		public function GraphicsCopy(graphics:Graphics = null) {
			_graphics = graphics;
		}
		
		/**
		 * copies the graphics of a GraphicsCopy into this GraphicsCopy
		 */
		public function copy(graphicsCopy:GraphicsCopy):void {
			var hist:Array = graphicsCopy.history;
			history = hist.slice();
			if (_graphics) {
				var i:int;
				var n:int = hist.length;
				_graphics.clear();
				for (i=0; i<n; i += 2) {
					_graphics[hist[i]].apply(_graphics, hist[i + 1]);
				}
			}
		}
		
		// PROXY overrides
		override flash_proxy function callProperty(methodName:*, ... args):* {
			methodName = String(methodName);
			switch(methodName) {
				case "clear":
					history.length = 0;
					break;
				default:
					history.push(methodName, args);
			}
			if (_graphics && methodName in _graphics) {
				return _graphics[methodName].apply(_graphics, args);
			}
		}
	}
}