Debian Codegraph @

dojox/gauges/AnalogIndicatorBase.js

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