Commit 3e11dbf3 authored by Aayush's avatar Aayush

parser for pd_message->ascii_art

parent 3c0e1b52
Pipeline #1660 failed with stage
in 0 seconds
//Example message
"use strict";
var pd_message = "#X obj 36 60 inlet;\n#X obj 36 416 outlet;\n#X obj 68 387 send;\n#X obj 93 282 loadbang;\n#X msg 93 308 dollarzero;\n#X obj 93 334 canvasinfo 1;\n#X msg 93 359 symbol \$1-result;\n#X connect 0 0 5 0;\n#X connect 1 0 4 0;\n#X connect 3 0 2 0;\n#X connect 4 0 3 0;\n#X connect 4 1 0 0;"
var x=parse_pd_message(pd_message);
alert(x);
document.getElementById("demo").innerHTML = x;
function makeArray(w, h, val) {
var arr = [];
for(let i = 0; i < h; i++) {
arr[i] = [];
for(let j = 0; j < w-1; j++) {
arr[i][j] = val;
}
arr[i][h-1]="\n<br>";
}
return arr;
}
function compare(a,b) {
if ( a.x < b.x ) return -1;
if ( a.x > b.x ) return 1;
return a.y - b.y;
}
function make_connection(matrix, connection, object1, object2, cell_x1, cell_y1, cell_x2, cell_y2) {
if ( cell_y1 == cell_y2 ) {
for ( let i = cell_x1+1 ; i < cell_x2 ; i++ ) {
matrix[i][cell_y1] = "|";
}
} else if ( cell_y1 < cell_y2 ) {
for ( let i = cell_x1+1 ; i < cell_x2 - 2; i++ ) {
matrix[i][cell_y1] = "|";
}
matrix[cell_x2-2][cell_y1] = "+";
for ( let j = cell_y1 + 1 ; j < cell_y2 ; j++ ) {
matrix[cell_x2 - 2][j] = "-";
}
matrix[cell_x2 - 2][cell_y2] = "+";
matrix[cell_x2 - 1][cell_y2] = "|";
} else {
for ( let i = cell_x1+1 ; i < cell_x2 - 2; i++ ) {
matrix[i][cell_y1] = "|";
}
matrix[cell_x2-2][cell_y1] = "+";
for ( let j = cell_y1 - 1 ; j > cell_y2 ; j-- ) {
matrix[cell_x2 - 2][j] = "-";
}
matrix[cell_x2 - 2][cell_y2] = "+";
matrix[cell_x2 - 1][cell_y2] = "|";
}
}
function parse_pd_message(message) {
var i,j,l,lines,
splitted_str,
temp_str,
obj_msg_info = [],
connection_info = [],
count = 0,
counter = 0,
str_count = 0,
object_msg_index_info = {},
matrix = makeArray(100,100,' ');
message.replace("\n" ,"");
lines = message.split(";");
for ( i = 0 ; i<lines.length-1 ; i++ ) {
lines[i].replace(";\n","");
splitted_str = lines[i].split(" ");
if ( splitted_str[1] == "connect") {
connection_info.push({start : splitted_str[2],
end : splitted_str[4],
point1 : splitted_str[3],
point2 : splitted_str[5]});
} else {
temp_str = splitted_str.slice(4,splitted_str.length);
obj_msg_info.push({type : splitted_str[1],
x : splitted_str[2],
y : splitted_str[3],
obj_no : count,
text : temp_str,
cell_start_x : 0,
cell_start_y : 0,
cell_end_x : 0,
cell_end_y : 0});
count = count+1;
}
}
obj_msg_info.sort(compare);
i=0;
j=0;
counter=0;
while( counter < obj_msg_info.length ) {
if(counter == 0) {
obj_msg_info[counter].cell_start_x = i;
obj_msg_info[counter].cell_start_y = j;
matrix[i][j] = "[";
j++;
str_count=0;
for ( l = j ; l < j + obj_msg_info[counter].text[0].length ; l++ ) {
matrix[i][l] = obj_msg_info[counter].text[0][str_count];
str_count++;
}
j=l;
if (obj_msg_info[counter].type == "obj")
matrix[i][j]="]";
else
matrix[i][j]="(";
obj_msg_info[counter].cell_end_x = i;
obj_msg_info[counter].cell_end_y = j;
counter+=1;
}
else {
if (obj_msg_info[counter].x == obj_msg_info[counter-1].x) {
j += 5;
j += (obj_msg_info[counter].y - obj_msg_info[counter-1].y)/50;
j = Math.floor(j);
obj_msg_info[counter].cell_start_x = i;
obj_msg_info[counter].cell_start_y = j;
matrix[i][j] = "[";
j++;
str_count=0;
for ( l = j ; l < j + obj_msg_info[counter].text[0].length ; l++ ) {
matrix[i][l] = obj_msg_info[counter].text[0][str_count];
str_count++;
}
j=l;
if (obj_msg_info[counter].type == "obj")
matrix[i][j]="]";
else
matrix[i][j]="(";
obj_msg_info[counter].cell_end_x = i;
obj_msg_info[counter].cell_end_y = j;
}
else {
i += 3;
i += (obj_msg_info[counter].x - obj_msg_info[counter-1].x)/10;
i = Math.floor(i);
j = ( obj_msg_info[counter].y)/50;
j = Math.floor(j);
obj_msg_info[counter].cell_start_x = i;
obj_msg_info[counter].cell_start_y = j;
matrix[i][j] = "[";
j++;
str_count=0;
for ( l = j ; l < j + obj_msg_info[counter].text[0].length ; l++ ) {
matrix[i][l] = obj_msg_info[counter].text[0][str_count];
str_count++;
}
j=l;
if (obj_msg_info[counter].type == "obj")
matrix[i][j]="]";
else
matrix[i][j]="(";
obj_msg_info[counter].cell_end_x = i;
obj_msg_info[counter].cell_end_y = j;
}
counter+=1;
}
}
for ( i = 0 ; i < obj_msg_info.length ; i++ ) {
//alert([obj_msg_info[i].obj_no,obj_msg_info[i].cell_start_x,obj_msg_info[i].cell_start_y,obj_msg_info[i].cell_end_x,obj_msg_info[i].cell_end_y]);
object_msg_index_info[obj_msg_info[i].obj_no] = {cell_start_x:obj_msg_info[i].cell_start_x,
cell_start_y:obj_msg_info[i].cell_start_y,
cell_end_x:obj_msg_info[i].cell_end_x,
cell_end_y:obj_msg_info[i].cell_end_y};
}
for ( i = 0 ; i < connection_info.length ; i++ ) {
if ( object_msg_index_info[connection_info[i].start].cell_start_x < object_msg_index_info[connection_info[i].end].cell_start_x ) {
if ( connection_info[i].point1 == 0 && connection_info[i].point2 == 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start].cell_start_x, object_msg_index_info[connection_info[i].start].cell_start_y, object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y );
} else if ( connection_info[i].point1 == 0 && connection_info[i].point2 != 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start].cell_start_x, object_msg_index_info[connection_info[i].start].cell_start_y, object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y );
} else if ( connection_info[i].point1 != 0 && connection_info[i].point2 == 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start].cell_end_x, object_msg_index_info[connection_info[i].start].cell_end_y, object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y );
} else {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start].cell_end_x, object_msg_index_info[connection_info[i].start].cell_end_y, object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y );
}
} else {
if ( connection_info[i].point1 == 0 && connection_info[i].point2 == 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y,
object_msg_index_info[connection_info[i].start].cell_start_x, object_msg_index_info[connection_info[i].start].cell_start_y);
} else if ( connection_info[i].point1 == 0 && connection_info[i].point2 != 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y,
object_msg_index_info[connection_info[i].start].cell_start_x, object_msg_index_info[connection_info[i].start].cell_start_y);
} else if ( connection_info[i].point1 != 0 && connection_info[i].point2 == 0 ) {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end].cell_start_x, object_msg_index_info[connection_info[i].end].cell_start_y,
object_msg_index_info[connection_info[i].start].cell_end_x, object_msg_index_info[connection_info[i].start].cell_end_y);
} else {
make_connection(matrix,connection_info[i], object_msg_index_info[connection_info[i].end], object_msg_index_info[connection_info[i].start], object_msg_index_info[connection_info[i].end].cell_end_x , object_msg_index_info[connection_info[i].end].cell_end_y, object_msg_index_info[connection_info[i].start].cell_end_x, object_msg_index_info[connection_info[i].start].cell_end_y );
}
}
}
return matrix;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment