source: Dev/branches/rest-dojo-ui/client/dojox/drawing/stencil/Path.js @ 257

Last change on this file since 257 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

  • Property svn:executable set to *
File size: 4.3 KB
Line 
1dojo.provide("dojox.drawing.stencil.Path");
2
3
4dojox.drawing.stencil.Path = dojox.drawing.util.oo.declare(
5        // summary:
6        //              Creates a dojox.gfx Path based on points provided.
7        //
8        dojox.drawing.stencil._Base,
9        function(options){
10                dojo.disconnect(this._postRenderCon);
11        },
12        {
13                type:"dojox.drawing.stencil.Path",
14                closePath: true,
15                baseRender:true,
16                closeRadius:10,
17                closeColor:{r:255,g:255,b:0,a:.5},
18               
19/*=====
20StencilData: {
21        // NOT SUPPORTED FOR PATH
22},
23
24StencilPoints: [
25        // summary:
26        //              An Array of StencilPoint objects that describe the Stencil
27        //      0: Object
28        //              First point
29        //      [1, 2, 3...] more points
30],
31=====*/
32               
33                _create: function(/*String*/shp, /*Object*/sty){
34                        // summary:
35                        //              Creates a dojox.gfx.shape based on passed arguments.
36                        //              Can be called many times by implementation to create
37                        //              multiple shapes in one stencil.
38                        //
39                        this.remove(this[shp]);
40                        if(!this.points.length){ return; }
41       
42                        if(dojox.gfx.renderer=="svg"){
43                                // NOTE:
44                                // In order to avoid the Safari d="" errors,
45                                // we'll need to build a string and set that.
46                                var strAr = [];
47                                dojo.forEach(this.points, function(o, i){
48                                        if(!o.skip){
49                                                if(i==0){
50                                                        strAr.push("M " + o.x +" "+ o.y);
51                                                }else{
52                                                        var cmd = (o.t || "") + " ";
53                                                        if(o.x===undefined){// Z + undefined works here, but checking anyway
54                                                                strAr.push(cmd);
55                                                        }else{
56                                                                strAr.push(cmd + o.x +" "+ o.y);
57                                                        }
58                                                }
59                                        }
60                                }, this);
61                                if(this.closePath){
62                                        strAr.push("Z");
63                                }
64                               
65                                this.stringPath = strAr.join(" ");
66                               
67                                this[shp] = this.container.createPath(strAr.join(" ")).setStroke(sty);
68                                this.closePath && this[shp].setFill(sty.fill);
69                               
70                        }else{
71                                // Leaving this code for VML. It seems slightly faster but times vary.
72                                this[shp] = this.container.createPath({}).setStroke(sty);
73                               
74                                this.closePath && this[shp].setFill(sty.fill);
75                               
76                                dojo.forEach(this.points, function(o, i){
77                                        if(!o.skip){
78                                                if(i==0 || o.t=="M"){
79                                                        this[shp].moveTo(o.x, o.y);
80                                                }else if(o.t=="Z"){
81                                                        this.closePath && this[shp].closePath();
82                                                }else{
83                                                        this[shp].lineTo(o.x, o.y);
84                                                }
85                                        }
86                                }, this);
87                               
88                                this.closePath && this[shp].closePath();
89                        }
90                       
91                        this._setNodeAtts(this[shp]);
92                },
93               
94                render: function(){
95                        // summary:
96                        //              Renders the 'hit' object (the shape used for an expanded
97                        //              hit area and for highlighting) and the'shape' (the actual
98                        //              display object).
99                        //
100                        this.onBeforeRender(this);
101                        this.renderHit && this._create("hit", this.style.currentHit);
102                        this._create("shape", this.style.current);
103                        //console.log("path render")
104                       
105                       
106                //console.log("---------------------rend hit", this.renderHit, this.id)
107                },
108                getBounds: function(/* ? Boolean*/absolute){
109                        // summary:
110                        //      Overwriting _Base.getBounds. Not sure how absolute should
111                        //      work for a path.
112                        var minx = 10000, miny = 10000, maxx = 0, maxy = 0;
113                        dojo.forEach(this.points, function(p){
114                                if(p.x!==undefined && !isNaN(p.x)){
115                                        minx = Math.min(minx, p.x);
116                                        miny = Math.min(miny, p.y);
117                                        maxx = Math.max(maxx, p.x);
118                                        maxy = Math.max(maxy, p.y);
119                                }
120                        });
121                       
122                        return {
123                                x1:minx,
124                                y1:miny,
125                                x2:maxx,
126                                y2:maxy,
127                                x:minx,
128                                y:miny,
129                                w:maxx-minx,
130                                h:maxy-miny
131                        };
132                },
133               
134                checkClosePoint: function(/*Object*/firstPt, /*Object*/currPt, /*Boolean*/remove){
135                        // summary:
136                        //              Checks if points are close enough to indicate that
137                        //              path should be close. Provides a visual cue.
138                        // description:
139                        //              Not actually used in stencil.path - this is used for
140                        //              drawable tools that extend it. Note that those tools
141                        //              need to remove the shape created: this.closeGuide, or
142                        //              add arg: remove
143                        //
144                        var dist = this.util.distance(firstPt.x, firstPt.y, currPt.x, currPt.y);
145                        if(this.points.length>1){
146                                if(dist<this.closeRadius && !this.closeGuide && !remove){
147                                        var c = {
148                                                cx:firstPt.x,
149                                                cy:firstPt.y,
150                                                rx:this.closeRadius,
151                                                ry:this.closeRadius
152                                        }
153                                        this.closeGuide = this.container.createEllipse(c)
154                                                .setFill(this.closeColor);
155                                               
156                                }else if(remove || dist > this.closeRadius && this.closeGuide){
157                                        this.remove(this.closeGuide);
158                                        this.closeGuide = null;
159                                }
160                        }
161                        // return if we are within close distance
162                        return dist < this.closeRadius; // Boolean
163                }
164        }
165);
166
167dojox.drawing.register({
168        name:"dojox.drawing.stencil.Path"
169}, "stencil");
Note: See TracBrowser for help on using the repository browser.