1 /*
  2  *  Copyright 2010 Scriptoid s.r.l
  3  */
  4 
  5 /** Group is ONLY a logical grouping of some figures.
  6  * It does not paint itself and it does not change the Z-Order of member figures
  7  * 
  8  * @constructor
  9  * @this {Group}
 10  * @author Alex, Zack Newsham zack_newsham@yahoo.co.uk
 11  */
 12 function Group(){
 13     /**Group's id*/
 14     this.id = stack.generateId();
 15     
 16     /**By default all groups are temporary....so it's up to you make them permanent*/
 17     this.permanent = false;
 18     
 19     /**An {Array} of 2 {Point}s that keeps the rotation of the Group*/
 20     this.rotationCoords = [];
 21     
 22     /**Serialization type*/
 23     this.oType = 'Group';
 24 }
 25 
 26 /**Creates a {Group} out of JSON parsed object
 27  *@param {JSONObject} o - the JSON parsed object
 28  *@return {Group} a newly constructed Group
 29  *@author Alex Gheorghiu <alex@scriptoid.com>
 30  **/
 31 Group.load = function(o){
 32     var newGroup = new Group(); //empty constructor
 33 
 34     newGroup.id = o.id;
 35     newGroup.permanent = o.permanent;
 36     newGroup.rotationCoords = Point.loadArray(o.rotationCoords);
 37 
 38     return newGroup;
 39 }
 40 
 41 
 42 /**Creates a new {Array} of {Group}s out of JSON parsed object
 43  *@param {JSONObject} v - the JSON parsed object
 44  *@return {Array} of newly constructed {Group}s
 45  *@author Alex Gheorghiu <alex@scriptoid.com>
 46  **/
 47 Group.loadArray = function(v){
 48     var newGroups = [];
 49 
 50     for(var i=0; i<v.length; i++){
 51         newGroups.push(Group.load(v[i]));
 52     }
 53 
 54     return newGroups;
 55 }
 56 
 57 
 58 Group.prototype = {
 59 
 60     /**Group is not painted. It is only a mental group
 61      * @deprecated
 62      */
 63     paint:function(context){
 64         throw "Group is not painted. It is only an abstract grouping";
 65     },
 66 
 67 
 68 
 69     /**See if a group contains a point
 70      *@param {Number} x - the x coordinate of the point
 71      *@param {Number} y - the y coordinate of the point
 72      **/
 73     contains:function(x,y){
 74         var figures = stack.figureGetByGroupId(this.id);
 75         for(var i = 0; i < figures.length; i++){
 76             if(figures[i].contains(x,y) == true){
 77                 return true;
 78             }
 79         }
 80         return false;
 81     },
 82 
 83 
 84     /**See if a point is near a group, within a radius
 85      *@param {Number} x - the x coordinate of the point
 86      *@param {Number} y - the y coordinate of the point
 87      *@param {Number} radius - the radius to search for
 88      **/
 89     near:function(x,y,radius){
 90         var figures = stack.figureGetByGroupId(this.id);
 91         for(var i = 0; i < figures.length; i++){
 92             if(figures[i].near(x,y,radius) == true){
 93                 return true;
 94             }
 95         }
 96         return false;
 97     },
 98 
 99 
100     /**
101      *Get a group bounds
102      **/
103     getBounds:function(){
104         var figures = stack.figureGetByGroupId(this.id);
105         var points = [];
106         for(var i = 0; i < figures.length; i++){
107             var bounds = figures[i].getBounds();
108             points.push(new Point(bounds[0], bounds[1]));
109             points.push(new Point(bounds[2], bounds[3]));
110         }
111         return Util.getBounds(points);
112     },
113 
114 
115     /**
116      *Get all points of a Group (collect them from all figures)
117      **/
118     getPoints:function(){
119         var figures = stack.figureGetByGroupId(this.id);
120         var points = [];
121         for(var i = 0; i < stack.figureIds.length; i++){
122             var fPoints = stack.figureGetById(stack.figureIds[i]).getPoints();
123             points = points.concat(fPoints);
124         }
125         return points;
126     },
127 
128     /**
129      *Transform the group
130      *@param {Matrix} matrix - the transformation matrix
131      **/
132     transform:function(matrix){
133         this.rotationCoords[0].transform(matrix);
134         this.rotationCoords[1].transform(matrix);
135         var figures = stack.figureGetByGroupId(this.id);
136         for(var i = 0; i < figures.length; i++){
137 
138             figures[i].transform(matrix);
139         }
140     },
141     
142     /**Compares to another Group
143      *@param {Group} group -  - the other glue
144      *@return {Boolean} - true if equals, false otherwise
145      **/
146     equals:function(group){
147         if(!group instanceof Group){
148             return false;
149         }
150 
151         for(var i=0; i<this.rotationCoords; i++){
152             if(!this.rotationCoords[i].equals(group.rotationCoords[i])){
153                 return false;
154             }
155         }
156 
157         return this.permanent == group.permanent;
158     },
159 
160 
161     /**Clone this group
162      *@return {Group} - the clone of current group*/
163     clone:function(){
164         var group = new Group();
165         group.permanent = this.permanent;
166         return group;
167     },
168 
169     /**
170      *String representation of a Group
171      **/
172     toString:function(){
173         return "Group id: " + this.id + " permanent: " + this.permanent;
174     }
175 
176 }
177 
178 
179 
180