// Paper.cpp: implementation of the CPaper class. // ////////////////////////////////////////////////////////////////////// #include "Paper.h" #include "math.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CPaper::CPaper(int ix,int iy,int iLayerWidth,int iLayerHeight,int iwidth,int iheight,UINT uiBG): CPanel(ix,iy,iwidth,iheight,uiBG) { iLayerX=0; iLayerY=0; CPanel::iLayerHeight=iLayerHeight; CPanel::iLayerWidth=iLayerWidth; HDC hDC=GetDC(GameEngine::GetEngine()->GetWindow()); bmpBackGround=new Bitmap(hDC,IDB_TREE_CANVAS, GameEngine::GetEngine()->GetInstance()); bmpLittle=new Bitmap(hDC,IDB_TREE_CANVAS_LITTLE,GameEngine::GetEngine()->GetInstance()); CPanel::ShowPanel(true); countOrgElements=0; countOrgBonds=0; tempStart=NULL; int i=0; for(;iGetInstance()); } //ReleaseDC(GameEngine::GetEngine()->GetWindow(),hDC); term=lastTime=0; highStart=NULL; bMouseDown=false; movementStart=NULL; countMovingBonds=countMovingElements=0; bMoving=false; links[0]=4; links[1]=1; links[2]=1; links[3]=2; links[4]=3; links[5]=1; links[6]=1; links[7]=2; links[8]=1; links[9]=1; links[10]=1; links[11]=1; } CPaper::~CPaper() { delete bmpBackGround; } void CPaper::DrawScreen(HDC hDC){ HDC _hLayerDC = CreateCompatibleDC(hDC); HBITMAP _hLayerBitmap = CreateCompatibleBitmap(hDC,iLayerWidth, iLayerHeight); SelectObject(_hLayerDC, _hLayerBitmap); //do painting int i = 0; for(i=0;i<=iLayerWidth/90;i++){ for(int j=0;j<=iLayerHeight/90;j++){ bmpBackGround->Draw(_hLayerDC,i*90,j*90); } } for(i=0;igetSelectedControl()==2 && orgBonds[i]->selected && bMoving){ if(orgBonds[i]->EndBondIndex==-1 || orgBonds[i]->StartBondIndex==-1){ continue; } } DrawBond(_hLayerDC,orgBonds[i]->iType+1,orgBonds[i]->start,orgBonds[i]->end,orgBonds[i]->selected); } for(i=0;iorgBond->start.x; pStart.y=movingBonds[i]->orgBond->start.y; pEnd.x=movingBonds[i]->orgBond->end.x; pEnd.y=movingBonds[i]->orgBond->end.y; if(movingBonds[i]->hasStart){ pStart.x=movingBonds[i]->newStart.x; pStart.y=movingBonds[i]->newStart.y; } if(movingBonds[i]->hasEnd){ pEnd.x=movingBonds[i]->newEnd.x; pEnd.y=movingBonds[i]->newEnd.y; } DrawBond(_hLayerDC,movingBonds[i]->orgBond->iType+1,pStart,pEnd,true); } for(i=0;igetSelectedControl()==2 && orgElements[i]->selected && bMoving) continue; bmpLittle->Draw(_hLayerDC,orgElements[i]->x-bmpLittle->GetWidth()/2, orgElements[i]->y-bmpLittle->GetHeight()/2); Bitmap **bmp; if(!orgElements[i]->selected) bmp=bmpElements; else bmp=xbmpElements; bmp[orgElements[i]->iType]->Draw(_hLayerDC,orgElements[i]->x-bmpElements[0]->GetWidth()/2, orgElements[i]->y-bmpElements[0]->GetHeight()/2,true); } for(i=0;iDraw(_hLayerDC,movingElements[i]->newx-bmpLittle->GetWidth()/2, movingElements[i]->newy-bmpLittle->GetHeight()/2); xbmpElements[movingElements[i]->orgElement->iType]->Draw(_hLayerDC,movingElements[i]->newx-bmpElements[0]->GetWidth()/2, movingElements[i]->newy-bmpElements[0]->GetHeight()/2,true); } if(tempStart!=NULL){ DrawBond(_hLayerDC,GameEngine::GetEngine()->getSelectedBond()+1,*tempStart,tempEnd,false); } HighlightLine(_hLayerDC); CPanel::DrawScreen(_hLayerDC); DeleteObject(_hLayerBitmap); DeleteDC(_hLayerDC); } void CPaper::MouseMove(int x,int y) { if(x>GetX() && xGetY() && yx; int yOffSet=iy-movementStart->y; int i=0; for(;iorgElement; movingElements[i]->newx=xOffSet+element->x; movingElements[i]->newy=yOffSet+element->y; for(int j=0;jbondsCount;j++){ if(element->bonds[j]->StartBondIndex==movingElements[i]->eleIndex){ element->bonds[j]->start.x=element->x+xOffSet; element->bonds[j]->start.y=element->y+yOffSet; } else if(element->bonds[j]->EndBondIndex==movingElements[i]->eleIndex){ element->bonds[j]->end.x=element->x+xOffSet; element->bonds[j]->end.y=element->y+yOffSet; } } } for(i=0;iorgBond; if(movingBonds[i]->hasStart){ movingBonds[i]->newStart.x=xOffSet+bond->start.x; movingBonds[i]->newStart.y=yOffSet+bond->start.y; } if(movingBonds[i]->hasEnd){ movingBonds[i]->newEnd.x=xOffSet+bond->end.x; movingBonds[i]->newEnd.y=yOffSet+bond->end.y; } } } CPanel::MouseMove(x,y); } int CPaper::MouseButtonDown(int x, int y, bool bLeft,WPARAM wParam) { bool disSelect=true; if(x>GetX() && xGetY() && ygetSelectedElement())>=0){ bool bReplace=false; OrgElement *thisElement=(OrgElement*)malloc(sizeof(OrgElement)); thisElement->x=x-GetX()+iLayerX; thisElement->y=y-GetY()+iLayerY; thisElement->iType=iSelected; thisElement->selected=false; thisElement->bondsCount=0; int i=0; for(;ix-orgElements[i]->x)<10 && abs(thisElement->y-orgElements[i]->y)<10){ int bondsCount=0; for(int j=0;jbondsCount;j++){ bondsCount+=orgElements[i]->bonds[j]->iType+1; } if(bondsCount>links[iSelected]){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Too many Bonds. Element can not be replaced","Info",MB_OK); //Let things happen as it was replaced; } else{ orgElements[i]->iType=thisElement->iType; } free(thisElement); bReplace=true; break; } } for(i=0;iend.x-thisElement->x)<10 && abs(orgBonds[i]->end.y-thisElement->y)<10){ int bondsCount=0; for(int j=0;jbondsCount;j++){ bondsCount+=thisElement->bonds[j]->iType+1; } if(bondsCount+orgBonds[i]->iType+1>links[iSelected]){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Element will not be Snaped to some bonds. It has reached its maximum","Info",MB_OK); bReplace=true; break; } thisElement->x=orgBonds[i]->end.x; thisElement->y=orgBonds[i]->end.y; if(thisElement->bondsCount==0) thisElement->bonds=(OrgBond**)malloc(sizeof(OrgBond*)*(thisElement->bondsCount+1)); else thisElement->bonds=(OrgBond**)realloc(thisElement->bonds,sizeof(OrgBond*)*(thisElement->bondsCount+1)); thisElement->bonds[thisElement->bondsCount]=orgBonds[i]; thisElement->bondsCount++; orgBonds[i]->EndBondIndex=countOrgElements; //break; } if(abs(orgBonds[i]->start.x-thisElement->x)<10 && abs(orgBonds[i]->start.y-thisElement->y)<10){ int bondsCount=0; for(int j=0;jbondsCount;j++){ bondsCount+=thisElement->bonds[j]->iType+1; } if(bondsCount+orgBonds[i]->iType+1>links[iSelected]){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Element will not be Snaped to some bonds. It has reached its maximum","Info",MB_OK); bReplace=true; break; } thisElement->x=orgBonds[i]->start.x; thisElement->y=orgBonds[i]->start.y; if(thisElement->bondsCount==0) thisElement->bonds=(OrgBond**)malloc(sizeof(OrgBond*)*(thisElement->bondsCount+1)); else thisElement->bonds=(OrgBond**)realloc(thisElement->bonds,sizeof(OrgBond*)*(thisElement->bondsCount+1)); thisElement->bonds[thisElement->bondsCount]=orgBonds[i]; thisElement->bondsCount++; orgBonds[i]->StartBondIndex=countOrgElements; //break; } } if(!bReplace){ if(countOrgElements==0){//add a new Element orgElements=((OrgElement**)malloc(sizeof(OrgElement*)*(countOrgElements+1))); }else{ orgElements=(OrgElement**)realloc(orgElements,sizeof(OrgElement*)*(countOrgElements+1)); } orgElements[countOrgElements]=thisElement; countOrgElements++; } } else if((iSelected=GameEngine::GetEngine()->getSelectedBond())>=0){ if(tempStart==NULL){ bool bValid=false; tempStart=(POINT*)malloc(sizeof(POINT)); tempStart->x=x-GetX()+iLayerX; tempStart->y=y-GetY()+iLayerY; int i=0; for(;ix-orgElements[i]->x)<10 && abs(tempStart->y-orgElements[i]->y)<10)){ int bondsCount=0; for(int j=0;jbondsCount;j++){ bondsCount+=orgElements[i]->bonds[j]->iType+1; } if(bondsCount+GameEngine::GetEngine()->getSelectedBond()+1>links[orgElements[i]->iType]){ MessageBox(GameEngine::GetEngine()->GetWindow(),"This bond can not be added to the element","Info",MB_OK); bValid=true; free(tempStart); tempStart=NULL; break; } tempEnd.x=tempStart->x=orgElements[i]->x; tempEnd.y=tempStart->y=orgElements[i]->y; startBondIndex=i; bValid=true; break; } } if(!bValid){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Bond must start from an Element","Info",MB_OK); free(tempStart); tempStart=NULL; } } } else if((iSelected=GameEngine::GetEngine()->getSelectedControl())>=0){ int ix=x-GetX()+iLayerX; int iy=y-GetY()+iLayerY; bool bAlreadyMarked=false; int iMarked=-1; int iMarkedBond=-1; //ifAlreadyMarked int i; switch(iSelected){ case 0: for(i=0;ix)<10 && abs(iy-orgElements[i]->y)<10){ iMarked=i; break; } } if(iMarked<0){ for(i=0;istart.x)*(orgBonds[i]->end.x-ix)>-100 && (iy-orgBonds[i]->start.y)*(orgBonds[i]->end.y-iy)>-100 && abs( (ix-orgBonds[i]->start.x)*(orgBonds[i]->end.y-iy)-(orgBonds[i]->end.x-ix)*(iy-orgBonds[i]->start.y) )<500 ){ iMarkedBond=i; break; } } } if(!(wParam & MK_CONTROL)){ int i=0; for(;iselected=false; } for(i=0;iselected=false; } } if(iMarked<0 && iMarkedBond<0){//still zero break; } if(!(iMarked<0)){ if(orgElements[iMarked]->selected){ orgElements[iMarked]->selected=false; bAlreadyMarked=true; } if(!bAlreadyMarked){ orgElements[iMarked]->selected=true; } } else if(!(iMarkedBond<0)){ if(orgBonds[iMarkedBond]->selected){ orgBonds[iMarkedBond]->selected=false; bAlreadyMarked=true; } if(!bAlreadyMarked){ orgBonds[iMarkedBond]->selected=true; } } break; case 1: highStart=(POINT*)malloc(sizeof(POINT)); highEnd.x=highStart->x=ix; highEnd.y=highStart->y=iy; disSelect=false; bMouseDown=true; if(!(wParam & MK_CONTROL)){ int i=0; for(;iselected=false; } for(i=0;iselected=false; } } break; case 2: movementStart=(POINT*)malloc(sizeof(POINT)); movementStart->x=ix; movementStart->y=iy; int i=0; for(;iselected){ MovingElement *thisElement=(MovingElement*)malloc(sizeof(MovingElement)); thisElement->orgElement=orgElements[i]; thisElement->newx=orgElements[i]->x; thisElement->newy=orgElements[i]->y; thisElement->eleIndex=i; if(countMovingElements==0){ movingElements=((MovingElement**)malloc(sizeof(MovingElement*)*(countMovingElements+1))); }else{ movingElements=(MovingElement**)realloc(movingElements,sizeof(MovingElement*)*(countMovingElements+1)); } movingElements[countMovingElements]=thisElement; countMovingElements++; } } for(i=0;iselected){ MovingBond *thisBond=(MovingBond*)malloc(sizeof(MovingBond)); thisBond->hasEnd=false; thisBond->hasStart=false; bool alone=false; if(orgBonds[i]->StartBondIndex==-1){ thisBond->orgBond=orgBonds[i]; thisBond->newStart.x=orgBonds[i]->start.x; thisBond->newStart.y=orgBonds[i]->start.y; thisBond->hasStart=alone=true; } if(orgBonds[i]->EndBondIndex==-1){ thisBond->orgBond=orgBonds[i]; thisBond->newEnd.x=orgBonds[i]->end.x; thisBond->newEnd.y=orgBonds[i]->end.y; thisBond->hasEnd=alone=true; } if(alone){ if(countMovingBonds==0){ movingBonds=(MovingBond**)malloc(sizeof(MovingBond*)); }else{ movingBonds=(MovingBond**)realloc(movingBonds,sizeof(MovingBond*)*(countMovingBonds+1)); } movingBonds[countMovingBonds]=thisBond; countMovingBonds++; }else{ free(thisBond); } } } bMoving=true; break; } } } if((GameEngine::GetEngine()->getSelectedControl())==3){ int i=0; for(;iselected){ int j = 0; for(j=0;jbondsCount;j++){ if(orgElements[i]->bonds[j]->StartBondIndex==i){ orgElements[i]->bonds[j]->StartBondIndex=-1; } if(orgElements[i]->bonds[j]->EndBondIndex==i){ orgElements[i]->bonds[j]->EndBondIndex=-1; } } free(orgElements[i]); countOrgElements--; for(j=i;jbondsCount;k++){ if(orgElements[j]->bonds[k]->StartBondIndex==j+1) orgElements[j]->bonds[k]->StartBondIndex=j; else if(orgElements[j]->bonds[k]->EndBondIndex==j+1) orgElements[j]->bonds[k]->EndBondIndex=j; } } i--; orgElements=(OrgElement**)realloc(orgElements,sizeof(OrgElement*)*countOrgElements); } } for(i=0;iselected){ if(orgBonds[i]->StartBondIndex>=0){ OrgElement* element=orgElements[orgBonds[i]->StartBondIndex]; int j = 0; for(j=0;jbondsCount;j++){//Searching corrosBond if(element->bonds[j]==orgBonds[i]){ for(int k=j;kbondsCount;k++){ element->bonds[k]=element->bonds[k+1]; } element->bondsCount-=1; element->bonds=(OrgBond**)realloc(element->bonds,sizeof(OrgBond*)*element->bondsCount); break; } } } if(orgBonds[i]->EndBondIndex>=0){ OrgElement* element=orgElements[orgBonds[i]->EndBondIndex]; int j = 0; for(j=0;jbondsCount;j++){//Searching corrosBond if(element->bonds[j]==orgBonds[i]){ for(int k=j;kbondsCount;k++){ element->bonds[k]=element->bonds[k+1]; } element->bondsCount-=1; element->bonds=(OrgBond**)realloc(element->bonds,sizeof(OrgBond*)*element->bondsCount); break; } } } free(orgBonds[i]); countOrgBonds--; int j = 0; for(j=i;jgetSelectedControl()==4){ if(countOrgElements<=0){ MessageBox(GameEngine::GetEngine()->GetWindow(),"You should add one or more elements","Info",MB_OK); return -1; } bool anyCarbon=false; int i=0; for(;iiType==0){ anyCarbon=true; break; } } if(!anyCarbon){ MessageBox(GameEngine::GetEngine()->GetWindow(),"You need to add at least 1 Carbon","Info",MB_OK); return -1; } for(i=0;iStartBondIndex==-1 && orgBonds[i]->EndBondIndex==-1){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Some of bonds are still isolated","Info",MB_OK); orgBonds[i]->selected=true; return -1; } } for(i=0;iStartBondIndex==-1){ for(int j=0;jEndBondIndex) continue; if(abs(orgElements[j]->x-orgBonds[i]->start.x)<10 && abs(orgElements[j]->y-orgBonds[i]->start.y)<10){ int effectiveBonds=0; for(int k=0;kbondsCount;k++){ effectiveBonds+=orgElements[j]->bonds[k]->iType+1; } effectiveBonds+=orgBonds[i]->iType+1; if(effectiveBonds<=links[orgElements[j]->iType]){ if(orgElements[j]->bondsCount==0){ orgElements[j]->bonds=(OrgBond**)malloc(sizeof(OrgBond*)); }else{ orgElements[j]->bonds=(OrgBond**)realloc(orgElements[j]->bonds,sizeof(OrgBond*)*(orgElements[j]->bondsCount+1)); } orgElements[j]->bonds[orgElements[j]->bondsCount]=orgBonds[i]; orgElements[j]->bondsCount++; orgBonds[i]->StartBondIndex=j; orgBonds[i]->start.x=orgElements[j]->x; orgBonds[i]->start.y=orgElements[j]->y; break; } } } } if(orgBonds[i]->EndBondIndex==-1){ for(int j=0;jStartBondIndex) continue; if(abs(orgElements[j]->x-orgBonds[i]->end.x)<10 && abs(orgElements[j]->y-orgBonds[i]->end.y)<10){ int effectiveBonds=0; for(int k=0;kbondsCount;k++){ effectiveBonds+=orgElements[j]->bonds[k]->iType+1; } effectiveBonds+=orgBonds[i]->iType+1; if(effectiveBonds<=links[orgElements[j]->iType]){ if(orgElements[j]->bondsCount==0){ orgElements[j]->bonds=(OrgBond**)malloc(sizeof(OrgBond*)); }else{ orgElements[j]->bonds=(OrgBond**)realloc(orgElements[j]->bonds,sizeof(OrgBond*)*(orgElements[j]->bondsCount+1)); } orgElements[j]->bonds[orgElements[j]->bondsCount]=orgBonds[i]; orgElements[j]->bondsCount++; orgBonds[i]->EndBondIndex=j; orgBonds[i]->end.x=orgElements[j]->x; orgBonds[i]->end.y=orgElements[j]->y; break; } } } } } for(i=0;iselected=false; } int countSelectedElement=checkingForLoops(-1,0); for(i=0;iselected=false; } if(countSelectedElement==-1){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Loops in the compound are not allowed","Info",MB_OK); return -1; } if(countSelectedElement!=countOrgElements){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Some element(s) is/are isolated. fail to return","Info",MB_OK); return -1; } for(i=0;iStartBondIndex==-1){ OrgElement* element=(OrgElement*)malloc(sizeof(OrgElement)); //adding a hydrogen or oxygen or nitrogen orgElements=(OrgElement**)realloc(orgElements,sizeof(OrgElement*)*(countOrgElements+1)); switch(orgBonds[i]->iType){ case 0: element->iType=1; break; case 1: element->iType=7; break; case 2: element->iType=4; break; } if(orgBonds[i]->EndBondIndex!=-1 && orgElements[orgBonds[i]->EndBondIndex]->iType==3) //if it is a Mg element->iType=11; //add a x element->bonds=(OrgBond**)malloc(sizeof(OrgBond*)); element->bonds[0]=orgBonds[i]; element->bondsCount=1; orgElements[countOrgElements]=element; orgBonds[i]->StartBondIndex=countOrgElements; countOrgElements++; } if(orgBonds[i]->EndBondIndex==-1){ OrgElement* element=(OrgElement*)malloc(sizeof(OrgElement)); //adding a hydrogen orgElements=(OrgElement**)realloc(orgElements,sizeof(OrgElement*)*(countOrgElements+1)); switch(orgBonds[i]->iType){ case 0: element->iType=1; break; case 1: element->iType=7; break; case 2: element->iType=4; break; } if(orgBonds[i]->StartBondIndex!=-1 && orgElements[orgBonds[i]->StartBondIndex]->iType==3) //if it is a Mg element->iType=11; //add a x element->bonds=(OrgBond**)malloc(sizeof(OrgBond*)); element->bonds[0]=orgBonds[i]; element->bondsCount=1; orgElements[countOrgElements]=element; orgBonds[i]->EndBondIndex=countOrgElements; countOrgElements++; } } for(i=0;ibondsCount;j++){ effectiveBondCount+=orgElements[i]->bonds[j]->iType+1; } while(effectiveBondCountiType]){ OrgBond* bond=(OrgBond*)malloc(sizeof(OrgBond)); //adding a single bond bond->iType=0; if(countOrgBonds==0){ orgBonds=(OrgBond**)malloc(sizeof(OrgBond*)); }else{ orgBonds=(OrgBond**)realloc(orgBonds,sizeof(OrgBond*)*(countOrgBonds+1)); } OrgElement* element=(OrgElement*)malloc(sizeof(OrgElement)); //adding a hydrogen orgElements=(OrgElement**)realloc(orgElements,sizeof(OrgElement*)*(countOrgElements+1)); element->iType=1; if(orgElements[i]->iType==3) //if it is a Mg element->iType=11; //add a x element->bonds=(OrgBond**)malloc(sizeof(OrgBond*)); element->bonds[0]=bond; element->bondsCount=1; orgElements[countOrgElements]=element; if(orgElements[i]->bondsCount==0){ orgElements[i]->bonds=(OrgBond**)malloc(sizeof(OrgBond*)); } else{ orgElements[i]->bonds=(OrgBond**)realloc(orgElements[i]->bonds,sizeof(OrgBond*)*(orgElements[i]->bondsCount+1)); } orgElements[i]->bonds[orgElements[i]->bondsCount]=bond; orgElements[i]->bondsCount++; effectiveBondCount++; bond->StartBondIndex=i; bond->EndBondIndex=countOrgElements; orgBonds[countOrgBonds]=bond; countOrgBonds++; countOrgElements++; } } GameEngine::GetEngine()->setCountElements(countOrgElements); GameEngine::GetEngine()->setCountBonds(countOrgBonds); GameEngine::GetEngine()->setOrgElements(orgElements); GameEngine::GetEngine()->setOrgBonds(orgBonds); countOrgElements=0; countOrgBonds=0; //ClearAll(); return 1; } if(GameEngine::GetEngine()->getSelectedControl()==5){ ClearAll(); } if(GameEngine::GetEngine()->getSelectedElement()>=0 || GameEngine::GetEngine()->getSelectedBond()>=0){ int i=0; for(;iselected=false; for(i=0;iselected=false; } if(highStart && disSelect){ free(highStart); highStart=NULL; } CPanel::MouseButtonDown(x,y,bLeft); return 0; } void CPaper::MouseButtonUp(int x, int y, bool bLeft) { if(x>GetX() && xGetY() && yorgElement->x=movingElements[i]->newx; int iy=movingElements[i]->orgElement->y=movingElements[i]->newy; } for(i=0;ihasStart){ int ix=movingBonds[i]->orgBond->start.x=movingBonds[i]->newStart.x; int iy=movingBonds[i]->orgBond->start.y=movingBonds[i]->newStart.y; } if(movingBonds[i]->hasEnd){ movingBonds[i]->orgBond->end.x=movingBonds[i]->newEnd.x; movingBonds[i]->orgBond->end.y=movingBonds[i]->newEnd.y; } } } if(tempStart!=NULL){ POINT *end; end=(POINT*)malloc(sizeof(POINT)); end->x=x-GetX()+iLayerX; end->y=y-GetY()+iLayerY; OrgBond * thisBond=(OrgBond*)malloc(sizeof(OrgBond)); thisBond->end=*end; thisBond->start=*tempStart; thisBond->iType=GameEngine::GetEngine()->getSelectedBond(); thisBond->selected=false; thisBond->StartBondIndex=-1; thisBond->EndBondIndex=-1; bool bReplace=false; int i=0; for(;istart.x-thisBond->start.x)<10 && abs(orgBonds[i]->start.y-thisBond->start.y)<10 && abs(orgBonds[i]->end.x-thisBond->end.x)<10 && abs(orgBonds[i]->end.y-thisBond->end.y)<10) || (abs(orgBonds[i]->end.x-thisBond->start.x)<10 && abs(orgBonds[i]->end.y-thisBond->start.y)<10 && abs(orgBonds[i]->start.x-thisBond->end.x)<10 && abs(orgBonds[i]->start.y-thisBond->end.y)<10)) { int temp=orgBonds[i]->iType; orgBonds[i]->iType=thisBond->iType; if(orgBonds[i]->StartBondIndex>=0){ OrgElement * startElement=orgElements[orgBonds[i]->StartBondIndex]; int bondsCount=0; for(int j=0;jbondsCount;j++){ bondsCount+=startElement->bonds[j]->iType+1; } if((bondsCount)>links[startElement->iType]){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Bond can not replaced. It exceed the maximum of some elements","Info",MB_OK); orgBonds[i]->iType=temp;// and let happen as it was replaced } } if(orgBonds[i]->EndBondIndex>=0){ OrgElement * endElement=orgElements[orgBonds[i]->EndBondIndex]; int bondsCount=0; for(int j=0;jbondsCount;j++){ bondsCount+=endElement->bonds[j]->iType+1; } if((bondsCount)>links[endElement->iType]){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Bond can not replaced. It exceed the maximum of some elements","Info",MB_OK); orgBonds[i]->iType=temp;// and let happen as it was replaced } } free(tempStart); free(end); free(thisBond); bReplace=true; break; } } bool validBond=true; if(!bReplace){ for(i=0;ix-orgElements[i]->x)<10 && abs(end->y-orgElements[i]->y)<10)){ if(i==startBondIndex){ free(tempStart); free(end); free(thisBond); validBond=false; break; } int bondsCount=0; for(int j=0;jbondsCount;j++){ bondsCount+=orgElements[i]->bonds[j]->iType+1; } if((bondsCount+thisBond->iType+1)>links[orgElements[i]->iType]){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Bond can not attached to the element. It exceed maximum bonds for some elements","Info",MB_OK); free(tempStart); free(end); free(thisBond); validBond=false; break; } thisBond->end.x=orgElements[i]->x; thisBond->end.y=orgElements[i]->y; if(orgElements[i]->bondsCount==0){ orgElements[i]->bonds=(OrgBond**)malloc(sizeof(OrgBond*)); } else{ orgElements[i]->bonds=(OrgBond**)realloc(orgElements[i]->bonds,sizeof(OrgBond*)*(orgElements[i]->bondsCount+1)); } orgElements[i]->bonds[orgElements[i]->bondsCount]=thisBond; orgElements[i]->bondsCount++; thisBond->EndBondIndex=i; break; } } if(validBond){ if(countOrgBonds==0){ orgBonds=((OrgBond**)malloc(sizeof(OrgBond*)*(countOrgBonds+1))); }else{ orgBonds=(OrgBond**)realloc(orgBonds,sizeof(OrgBond*)*(countOrgBonds+1)); } orgBonds[countOrgBonds]=thisBond; countOrgBonds++; ///////// //*assigning the bond to starting element if(orgElements[startBondIndex]->bondsCount==0){ orgElements[startBondIndex]->bonds=(OrgBond**)malloc(sizeof(OrgBond*)); } else{ orgElements[startBondIndex]->bonds=(OrgBond**)realloc(orgElements[startBondIndex]->bonds,sizeof(OrgBond*)*(orgElements[startBondIndex]->bondsCount+1)); } orgElements[startBondIndex]->bonds[orgElements[startBondIndex]->bondsCount]=thisBond; orgElements[startBondIndex]->bondsCount++; thisBond->StartBondIndex=startBondIndex; } } tempStart=NULL; } }else{ if(tempStart!=NULL){ free(tempStart); tempStart=NULL; } } if(highStart){ highEnd.x=x-GetX()+iLayerX; highEnd.y=y-GetY()+iLayerY; bMouseDown=false; int i=0; for(;ix)*(orgElements[i]->x-highStart->x)>0 && (highEnd.y-orgElements[i]->y)*(orgElements[i]->y-highStart->y)>0){ orgElements[i]->selected=true; } } for(i=0;istart.x)*(orgBonds[i]->start.x-highStart->x)>0 && (highEnd.y-orgBonds[i]->start.y)*(orgBonds[i]->start.y-highStart->y)>0) || ((highEnd.x-orgBonds[i]->end.x)*(orgBonds[i]->end.x-highStart->x)>0 && (highEnd.y-orgBonds[i]->end.y)*(orgBonds[i]->end.y-highStart->y)>0)) { orgBonds[i]->selected=true; } } } if(bMoving){ int i=0; for(;iorgElement; for(int j=0;jbondsCount;j++){ if(movingElements[i]->eleIndex==element->bonds[j]->StartBondIndex){ element->bonds[j]->start.x=element->x; element->bonds[j]->start.y=element->y; } else if(movingElements[i]->eleIndex==element->bonds[j]->EndBondIndex){ element->bonds[j]->end.x=element->x; element->bonds[j]->end.y=element->y; } } free(movingElements[i]); } if(countMovingElements)free(movingElements); countMovingElements=0; for(i=0;iabs(ptStart.y-ptEnd.y)){ MoveToEx(hDC,ptStart.x,ptStart.y+2,NULL); LineTo(hDC,ptEnd.x,ptEnd.y+2); MoveToEx(hDC,ptStart.x,ptStart.y-2,NULL); LineTo(hDC,ptEnd.x,ptEnd.y-2); } else{ MoveToEx(hDC,ptStart.x+2,ptStart.y,NULL); LineTo(hDC,ptEnd.x+2,ptEnd.y); MoveToEx(hDC,ptStart.x-2,ptStart.y,NULL); LineTo(hDC,ptEnd.x-2,ptEnd.y); } } else if(iBondType==3){ if(abs(ptStart.x-ptEnd.x)>abs(ptStart.y-ptEnd.y)){ MoveToEx(hDC, ptStart.x,ptStart.y-3, NULL); LineTo(hDC, ptEnd.x,ptEnd.y-3); MoveToEx(hDC, ptStart.x,ptStart.y, NULL); LineTo(hDC, ptEnd.x,ptEnd.y); MoveToEx(hDC, ptStart.x,ptStart.y+3, NULL); LineTo(hDC, ptEnd.x,ptEnd.y+3); } else{ MoveToEx(hDC, ptStart.x-3,ptStart.y, NULL); LineTo(hDC, ptEnd.x-3,ptEnd.y); MoveToEx(hDC, ptStart.x,ptStart.y, NULL); LineTo(hDC, ptEnd.x,ptEnd.y); MoveToEx(hDC, ptStart.x+3,ptStart.y, NULL); LineTo(hDC, ptEnd.x+3,ptEnd.y); } } SelectObject(hDC, hPen); DeleteObject(hGridPen); } void CPaper::HighlightLine(HDC hDC) { if(highStart==NULL) return; int time=GetTickCount(); int length=6; static int counter=0; if((time-lastTime)>50){ if(counter<10) (term<3)?term++:term=0; else (term>0)?term--:term=3; lastTime=time; counter<19?counter++:counter=0; } int XUp,YUp,XRight,YRight,XDown,YDown,XLeft,YLeft; int temp; bool xInterChanged=false; bool yInterChanged=false; if(highStart->x>highEnd.x){ temp=highEnd.x; highEnd.x=highStart->x; highStart->x=temp; xInterChanged=true; } if(highStart->y>highEnd.y){ temp=highEnd.y; highEnd.y=highStart->y; highStart->y=temp; yInterChanged=true; } switch(term){ case 0: XUp=highStart->x; YUp=highStart->y; XRight=highEnd.x; YRight=highStart->y; XDown=highEnd.x; YDown=highEnd.y; XLeft=highStart->x; YLeft=highEnd.y; break; case 1: XUp=highStart->x+length/2; YUp=highStart->y; XRight=highEnd.x; YRight=highStart->y+length/2; XDown=highEnd.x-length/2; YDown=highEnd.y; XLeft=highStart->x; YLeft=highEnd.y-length/2; break; case 2: XUp=highStart->x+length; YUp=highStart->y; XRight=highEnd.x; YRight=highStart->y+length; XDown=highEnd.x-length; YDown=highEnd.y; XLeft=highStart->x; YLeft=highEnd.y-length; break; case 3: XUp=highStart->x+length*3/2; YUp=highStart->y; if((highEnd.x-highStart->x)>=length*2)DrawLine(hDC,highStart->x,YUp,highStart->x+length/2,YUp); XRight=highEnd.x; YRight=highStart->y+length*3/2; if((highEnd.y-highStart->y)>=length*2)DrawLine(hDC,XRight,highStart->y,XRight,highStart->y+length/2); XDown=highEnd.x-length*3/2; YDown=highEnd.y; if((highEnd.x-highStart->x)>=length*2)DrawLine(hDC,highEnd.x,YDown,highEnd.x-length/2,YDown); XLeft=highStart->x; YLeft=highEnd.y-length*3/2; if((highEnd.y-highStart->y)>=length*2)DrawLine(hDC,XLeft,highEnd.y,XLeft,highEnd.y-length/2); break; } while(true){ if(XUp+length*2>=highEnd.x){ //at the end DrawLine(hDC,XUp,YUp,highEnd.xx>XDown-length?highStart->x:XDown-length,YDown); break; } else{ DrawLine(hDC,XUp,YUp,XUp+length,YUp); DrawLine(hDC,XDown,YDown,XDown-length,YDown); XUp+=length*2; XDown-=length*2; } } while(true){ if(YRight+length*2>=highEnd.y){ //at the end DrawLine(hDC,XRight,YRight,XRight,highEnd.yy>YLeft-length?highStart->y:YLeft-length); break; } else{ DrawLine(hDC,XRight,YRight,XRight,YRight+length); DrawLine(hDC,XLeft,YLeft,XLeft,YLeft-length); YRight+=length*2; YLeft-=length*2; } } if(xInterChanged){ temp=highEnd.x; highEnd.x=highStart->x; highStart->x=temp; } if(yInterChanged){ temp=highEnd.y; highEnd.y=highStart->y; highStart->y=temp; } } void CPaper::DrawLine(HDC hDC,int startx,int starty,int endx,int endy) { MoveToEx(hDC,startx,starty,NULL); LineTo(hDC,endx,endy); } void CPaper::ClearAll() { int i=0; for(;iselected=true; bool loopExist=false; int i=0; for(;ibondsCount;i++){ int otherIndex; if(element->bonds[i]->StartBondIndex==index){ otherIndex=element->bonds[i]->EndBondIndex; } else if(element->bonds[i]->EndBondIndex==index){ otherIndex=element->bonds[i]->StartBondIndex; } if(otherIndex==-1) continue; if(otherIndex==fromIndex) continue; if(orgElements[otherIndex]->selected){ loopExist=true; break; } else{ if(checkingForLoops(index,otherIndex)==-1){ loopExist=true; break; } } } if(loopExist) return -1; return count; } void CPaper::getReadyForACompound() { orgElements=GameEngine::GetEngine()->getOrgElements(); orgBonds=GameEngine::GetEngine()->getOrgBonds(); countOrgElements=GameEngine::GetEngine()->getCountElements(); countOrgBonds=GameEngine::GetEngine()->getCountBonds(); int i=0; for(;ix+=50; orgElements[i]->y+=50; if(iLayerWidthx) iLayerWidth=orgElements[i]->x; if(iLayerHeighty) iLayerHeight=orgElements[i]->y; } for(i=0;istart.x+=50; orgBonds[i]->start.y+=50; orgBonds[i]->end.x+=50; orgBonds[i]->end.y+=50; if(iLayerWidthstart.x) iLayerWidth=orgBonds[i]->start.x; if(iLayerHeightstart.y) iLayerHeight=orgBonds[i]->start.y; if(iLayerWidthend.x) iLayerWidth=orgBonds[i]->end.x; if(iLayerHeightend.y) iLayerHeight=orgBonds[i]->end.y; } }