I was trying to program a simple draw program that put out nodes and connect them with double chained links, it will break the chain it draw by presssing CTRL and of course you must be able to connect it to a node already there. Also you must be able to start draw from a node already there.
Probably i should done some form of case study and started out there, because the amount of spagetti code this "simple?" task generated is unfatoomable.
Either i started out in wrong end, or the logic behind the linking of nodetree is more complex than i really understand.
I started without any real idea where it should end. Maybe there acutally is more cases than ome recognize? If anyone have an idea howto do this in a more ***structured*** and coherent manner i am all ears. Drawtool is the function called when draw start.
SPAGETTI WARNING!!!
http://anybase.co.nf
function drawTool(){
message2="DRAWTOOL SELECTED";
justdraw=true;
overlapping=false;
for(var b=0;b<arr.length;b++){
if (mousePos.x >= arr[b].rposX && mousePos.x <= (arr[b].rposX + arr[b].rwidth) && mousePos.y >= arr[b].rposY && mousePos.y <= (arr[b].rposY + arr[b].rheigth))
{m=b;justdraw=false;selectednodeoverlap();} else {}
}
if (justdraw==true) {drawLine();justdraw=false;}
}
function selectednodeoverlap() {
if (drawstart == true) {message2="FOCUSED="+m; oldnode=m; drawstart=false;} else {
inarray = arr[m].nodelinks.indexOf(oldnode);
if (inarray == -1 && m!=oldnode) {
arr[oldnode].nodelinks.push(m);
arr[m].nodelinks.push(oldnode);
drawstart=true;
}
}
out_update();
}
function drawLine(){
if (drawstart==true){
standardcolor = rndColor();
m = arr.length;
arr[m] = {
nodelinks: [],
hold: false,
rposX: mousePos.x,
rposY: mousePos.y,
rwidth: 10,
rheigth: 10,
rcolor: standardcolor,
bcolor: standardcolor,
print: true
};
nodes = m + 1;
document.NoNodes.nodeval.value = nodes;
drawstart=false;
out_update();
} else if (drawstart==false) {
standardcolor = rndColor();
m = arr.length;
check_overlapping();
if(overlapping==false){
arr[m] = {
nodelinks: [],
hold: false,
rposX: mousePos.x,
rposY: mousePos.y,
rwidth: 10,
rheigth: 10,
rcolor: standardcolor,
bcolor: standardcolor,
print: true
};
printnode[m] = m;
if(m>0){
inarray = arr[m].nodelinks.indexOf(m-1);
if (inarray == -1) {
arr[m].nodelinks.push(m-1);
arr[m-1].nodelinks.push(m);
oldnode=m;
}
}
nodes = m + 1;
document.NoNodes.nodeval.value = nodes;
out_update();
}
}
}
function check_overlapping(){
//alert("checking");
overlapping=false;
for(var b=0;b<arr.length;b++){
if (mousePos.x >= arr[b].rposX && mousePos.x <= (arr[b].rposX + arr[b].rwidth) && mousePos.y >= arr[b].rposY && mousePos.y <= (arr[b].rposY + arr[b].rheigth))
{
overlapping=true;
inarray = arr[b].nodelinks.indexOf(m-1);
if (inarray == -1) {
arr[m-1].nodelinks.push(b);
arr[b].nodelinks.push(m-1);
m=oldnode;
out_update();
}
}
}
return overlapping;
}