Projet

Général

Profil

Paste
Télécharger (33,7 ko) Statistiques
| Branche: | Révision:

root / js / pano.js @ 83478686

1
if (img_prefix == undefined) var img_prefix = 'http://pano.tetaneutral.net/data/tsf2/vpongnian/tiles/ttn_mediatheque/mediatheque_70';
2
if (title == undefined) var title = 'point non défini';
3
if (to_cap == undefined) var to_cap = 0;
4
if (to_ele == undefined) var to_ele = 0;
5
if (to_zoom == undefined) var to_zoom = 0;
6
if (cap == undefined) var cap = 0;
7
if (cap_min == undefined) var cap_min = cap;
8
if (cap_max == undefined) var cap_max = cap_min+360;
9
if (ref_points == undefined) var ref_points = new Array();
10
if (image_loop == undefined) var image_loop = false;
11

    
12
var debug_mode = false;
13
var canvas;
14
var cntext;
15
var point_list = new Array();
16
var zoom = 0;
17
var zooms = new Array();
18
var prev_zm;
19
var zm;
20
var tile = {width:256, height:256};
21
var ntiles = {x:228, y:9};
22
var border_width = 2;
23
var imageObj = new Array();
24

    
25

    
26
var fingr = 0;  // mémorisation de lécart entre doigts;
27
var last  = {x:0,y:0};
28
var shift = {x:0,y:0};
29
var mouse = {x:0,y:0};
30
var speed = {x:0,y:0};
31
var canvas_pos = {x:0,y:0};
32
var tmt;
33
var is_located = false;
34
var point_colors = {
35
        'pano_point' : '255,128,128', // red
36
        'ref_point'  : '128,128,255', // blue
37
        'loc_point'  : '128,255,128', // green
38
        'temporary'  : '255,255,128', // yellow
39
        'unlocated'  : '255,255,255'  // white
40
};
41
var test = {x:0, y:0, i:100};
42

    
43

    
44
function getXMLHttpRequest() {
45
        var xhr = null;
46
        
47
        if (window.XMLHttpRequest || window.ActiveXObject) {
48
                if (window.ActiveXObject) {
49
                        try {
50
                                xhr = new ActiveXObject("Msxml2.XMLHTTP");
51
                        } catch(e) {
52
                                xhr = new ActiveXObject("Microsoft.XMLHTTP");
53
                        }
54
                } else {
55
                        xhr = new XMLHttpRequest(); 
56
                }
57
        } else {
58
                alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
59
                return null;
60
        }
61
        
62
        return xhr;
63
}
64

    
65

    
66

    
67
function nmodulo(val, div) {                // pour obtenir un modulo dans l'espace des nombres naturels N.
68
    return Math.floor((val%div+div)%div);   // il y a peut être plus simple, mais en attendant ....
69
}
70

    
71
function fmodulo(val, div) {                // pour obtenir un modulo dans l'espace des nombres réels positifs.
72
    return (val%div+div)%div;               // il y a peut être plus simple, mais en attendant ....
73
}
74

    
75
function distort_canvas(p, x, y) {
76
    if (p == 0) distort = 0;
77
    else {
78
        cntext.save();
79
        distort++;
80
        cntext.clearRect(0, 0, canvas.width, 2*canvas.height);
81
        var ratio = (canvas.width-2*distort)/canvas.width;
82
        var shift = canvas.height/2*(1-ratio);
83
        cntext.scale(1, ratio);
84
        if (p == 1) cntext.translate(0, 0);
85
        else if (p == -1) cntext.translate(0, 0);
86
        draw_image(x, y);
87
        cntext.restore();
88
        document.getElementById('res').innerHTML = 'distort : ' + distort + ' shift ' + shift + ' ratio : ' + ratio + '<br/>';
89
    }
90
}
91

    
92
function draw_image(ox, oy) {
93
    var ref_vals  = {x:last.x, y:last.y, zoom:zoom};
94
    ox = nmodulo(ox-canvas.width/2, zm.im.width);        // pour placer l'origine au centre du canvas
95
    oy = Math.floor(oy-canvas.height/2);                 // pas de rebouclage vertical
96

    
97
    cntext.clearRect(0, 0, canvas.width, canvas.height);
98
    cntext.fillStyle = "rgba(128,128,128,0.8)";
99

    
100
    if (canvas.height > zm.im.height) {
101
        var fy = Math.floor((oy+canvas.height/2-zm.im.height/2)/(tile.height*zm.ntiles.y))*zm.ntiles.y;
102
        if (fy < 0) fy = 0;
103
        var ly = fy + zm.ntiles.y;
104
    } else {
105
        var fy = Math.floor(oy/tile.height);
106
        var ly = Math.floor((oy+canvas.height+tile.height-1)/tile.height+1);
107
        if (fy < 0) fy = 0;
108
        if (ly > zm.ntiles.y) ly = zm.ntiles.y;
109
    }
110

    
111
    for (var j=fy; j<ly; j++) {
112
        var delta_y = (Math.floor(j/zm.ntiles.y) - Math.floor(fy/zm.ntiles.y)) * (tile.height - zm.last_tile.height);
113
        var dy = j*tile.height - oy - delta_y;
114
        var ny = j%ntiles.y;
115
        var wy = zm.tile.width;
116
        if (ny == zm.ntiles.y - 1) wy = zm.last_tile.height;
117

    
118
        var cpx = 0;
119
        var i = 0;
120
        var Nx = zm.ntiles.x;
121
        while (cpx < ox+canvas.width) {
122
            var cur_width = zm.tile.width;
123
            if (i%Nx == zm.ntiles.x-1) cur_width = zm.last_tile.width;
124
            if (cpx >= ox-cur_width) {
125
                var nx = i%Nx;
126
                var idx = nx+'-'+ny+'-'+ref_vals.zoom;
127
                if (imageObj[idx] && imageObj[idx].complete) {
128
                    draw_tile(idx, cpx-ox, dy); // l'image est déja en mémoire, on force le dessin sans attendre.
129
                } else {
130
                    var fname = get_file_name(nx, ny, ref_vals.zoom);
131
                    imageObj[idx] = new Image();
132
                    imageObj[idx].src = fname;
133
                    var ts = zm.get_tile_size(nx, ny);
134
                    cntext.fillRect(cpx-ox, dy, ts.width, ts.height);
135
                    imageObj[idx].addEventListener('load', (function(ref, idx, dx, dy, ox, oy, ts) {
136
                        return function() {        // closure nécéssaire pour gestion assynchronisme !!!
137
                            draw_tile_del(ref, idx, dx, dy, ox, oy, ts.width, ts.height);
138
                        };
139
                    })(ref_vals, idx, cpx-ox, dy, ox, oy, ts), false);
140
                }
141
//                load_image(zoom, nx, ny, shx, shy, cpx-ox, dy, ox, oy);
142
            }
143
            cpx += cur_width;
144
            i++;
145
        }
146
    }
147
    drawDecorations(ox, oy);
148
    var cap_ele = zm.get_cap_ele(last.x, zm.im.height/2-last.y);
149
    angle_control.value = cap_ele.cap.toFixed(2);
150
    elvtn_control.value = cap_ele.ele.toFixed(2);
151
}
152

    
153
function draw_tile_del(ref, idx, tx, ty, ox, oy, twidth, theight) {
154
    if (ref.zoom == zoom && ref.x == last.x && ref.y == last.y) {
155
        draw_tile(idx, tx, ty);
156
        drawDecorations(ox, oy, tx, ty, twidth, theight);
157
    }
158
}
159

    
160
function draw_tile(idx, ox, oy) {
161
    var img = imageObj[idx];
162
    cntext.drawImage(img, ox, oy);
163
}
164

    
165
function drawDecorations(ox, oy, tx, ty, twidth, theight) {
166
    if (twidth) {
167
        cntext.save();
168
        cntext.beginPath();
169
        cntext.rect(tx, ty, twidth, theight);
170
        cntext.clip();
171
    }
172
    var wgrd = zm.im.width/360;
173
    var od = ((ox+canvas.width/2)/wgrd)%360;
174
    var el = (zm.im.height/2 - (oy+canvas.height/2))/wgrd;
175
    cntext.fillStyle = "rgba(0,128,128,0.9)";
176
    cntext.strokeStyle = "rgb(255,255,255)";
177
    cntext.lineWidth = 1;
178
    cntext.fillRect(canvas.width/2-5, canvas.height/2-5, 10, 10);
179
    cntext.strokeRect(canvas.width/2-5, canvas.height/2-5, 10, 10);
180
    for(var i = 0; i < zm.pt_list.length; i++) {
181
        if (zm.pt_list[i]['type'] != 'unlocated') {
182
            cntext.fillStyle = 'rgba('+point_colors[zm.pt_list[i]['type']]+',0.5)';
183
            var cx = nmodulo(zm.pt_list[i]['xc'] - ox, zm.im.width);
184
            var cy = zm.pt_list[i]['yc'] - oy;
185
            cntext.beginPath();
186
            cntext.arc(cx, cy, 20, 0, 2*Math.PI, true);
187
            cntext.fill();
188
        }
189
    }
190

    
191
    if (twidth) {
192
        cntext.restore();
193
    }
194

    
195
}
196

    
197
function insert_drawn_point(lat, lon, alt) {
198
    var rt = 6371;  // Rayon de la terre
199
    var pt_alt = document.getElementById('pos_alt').childNodes[0].nodeValue;
200
    var pt_lat = document.getElementById('pos_lat').childNodes[0].nodeValue;
201
    var pt_lon = document.getElementById('pos_lon').childNodes[0].nodeValue;
202

    
203
    var alt1 = pt_alt;
204
    var lat1 = pt_lat*Math.PI/180;
205
    var lon1 = pt_lon*Math.PI/180;
206
    var alt2 = alt;
207
    var lat2 = lat*Math.PI/180;
208
    var lon2 = lon*Math.PI/180;
209

    
210
    var dLat = lat2-lat1;
211
    var dLon = lon2-lon1;
212

    
213
    var a = Math.sin(dLat/2)*Math.sin(dLat/2) + Math.sin(dLon/2)*Math.sin(dLon/2)*Math.cos(lat1)*Math.cos(lat2);  //
214
    var angle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
215
    var d = angle*rt;                    // distance du point en Kms
216

    
217
    var y = Math.sin(dLon) * Math.cos(lat2);
218
    var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
219
    var cap = Math.atan2(y,x);                 // cap pour atteindre le point en radians
220
    var e = Math.atan2((alt2 - alt1)/1000 - d*d/(2*rt),d);  // angle de l'élévation en radians
221

    
222
    return {d:d, cap:cap*180/Math.PI, ele:e*180/Math.PI};   // les résultats sont en degrés
223
}
224

    
225
function localate_point() {
226
    var lat = document.getElementById("loca_latitude").value;
227
    var lon = document.getElementById("loca_longitude").value;
228
    var alt = document.getElementById("loca_altitude").value;
229
    if (lat == '' || isNaN(lat) || lat <= -90 || lat > 90) {
230
        alert("La latitude "+lat+"n'est pas correcte");
231
        return;
232
    }
233
    if (lat == '' || isNaN(lon) || lon <= -180 || lon > 180) {
234
        alert("La longitude "+lon+"n'est pas correcte");
235
        return;
236
    }
237
    if (lat == '' || isNaN(alt) || alt < -400 || alt > 10000000) {
238
        alert("l'altitude "+alt+"n'est pas correcte");
239
        return;
240
    }
241
    var opt_ced = new Array();
242
    opt_dce = insert_drawn_point(lat, lon, alt);
243
    display_temp(opt_dce.d, opt_dce.cap, opt_dce.ele);
244
}
245

    
246
function display_temp(d,cap,ele) {
247
    point_list[point_list.length] = new Array("point temporaire", d,cap,ele, "temporary");
248
    reset_zooms();
249
    putImage(last.x, last.y);
250
}
251

    
252
function arrayUnset(array, value){
253
    array.splice(array.indexOf(value), 1);
254
}
255

    
256
function erase_point() {
257
        for (var i=0; i<point_list.length; i++) {
258
                if(point_list[i][0] == "point temporaire"){
259
                        arrayUnset(point_list,point_list[i]);
260
                        loop = erase_point();
261
                }
262
        }
263
        reset_zooms();
264
    putImage(last.x, last.y);
265
}
266

    
267
/** Returns a 3-width zero-padded version of an int
268
 * ex: 3 -> "003"
269
 */
270
function zero_pad(number) {
271
        var temp = number.toString(10);
272
        while (temp.length < 3) {
273
                temp = '0' + temp;
274
        }
275
        return temp;
276
}
277

    
278
function get_file_name(x, y, z) { // recherche du fichier correspondant au zoom et à la position
279
        return img_prefix+'/'+zero_pad(z)+'_'+zero_pad(x)+'_'+zero_pad(y)+'.jpg';
280
}
281

    
282
function get_base_name() {
283
        /** 
284
         * @returns the base name, which is the name (not path) of the folder where
285
         * the tiles are.
286
         */
287
        return img_prefix.split('/').reverse()[0];
288
}
289

    
290

    
291
function keys(key) {
292

    
293
    hide_links();
294
    evt = key || event;
295
    //evt.preventDefault();
296
    //evt.stopPropagation();
297
    if (!key) {
298
        key = window.event;
299
        key.which = key.keyCode;
300
    }
301
//    alert(key);
302
//    if (!evt.shiftKey) return;
303
    switch (key.which) {
304
    /*case 66: // b
305
        alert(key.pageX);
306
        test.x=tile.width*(ntiles.x-3);
307
        test.y=0;
308
        putImage(test.x, test.y);
309
        return;
310
    case 67: // c
311
        test.x=0;
312
        test.y=tile.height*(ntiles.y-3);
313
        putImage(test.x, test.y);
314
        return;*/
315
    case 36: // home
316
        putImage(0, zm.im.height/2);
317
        return;
318
    case 35: // end
319
        putImage(last.x+zm.im.width/2, last.y);
320
        return;
321
    case 39: // left
322
        putImage(last.x+40, last.y);
323
        return;
324
    case 40: // up
325
        putImage(last.x, last.y+20);
326
        return;
327
    case 37: // right
328
        putImage(last.x-40, last.y);
329
        return;
330
    case 38: // down
331
        putImage(last.x, last.y-20);
332
        return;
333
    case 33: // pageup
334
        zoom_control.value--;
335
        change_zoom()
336
        return;
337
    case 34: // pagedown
338
        zoom_control.value++;
339
        change_zoom()
340
        return;
341
    default:
342
//        alert(key.which)
343
        return;
344
    }
345
}
346

    
347
function onImageClick(e) {
348
    hide_contextmenu();
349
    var index = {};
350
    if (e.touches && e.touches.length == 2) {
351
        e.preventDefault();
352
        fingr = Math.sqrt((e.touches[0].clientX - e.touches[1].clientX)^2 +
353
                          (e.touches[0].clientY - e.touches[1].clientY)^2);
354
    } else {
355
        if (e.touches) {
356
            e.preventDefault();
357
            index.x = e.changedTouches[0].clientX;
358
            index.y = e.changedTouches[0].clientY;
359
        } else {
360
            index.x = e.pageX;
361
            index.y = e.pageY;
362
        }
363

    
364
        shift.x = last.x;
365
        shift.y = last.y;
366
        speed.x = 0;
367
        speed.y = 0;
368
        mouse.x = index.x;
369
        mouse.y = index.y;
370
    }
371
    clearTimeout(tmt);  //arrêt de l'éffet eventuel d'amorti en cours.
372
    canvas.addEventListener('mousemove', stickImage, false);
373
    canvas.addEventListener('touchmove', stickImage, false);
374
    canvas.addEventListener('mouseup', launchImage, false);
375
    canvas.addEventListener('touchend', launchImage, false);
376
    //canvas.addEventListener('mouseout', launchImage, false);
377
    canvas.style.cursor='move';
378
    //document.onmousemove = stickImage;
379
    //document.onmouseup = launchImage;
380
    hide_links();
381
}
382

    
383

    
384
function stickImage(e) {
385
    var index = {};
386
    if (e.changedTouches && e.changedTouches.length == 2) {
387
        e.preventDefault();
388
        // cas du zoom à 2 doigts
389
        var nfingr = Math.sqrt((e.changedTouches[0].clientX - e.changedTouches[1].clientX)^2 +
390
                               (e.changedTouches[0].clientY - e.changedTouches[1].clientY)^2);
391
        var evt = {}
392
        evt.pageX = (e.changedTouches[0].clientX + e.changedTouches[1].clientX)/2;
393
        evt.pageY = (e.changedTouches[0].clientY + e.changedTouches[1].clientY)/2;
394
        if (fingr > nfingr*2 || fingr < nfingr/2) {
395
            evt.wheelDelta = fingr - nfingr;
396
            fingr = nfingr;
397
            return wheel_zoom(evt);
398
        } else {
399
            return;
400
        }
401
    }
402
    if (e.touches) {
403
        e.preventDefault();
404
        index.x = e.changedTouches[0].clientX;
405
        index.y = e.changedTouches[0].clientY;
406
    } else {
407
        index.x = e.pageX;
408
        index.y = e.pageY;
409
    }
410

    
411
    var xs = mouse.x - index.x + shift.x;
412
    var ys = mouse.y - index.y + shift.y;
413
    speed.x = xs - last.x;  //mémorisation des vitesses horizontales
414
    speed.y = ys - last.y;  //et verticales lors de ce déplacement
415
    putImage(xs, ys);
416
}
417

    
418
function launchImage(e) {
419
    var index = {};
420
    if (e.touches) {
421
        e.preventDefault();
422
        index.x = e.changedTouches[0].clientX;
423
        index.y = e.changedTouches[0].clientY;
424
    } else {
425
        index.x = e.pageX;
426
        index.y = e.pageY;
427
    }
428
    distort_canvas(0);
429
    canvas.removeEventListener('mousemove', stickImage, false);
430
    canvas.removeEventListener('touchmove', stickImage, false);
431
    //document.onmousemove = null;
432
    shift.x = index.x - mouse.x + shift.x;
433
    shift.y = index.y - mouse.y + shift.y;
434
    tmt = setTimeout(inertialImage, 100);
435
}
436

    
437
function putImage(x, y) { // est destiné à permettre l'effet d'amortissement par la mémorisation de la position courante.
438
    if (!zm.is_updated) return;
439
    if (x >= zm.im.width) {   // rebouclage horizontal
440
        shift.x -= zm.im.width;
441
        x -= zm.im.width;
442
    } else if (x < 0) {
443
        shift.x += zm.im.width;
444
        x += zm.im.width;
445
    }
446
    if (y >= zm.im.height) {   // pas de rebouclage vertical mais blocage
447
        //distort_canvas(1, x, y);
448
        shift.y = zm.im.height-1;
449
        y = zm.im.height-1;
450
    } else if (y < 0) {
451
        //distort_canvas(-1, x, y);
452
        shift.y = 0;
453
        y = 0;
454
    }
455

    
456
    last.x = x;
457
    last.y = y;
458
    draw_image(x, y);
459
}
460

    
461
function inertialImage() {
462
    speed.x *= 0.9;
463
    speed.y *= 0.9;
464
    if (Math.abs(speed.x) > 2 || Math.abs(speed.y) > 2) {
465
        putImage(last.x+speed.x, last.y+speed.y);
466
        tmt = setTimeout(inertialImage, 100);
467
    } else {
468
        show_links();
469
    }
470
}
471

    
472
function tri_ref_points(v1, v2) {
473
    return v1['x'] - v2['x'];
474
}
475

    
476

    
477

    
478
function tzoom(zv) {
479
    this.value = zv;
480
    this.ntiles = {x:0,y:0};
481
    this.tile = {width:0,height:0};
482
    this.last_tile = {width:0,height:0};
483
    this.max_tile = {width:0,height:0};
484
    this.im = {width:0,height:0};
485
    this.is_updated = false;
486

    
487
    this.refresh = function() {
488
            this.im.visible_width = this.tile.width*(this.ntiles.x-1)+this.last_tile.width;
489
            this.is_updated = true;
490

    
491
            this.im.width = this.im.visible_width;
492
            this.im.height = this.tile.height*(this.ntiles.y-1)+this.last_tile.height;
493
            if (this.last_tile.width > this.tile.width) {
494
                    this.max_tile.width = this.im.last_tile.width;
495
            } else {
496
                    this.max_tile.width = this.tile.width;
497
            }
498
            if (this.last_tile.height > this.tile.height) {
499
                    this.max_tile.height = this.im.last_tile.height;
500
            } else {
501
                    this.max_tile.height = this.tile.height;
502
            }
503

    
504
            var ord_pts = new Array();
505
            for(var label in ref_points) {
506
                    ord_pts.push(ref_points[label]);
507
            }
508
            ord_pts = ord_pts.sort(tri_ref_points);
509
            is_located = i > 1 || image_loop && i > 0;
510

    
511
            var alpha_domain = {start:0, end:360};
512
            this.pixel_y_ratio = this.im.width/360;
513
            if (is_located) {
514
                    this.ref_pixels = new Array;
515
                    this.ref_pixels[0] = new Array();    // Attention il faut compter un intervalle de plus !
516
                    for (var i=0; i < ord_pts.length; i++) { // premier parcours pour les paramètres cap/x
517
                            this.ref_pixels[i+1] = new Array();
518
                            this.ref_pixels[i+1].x = Math.floor(ord_pts[i].x*this.im.width);
519
                            this.ref_pixels[i+1].cap = fmodulo(ord_pts[i].cap, 360);
520
                            if (i != ord_pts.length-1) {
521
                                    this.ref_pixels[i+1].ratio_x = (ord_pts[i+1].x - ord_pts[i].x) /
522
                                            fmodulo(ord_pts[i+1].cap - ord_pts[i].cap, 360)*this.im.width;
523
                            }
524
                    }
525
                    if (image_loop == true) {
526
                            var dpix = this.im.width;
527
                            var dangle = 360;
528
                            if (ord_pts.length > 1) {
529
                                    dpix = this.im.width - this.ref_pixels[this.ref_pixels.length-1].x + this.ref_pixels[1].x;
530
                                    dangle = fmodulo(this.ref_pixels[1].cap - this.ref_pixels[this.ref_pixels.length-1].cap, 360);
531
                            }
532
                            this.ref_pixels[0].ratio_x = dpix/dangle;
533
                            this.ref_pixels[ord_pts.length].ratio_x = this.ref_pixels[0].ratio_x;
534
                            dpix = this.im.width - this.ref_pixels[ord_pts.length].x;
535
                            this.ref_pixels[0].cap = fmodulo(this.ref_pixels[ord_pts.length].cap + dpix / this.ref_pixels[0].ratio_x, 360);
536
                    } else {
537
                            this.ref_pixels[0].ratio_x = this.ref_pixels[1].ratio_x;
538
                            this.ref_pixels[ord_pts.length].ratio_x = this.ref_pixels[ord_pts.length-1].ratio_x;
539
                            this.ref_pixels[0].cap = fmodulo(this.ref_pixels[1].cap - this.ref_pixels[1].x / this.ref_pixels[1].ratio_x, 360);
540
                            alpha_domain.start = this.ref_pixels[0].cap;
541
                            alpha_domain.end = fmodulo(this.ref_pixels[ord_pts.length].cap+(this.im.width-this.ref_pixels[ord_pts.length].x)/this.ref_pixels[ord_pts.length].ratio_x, 360);
542
                            this.pixel_y_ratio = this.im.width/fmodulo(alpha_domain.end-alpha_domain.start, 360);
543
                    }
544
                    this.ref_pixels[0].x = 0;
545

    
546
                    for (var i=0; i < ord_pts.length; i++) { // second parcours pour les paramètres elevation/y
547
                            this.ref_pixels[i+1].shift_y = Math.floor(this.pixel_y_ratio*ord_pts[i].ele - ord_pts[i].y*this.im.height);
548
                            if (i != ord_pts.length-1) {
549
                                    var next_shift = Math.floor(this.pixel_y_ratio*ord_pts[i+1].ele - ord_pts[i+1].y*this.im.height);
550
                                    this.ref_pixels[i+1].dshft_y = (next_shift - this.ref_pixels[i+1].shift_y)/(this.ref_pixels[i+2].x - this.ref_pixels[i+1].x);
551
                            }
552
                    }
553

    
554
                    if (image_loop == true) {
555
                            var dpix  = this.im.width;
556
                            var delt = 0;
557
                            if (ord_pts.length > 1) {
558
                                    dpix  = this.im.width - this.ref_pixels[this.ref_pixels.length-1].x + this.ref_pixels[1].x;
559
                                    delt = this.ref_pixels[this.ref_pixels.length-1].shift_y - this.ref_pixels[1].shift_y;
560
                            }
561
                            this.ref_pixels[0].dshft_y = -delt/dpix;
562
                            this.ref_pixels[ord_pts.length].dshft_y = this.ref_pixels[0].dshft_y;
563
                            dpix = this.im.width - this.ref_pixels[ord_pts.length].x;
564
                            this.ref_pixels[0].shift_y = this.ref_pixels[ord_pts.length].shift_y + dpix*this.ref_pixels[0].dshft_y;
565
                    } else {
566
                            this.ref_pixels[0].shift_y = this.ref_pixels[1].shift_y;
567
                            this.ref_pixels[0].dshft_y = 0;
568
                            this.ref_pixels[ord_pts.length].dshft_y = 0;
569
                    }
570

    
571
                    if (debug_mode) {
572
                            var res = document.getElementById('res');
573
                            res.innerHTML = 'liste des '+this.ref_pixels.length+' valeurs de correction (image = '+this.im.width+'x'+this.im.height+') zoom = '+this.value+':<br/>';
574
                            for (var i=0; i < this.ref_pixels.length; i++) { // pour le debug
575
                                    res.innerHTML += '<p>point '+i+' :</p><ul>';
576
                                    for (var key in this.ref_pixels[i]) { // pour le debug
577
                                            res.innerHTML += '<li>'+key + '['+i+'] = '+this.ref_pixels[i][key]+'</li>';
578
                                    }
579
                                    if (i != this.ref_pixels.length-1) {
580
                                            var tx0 = this.ref_pixels[i+1].x-1;
581
                                            //var ty0 = this.ref_pixels[i+1].shift_y;
582
                                            var ty0 = 0;
583
                                    } else {
584
                                            var tx0 = this.im.width-1;
585
                                            var ty0 = 0;
586
                                    }
587
                                    res.innerHTML += '</ul><p>test sur : '+tx0+','+ty0+'</p>';
588
                                    var tst = this.get_cap_ele(tx0, ty0);
589
                                    res.innerHTML += '<p>cap:'+tst.cap+', ele:'+tst.ele+'</p>';
590
                                    var tst2 = this.get_pos_xy(tst.cap, tst.ele);
591
                                    res.innerHTML += '</ul><p>x:'+tst2.x+', y:'+tst2.y+'</p>';
592
                            }
593
                    }
594
            }
595

    
596
            this.pt_list = new Array();
597
            for (var i=0; i<point_list.length; i++) {
598
                    var lbl = point_list[i][0];
599
                    var dst = point_list[i][1];
600
                    var cap = point_list[i][2];
601
                    var ele = point_list[i][3];
602
                    var lnk = point_list[i][4];
603
                    var typ = 'unlocated';
604
                    var rxy = this.get_pos_xy(cap, ele);
605
                    var is_visible = fmodulo(cap - alpha_domain.start, 360) <= fmodulo(alpha_domain.end - alpha_domain.start -0.0001, 360)+0.0001 && is_located;
606

    
607
                    this.pt_list[i] = new Array();
608
                    if (ref_points[lbl] != undefined) {
609
                            typ = 'ref_point';
610
                            if (!is_located) rxy = {x:ref_points[lbl].x*this.im.width, y:ref_points[lbl].y*this.im.height}
611
                    } else if(lnk == '' && is_visible && lbl != 'point temporaire') {
612
                            typ = 'loc_point';
613
                    }else if(is_visible && lbl =='point temporaire') {
614
                            typ = 'temporary';
615

    
616
                    } else if(is_visible) {
617
                            typ = 'pano_point';
618
                            lnk += '&to_zoom='+this.value;
619
                    }
620
                    this.pt_list[i]['type'] = typ;
621
                    this.pt_list[i]['cap'] = cap;
622
                    this.pt_list[i]['ele'] = ele;
623
                    this.pt_list[i]['dist'] = dst;
624
                    this.pt_list[i]['label'] = lbl;
625
                    this.pt_list[i]['lnk'] = lnk;
626
                    this.pt_list[i]['xc'] = rxy.x;
627
                    this.pt_list[i]['yc'] = Math.floor(this.im.height/2 - rxy.y);
628
            }
629
    },
630

    
631
    this.get_tile_size = function(nx, ny) {
632
        var res = {width:0, height:0};
633
        if (nx == this.ntiles.x-1) res.width = this.last_tile.width;
634
        else res.width = this.tile.width;
635
        if (ny == this.ntiles.y-1) res.height = this.last_tile.height;
636
        else res.height = this.tile.height;
637
        return res;
638
    }
639

    
640
    this.get_cap_ele = function(px, py) {               // recherche d'un cap et d'une élévation à partir d'un pixel X,Y.
641
        if (is_located) {
642
            for (var i=0; i < this.ref_pixels.length; i++) {
643
                if (i == this.ref_pixels.length - 1 || px < this.ref_pixels[i+1].x) {
644
                    var dpix = px-this.ref_pixels[i].x;
645
                    var cp = fmodulo(this.ref_pixels[i].cap + dpix/this.ref_pixels[i].ratio_x, 360);
646
                    var el = (py+this.ref_pixels[i].shift_y+this.ref_pixels[i].dshft_y*dpix)/this.pixel_y_ratio;
647
                    return {cap:cp, ele:el};
648
                }
649
            }
650
        } else {
651
            var cp = 360*px/this.im.width;
652
            var el = 360*py/this.im.height;
653
            return {cap:cp, ele:el};
654
        }
655
    }
656

    
657
    this.get_pos_xy = function(cap, ele) {                  // recherche des coordonnées pixel à partir d'un cap et d'une élévation.
658
        if (is_located) {
659
            var dcap = fmodulo(cap-this.ref_pixels[0].cap, 360);
660
            for (var i=0; i < this.ref_pixels.length; i++) {
661
                if (i == this.ref_pixels.length - 1 || dcap < fmodulo(this.ref_pixels[i+1].cap-this.ref_pixels[0].cap, 360)) {
662
                    var px = this.ref_pixels[i].x + this.ref_pixels[i].ratio_x*fmodulo(cap - this.ref_pixels[i].cap, 360);
663
                    var dpix = px-this.ref_pixels[i].x;
664
                    var py = this.pixel_y_ratio*ele - this.ref_pixels[i].shift_y - this.ref_pixels[i].dshft_y*dpix;
665
                    return {x:px, y:py};
666
                }
667
            }
668
        } else {
669
            var px = fmodulo(cap, 360)/360*this.im.width;
670
            var py = ele/360*this.im.height;
671
            return {x:px, y:py};
672
        }
673
    }
674
}
675

    
676
function reset_zooms () {
677
    for(i=0; i<zooms.length; i++) zooms[i].is_updated = false;
678
    zm.refresh();
679
}
680

    
681
function wheel_zoom (event) {
682
    var zshift = {x:0, y:0};
683
    if (event.pageX != undefined && event.pageX != undefined) {
684
        zshift.x = event.pageX-canvas.width/2-canvas_pos.x;
685
        zshift.y = event.pageY-canvas.height/2-canvas_pos.y;
686
    }
687
    //event.preventDefault();
688
    if (event.wheelDelta < 0 && zoom_control.value < zoom_control.max) {
689
        zoom_control.value++;
690
        change_zoom(zshift.x, zshift.y);
691
    } else if (event.wheelDelta > 0 && zoom_control.value > zoom_control.min) {
692
        zoom_control.value--;
693
        change_zoom(zshift.x, zshift.y);
694
    }
695
}
696

    
697
function change_zoom(shx, shy) {
698
    var zoom_control = document.getElementById("zoom_ctrl");
699
    var v = zoom_control.value;
700

    
701
    prev_zm = zm;
702

    
703
    if (zooms[v]) {
704
        if (!zooms[v].is_updated) zooms[v].refresh();
705
    } else {
706
        zooms[v] = new tzoom(v);
707
    }
708

    
709
    if (zooms[v].is_updated) {
710
        if (shx == undefined || shy == undefined) {
711
            shx=0;
712
            shy=0;
713
        }
714
        zm = zooms[v];
715
        var px = (last.x+shx)*zm.im.width/prev_zm.im.width - shx;
716
        var py = (last.y+shy)*zm.im.height/prev_zm.im.height - shy;
717
        if (py < zm.im.height && py >= 0) {
718
            zoom = zm.value;
719
            tile = zm.tile;
720
            ntiles = zm.ntiles;
721
            putImage(px, py);
722
        } else {
723
            zm = prev_zm;
724
            zoom_control.value = zm.value;
725
        }
726
    }
727
}
728

    
729
function change_angle() {
730
    var elvtn_control = document.getElementById('elvtn_ctrl');
731
    var angle_control = document.getElementById('angle_ctrl');
732
    var resxy = zm.get_pos_xy(angle_control.value, elvtn_control.value);
733
    var pos_x = resxy.x;
734
    var pos_y = Math.floor(zm.im.height/2 - resxy.y);
735
    putImage(pos_x, pos_y);
736
}
737

    
738
function check_prox(x, y, r) {   //verification si un point de coordonnées x, y est bien dans un cercle de rayon r centré en X,Y.
739
    return Math.sqrt(x*x + y*y) < r;
740
}
741

    
742
function check_links(e) {
743
    var mouse_x = e.pageX-canvas_pos.x;
744
    var mouse_y = e.pageY-canvas_pos.y;
745
    var pos_x = nmodulo(last.x + mouse_x - canvas.width/2, zm.im.width);
746
    var pos_y = last.y + mouse_y - canvas.height/2;
747
    for(var i = 0; i < zm.pt_list.length; i++) {
748
        if (is_located && zm.pt_list[i]['type'] == 'pano_point') {
749
            if (check_prox(zm.pt_list[i]['xc']-pos_x, zm.pt_list[i]['yc']-pos_y, 20)) {
750
                if (zm.pt_list[i]['lnk'] != '') window.location = zm.pt_list[i]['lnk'];
751
                break;
752
            }
753
        }
754
    }
755
}
756

    
757
function display_links(e) {
758
    var index = {};
759
    if (e.touches) {
760
        e.preventDefault();
761
        index.x = e.changedTouches[0].clientX;
762
        index.y = e.changedTouches[0].clientY;
763
    } else {
764
        index.x = e.pageX;
765
        index.y = e.pageY;
766
    }
767
    var info = document.getElementById('info');
768
    var mouse_x = index.x-canvas_pos.x;
769
    var mouse_y = index.y-canvas_pos.y;
770
    var pos_x = nmodulo(last.x + mouse_x - canvas.width/2, zm.im.width);
771
    var pos_y = last.y + mouse_y - canvas.height/2;
772
    //var cap = ((pos_x/zm.im.width)*360).toFixed(2);
773
    var res = zm.get_cap_ele(pos_x, zm.im.height/2 - pos_y);
774
    //var ele = ((zm.im.height/2 - pos_y)/zm.im.width)*360;
775
    info.innerHTML = 'élévation : '+res.ele.toFixed(2)+'<br/>cap : '+res.cap.toFixed(2);
776
    info.style.top = index.y+'px';
777
    info.style.left = index.x+'px';
778
    info.style.backgroundColor = '#FFC';
779
    info.style.display = 'block';
780
    canvas.style.cursor='crosshair';
781
    for(var i = 0; i < zm.pt_list.length; i++) {
782
        if (is_located || zm.pt_list[i]['type'] == 'ref_point') {
783
            if (check_prox(zm.pt_list[i]['xc']-pos_x, zm.pt_list[i]['yc']-pos_y, 20)) {
784
                info.innerHTML = zm.pt_list[i]['label'];
785
                if (zm.pt_list[i]['dist'] < 10) var dst = Math.round(zm.pt_list[i]['dist']*1000)+' m';
786
                else var dst = zm.pt_list[i]['dist'].toFixed(1)+' kms';
787
                info.innerHTML += '<br/> à ' + dst;
788
                info.style.backgroundColor = 'rgb('+point_colors[zm.pt_list[i]['type']]+')';
789
                canvas.style.cursor='auto';
790
                break;
791
            }
792
        }
793
    }
794
}
795

    
796
function hide_links() {
797
    canvas.removeEventListener( "mousemove", display_links, false);
798
    canvas.removeEventListener( "touchmove", display_links, false);
799
    var info = document.getElementById('info');
800
    info.style.display = 'none';
801
}
802

    
803
function show_links() {
804
    canvas.addEventListener( "mousemove", display_links, false);
805
    canvas.addEventListener( "touchmove", display_links, false);
806
//    var info = document.getElementById('info');
807
//    info.style.display = 'block';
808
}
809

    
810
function hide_contextmenu() {
811
    document.getElementById('insert').style.display = 'none';
812
}
813

    
814
function manage_ref_points(e) {
815
    //event.preventDefault();
816
    //event.stopPropagation();
817
        var sel_pt = document.getElementById('sel_point');
818
    var do_insert = document.getElementById('do-insert');
819
        var do_delete = document.getElementById('do-delete');
820
        var do_cancel = document.getElementById('do-cancel');
821
        var show_cap = document.getElementById('show-cap');
822
        var insrt = document.getElementById('insert');
823

    
824
    var pos_x = nmodulo(last.x + e.pageX - canvas_pos.x - canvas.width/2, zm.im.width);
825
        var pos_y = last.y + e.pageY - canvas_pos.y - canvas.height/2;
826

    
827
        insrt.style.left = e.pageX+'px';
828
        insrt.style.top = e.pageY+'px';
829
        insrt.style.display = 'block';
830

    
831
        if (do_insert) {// true if there are ref points
832
                    for(var i = 0; i < zm.pt_list.length; i++) {
833
                            if (zm.pt_list[i]['type'] == 'ref_point') {
834
                                    if (check_prox(zm.pt_list[i]['xc']-pos_x,
835
                                                   zm.pt_list[i]['yc']-pos_y, 20)) {
836
                                            sel_pt.value = zm.pt_list[i]['label'];
837
                                    }
838
                            }
839
                    }
840
                do_delete.onclick = function() {delete_ref_point(insrt)};
841
                do_insert.onclick = function() {insert_ref_point(insrt, e.pageX-canvas_pos.x, e.pageY-canvas_pos.y)};
842
                show_cap.onclick = function() {
843
                        window.open('show_capline.php?title='+encodeURIComponent(btoa(title))+'&cap='+res.cap+'&org_lat='+pt_lat+'&org_lon='+pt_lon+'&dist=120000');
844
                };
845
        }
846

    
847
        do_cancel.onclick = hide_contextmenu;
848
    var res = zm.get_cap_ele(pos_x, zm.im.height/2 - pos_y);
849
    var pt_lat = document.getElementById('pos_lat').childNodes[0].nodeValue;
850
    var pt_lon = document.getElementById('pos_lon').childNodes[0].nodeValue;
851
    return false;
852
}
853

    
854
function insert_ref_point(el, x, y) {
855
        var label, posx, posy;
856
    el.style.display = 'none';
857
    for(var i = 0; i < zm.pt_list.length; i++) {
858
        label = zm.pt_list[i]['label'];
859
        if(label == document.getElementById('sel_point').value) {
860
            posx = nmodulo(last.x + x - canvas.width/2, zm.im.width)/zm.im.width;
861
            posy = 0.5 - (last.y + y - canvas.height/2)/zm.im.height;
862
            var pval = {x:posx, y:posy, cap:zm.pt_list[i]['cap'], ele:zm.pt_list[i]['ele'], label:label};
863
            ref_points[label] = pval;
864
            document.getElementById('res').innerHTML = '<h4>Dernier point entré</h4>';
865
            document.getElementById('res').innerHTML += '<p>reference["'+label+'"] = '+posx.toFixed(5)+','+posy.toFixed(5)+'</p>';
866
            reset_zooms();
867
             putImage(last.x, last.y);
868
            break;
869
        }
870
    }
871
        show_result();
872
        
873
        // Then push the modif
874
        var xhr = getXMLHttpRequest();
875
        xhr.open("POST", "ajax/add_reference.php", true);
876
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
877
        xhr.send("ref_point="+encodeURIComponent(label)
878
                 +"&panorama="+encodeURIComponent(get_base_name())
879
                 +"&x="+posx+"&y="+posy);
880
}
881

    
882
function show_result(clear_before) {
883
    var res = document.getElementById('res');
884
    var strg = '';
885
    for (var lbl in ref_points) {
886
        strg += '<li>reference["'+lbl+'"] = '+ref_points[lbl].x.toFixed(5)+','+ref_points[lbl].y.toFixed(5)+'</li>';
887
    }
888
    if (strg) strg = '<h3>Liste de tous les points de référence</h3>\n<ul>' + strg + '</ul>';
889
    if (clear_before) res.innerHTML = strg;
890
    else res.innerHTML += strg;
891
}
892

    
893
function delete_ref_point(el) {
894
        var ref_name = document.getElementById('sel_point').value;
895
    el.style.display = 'none';
896
    delete ref_points[ref_name];
897
    reset_zooms();
898
    putImage(last.x, last.y);
899
        show_result(true);
900
        
901
        // Then push the modif
902
        var xhr = getXMLHttpRequest();
903
        xhr.open("POST", "ajax/rm_reference.php", true);
904
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
905
        xhr.send("ref_point="+encodeURIComponent(ref_name)
906
                 +"&panorama="+encodeURIComponent(get_base_name()));
907
}
908

    
909
function clean_canvas_events(e) {
910
    canvas.removeEventListener('mousemove', stickImage, false);
911
    canvas.removeEventListener('touchmove', stickImage, false);
912
    document.getElementById('info').style.display = 'none';
913
    speed.x = 0;
914
    speed.y = 0;
915
}
916

    
917
canvas_set_size = function() {
918
    canvas.style.border = border_width+"px solid red";
919
    canvas.width = window.innerWidth-2*border_width;
920
    canvas.height = window.innerHeight-2*border_width;
921
    canvas_pos.x = canvas.offsetLeft+border_width;
922
    canvas_pos.y = canvas.offsetTop+border_width;
923
}
924

    
925
canvas_resize = function() {
926
    canvas_set_size();
927
    putImage(last.x, last.y);
928
}
929

    
930
function paramIn(e) {
931
    e = e || window.event;
932
    var relatedTarget = e.relatedTarget || e.fromElement;
933

    
934
    while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
935
        relatedTarget = relatedTarget.parentNode;
936
    }
