Projet

Général

Profil

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

root / js / pano.js @ 8611fdf2

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
function get_file_name(x, y, z) { // recherche du fichier correspondant au zoom et à la position
268
    var prm = [z, x, y];
269
    var fname = img_prefix;
270
    for (var i = 0; i < prm.length; i++) {
271
        fname += '_';
272
        if (prm[i] < 10) fname += '00';
273
        else if (prm[i] < 100) fname += '0';
274
        fname += prm[i];
275
    }
276
    fname += '.jpg';
277
    return fname;
278
}
279

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

    
288

    
289
function keys(key) {
290

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

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

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

    
381

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

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

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

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

    
454
    last.x = x;
455
    last.y = y;
456
    draw_image(x, y);
457
}
458

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

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

    
474

    
475

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

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

    
489
        this.im.width = this.im.visible_width;
490
        this.im.height = this.tile.height*(this.ntiles.y-1)+this.last_tile.height;
491
        if (this.last_tile.width > this.tile.width) this.max_tile.width = this.im.last_tile.width;
492
        else this.max_tile.width = this.tile.width;
493
        if (this.last_tile.height > this.tile.height) this.max_tile.height = this.im.last_tile.height;
494
        else this.max_tile.height = this.tile.height;
495

    
496
        var ord_pts = new Array();
497
        i=0;
498
        for(var label in ref_points) {
499
            ord_pts[i++] = ref_points[label]
500
        }
501
        ord_pts = ord_pts.sort(tri_ref_points);
502
        is_located = i > 1 || image_loop && i > 0;
503

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

    
538
            for (var i=0; i < ord_pts.length; i++) { // second parcours pour les paramètres elevation/y
539
                this.ref_pixels[i+1].shift_y = Math.floor(this.pixel_y_ratio*ord_pts[i].ele - ord_pts[i].y*this.im.height);
540
                if (i != ord_pts.length-1) {
541
                    var next_shift = Math.floor(this.pixel_y_ratio*ord_pts[i+1].ele - ord_pts[i+1].y*this.im.height);
542
                    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);
543
                }
544
            }
545

    
546
            if (image_loop == true) {
547
                var dpix  = this.im.width;
548
                var delt = 0;
549
                if (ord_pts.length > 1) {
550
                    dpix  = this.im.width - this.ref_pixels[this.ref_pixels.length-1].x + this.ref_pixels[1].x;
551
                    delt = this.ref_pixels[this.ref_pixels.length-1].shift_y - this.ref_pixels[1].shift_y;
552
                }
553
                this.ref_pixels[0].dshft_y = -delt/dpix;
554
                this.ref_pixels[ord_pts.length].dshft_y = this.ref_pixels[0].dshft_y;
555
                dpix = this.im.width - this.ref_pixels[ord_pts.length].x;
556
                this.ref_pixels[0].shift_y = this.ref_pixels[ord_pts.length].shift_y + dpix*this.ref_pixels[0].dshft_y;
557
            } else {
558
                this.ref_pixels[0].shift_y = this.ref_pixels[1].shift_y;
559
                this.ref_pixels[0].dshft_y = 0;
560
                this.ref_pixels[ord_pts.length].dshft_y = 0;
561
            }
562

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

    
588
        this.pt_list = new Array();
589
        for (var i=0; i<point_list.length; i++) {
590
            var lbl = point_list[i][0];
591
            var dst = point_list[i][1];
592
            var cap = point_list[i][2];
593
            var ele = point_list[i][3];
594
            var lnk = point_list[i][4];
595
            var typ = 'unlocated';
596
            var rxy = this.get_pos_xy(cap, ele);
597
            var is_visible = fmodulo(cap - alpha_domain.start, 360) <= fmodulo(alpha_domain.end - alpha_domain.start -0.0001, 360)+0.0001 && is_located;
598

    
599
            this.pt_list[i] = new Array();
600
            if (ref_points[lbl] != undefined) {
601
                typ = 'ref_point';
602
                if (!is_located) rxy = {x:ref_points[lbl].x*this.im.width, y:ref_points[lbl].y*this.im.height}
603
            } else if(lnk == '' && is_visible && lbl != 'point temporaire') {
604
                typ = 'loc_point';
605
            }else if(is_visible && lbl =='point temporaire') {
606
            typ = 'temporary';
607

    
608
            } else if(is_visible) {
609
                typ = 'pano_point';
610
                lnk += '&to_zoom='+this.value;
611
            }
612
            this.pt_list[i]['type'] = typ;
613
            this.pt_list[i]['cap'] = cap;
614
            this.pt_list[i]['ele'] = ele;
615
            this.pt_list[i]['dist'] = dst;
616
            this.pt_list[i]['label'] = lbl;
617
            this.pt_list[i]['lnk'] = lnk;
618
            this.pt_list[i]['xc'] = rxy.x;
619
            this.pt_list[i]['yc'] = Math.floor(this.im.height/2 - rxy.y);
620
        }
