1 /*
  2     Copyright 2008,2009
  3         Matthias Ehmann,
  4         Michael Gerhaeuser,
  5         Carsten Miller,
  6         Bianca Valentin,
  7         Alfred Wassermann,
  8         Peter Wilfahrt
  9 
 10     This file is part of JSXGraph.
 11 
 12     JSXGraph is free software: you can redistribute it and/or modify
 13     it under the terms of the GNU Lesser General Public License as published by
 14     the Free Software Foundation, either version 3 of the License, or
 15     (at your option) any later version.
 16 
 17     JSXGraph is distributed in the hope that it will be useful,
 18     but WITHOUT ANY WARRANTY; without even the implied warranty of
 19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20     GNU Lesser General Public License for more details.
 21 
 22     You should have received a copy of the GNU Lesser General Public License
 23     along with JSXGraph.  If not, see <http://www.gnu.org/licenses/>.
 24 */
 25 
 26 /**
 27  * @fileoverview The geometry object Line is defined in this file. Line stores all
 28  * style and functional properties that are required to draw and move a line on
 29  * a board.
 30  */
 31 
 32 /**
 33  * The Slider class is....
 34  * Slider (Schieberegler)
 35  * input: 3 arrays:
 36  * [x0,y0],[x1,y1],[min,start,max]
 37  * The slider is line from [x0,y0] to [x1,y1].
 38  * The position [x0,y0]  corresponds to the value "min",
 39  * [x1,y1] corresponds to the value max.
 40  * Initally, the slider is at position [x0,y0] + ([x1,y1]-[x0,y0])*start/(max-min)
 41  * The return value is an invisible point, whos X() or Y() value
 42  * returns the position between max and min,
 43  * Further, there is a method Value() returning the same value.
 44  * @class Creates a new basic slider object. Do not use this constructor to create a slider. Use {@link JXG.Board#create} with
 45  * type {@link Line}, {@link Arrow}, or {@link Axis} instead.  
 46  * Attributes: withTicks;
 47  * @constructor
 48  * @augments JXG.GeometryElement
 49  * @param {String,JXG.Board} board The board the new line is drawn on.
 50  * @param {Point} p1 Startpoint of the line.
 51  * @param {Point} p2 Endpoint of the line.
 52  * @param {String} id Unique identifier for this object. If null or an empty string is given,
 53  * an unique id will be generated by Board
 54  * @param {String} name Not necessarily unique name. If null or an
 55  * empty string is given, an unique name will be generated.
 56  * @see JXG.Board#generateName
 57  */
 58 JXG.createSlider = function(board, parents, atts) {
 59     var pos0, pos1, smin, start, smax, sdiff, p1, p2, l1, ticks, ti, startx, starty, p3, l2, n, t,
 60         snapWidth, fixed;
 61         
 62     pos0 = parents[0];
 63     pos1 = parents[1];
 64     smin = parents[2][0];
 65     start = parents[2][1];
 66     smax = parents[2][2];
 67     sdiff = smax -smin;
 68     
 69     atts = JXG.checkAttributes(atts,{strokeColor:'#000000', fillColor:'#ffffff', withTicks:true});
 70 
 71     fixed = JXG.str2Bool(atts['fixed']);
 72     p1 = board.create('point', pos0, 
 73         {visible:!fixed, fixed:fixed, name:'',withLabel:false,face:'<>', size:5, strokeColor:'#000000', fillColor:'#ffffff'}); 
 74     p2 = board.create('point', pos1, 
 75         {visible:!fixed, fixed:fixed, name:'',withLabel:false,face:'<>', size:5, strokeColor:'#000000', fillColor:'#ffffff'}); 
 76     board.create('group',[p1,p2]);
 77     l1 = board.create('segment', [p1,p2], 
 78                 {strokewidth:1, 
 79                 name:'',
 80                 withLabel:false,
 81                 strokeColor:atts['strokeColor']});
 82     if (atts['withTicks']) {
 83         ticks  = 2;
 84         ti = board.create('ticks', [l1, p2.Dist(p1)/ticks],
 85                     {insertTicks:true, minorTicks:0, drawLabels:false, drawZero:true}); 
 86     }
 87 
 88     if (fixed) {
 89         p1.setProperty({needsRegularUpdate : false});
 90         p2.setProperty({needsRegularUpdate : false});
 91         l1.setProperty({needsRegularUpdate : false});
 92     }
 93     
 94     startx = pos0[0]+(pos1[0]-pos0[0])*(start-smin)/(smax-smin);
 95     starty = pos0[1]+(pos1[1]-pos0[1])*(start-smin)/(smax-smin);
 96 
 97     if (atts['snapWidth']!=null) snapWidth = atts['snapWidth'];
 98     if (atts['snapwidth']!=null) snapWidth = atts['snapwidth'];
 99     
100     p3 = board.create('glider', [startx,starty,l1],
101                 {style:6,strokeColor:atts['strokeColor'],
102                  fillColor:atts['fillColor'],
103                  showInfobox:false,name:atts['name'], withLabel:false,
104                  snapWidth:snapWidth});
105     
106     l2 = board.create('line', [p1,p3], 
107                 {straightFirst:false, 
108                  straightLast:false, strokewidth:3, 
109                  strokeColor:atts['strokeColor'],
110                  name:'',
111                  withLabel:false}); 
112                  
113     //p3.Value = function() { return this.position*(smax - smin)+smin; };
114     //p3.type = JXG.OBJECT_TYPE_SLIDER;
115     p3.Value = function() { return this.position*sdiff+smin; };
116     p3._smax = smax;
117     p3._smin = smin;
118 
119     if (typeof atts['withLabel']=='undefined' || atts['withLabel']==true) {
120         if (atts['name'] && atts['name']!='') {
121             n = atts['name'] + ' = ';
122         } else {
123             n = '';
124         }
125         t = board.create('text', [function(){return (p2.X()-p1.X())*0.05+p2.X();},
126                                   function(){return (p2.Y()-p1.Y())*0.05+p2.Y();},
127                                   function(){return n+(p3.Value()).toFixed(2);}],
128                                      {name:''}); 
129     }                                     
130     return p3;
131 };    
132 
133 JXG.JSXGraph.registerElement('slider', JXG.createSlider);
134