937

    
938
    if (relatedTarget != adding && relatedTarget != localisation) {
939
        document.removeEventListener('keydown', keys, false);
940
    }
941
}
942

    
943
function paramOut(e) {
944

    
945
    e = e || window.event;
946
    var relatedTarget = e.relatedTarget || e.toElement;
947

    
948
    while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
949
        relatedTarget = relatedTarget.parentNode;
950
    }
951

    
952
    if (relatedTarget != adding && relatedTarget != localisation) {
953
            document.addEventListener('keydown', keys, false);
954
    }
955

    
956
}
957

    
958

    
959
function load_pano() {
960
        localisation = document.getElementById("locadraw");
961
    adding = document.getElementById("adding");
962
    canvas = document.getElementById("mon-canvas");
963
    cntext = canvas.getContext("2d");
964
    canvas_set_size();
965
    canvas.addEventListener("click", check_links, false);
966
    //canvas.addEventListener("oncontextmenu", manage_ref_points, false);
967
    canvas.oncontextmenu = manage_ref_points;
968
    canvas.addEventListener("mouseout" , clean_canvas_events, false);
969
    show_links();
970

    
971
    var max_zoom = zooms.length - 1;
972
    zoom_control = document.getElementById("zoom_ctrl");
973
    zoom_control.onchange = change_zoom;
974
    zoom_control.max = max_zoom;
975
    if (to_zoom > max_zoom) to_zoom = Math.floor(max_zoom/2);
976
    zm = zooms[to_zoom];
977
    zoom_control.value = to_zoom;
978
    zm.refresh();
979

    
980
    zoom = zm.value;
981
    tile = zm.tile;
982
    ntiles = zm.ntiles;
983

    
984
    angle_control = document.getElementById("angle_ctrl");
985
    angle_control.value = to_cap;
986
    angle_control.onchange = change_angle;
987
    angle_control.onclick = change_angle;
988
    elvtn_control = document.getElementById("elvtn_ctrl");
989
    elvtn_control.value = to_ele;
990
    elvtn_control.onchange = change_angle;
991
    elvtn_control.onclick = change_angle;
992

    
993
    change_angle();
994
    loca_temp = document.getElementById("loca_show");
995
    if (loca_temp) {
996
        loca_temp.onclick = showLoca;
997
        loca_temp = document.getElementById("loca_hide");
998
        loca_temp.onclick = hideLoca;
999
        loca_temp = document.getElementById("loca_button");
1000
        loca_temp.onclick = localate_point;
1001
        loca_erase = document.getElementById("loca_erase");
1002
        loca_erase.onclick = erase_point;
1003
        localisation.addEventListener('mouseover',paramIn,false);
1004
        localisation.addEventListener('mouseout',paramOut,false);
1005
    }
1006
    canvas.addEventListener('mousedown', onImageClick, false);
1007
    canvas.addEventListener('touchstart', onImageClick, false);
1008
    document.addEventListener('keydown', keys, false);
1009
    canvas.addEventListener('mousewheel', wheel_zoom, false);
1010
    window.onresize = canvas_resize;
1011
    if (adding) {
1012
        document.getElementById("paramFormHide").onclick = hideForm;
1013
        document.getElementById("paramFormShow").onclick = showForm;
1014
        adding.addEventListener('mouseover', paramIn, false);
1015
        adding.addEventListener('mouseout', paramOut, false);
1016
    }
1017
};