Diff
Not logged in

Differences From Artifact [e44e8eb350]:

To Artifact [d50a5e8508]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22
23
24

25
26
27
28
29
30
31
1
2
3
4
5






6


7





8


9

10
11
12
13
14
15
16
17





-
-
-
-
-
-

-
-
+
-
-
-
-
-

-
-

-
+







<html>
<script src='matrix_vector_lib.js' ></script>

<script>

//
//  Editor modes
//
//  Motion mode  -- change the camera center using the original movement system
//  --------Camera mode  -- adjust the camera,  scale zoom and position zoom, make it stationary or rotating about the center
//  Edit mode -- create points at camera center, move points, create lines by clicking on successive points, create closed polygons etc.

//  Editing Strategy

//  TODO control with zoom rotate or pan means that action is only applied to the camera not the selected points.
//  During draw mode only the very edges of the screen rotate the view, and at reduced speed
//  pressing enter or clicking on the center point creates a new point.
//  points are moved with the arrow keys, the direction is absolute, that means it does not depend on the camera angle.
//  the arrow keys move the selected point.
// clicking on a second point when one point is already selected creates a line.

//  TODO  control with zoom scales the selected object
//        control with rotate rotates the selected objects

//  TODO clicking and dragging on a line allows you to place a point somewhere on that line.  Perhaps with the ctrl key it would break up the existing line into line segments.
//  TODO clicking and dragging on a line allows you to place a point somewhere on that line.  Perhaps with the modifier key it would break up the existing line into line segments.

//  TODO when lines are deleted, readd the solo points to the solo_points list
//  

//  TODO z for undo shift + z for redo.
//

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
117
118
119
120
121
122
123

124
125
126
127
128
129
130







-







        var button = event.button;
        var keycode = button + 1000;  //treat clicks like a button press but with higher keycode.
        keyDown({keyCode: keycode}); }  // it seems the keyCode of the original event cant be overwritten.
        
    document.body.onmousewheel = function(event){
        var delta = event.wheelDelta;
        
        //if(editor_mode == MODE_DRAWING){
		if(delta < 0)
			zoom_dist /= zoom_factor;
		else if(delta > 0)
			zoom_dist *= zoom_factor; }
        
        
    document.body.onmouseup = function(event){
185
186
187
188
189
190
191
192
193
194
195

196
197
198
199
200
201
202

203
204
205
206
207
208
209
170
171
172
173
174
175
176




177
178
179
180




181
182
183
184
185
186
187
188







-
-
-
-
+



-
-
-
-
+







    changeVelocity(2, false, s, lasts); }
    
function forwardPress(e, s, lasts){
    changeVelocity(2, true, s, lasts); }
        
        
function zoomInPress(e, s, lasts){
    //if(editor_mode == MODE_CAMERA_POSITION)
    //    changeVelocity(2, true, s, lasts);
    //else if(editor_mode == MODE_CAMERA_ROTATION)
        zoom_dist *= zoom_factor; }
    zoom_dist *= zoom_factor; }
        
        
function zoomOutPress(e, s, lasts){
    //if(editor_mode == MODE_CAMERA_POSITION)
    //    changeVelocity(2, true, s, lasts);
    //else if(editor_mode == MODE_CAMERA_ROTATION)
        zoom_dist *= zoom_factor; }
    zoom_dist *= zoom_factor; }