621
    }
622

    
623
    this.get_tile_size = function(nx, ny) {
624
        var res = {width:0, height:0};
625
        if (nx == this.ntiles.x-1) res.width = this.last_tile.width;
626
        else res.width = this.tile.width;
627
        if (ny == this.ntiles.y-1) res.height = this.last_tile.height;
628
        else res.height = this.tile.height;
629
        return res;
630
    }
631

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

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

    
668
function reset_zooms () {
669
    for(i=0; i<zooms.length; i++) zooms[i].is_updated = false;
670
    zm.refresh();
671
}
672

    
673
function wheel_zoom (event) {
674
    var zshift = {x:0, y:0};
675
    if (event.pageX != undefined && event.pageX != undefined) {
676
        zshift.x = event.pageX-canvas.width/2-canvas_pos.x;
677
        zshift.y = event.pageY-canvas.height/2-canvas_pos.y;
678
    }
679
    //event.preventDefault();
680
    if (event.wheelDelta < 0 && zoom_control.value < zoom_control.max) {
681
        zoom_control.value++;
682
        change_zoom(zshift.x, zshift.y);
683
    } else if (event.wheelDelta > 0 && zoom_control.value > zoom_control.min) {
684
        zoom_control.value--;
685
        change_zoom(zshift.x, zshift.y);
686
    }
687
}
688

    
689
function change_zoom(shx, shy) {
690
    var zoom_control = document.getElementById("zoom_ctrl");
691
    var v = zoom_control.value;
692

    
693
    prev_zm = zm;
694

    
695
    if (zooms[v]) {
696
        if (!zooms[v].is_updated) zooms[v].refresh();
697
    } else {
698
        zooms[v] = new tzoom(v);
699
    }
700

    
701
    if (zooms[v].is_updated) {
702
        if (shx == undefined || shy == undefined) {
703
            shx=0;
704
            shy=0;
705
        }
706
        zm = zooms[v];
707
        var px = (last.x+shx)*zm.im.width/prev_zm.im.width - shx;
708
        var py = (last.y+shy)*zm.im.height/prev_zm.im.height - shy;
709
        if (py < zm.im.height && py >= 0) {
710
            zoom = zm.value;
711
            tile = zm.tile;
712
            ntiles = zm.ntiles;
713
            putImage(px, py);
714
        } else {
715
            zm = prev_zm;
716
            zoom_control.value = zm.value;
717
        }
718
    }
719
}
720

    
721
function change_angle() {
722
    var elvtn_control = document.getElementById('elvtn_ctrl');
723
    var angle_control = document.getElementById('angle_ctrl');
724
    var resxy = zm.get_pos_xy(angle_control.value, elvtn_control.value);
725
    var pos_x = resxy.x;
726
    var pos_y = Math.floor(zm.im.height/2 - resxy.y);
727
    putImage(pos_x, pos_y);
728
}
729

    
730
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.
731
    return Math.sqrt(x*x + y*y) < r;
