source: Dev/branches/rest-dojo-ui/client/dojox/gauges/AnalogIndicatorBase.js @ 256

Last change on this file since 256 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).

File size: 5.8 KB
Line 
1define(["dojo/_base/lang","dojo/_base/declare","dojo/_base/connect","dojo/_base/fx","dojox/gfx","./_Indicator"],
2  function(lang, declare, connect, fx, gfx, Indicator) {
3 
4/*=====
5        Indicator = dojox.gauges._indicator;
6=====*/
7
8return declare("dojox.gauges.AnalogIndicatorBase",[Indicator],{
9                //      summary:
10                //              An abstract base class for indicators that can be used in an AnalogGauge.
11                //
12
13        draw: function(/*dojox.gfx.Group*/ group, /*Boolean?*/ dontAnimate){
14                // summary:
15                //              Override of dojox.gauges._Indicator.draw
16                // group: dojox.gfx.Group
17                //              The GFX group when the indicator must be drawn
18                // dontAnimate: Boolean
19                //              Indicates if the drawing should not be animated (vs. the default of doing an animation)
20                if(this.shape){
21                        this._move(dontAnimate);
22                }else{
23                        if(this.text){
24                                this.text.parent.remove(this.text);
25                                this.text = null;
26                        }
27                        var a = this._gauge._getAngle(Math.min(Math.max(this.value, this._gauge.min), this._gauge.max));
28
29                        this.color = this.color || '#000000';
30                        this.length = this.length || this._gauge.radius;
31                        this.width = this.width || 1;
32                        this.offset = this.offset || 0;
33                        this.highlight = this.highlight || '#D0D0D0';
34
35                        var shapes = this._getShapes(group, this._gauge, this);
36
37                        if (shapes){
38                                if (shapes.length > 1){
39                                        this.shape = group.createGroup();
40                                        for (var s = 0; s < shapes.length; s++){
41                                                this.shape.add(shapes[s]);
42                                        }
43                                }
44                                else
45                                        this.shape = shapes[0];
46 
47                                this.shape.setTransform([{
48                                        dx: this._gauge.cx,
49                                        dy: this._gauge.cy
50                                }, gfx.matrix.rotateg(a)]);
51
52                                this.shape.connect("onmouseover", this, this.handleMouseOver);
53                                this.shape.connect("onmouseout", this, this.handleMouseOut);
54                                this.shape.connect("onmousedown", this, this.handleMouseDown);
55                                this.shape.connect("touchstart", this, this.handleTouchStart);
56}
57                        if(this.label){
58                                var direction = this.direction;
59                                if (!direction) direction = 'outside';
60               
61                                var len;
62                                if (direction == 'inside')
63                                        len=-this.length+this.offset - 5;
64                                else
65                                        len=this.length+this.offset + 5;
66                               
67                                var rad=this._gauge._getRadians(90-a);
68                                this._layoutLabel(group, this.label+'', this._gauge.cx, this._gauge.cy,len ,rad , direction);
69                               
70                        }
71                        this.currentValue = this.value;
72                }
73        },
74       
75        _layoutLabel:function(group, txt, ox, oy, lrad, angle, labelPlacement){
76                // summary:
77                //              Places the label on the side of the tick.
78       
79                var font = this.font ? this.font : gfx.defaultFont;
80
81                var box =  gfx._base._getTextBox(txt,
82                {
83                        font: gfx.makeFontString(gfx.makeParameters(gfx.defaultFont,font))
84                });
85       
86                var tw = box.w;
87                var fz = font.size;
88                var th = gfx.normalizedLength(fz);
89       
90                var tfx = ox + Math.cos(angle) * lrad - tw / 2;
91                var tfy = oy - Math.sin(angle) * lrad - th / 2;
92                var side;
93
94                var intersections = [];
95
96                // Intersection with top segment
97                side = tfx;
98                var ipx = side;
99                var ipy = -Math.tan(angle) * side + oy + Math.tan(angle) * ox;
100                // Verify if intersection is on segment
101                if (ipy >= tfy && ipy <= tfy + th)
102                        intersections.push({
103                                x: ipx,
104                                y: ipy
105                        });
106
107                        // Intersection with bottom segment
108                        side = tfx + tw;
109                        ipx = side;
110                        ipy = -Math.tan(angle) * side + oy + Math.tan(angle) * ox;
111                        // Verify if intersection is on segment
112                        if (ipy >= tfy && ipy <= tfy + th) intersections.push({
113                                x: ipx,
114                                y: ipy
115                        });
116
117                // Intersection with left segment
118                side = tfy;
119                ipx = -1 / Math.tan(angle) * side + ox + 1 / Math.tan(angle) * oy;
120                ipy = side;
121                // Verify if intersection is on segment
122                if (ipx >= tfx && ipx <= tfx + tw)
123                        intersections.push({
124                                x: ipx,
125                                y: ipy
126                        });
127
128                // Intersection with right segment
129                side = tfy + th;
130                ipx = -1 / Math.tan(angle) * side + ox + 1 / Math.tan(angle) * oy;
131                ipy = side;
132                // Verify if intersection is on segment
133                if (ipx >= tfx && ipx <= tfx + tw)
134                        intersections.push({
135                                x: ipx,
136                                y: ipy
137                        });
138
139                var dif;
140                if (labelPlacement == "inside"){
141                        for (var it = 0; it < intersections.length; it++){
142                                var ip = intersections[it];
143                                dif = this._distance(ip.x, ip.y, ox, oy) - lrad;
144                                if (dif >= 0){
145                                        // Place reference intersection point on reference circle
146                                        tfx = ox + Math.cos(angle) * (lrad - dif) - tw / 2;
147                                        tfy = oy - Math.sin(angle) * (lrad - dif) - th / 2;
148                                        break;
149                                }
150                        }
151                }
152                else // "outside" placement
153                {
154                        for (it = 0; it < intersections.length; it++){
155                                ip = intersections[it];
156                                dif = this._distance(ip.x, ip.y, ox, oy) - lrad;
157                                if (dif <= 0){
158                                        // Place reference intersection point on reference circle
159                                        tfx = ox + Math.cos(angle) * (lrad - dif) - tw / 2;
160                                        tfy = oy - Math.sin(angle) * (lrad - dif) - th / 2;
161
162                                        break;
163                                }
164                        }
165                }
166                // since the size computed by getTextBox is too big,
167                // to lower the problem, we align this to the middle and
168                // place at the middle of the computed size.
169                this.text = this._gauge.drawText(group, txt, tfx + tw / 2, tfy + th, 'middle', this.color, this.font);
170        },
171       
172        _distance: function(x1,y1,x2,y2){
173                return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
174        },
175       
176        _move: function(/*Boolean?*/ dontAnimate){
177                // summary:
178                // dontAnimate: Boolean
179                //              Indicates if the drawing should not be animated (vs. the default of doing an animation)
180                var v = Math.min(Math.max(this.value, this._gauge.min), this._gauge.max),
181                        c = this.currentValue
182                ;
183                if(dontAnimate){
184                        var angle = this._gauge._getAngle(v);
185                        this.shape.setTransform([{dx:this._gauge.cx,dy:this._gauge.cy}, gfx.matrix.rotateg(angle)]);
186                        this.currentValue = v;
187                }else{
188                        if(c!=v){
189                                var anim = new fx.Animation({curve: [c, v], duration: this.duration, easing: this.easing});
190                                connect.connect(anim, "onAnimate", lang.hitch(this, function(step){
191                                this.shape.setTransform([{dx:this._gauge.cx,dy:this._gauge.cy}, gfx.matrix.rotateg(this._gauge._getAngle(step))]);
192                                       
193                                this.currentValue = step;
194                                }));
195                                anim.play();
196                        }
197                }
198        }
199});
200});
Note: See TracBrowser for help on using the repository browser.