function copySelectedPoints(e, s, lasts){
    if(s){
        var new_selection = [];
        var selection_map = {};
        
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304



















305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352















































353
354
355
356
357
358
359
225
226
227
228
229
230
231


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250



251





252
253
254




















255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274















































275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328







-
-



















-
-
-
+
-
-
-
-
-



-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







addKeyListener(83, backwardPress);
addKeyListener(69, upPress);
addKeyListener(81, downPress);


addKeyListener(88, function(e, s, lasts){  // x

    //if(editor_mode == MODE_DRAWING && s){  // alert('deleting points');
	
    if(s){
        if(highlight_object !== null && highlight_object > 0){  // delete highlighted object
            
            if(highlight_object < point_projections.length){  // point
                points[highlight_object] = null; }
                
            else if(highlight_object < point_projections.length + line_midpoint_projections.length){  // line
                lines[highlight_object - point_projections.length] = null; }
            else throw "highlight object index to large"; }
        
        else if(selected_points.length){  // delete selected points
            for(var i = 0; i < selected_points.length; ++i){
                points[selected_points[i]] = null; }
            selected_points.length = 0; }
        
        cleanupDeletedPoints();
        highlight_object = null;
        selected_points.length = 0; }});

// addKeyListener(1000, aForwardPress);    // mouse buttons
// addKeyListener(1002, cameraBackwardPress);

		


// addKeyListener(13, function(e, s, lasts){ if(s) nextMode(); });  // switch modes


//  select point
addKeyListener(1000, function(e, s, lasts){

    //if(editor_mode == MODE_DRAWING){
        if(s){
            if(highlight_object !== null){
                if(highlight_object == -1)
                    highlight_object = addPoint(origin);  // create a new point at the origin
                    
                
                var alreadySelected = false;  // only set for shift clicks
                
                if(key_state[16] && key_state[16].state){  // shift key is pressed.
                
                    for(var i = 0; i < selected_points.length; ++i){
                        if(selected_points[i] == highlight_object){
                           selected_points.splice(i, 1); // deselect point
                           alreadySelected = true; 
                           break; }}}
                else{
                    selected_points.length = 0;}
                    
                if(!alreadySelected) selected_points.push(highlight_object); }
	if(s){
		if(highlight_object !== null){
			if(highlight_object == -1)
				highlight_object = addPoint(origin);  // create a new point at the origin
				
			
			var alreadySelected = false;  // only set for shift clicks
			
			if(key_state[16] && key_state[16].state){  // shift key is pressed.
			
				for(var i = 0; i < selected_points.length; ++i){
					if(selected_points[i] == highlight_object){
					   selected_points.splice(i, 1); // deselect point
					   alreadySelected = true; 
					   break; }}}
			else{
				selected_points.length = 0;}
				
			if(!alreadySelected) selected_points.push(highlight_object); }

            else if(!key_state[16] || !key_state[16].state)
                selected_points.length = 0; }
            
            
        else if(lasts && mouse_dragging ){  // drag actions
            
            //  connect selected points to highlight point with lines
            if(selected_points.length && (!key_state[16] || !key_state[16].state)){  //  TODO right now this really only works for drawing a single line, 
            
                if(highlight_object !== null && highlight_object < point_projections.length){
                    if(highlight_object == -1) highlight_object = addPoint(origin);
                    
                    for(var i = 0; i < selected_points.length; ++i){
                        var pt = selected_points[i];
                        if(pt == highlight_object) continue;
                        addLine(pt, highlight_object); }
                    selected_points.length = 0; }}
                
            // select points inside selection rectangle
            else if(mouse_loc && last_mouse_down){ 
            
                var minx = Math.min(mouse_loc[0], last_mouse_down[0]);
                var maxx = Math.max(mouse_loc[0], last_mouse_down[0]);
                var miny = Math.min(mouse_loc[1], last_mouse_down[1]);
                var maxy = Math.max(mouse_loc[1], last_mouse_down[1]);
                
                
                var selected_point_map = {};             
                
                if(!key_state[16] || !key_state[16].state){
                    selected_points.length = 0;
                    selected_lines.length = 0; }
                else{
                    for(var i = 0; i < selected_points.length; ++i){
                        selected_point_map[selected_points[i]] = i; }}
                
                for(var i = 0; i < point_projections.length; ++i){
                    if(selected_point_map[i]) continue; // already selected.
                    
                    var pt = point_projections[i];
                    if(!pt) continue;
                    var _x = pt[0];
                    var _y = pt[1];
                    
                    if(minx < _x && _x < maxx
                     && miny < _y && _y < maxy){
                       selected_points.push(i); }}}                
		else if(!key_state[16] || !key_state[16].state)
			selected_points.length = 0; }
		
		
	else if(lasts && mouse_dragging ){  // drag actions
		
		//  connect selected points to highlight point with lines
		if(selected_points.length && (!key_state[16] || !key_state[16].state)){  //  TODO right now this really only works for drawing a single line, 
		
			if(highlight_object !== null && highlight_object < point_projections.length){
				if(highlight_object == -1) highlight_object = addPoint(origin);
				
				for(var i = 0; i < selected_points.length; ++i){
					var pt = selected_points[i];
					if(pt == highlight_object) continue;
					addLine(pt, highlight_object); }
				selected_points.length = 0; }}
			
		// select points inside selection rectangle
		else if(mouse_loc && last_mouse_down){ 
		
			var minx = Math.min(mouse_loc[0], last_mouse_down[0]);
			var maxx = Math.max(mouse_loc[0], last_mouse_down[0]);
			var miny = Math.min(mouse_loc[1], last_mouse_down[1]);
			var maxy = Math.max(mouse_loc[1], last_mouse_down[1]);
			
			
			var selected_point_map = {};             
			
			if(!key_state[16] || !key_state[16].state){
				selected_points.length = 0;
				selected_lines.length = 0; }
			else{
				for(var i = 0; i < selected_points.length; ++i){
					selected_point_map[selected_points[i]] = i; }}
			
			for(var i = 0; i < point_projections.length; ++i){
				if(selected_point_map[i]) continue; // already selected.
				
				var pt = point_projections[i];
				if(!pt) continue;
				var _x = pt[0];
				var _y = pt[1];
				
				if(minx < _x && _x < maxx
				 && miny < _y && _y < maxy){
				   selected_points.push(i); }}}                
        }});





// to prevent camera from rotating beyond straight vertical
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
339
340
341
342
343
344
345



346






































347
348
349
350
351
352
353







-
-
-

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







var max_angle_delta = Math.PI / 2;

var delta_horizontal_angle = 0;
var delta_vertical_angle = 0;
var delta_position = [0,0,0];


// var MODE_DRAWING = 0;
// var MODE_CAMERA_POSITION = 1; //  change the camera focus point
// var MODE_CAMERA_ROTATION = 2; //  rotate and zoom camera

// var editor_mode = MODE_DRAWING;

/*
function drawMode(){
    editor_mode = MODE_DRAWING;
    delta_position = [0,0,0];
    // delta_horizontal_angle = 0;
    // delta_vertical_angle = 0;
    if(last_zoom_dist) zoom_dist = last_zoom_dist;
    else zoom_dist = 1;
               
    document.body.style.cursor = "crosshair"; }
    
function cameraPositionMode(){
    document.body.style.cursor = "default";
    last_zoom_dist = zoom_dist;
    delta_horizontal_angle = 0;
    delta_vertical_angle = 0;
    zoom_dist = NAVIGATION_ZOOM_DIST;
    editor_mode = MODE_CAMERA_POSITION; }

function cameraRotationMode(){
    document.body.style.cursor = "default";
    delta_horizontal_angle = 0;
    delta_vertical_angle = 0;
    if(last_zoom_dist) zoom_dist = last_zoom_dist;
    else zoom_dist = 1;
    editor_mode = MODE_CAMERA_ROTATION; }
    
function nextMode(){
    if(editor_mode == MODE_DRAWING) cameraPositionMode();
    else if(editor_mode == MODE_CAMERA_POSITION) drawMode(); }

    // skipping camera rotation mode
    //else if(editor_mode == MODE_CAMERA_POSITION) cameraRotationMode();
    //else if(editor_mode == MODE_CAMERA_ROTATION)  drawMode(); }  
    
   */ 


var points = []; //global
var solo_points = {};
var lines = [];

var point_projections = [];
570
571
572
573
574
575
576
577

578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
498
499
500
501
502
503
504

505










































506
507
508
509
510
511
512







-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







    return index; }
    
    
function addLine(pt0, pt1){
    delete solo_points[pt0];
    delete solo_points[pt1];
    lines.push([pt0, pt1]);
    return lines.length - 1; }
    return lines.length - 1; }    

function addBox(xyz, wdh){
    var x = xyz[0];
    var y = xyz[1];
    var z = xyz[2];
    
    var w = wdh[0];
    var d = wdh[1];
    var h = wdh[2];
    
    var x0 = x - w/2;
    var y0 = y - d/2;
    var z0 = z - d/2;
    
    var x1 = x + w/2;
    var y1 = y + d/2;
    var z1 = z + h/2;
    
    var a = addPoint([x0, y0, z0]);
    var b = addPoint([x1, y0, z0]);
    var c = addPoint([x1, y1, z0]);
    var d = addPoint([x0, y1, z0]);
    var e = addPoint([x0, y0, z1]);
    var f = addPoint([x1, y0, z1]);
    var g = addPoint([x1, y1, z1]);
    var h = addPoint([x0, y1, z1]);
   

    addLine(a, b);
    addLine(b, c);
    addLine(c, d);
    addLine(d, a);
    addLine(e, f);
    addLine(f, g);
    addLine(g, h);
    addLine(h, e);
    
    addLine(a, e);
    addLine(b, f);
    addLine(c, g);
    addLine(d, h); }
    
    
    
    
function draw(canvas){
    var ctx = canvas.getContext('2d');
    var w = canvas.width;
    var h = canvas.height;
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
560
561
562
563
564
565
566

567
568
569
570
571
572
573







-







    for(var k in solo_points){
       var pt = point_projections[k];
       if(pt) ctx.fillRect(pt[0] - 2, pt[1] - 2, 4, 4); }
        
    
    
    
    //if(editor_mode == MODE_DRAWING){
	ctx.fillStyle = "rgba(250, 0, 0, 0.9)";
	ctx.fillRect(w/2 - 2, h/2 - 2, 4, 4);
	
	for(var i = 0; i < selected_points.length; ++i){
		var pt;
		
		if(selected_points[i] == -1)
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
638
639
640
641
642
643
644







































































645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662





















663
664
665
666
667
668
669







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-


















-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







	}
    
    ctx.fillStyle = "rgb(0,0,0)";
    ctx.fillText(msg, 5, 20); }

    
    
//function buildFractal(children, func, levels, origin, scale, baseonly){
    
//}


function serpenskiPyramid(level, origin, scale){
    if(!scale) scale = 1;
    if(!origin) origin = [0, 0, 0];
    if(level === undefined) level = 4;

    var spoints = [[0,0,0], [0.5,0,0], [0, -0.5, 0], [0, 0, 0.5]];
    
    
    for(var i = 0; i < spoints.length; ++i){
        var x = spoints[i];
        vector_scale(x, scale);
        vector_add(x, origin, x); }
    
    
    if(level > 0){
        for(var i = 0; i < spoints.length; ++i){
            var point = spoints[i];
            vector_add(origin, point, origin);
            serpenskiPyramid(level - 1, origin, 0.5 * scale);
            vector_minus(origin, point, origin); }
            
    }
    else if(level == 0){
        var pointRefs = [];
        for(var i = 0; i < spoints.length; ++i){
            vector_scale(spoints[i], 2);  // scale to points to full length;
            pointRefs[i] = addPoint(spoints[i]); }
            
        
        for(var i = 0; i < spoints.length; ++i){
            var a = pointRefs[i];
            
            for(var j = i + 1; j < spoints.length; ++j){
                var b = pointRefs[j];
                addLine(a, b); }}}
}

    
function buildArray(origin, increment, size, buildfunc, extra1, extra2, extra3){
    var point = origin.slice(0);
    point_indexes = [];
    
    for(var i = 0; i < size.length; ++i){
        point_indexes[i] = 0; }
    
    var last_index = point_indexes.length - 1;
    var index = last_index;
    
    // build object at origin.
    buildfunc(point, extra1, extra2, extra3);
    
    while(index >= 0){
        //depth first
        
        if(point_indexes[index] == size[index] - 1){
            point[index] = origin[index];
            point_indexes[index] = 0;
            --index; }
        else{
            ++point_indexes[index];
            point[index] += increment[index];
            index = last_index;  // always go to the end position
            buildfunc(point, extra1, extra2, extra3); }}
}
    

window.onload = function(){

    // load localStorage saved state.
    if(localStorage.points && localStorage.lines){
        points = JSON.parse(localStorage.points);
        lines = JSON.parse(localStorage.lines); }
    
    canvas = document.createElement('canvas');
    document.body.appendChild(canvas);
    
    canvas.width = window.innerWidth - 20;
    canvas.height = window.innerHeight - 20;
    
    
    window.onresize = function(){
        canvas.width = window.innerWidth - 20;
        canvas.height = window.innerHeight - 20; }

    //addBox([0, 0, 0], [.5, .5, .5]);
    
    /*
    buildArray( [0, 0, 0],  // origin
                [2, 2, 2], // increment
                [11, 1, 11], // size
                addBox,
                [.5, .5, .5]); */
    
    //var a = addPoint([0, 0, 1]);
    //var b = addPoint([.5, 0, 1]);
    //var c = addPoint([0, .5, 1]);
    

    
    //addLine(a, b);
    //addLine(b, c);
    //addLine(c, a);
    
    //serpenskiPyramid(5, null, 1);
    
    
    
    function getRotationAngle(x, size, max, center_size){

        if(!center_size) center_size = 0.25;  // the proportional size of the area in which no rotation is effected by the mouse movement
        if(!max) max = Math.PI/128;
        x -= size/2;
900
901
902
903
904
905
906
907
908
909

910
911
912

913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
693
694
695
696
697
698
699



700



701









702
703
704
705
706
707
708







-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-







            var _y = y - last_mouse_down[1];
            
            var dist2 = _x*_x + _y*_y;
            
            if(dist2 > MIN_DRAG_DIST * MIN_DRAG_DIST){
                mouse_dragging = true; }}
                
    /*
        if(editor_mode == MODE_CAMERA_ROTATION){   //  rotate about center
        

            delta_horizontal_angle = getRotationAngle(x, w, max_angle_delta/frame_rate);
            delta_vertical_angle = -getRotationAngle(y, h, max_angle_delta/frame_rate); }

				

        if(editor_mode == MODE_CAMERA_POSITION){        
        
            delta_horizontal_angle = -getRotationAngle(x, w, max_angle_delta/frame_rate);
            delta_vertical_angle = getRotationAngle(y, h, max_angle_delta/frame_rate); }
            

        else if(editor_mode == MODE_DRAWING){ */

		delta_horizontal_angle = getRotationAngle(x, w, max_angle_delta/frame_rate, no_rotate_area);
		delta_vertical_angle = -getRotationAngle(y, h, max_angle_delta/frame_rate, no_rotate_area);
		
		var min_point_dist2 = LINE_HIGHLIGHT_DIST * LINE_HIGHLIGHT_DIST;
		highlight_object = null;
		
		var _x = canvas.width/2 - x;
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987

988
989
990
991
992
993
994
995
996
997
752
753
754
755
756
757
758

759
760
761
762
763
764
765

766



767
768
769
770
771
772
773







-







-
+
-
-
-







    // ctx.fillRect(0, 0, canvas.width, canvas.height);
    
    // 
    
    
    function animateLoop(){
        
        //if(editor_mode == MODE_DRAWING){
		if(selected_points.length){  // move selection if exists
			for(var i = 0; i < selected_points.length; ++i){
				var pt = points[selected_points[i]];
				vector_add(pt, delta_position, pt); }}
		
		else{
			moveCamera(delta_position, false); }
        
      
        //else if(editor_mode == MODE_CAMERA_POSITION){
        //    if(delta_position)  moveCamera(delta_position, true); }  // motion depends on camera angle
        
        if(delta_horizontal_angle) rotateHorizontal(delta_horizontal_angle);
        if(delta_vertical_angle) rotateVertical(delta_vertical_angle);
        
       
        //  overwrite point projections
        //  TODO this may not be perfect if points are deleted etc.        
        point_projections.length = 0;