732
}
733

    
734
function check_links(e) {
735
    var mouse_x = e.pageX-canvas_pos.x;
736
    var mouse_y = e.pageY-canvas_pos.y;
737
    var pos_x = nmodulo(last.x + mouse_x - canvas.width/2, zm.im.width);
738
    var pos_y = last.y + mouse_y - canvas.height/2;
739
    for(var i = 0; i < zm.pt_list.length; i++) {
740
        if (is_located && zm.pt_list[i]['type'] == 'pano_point') {
741
            if (check_prox(zm.pt_list[i]['xc']-pos_x, zm.pt_list[i]['yc']-pos_y, 20)) {
742
                if (zm.pt_list[i]['lnk'] != '') window.location = zm.pt_list[i]['lnk'];
743
                break;
744
            }
745
        }
746
    }
747
}
748

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

    
788
function hide_links() {
789
    canvas.removeEventListener( "mousemove", display_links, false);
790
    canvas.removeEventListener( "touchmove", display_links, false);
791
    var info = document.getElementById('info');
792
    info.style.display = 'none';
793
}
794

    
795
function show_links() {
796
    canvas.addEventListener( "mousemove", display_links, false);
797
    canvas.addEventListener( "touchmove", display_links, false);
798
//    var info = document.getElementById('info');
799
//    info.style.display = 'block';
800
}
801

    
802
function hide_contextmenu() {
803
    document.getElementById('insert').style.display = 'none';
804
}
805

    
806
function manage_ref_points(e) {
807
    //event.preventDefault();
808
    //event.stopPropagation();
809
        var sel_pt = document.getElementById('sel_point');
810
    var do_insert = document.getElementById('do-insert');
811
        var do_delete = document.getElementById('do-delete');
812
        var do_cancel = document.getElementById('do-cancel');
813
        var show_cap = document.getElementById('show-cap');
814
        var insrt = document.getElementById('insert');
815

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

    
819
        insrt.style.left = e.pageX+'px';
820
        insrt.style.top = e.pageY+'px';
821
        insrt.style.display = 'block';
822

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

    
839
        do_cancel.onclick = hide_contextmenu;
840
    var res = zm.get_cap_ele(pos_x, zm.im.height/2 - pos_y);
841
    var pt_lat = document.getElementById('pos_lat').childNodes[0].nodeValue;
842
    var pt_lon = document.getElementById('pos_lon').childNodes[0].nodeValue;
843
    return false;
844
}
845

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

    
874
function show_result(clear_before) {
875
    var res = document.getElementById('res');
876
    var strg = '';
877
    for (var lbl in ref_points) {
878
        strg += '<li>reference["'+lbl+'"] = '+ref_points[lbl].x.toFixed(5)+','+ref_points[lbl].y.toFixed(5)+'</li>';
879
    }
880
    if (strg) strg = '<h3>Liste de tous les points de référence</h3>\n<ul>' + strg + '</ul>';
881
    if (clear_before) res.innerHTML = strg;
882
    else res.innerHTML += strg;
883
}
884

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

    
901
function clean_canvas_events(e) {
902
    canvas.removeEventListener('mousemove', stickImage, false);
903
    canvas.removeEventListener('touchmove', stickImage, false);
904
    document.getElementById('info').style.display = 'none';
905
    speed.x = 0;
906
    speed.y = 0;
907
}
908

    
909
canvas_set_size = function() {
910
    canvas.style.border = border_width+"px solid red";
911
    canvas.width = window.innerWidth-2*border_width;
912
    canvas.height = window.innerHeight-2*border_width;
913
    canvas_pos.x = canvas.offsetLeft+border_width;
914
    canvas_pos.y = canvas.offsetTop+border_width;
915
}
916

    
917
canvas_resize = function() {
918
    canvas_set_size();
919
    putImage(last.x, last.y);
920
}
921

    
922
function paramIn(e) {
923
    e = e || window.event;
924
    var relatedTarget = e.relatedTarget || e.fromElement;
925

    
926
    while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
927
        relatedTarget = relatedTarget.parentNode;
928
    }
929

    
930
    if (relatedTarget != adding && relatedTarget != localisation) {
931
        document.removeEventListener('keydown', keys, false);
932
    }
933
}
934

    
935
function paramOut(e) {
936

    
937
    e = e || window.event;
938
    var relatedTarget = e.relatedTarget || e.toElement;
939

    
940
    while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
941
        relatedTarget = relatedTarget.parentNode;
942
    }
943

    
944
    if (relatedTarget != adding && relatedTarget != localisation) {
945
            document.addEventListener('keydown', keys, false);
946
    }
947

    
948
}
949

    
950

    
951
function load_pano() {
952
        localisation = document.getElementById("locadraw");
953
    adding = document.getElementById("adding");
954
    canvas = document.getElementById("mon-canvas");
955
    cntext = canvas.getContext("2d");
956
    canvas_set_size();
957
    canvas.addEventListener("click", check_links, false);
958
    //canvas.addEventListener("oncontextmenu", manage_ref_points, false);
959
    canvas.oncontextmenu = manage_ref_points;
960
    canvas.addEventListener("mouseout" , clean_canvas_events, false);
961
    show_links();
962

    
963
    var max_zoom = zooms.length - 1;
964
    zoom_control = document.getElementById("zoom_ctrl");
965
    zoom_control.onchange = change_zoom;
966
    zoom_control.max = max_zoom;
967
    if (to_zoom > max_zoom) to_zoom = Math.floor(max_zoom/2);
968
    zm = zooms[to_zoom];
969
    zoom_control.value = to_zoom;
970
    zm.refresh();
971

    
972
    zoom = zm.value;
973
    tile = zm.tile;
974
    ntiles = zm.ntiles;
975

    
976
    angle_control = document.getElementById("angle_ctrl");
977
    angle_control.value = to_cap;
978
    angle_control.onchange = change_angle;
979
    angle_control.onclick = change_angle;
980
    elvtn_control = document.getElementById("elvtn_ctrl");
981
    elvtn_control.value = to_ele;
982
    elvtn_control.onchange = change_angle;
983
    elvtn_control.onclick = change_angle;
984

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