// Canvas.cpp: implementation of the CCanvas class. // ////////////////////////////////////////////////////////////////////// #include "Canvas.h" #define BOND_LENGTH 60 #define OFF_LENGTH 15 #define DUP_LENGTH 9 #define SLIDE_START_X 300 #define SLIDE_START_Y 280 ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CCanvas::CCanvas(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_BG, GameEngine::GetEngine()->GetInstance()); CPanel::ShowPanel(true); //paper=new CPaper(160,20,700,800,620,560,IDB_TREE_BG_LEFTTOP); //countCompounds=0; countChains=0; //countInorgCompounds=countConditions=0; countCompoundCollections=1; compoundCollections=(CompoundCollection**)malloc(sizeof(CompoundCollection*)); compoundCollections[0]=(CompoundCollection*)malloc(sizeof(CompoundCollection)); compoundCollections[0]->countCompounds=compoundCollections[0]->countInorgCompounds=compoundCollections[0]->countConditions=0; IUPACParser=new CFileParser(".\\Data\\IUPAC\\index.txt",".\\Data\\IUPAC\\"); selectedCompound=-1; bmpLittle=new Bitmap(hDC,IDB_TREE_CANVAS_LITTLE_CANVAS,GameEngine::GetEngine()->GetInstance()); bmpElements=new Bitmap*[NUMBER_OF_TOGLLE_BUTTON]; xbmpElements=new Bitmap*[NUMBER_OF_TOGLLE_BUTTON]; int i= 0; for(;iGetInstance()); xbmpElements[i]=new Bitmap(hDC,IDB_XELEMENT_C+i,GameEngine::GetEngine()->GetInstance()); } inorgElements=new Bitmap*[INORG_COMPOUNDS]; for(i=0;iGetInstance()); } conditonElements=new Bitmap*[ADDN_CONDITIONS]; for(i=0;iGetInstance()); } reactionsParser=new CFileParser(".\\Data\\Reactions\\index.txt",".\\Data\\Reactions\\"); isEditable=true; compoundSetIndex=0; ReleaseDC(GameEngine::GetEngine()->GetWindow(),hDC); } CCanvas::~CCanvas() { } void CCanvas::DrawScreen(HDC hDC){ //paper->DrawScreen(hDC); HDC _hLayerDC = CreateCompatibleDC(hDC); HBITMAP _hLayerBitmap = CreateCompatibleBitmap(hDC,iLayerWidth, iLayerHeight); SelectObject(_hLayerDC, _hLayerBitmap); if(compoundCollections[countCompoundCollections-1]->end.y+30>GetHeight()+20){ iLayerHeight=compoundCollections[countCompoundCollections-1]->end.y+20; }else{ iLayerHeight=GetHeight()+20; } //do painting int i=0; for(;i<=iLayerWidth/90;i++){ int j=0; for(;j<=iLayerHeight/90;j++){ bmpBackGround->Draw(_hLayerDC,i*90,j*90); } } //if(isEditable){ int k=0; for(;kcountCompounds;j++){ int i=0; for(;icompounds[j]->bondsCount;i++){ DrawBond(_hLayerDC,compoundCollections[k]->compounds[j]->bonds[i]->iType+1, addPoints(compoundCollections[k]->start,addPoints(compoundCollections[k]->compounds[j]->start,compoundCollections[k]->compounds[j]->bonds[i]->start)), addPoints(compoundCollections[k]->start,addPoints(compoundCollections[k]->compounds[j]->start,compoundCollections[k]->compounds[j]->bonds[i]->end)),compoundCollections[k]->compounds[j]->selected); } for(i=0;icompounds[j]->elementsCount;i++){ bmpLittle->Draw(_hLayerDC,compoundCollections[k]->start.x+compoundCollections[k]->compounds[j]->start.x+compoundCollections[k]->compounds[j]->elements[i]->x-bmpLittle->GetWidth()/2, compoundCollections[k]->start.y+compoundCollections[k]->compounds[j]->start.y+compoundCollections[k]->compounds[j]->elements[i]->y-bmpLittle->GetHeight()/2); Bitmap *bmp; if(compoundCollections[k]->compounds[j]->selected) bmp=xbmpElements[compoundCollections[k]->compounds[j]->elements[i]->iType]; else bmp=bmpElements[compoundCollections[k]->compounds[j]->elements[i]->iType]; bmp->Draw(_hLayerDC,compoundCollections[k]->start.x+compoundCollections[k]->compounds[j]->start.x+compoundCollections[k]->compounds[j]->elements[i]->x-bmpElements[0]->GetWidth()/2, compoundCollections[k]->start.y+compoundCollections[k]->compounds[j]->start.y+compoundCollections[k]->compounds[j]->elements[i]->y-bmpElements[0]->GetHeight()/2,true); } COLORREF clfOld=(COLORREF)SetTextColor(_hLayerDC,compoundCollections[k]->compounds[j]->selected?RGB(255,128,0):RGB(255,0,0)); int iBackMode=SetBkMode(_hLayerDC,TRANSPARENT); HFONT fntRoman=CreateFont(20, 0, 0,0, FALSE, FALSE, compoundCollections[k]->compounds[j]->selected?FW_BOLD:FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Times New Roman"); HFONT fnt=(HFONT)SelectObject(_hLayerDC,fntRoman); TEXTMETRIC textMextrics; GetTextMetrics(_hLayerDC,&textMextrics); int strLength=textMextrics.tmAveCharWidth*strlen(compoundCollections[k]->compounds[j]->name); int offSet=GetWidth()/2-strLength/2; if(offSet<10) offSet=10; TextOut(_hLayerDC,offSet,compoundCollections[k]->start.y+compoundCollections[k]->compounds[j]->end.y-20, compoundCollections[k]->compounds[j]->name,strlen(compoundCollections[k]->compounds[j]->name)); SelectObject(_hLayerDC,fnt); DeleteObject(fntRoman); SetBkMode(_hLayerDC,iBackMode); SetTextColor(_hLayerDC,clfOld); if(j!=compoundCollections[k]->countCompounds-1){ HPEN hGridPen = CreatePen(PS_SOLID, 1, RGB(255,0,0));//(255, 204, 153)); HPEN hPen = (HPEN)SelectObject(_hLayerDC, hGridPen); MoveToEx(_hLayerDC, GetWidth()/2-8,compoundCollections[k]->start.y+compoundCollections[k]->compounds[j]->end.y+15, NULL); LineTo(_hLayerDC, GetWidth()/2+8,compoundCollections[k]->start.y+compoundCollections[k]->compounds[j]->end.y+15); MoveToEx(_hLayerDC, GetWidth()/2,compoundCollections[k]->start.y+compoundCollections[k]->compounds[j]->end.y+15-8, NULL); LineTo(_hLayerDC, GetWidth()/2,compoundCollections[k]->start.y+compoundCollections[k]->compounds[j]->end.y+15+8); SelectObject(_hLayerDC,hPen); DeleteObject(hGridPen); } } if(compoundCollections[k]->countCompounds>0){ HPEN hGridPen = CreatePen(PS_SOLID, 1, RGB(255,0,0));//(255, 204, 153)); HPEN hPen = (HPEN)SelectObject(_hLayerDC, hGridPen); MoveToEx(_hLayerDC, compoundCollections[k]->start.x+compoundCollections[k]->arrow.ptStart.x,compoundCollections[k]->start.y+compoundCollections[k]->arrow.ptStart.y, NULL); LineTo(_hLayerDC, compoundCollections[k]->start.x+compoundCollections[k]->arrow.ptEnd.x,compoundCollections[k]->start.y+compoundCollections[k]->arrow.ptEnd.y); LineTo(_hLayerDC, compoundCollections[k]->start.x+compoundCollections[k]->arrow.ptEnd.x-5,compoundCollections[k]->start.y+compoundCollections[k]->arrow.ptEnd.y-5); MoveToEx(_hLayerDC, compoundCollections[k]->start.x+compoundCollections[k]->arrow.ptEnd.x,compoundCollections[k]->start.y+compoundCollections[k]->arrow.ptEnd.y, NULL); LineTo(_hLayerDC, compoundCollections[k]->start.x+compoundCollections[k]->arrow.ptEnd.x+5,compoundCollections[k]->start.y+compoundCollections[k]->arrow.ptEnd.y-5); SelectObject(_hLayerDC,hPen); DeleteObject(hGridPen); } else{ continue; //next 2 loops will not occur } for(i=0;icountInorgCompounds;i++){ inorgElements[compoundCollections[k]->inorgCompounds[i]->iType]->Draw(_hLayerDC,compoundCollections[k]->start.x+compoundCollections[k]->inorgCompounds[i]->x,compoundCollections[k]->start.y+compoundCollections[k]->inorgCompounds[i]->y,true); } for(i=0;icountConditions;i++){ conditonElements[compoundCollections[k]->conditions[i]->iType]->Draw(_hLayerDC,compoundCollections[k]->start.x+compoundCollections[k]->conditions[i]->x,compoundCollections[k]->start.y+compoundCollections[k]->conditions[i]->y,true); } } CPanel::DrawScreen(_hLayerDC); DeleteObject(_hLayerBitmap); DeleteDC(_hLayerDC); } void CCanvas::AddNewCompound() { compoundSetIndex=countCompoundCollections-1; if(GameEngine::GetEngine()->getCountCompounds()>compoundCollections[compoundSetIndex]->countCompounds){ editingIndex=compoundCollections[compoundSetIndex]->countCompounds; compoundCollections[compoundSetIndex]->countCompounds++; } else{ free(compoundCollections[compoundSetIndex]->compounds[editingIndex]); } if(compoundCollections[compoundSetIndex]->countCompounds==1){ compoundCollections[compoundSetIndex]->compounds=(OrgCompound**)malloc(sizeof(OrgCompound*)); } else{ compoundCollections[compoundSetIndex]->compounds=(OrgCompound**)realloc(compoundCollections[compoundSetIndex]->compounds,sizeof(OrgCompound*)*(compoundCollections[compoundSetIndex]->countCompounds)); } OrgCompound * compound=(OrgCompound*)malloc(sizeof(OrgCompound)); compoundCollections[compoundSetIndex]->compounds[editingIndex]=compound; compound->elements=GameEngine::GetEngine()->getOrgElements(); compound->bonds=GameEngine::GetEngine()->getOrgBonds(); compound->elementsCount=GameEngine::GetEngine()->getCountElements(); compound->bondsCount=GameEngine::GetEngine()->getCountBonds(); takeCareCompound(compound,compoundSetIndex); compound->selected=true; placeCompoundCollection(); //char sz[32]; //wsprintf(sz,"%d",0xa); //MessageBox(GameEngine::GetEngine()->GetWindow(),compound->name,sz,MB_OK); } void CCanvas::fillToChains(int elementIndex,OrgChain* chain,int fromElementIndex,OrgCompound * compound) { //Here mostly elements referes to only c elements OrgElement* element=compound->elements[elementIndex]; int connectedElements=0; int connectedElementIndex; int i=0; for(;ibondsCount;i++){ connectedElementIndex=getConnectedElementIndex(elementIndex,element->bonds[i]); if(connectedElementIndex==-1) continue; if(compound->elements[connectedElementIndex]->iType==0 && connectedElementIndex!=fromElementIndex){//if it is connected to another bond connectedElements++; } } chain->endElementIndex=elementIndex; if(connectedElements==0){ chain->endElementIndex=-1; if(chain->countElements==0){ chain->elements=(OrgElement**)malloc(sizeof(OrgElement*)); //add a new element to chain } else{ chain->elements=(OrgElement**)realloc(chain->elements,sizeof(OrgElement*)*(chain->countElements+1)); } chain->elements[chain->countElements]=element; chain->countElements++; return; } if(connectedElements==1){//if one or zero connected elements except from element int i=0; for(;ibondsCount;i++){ connectedElementIndex=getConnectedElementIndex(elementIndex,element->bonds[i]); if(connectedElementIndex==-1) continue; if(compound->elements[connectedElementIndex]->iType==0 && connectedElementIndex!=fromElementIndex){//if it is connected to another bond but not the one it is called if(chain->countElements==0){ chain->elements=(OrgElement**)malloc(sizeof(OrgElement*)); //add a new element to chain } else{ chain->elements=(OrgElement**)realloc(chain->elements,sizeof(OrgElement*)*(chain->countElements+1)); } chain->elements[chain->countElements]=element; chain->countElements++; if(chain->countBonds==0){ chain->bonds=(OrgBond**)malloc(sizeof(OrgBond*)); } else{ chain->bonds=(OrgBond**)realloc(chain->bonds,sizeof(OrgBond*)*(chain->countBonds+1)); } chain->bonds[chain->countBonds]=element->bonds[i]; chain->countBonds++; fillToChains(connectedElementIndex,chain,elementIndex,compound); break; } } return; } //if there are 2 or more connected c elements create a new chain and end this chain OrgChain * chainLone; if(chain->countElements!=0){ if(countChains==0) orgChains=(OrgChain**)malloc(sizeof(OrgChain*)); else orgChains=(OrgChain**)realloc(orgChains,sizeof(OrgChain*)*(countChains+1)); chainLone=(OrgChain*)malloc(sizeof(OrgChain)); orgChains[countChains]=chainLone; countChains++; chainLone->startElementIndex=elementIndex; chainLone->countBonds=0; } else chainLone=chain; chainLone->countChains=0; chainLone->countElements=0; chainLone->endElementIndex=elementIndex; if(chainLone->countElements==0){ chainLone->elements=(OrgElement**)malloc(sizeof(OrgElement*)); //add a new element to chain } else{ chainLone->elements=(OrgElement**)realloc(chain->elements,sizeof(OrgElement*)*(chain->countElements+1)); } chainLone->elements[chainLone->countElements]=element; chainLone->countElements++; for(i=0;ibondsCount;i++){ connectedElementIndex=getConnectedElementIndex(elementIndex,element->bonds[i]); if(connectedElementIndex==-1) continue; if(compound->elements[connectedElementIndex]->iType==0 && connectedElementIndex!=fromElementIndex){//if it is connected to another bond but not the one it is called if(countChains==0) orgChains=(OrgChain**)malloc(sizeof(OrgChain*)); else orgChains=(OrgChain**)realloc(orgChains,sizeof(OrgChain*)*(countChains+1)); OrgChain * chain=(OrgChain*)malloc(sizeof(OrgChain)); orgChains[countChains]=chain; chain->countBonds=0; chain->countChains=0; chain->countElements=0; chain->startElementIndex=elementIndex; countChains++; if(chain->countBonds==0){ chain->bonds=(OrgBond**)malloc(sizeof(OrgBond*)); } else{ chain->bonds=(OrgBond**)realloc(chain->bonds,sizeof(OrgBond*)*(chain->countBonds+1)); } chain->bonds[chain->countBonds]=element->bonds[i]; chain->countBonds++; fillToChains(connectedElementIndex,chain,elementIndex,compound); } } } int CCanvas::getConnectedElementIndex(int elementIndex, OrgBond* bond) { if(bond->StartBondIndex==elementIndex) return bond->EndBondIndex; if(bond->EndBondIndex==elementIndex) return bond->StartBondIndex; return -1; } void CCanvas::assignLevels(OrgCompound *compound,OrgChain** chains,int count) { int i=0; for(;ilevels[j]=0; } for(j=0;jcountBonds;j++){ if(chains[i]->bonds[j]->iType==1){ chains[i]->levels[10]++; //10 for double bonds } if(chains[i]->bonds[j]->iType==2){ chains[i]->levels[11]++; //11 for trible bonds } } for(j=0;jcountElements;j++){ //assigning levels by chains IUPACParser->SetLevels(chains[i]->elements[j],chains[i]->levels,compound->elements,compound->elementsCount,compound->bonds,compound->bondsCount); } char sz[16]; for(j=0;j<12;j++){ wsprintf(sz,"%d=%d",j,chains[i]->levels[j]); //MessageBox(GameEngine::GetEngine()->GetWindow(),sz,"test",MB_OK); } } } void CCanvas::FillMainChains(OrgCompound *compound) { //something must not forget if(countChains==1){ orgChains=(OrgChain**)realloc(orgChains,sizeof(OrgChain*)*2); orgChains[1]=(OrgChain*)malloc(sizeof(OrgChain)); orgChains[1]->countElements=orgChains[0]->countElements; orgChains[1]->elements=(OrgElement**)malloc(sizeof(OrgElement*)*orgChains[1]->countElements); int i=0; for(;icountElements;i++){ orgChains[1]->elements[i]=orgChains[0]->elements[orgChains[1]->countElements-1-i]; } orgChains[1]->countBonds=orgChains[0]->countBonds; orgChains[1]->bonds=(OrgBond**)malloc(sizeof(OrgBond*)*orgChains[1]->countBonds ); for( i=0;icountBonds;i++){ orgChains[1]->bonds[i]=orgChains[0]->bonds[orgChains[1]->countBonds-1-i]; } orgChains[1]->startElementIndex=orgChains[0]->endElementIndex; orgChains[1]->endElementIndex=orgChains[0]->startElementIndex; for(i=0;i<12;i++){ orgChains[1]->levels[i]=orgChains[0]->levels[i]; } countChains++; } mainChains=(OrgChain**)malloc(sizeof(OrgChain*)); mainChains[0]=(OrgChain*)malloc(sizeof(OrgChain)); int i=0; for(;i<12;i++){ mainChains[0]->levels[i]=0; } countMainChains++; for(i=0;istartElementIndex!=-1 && orgChains[i]->endElementIndex!=-1) continue; OrgChain* thisChain=(OrgChain*)malloc(sizeof(OrgChain)); thisChain->countBonds=0; thisChain->countElements=0; int j=0; for(;j<12;j++){ thisChain->levels[j]=0; } OrgChain *earlyChains[4]; for(j=0;j<4;j++){ earlyChains[j]=NULL; } checkForMain(orgChains[i],thisChain,orgChains[i]->endElementIndex==-1,earlyChains,0,( (orgChains[i]->startElementIndex==-1)?orgChains[i]->endElementIndex:orgChains[i]->startElementIndex),compound); } //int k=0; } void CCanvas::checkForMain(OrgChain *fromChain, OrgChain *chain, bool reversed,OrgChain ** earlyChains,int nextToFill,int linkElement,OrgCompound* compound) { if(chain->countBonds==0){ chain->bonds=(OrgBond**)malloc(sizeof(OrgBond*)*(fromChain->countBonds+chain->countBonds)); }else{ chain->bonds=(OrgBond**)realloc(chain->bonds,sizeof(OrgBond*)*(fromChain->countBonds+chain->countBonds)); } int i=0; for(;icountBonds;i++){ if(reversed) chain->bonds[chain->countBonds++]=fromChain->bonds[fromChain->countBonds-1-i]; else chain->bonds[chain->countBonds++]=fromChain->bonds[i]; } if(chain->countElements==0){ chain->elements=(OrgElement**)malloc(sizeof(OrgElement*)*(fromChain->countElements+chain->countElements)); }else{ chain->elements=(OrgElement**)realloc(chain->elements,sizeof(OrgElement*)*(fromChain->countElements+chain->countElements)); } for(i=0;icountElements;i++){ if(reversed) chain->elements[chain->countElements++]=fromChain->elements[fromChain->countElements-1-i]; else chain->elements[chain->countElements++]=fromChain->elements[i]; } /*for(i=0;i<12;i++){ chain->levels[i]+=fromChain->levels[i]; }*/ bool found=false; for(i=9;i>=1;i--){ if(!found){ if(chain->levels[i]!=0 && chain->levels[i]>=fromChain->levels[i]){ chain->levels[i]+=fromChain->levels[i]; found=true; } else if(fromChain->levels[i]!=0 && chain->levels[i]levels[i]){ chain->levels[i]+=fromChain->levels[i]; found=true; } }else{ chain->levels[1]+=fromChain->levels[i]; if(i!=1){ chain->levels[1]+=chain->levels[i]; chain->levels[i]=0; } } } for(i=10;i<12;i++){ chain->levels[i]+=fromChain->levels[i]; } if(linkElement==-1){ int succeed=0; //-1=hopeless 0=hopeful 1=succeed for(i=9;i>1;i--){ //first checking main bonds if(mainChains[0]->levels[i]==chain->levels[i]) continue; else if(mainChains[0]->levels[i]>chain->levels[i]){ succeed=-1; break; } else if(mainChains[0]->levels[i]levels[i]){ succeed=1; break; } } //then counting bond types for(i=11;succeed==0 && i>9;i--){ if(mainChains[0]->levels[i]==chain->levels[i]) continue; else if(mainChains[0]->levels[i]>chain->levels[i]){ succeed=-1; break; } else if(mainChains[0]->levels[i]levels[i]){ succeed=1; break; } } //counting Number of elements if(succeed==0){ if(mainChains[0]->countElements>chain->countElements) succeed=-1; if(mainChains[0]->countElementscountElements) succeed=1; } //finaly checking substitutional elements if(succeed==0){ if(mainChains[0]->levels[1]>chain->levels[1]) succeed=-1; if(mainChains[0]->levels[1]levels[1]) succeed=1; } //add the end checking for connected carbon chains if(succeed==0){ int noElementsConnected=0; for(i=1;icountElements-1;i++){ //in the middle of chain int j=0; for(;jelements[i]==compound->elements[orgChains[j]->startElementIndex]){ noElementsConnected+=orgChains[j]->countElements; } } } int noElementsConnectedInMain=0; for(i=1;icountElements-1;i++){ //in the middle of chain int j=0; for(;jelements[i]==compound->elements[orgChains[j]->startElementIndex]){ noElementsConnectedInMain+=orgChains[j]->countElements; } } } if(noElementsConnectednoElementsConnectedInMain) succeed=1; } //everything checked if succeed==-1 just ignore(delete chain) succeed==0 add another main succeed===1 replace main if(succeed==-1){ free(chain->elements); free(chain->bonds); free(chain); } if(succeed==0){ mainChains=(OrgChain**)realloc(mainChains,sizeof(OrgChain*)*(countMainChains+1)); mainChains[countMainChains]=chain; countMainChains++; } if(succeed==1){ for(i=0;istartElementIndex==linkElement && orgChains[i]!=fromChain){ earlyChains[nextToFill]=fromChain; checkForMain(orgChains[i],getACopy(chain),false,earlyChains,(nextToFill==3?0:nextToFill++),orgChains[i]->endElementIndex,compound); } else if(orgChains[i]->endElementIndex==linkElement && orgChains[i]!=fromChain){ earlyChains[nextToFill]=fromChain; checkForMain(orgChains[i],getACopy(chain),true,earlyChains,(nextToFill==3?0:nextToFill++),orgChains[i]->startElementIndex,compound); } } free(chain->elements); free(chain->bonds); free(chain); } OrgChain* CCanvas::getACopy(OrgChain *fromChain) { OrgChain* chain=(OrgChain*)malloc(sizeof(OrgChain)); chain->bonds=(OrgBond**)malloc(sizeof(OrgBond*)*fromChain->countBonds); int i=0; for(;icountBonds;i++){ chain->bonds[i]=fromChain->bonds[i]; } chain->countBonds=fromChain->countBonds; chain->elements=(OrgElement**)malloc(sizeof(OrgElement*)*fromChain->countElements); for(i=0;icountElements;i++){ chain->elements[i]=fromChain->elements[i]; } chain->countElements=fromChain->countElements; for(i=0;i<12;i++){ chain->levels[i]=fromChain->levels[i]; } return chain; } void CCanvas::FilterTheMain() { int j=1; for(;j1;i--){ //first checking main bonds if(mainChains[0]->levels[i]==mainChains[j]->levels[i]) continue; else if(mainChains[0]->levels[i]levels[i]){ if(mainChains[0]->levels[i]==0){ succeed=1; break; }else{ succeed=-1; break; } } else if(mainChains[0]->levels[i]>mainChains[j]->levels[i]){ if(mainChains[j]->levels[i]==0){ succeed=-1; break; }else{ succeed=1; break; } } } //then counting bond types for(i=11;succeed==0 && i>9;i--){ if(mainChains[0]->levels[i]==mainChains[j]->levels[i]) continue; else if(mainChains[0]->levels[i]levels[i]){ succeed=-1; break; } else if(mainChains[0]->levels[i]>mainChains[j]->levels[i]){ succeed=1; break; } } //finaly checking substitutional elements if(succeed==0){ if(mainChains[0]->levels[1]levels[1]) succeed=-1; if(mainChains[0]->levels[1]>mainChains[j]->levels[1]) succeed=1; } if(succeed==0){ //sorry you also dont need free(mainChains[j]); } else if(succeed==-1){ free(mainChains[j]); } else if(succeed==1){ free(mainChains[0]); mainChains[0]=mainChains[j]; } if(succeed==-1||succeed==1|| succeed==0){ countMainChains--; for(int k=j;klevels[j]); } char sz2[128]; wsprintf(sz2,"%d ",mainChains[i]->countElements); //MessageBox(GameEngine::GetEngine()->GetWindow(),sz,sz2,MB_OK); } } void CCanvas::assingnNumbersForLevelsOfMainChains(OrgCompound * compound) { int i=0; for(;ilevels[j]=0; } for(j=0;jcountElements;j++){ //set total for elements IUPACParser->setNumberForLevel(j,mainChains[i]->elements[j],mainChains[i],mainChains[i]->levels,compound->elements,compound->elementsCount,compound->bonds,compound->bondsCount); } for(j=0;jcountBonds;j++){ if(mainChains[i]->bonds[j]->iType==1){ mainChains[i]->levels[10]+=j+1; //10 for double bonds } if(mainChains[i]->bonds[j]->iType==2){ mainChains[i]->levels[11]+=j+1; //11 for triple bonds } } /*char sz[128]; sz[0]='\0'; for(j=0;j<12;j++){ wsprintf(sz,"%s\n%d=%d",sz,j,mainChains[i]->levels[j]); } char sz2[128]; wsprintf(sz2,"%d of %d",i,countMainChains);*/ //MessageBox(GameEngine::GetEngine()->GetWindow(),sz,sz2,MB_OK); } } void CCanvas::setIUPACName(OrgCompound * compound) { bool nameable=true; strcpy(compound->name,""); Link ***links=(Link***)malloc(sizeof(Link**)); int countLinks=0; int i=0; for(;imainChain->countElements;i++){ if(nameable) nameable=IUPACParser->setLinks(i,links,&countLinks,compound); } //fixing the last part char activePartName[32]; strcpy(activePartName,"e"); int maximumLevel=0; int indices[10]; int indicesCount=0; for(i=0;ilevel>20 || links[0][i]->level<=1) continue; if(links[0][i]->level==maximumLevel){ indices[indicesCount]=i; indicesCount++; } if(links[0][i]->level>maximumLevel){ indicesCount=1; indices[0]=i; maximumLevel=links[0][i]->level; } } char nameCount[12][8]; strcpy(nameCount[1],""); strcpy(nameCount[2],"di"); strcpy(nameCount[3],"tri"); strcpy(nameCount[4],"tetra"); strcpy(nameCount[5],"penta"); strcpy(nameCount[6],"hexa"); strcpy(nameCount[7],"hepta"); strcpy(nameCount[8],"octa"); strcpy(nameCount[9],"nona"); strcpy(nameCount[10],"deca"); if(indicesCount && compound->mainChain->countElements==1) links[0][indices[0]]->noNumber=true; for(i=0;inoNumber ;i++){ if(i==0){ wsprintf(activePartName,"-"); }else{ wsprintf(activePartName,"%s,",activePartName); } wsprintf(activePartName,"%s%d",activePartName,links[0][indices[i]]->elementIndex+1); } if(indicesCount>10) nameable=false; if(indicesCount>0){ if(!links[0][indices[0]]->noNumber){ wsprintf(activePartName,"%s-",activePartName); } else wsprintf(activePartName,""); wsprintf(activePartName,"%s%s%s",activePartName,nameCount[indicesCount],links[0][indices[0]]->baseName); } //links[0]=(Link**)realloc(links[0],sizeof(Link*)*countLinks); //removing active part links for(i=0;i1;i++){ if(links[0][i]->level==maximumLevel){ free(links[0][i]); int j=i; for(;jGetWindow(),activePartName,"Check",MB_OK); //setting the name for bond type char bondTypeName[32]; strcpy(bondTypeName,"an"); int ibondType=0; //default single bond;1=double bond found 2=trible bond found int iNumberOfBonds=0; bool skipNumber=false; for(i=0;imainChain->countBonds && nameable;i++){ if(compound->mainChain->bonds[i]->iType!=0){ if(ibondType!=0 && ibondType!=compound->mainChain->bonds[i]->iType){ nameable=false; break; } if(iNumberOfBonds==0){ wsprintf(bondTypeName,"-"); } else{ wsprintf(bondTypeName,"%s,",bondTypeName); } ibondType=compound->mainChain->bonds[i]->iType; iNumberOfBonds++; wsprintf(bondTypeName,"%s%d",bondTypeName,i+1); } } if(iNumberOfBonds && iNumberOfBonds==compound->mainChain->countBonds){ wsprintf(bondTypeName,""); skipNumber=true; } if(iNumberOfBonds>10) nameable=false; if(ibondType && nameable){ if(!skipNumber)wsprintf(bondTypeName,"%s-",bondTypeName); wsprintf(bondTypeName,"%s%s",bondTypeName,nameCount[iNumberOfBonds]); wsprintf(bondTypeName,"%s%s",bondTypeName,(ibondType==1?"en":"yn")); } //MessageBox(GameEngine::GetEngine()->GetWindow(),bondTypeName,"Check",MB_OK); char elementSize[32]; char sizeName[12][8]; strcpy(sizeName[1],"meth"); strcpy(sizeName[2],"eth"); strcpy(sizeName[3],"prop"); strcpy(sizeName[4],"but"); strcpy(sizeName[5],"pent"); strcpy(sizeName[6],"hex"); strcpy(sizeName[7],"hept"); strcpy(sizeName[8],"oct"); strcpy(sizeName[9],"non"); strcpy(sizeName[10],"dec"); if(compound->mainChain->countElements>10){ nameable=false; } else{ wsprintf(elementSize,"%s",sizeName[compound->mainChain->countElements]); } //MessageBox(GameEngine::GetEngine()->GetWindow(),compound->name,"Check",MB_OK); sortLinks(links[0],countLinks); char szSubLinks[256]; int countOccurance=0; bool noNeedNumbering=false; if(compound->mainChain->countBonds<=1) noNeedNumbering=true; strcpy(szSubLinks,""); for(i=0;ielementIndex+1); countOccurance++; } else if(!strcmp(links[0][i]->subName,links[0][i-1]->subName)){ if(!noNeedNumbering)wsprintf(szSubLinks,"%s,%d",szSubLinks,links[0][i]->elementIndex+1); countOccurance++; } else if(strcmp(links[0][i]->subName,links[0][i-1]->subName)){ //ending the earlier link if(!noNeedNumbering)wsprintf(szSubLinks,"%s-",szSubLinks); wsprintf(szSubLinks,"%s%s%s",szSubLinks,nameCount[countOccurance],links[0][i-1]->subName); //starting current link if(!noNeedNumbering)wsprintf(szSubLinks,"%s-%d",szSubLinks,links[0][i]->elementIndex+1); countOccurance=1; } } if(countOccurance>10) nameable=false; if(countLinks!=0){ if(!noNeedNumbering)wsprintf(szSubLinks,"%s-",szSubLinks); wsprintf(szSubLinks,"%s%s%s",szSubLinks,nameCount[countOccurance],links[0][countLinks-1]->subName); } if(nameable)wsprintf(compound->name,"%s%s%s%s",szSubLinks,elementSize,bondTypeName,activePartName); /*char sz[32]; wsprintf(sz,"%s",nameable?"Here is the name":"no name"); MessageBox(GameEngine::GetEngine()->GetWindow(),compound->name,sz,MB_OK);*/ for(i=0;i0)free(links[0]); free(links); } void CCanvas::sortLinks(Link **&links,int countLinks) { int i=0; for(;isubName,links[j]->subName)>0){ tempLink=links[i]; links[i]=links[j]; links[j]=tempLink; } } } } void CCanvas::givePosition(OrgCompound *compound, int elementIndex,OrgBond *bond,int earlyX,int earlyY,int xIncrement, int yIncrement,int earlyType) { //earlyType=0 bend //earlyType=1 no bend int thisX,tempX; int thisY,tempY; //earlyX=-1 to decrease 1 to increase;0 to remain same; so as earlyY if(earlyType){ tempX=(int)xIncrement*BOND_LENGTH*1.2; tempY=(int)yIncrement*BOND_LENGTH*1.2; }else{ if(xIncrement){ tempX=xIncrement*BOND_LENGTH; tempY=xIncrement*OFF_LENGTH; } else if(yIncrement){ tempX=yIncrement*-1*OFF_LENGTH; tempY=yIncrement*BOND_LENGTH; } } if(compound->elements[elementIndex]->iType!=0){ tempX/=2; tempY/=2; } thisX=earlyX+tempX; thisY=earlyY+tempY; int i=0; for(;ielements[elementIndex]->x=thisX; compound->elements[elementIndex]->y=thisY; if(numberOfPointselementsCount){ occupiedPoints[numberOfPoints].x=thisX; occupiedPoints[numberOfPoints++].y=thisY; } if(bond){ //setting the bond if(elementIndex==bond->StartBondIndex){ bond->start.x=thisX; bond->start.y=thisY; } else{ bond->end.x=thisX; bond->end.y=thisY; } } //setting the length of compound if(compound->end.xend.x=thisX; if(compound->end.yend.y=thisY; if(compound->start.x>thisX) compound->start.x=thisX; if(compound->start.y>thisY) compound->start.y=thisY; //setSide=0x1 right set //setSide=0x10 top set //setSide=0x100 left set //setSide=0x1000 down set int setSide=0; static bool sideFlag=false; //managing connectedElements for(i=0;ielements[elementIndex]->bondsCount;i++){//for each every bond connected if(compound->elements[elementIndex]->bonds[i]==bond) // except the called bond continue; if(elementIndex==compound->elements[elementIndex]->bonds[i]->StartBondIndex){ compound->elements[elementIndex]->bonds[i]->start.x=thisX; compound->elements[elementIndex]->bonds[i]->start.y=thisY; }else{ compound->elements[elementIndex]->bonds[i]->end.x=thisX; compound->elements[elementIndex]->bonds[i]->end.y=thisY; } int targetIndex=getConnectedElementIndex(elementIndex,compound->elements[elementIndex]->bonds[i]); OrgElement * targetElement=compound->elements[targetIndex]; bool partOfMainChain=false; int j=0; for(;jmainChain->countElements;j++){ if(targetElement==compound->mainChain->elements[j]){ //sure it have to be on right givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,1,0,1); setSide|=0x1; partOfMainChain=true; break; } } if(partOfMainChain) continue; //check more c exist bool endOfChain=false; for(j=0;jelements[targetIndex]->bondsCount;j++){//for each every bond connected int targetIndex2=getConnectedElementIndex(targetIndex,compound->elements[targetIndex]->bonds[j]); OrgElement * targetElement2=compound->elements[targetIndex2]; if(targetElement2==compound->mainChain->elements[compound->mainChain->countElements-1]) endOfChain=true; } // it just another c bool followAsNonCarbon=false; if(targetElement->iType==0){ //check whether there are more c exist bool moreCFound=false; int j=i+1; for(;jelements[elementIndex]->bondsCount;j++){//for each every bond connected if(compound->elements[elementIndex]->bonds[j]==bond) // except the called bond continue; int targetIndex2=getConnectedElementIndex(elementIndex,compound->elements[elementIndex]->bonds[j]); OrgElement * targetElement2=compound->elements[targetIndex2]; /*bool partOfMainChain=false; for(int k=0;kmainChain->countElements;k++){ if(targetElement==compound->mainChain->elements[k]){ partOfMainChain=true; break; } } if(partOfMainChain) continue;*/ if(targetElement2->iType==0) moreCFound=true; } if(!moreCFound){ if(xIncrement>0 && (setSide & 1)){ moreCFound=true; //break; } else if(xIncrement<0 && (setSide & 0x100)){ moreCFound=true; //break; } else if(yIncrement<0 && (setSide & 0x10)){ moreCFound=true; //break; } else if(yIncrement>0 && (setSide & 0x1000)){ moreCFound=true; //break; } else{ givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,xIncrement,yIncrement,1); if(xIncrement>0) setSide|=1; else if(xIncrement<0) setSide|=0x100; else if(yIncrement>0) setSide|=0x10; else if(yIncrement<0) setSide|=0x1000; continue; } } if(moreCFound){ (sideFlag)?sideFlag=false:sideFlag=true; followAsNonCarbon=true; } } if(targetElement->iType!=0 || followAsNonCarbon){ //if it is not a c if((!bond) && !(setSide & 0x100)){ // the first element of main bond thrown givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,-1,0,0); setSide|=0x100; //place it left continue; } if(compound->elements[elementIndex]->iType!=0 && compound->elements[elementIndex]->bondsCount==2){ givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,xIncrement,yIncrement,0); continue; } if(xIncrement){ if(endOfChain && i==compound->elements[elementIndex]->bondsCount-1){//the last bond of the element givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,xIncrement,0,0); continue; } if(sideFlag && !(setSide & 0x1000)){ //if bottom is not full givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,0,-1,0); setSide|=0x1000; //fill botom //(sideFlag)?sideFlag=false:sideFlag=true; continue; } else if(sideFlag &&!(setSide & 0x10)){// if bottom full and top not full givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,0,1,0); setSide|=0x10; // fill top //(sideFlag)?sideFlag=false:sideFlag=true; continue; } else if(!sideFlag &&!(setSide & 0x10)){// if bottom full and top not full givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,0,1,0); setSide|=0x10; // fill top //(sideFlag)?sideFlag=false:sideFlag=true; continue; } else if(!sideFlag && !(setSide & 0x1000)){ //if bottom is not full givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,0,-1,0); setSide|=0x1000; //fill botom //(sideFlag)?sideFlag=false:sideFlag=true; continue; } else{ //surely it have to be same side as X increment givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,xIncrement,0,0); //break; // no space to fill } } else if(yIncrement){ if(endOfChain && i==compound->elements[elementIndex]->bondsCount-1){ givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,0,yIncrement,0); continue; } if(sideFlag && !(setSide & 0x100)){ //if left is not full givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,-1,0,0); setSide|=0x100; //fill left //(sideFlag)?sideFlag=false:sideFlag=true; continue; } else if(sideFlag && !(setSide & 0x1)){// if left full and right not full givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,1,0,0); setSide|=0x1; // fill right //(sideFlag)?sideFlag=false:sideFlag=true; continue; } else if(!sideFlag && !(setSide & 0x1)){// if left full and right not full givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,1,0,0); setSide|=0x1; // fill right //(sideFlag)?sideFlag=false:sideFlag=true; continue; } else if(!sideFlag && !(setSide & 0x100)){ //if left is not full givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,-1,0,0); setSide|=0x100; //fill left //(sideFlag)?sideFlag=false:sideFlag=true; continue; } else{ //surely it have to be same side as y increment givePosition(compound,targetIndex,compound->elements[elementIndex]->bonds[i],thisX,thisY,0,yIncrement,0); //break; // no space to fill } } } } } void CCanvas::DrawBond(HDC hDC,int iBondType,POINT ptStart,POINT ptEnd,bool bSelected){ HPEN hGridPen = CreatePen(PS_SOLID, 1, bSelected?RGB(255,128,0):RGB(255,0,0));//(255, 204, 153)); HPEN hPen = (HPEN)SelectObject(hDC, hGridPen); if(iBondType==1){ MoveToEx(hDC, ptStart.x,ptStart.y, NULL); LineTo(hDC, ptEnd.x,ptEnd.y); } else if(iBondType==2){ if(abs(ptStart.x-ptEnd.x)>abs(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 CCanvas::placeCompoundsPosition(CompoundCollection* collection,bool withInorganics) { collection->iCursor=20; int iMargin=20; int i=0; for(;icountCompounds;i++){ collection->compounds[i]->start.x=GetWidth()/2-collection->compounds[i]->length.x/2; if(collection->compounds[i]->start.xcompounds[i]->start.x=iMargin; collection->compounds[i]->start.y=collection->iCursor; collection->compounds[i]->end.x=collection->compounds[i]->start.x+collection->compounds[i]->length.x; collection->iCursor=collection->compounds[i]->end.y=collection->compounds[i]->start.y+collection->compounds[i]->length.y+30; collection->iCursor+=30; if(collection->length.xcompounds[i]->end.x+50){ collection->length.x=collection->compounds[i]->end.x+50; } collection->compounds[i]->selected=false; } collection->length.y=collection->iCursor; /*if(iLayerHeightiCursor) iLayerHeight=collection->iCursor;*/ if(withInorganics)placeInorgNConditions(collection); } POINT CCanvas::addPoints(POINT a, POINT b) { POINT c; c.x=a.x+b.x; c.y=a.y+b.y; return c; } void CCanvas::MouseButtonDown(int x, int y, bool bLeft) { if(x>GetX() && x< GetX()+GetWidth()-5 && y>GetY() && ycountCompounds;i++){ if(relativeX>compoundCollections[j]->start.x+compoundCollections[j]->compounds[i]->start.x && relativeXstart.x+compoundCollections[j]->compounds[i]->end.x && relativeY>compoundCollections[j]->start.y+compoundCollections[j]->compounds[i]->start.y && relativeYstart.y+compoundCollections[j]->compounds[i]->end.y){ compoundCollections[j]->compounds[i]->selected=(compoundCollections[j]->compounds[i]->selected)?false:true; } else compoundCollections[j]->compounds[i]->selected=false; } } } } CPanel::MouseButtonDown(x,y,bLeft); } void CCanvas::MouseButtonUp(int x, int y, bool bLeft) { CPanel::MouseButtonUp(x,y,bLeft); } void CCanvas::MouseMove(int x, int y) { CPanel::MouseMove(x,y); } bool CCanvas::deploySelected() { compoundSetIndex=countCompoundCollections-1; int i=0; for(;icountCompounds;i++){ if(compoundCollections[compoundSetIndex]->compounds[i]->selected){ GameEngine::GetEngine()->setOrgElements(compoundCollections[compoundSetIndex]->compounds[i]->elements); GameEngine::GetEngine()->setOrgBonds(compoundCollections[compoundSetIndex]->compounds[i]->bonds); GameEngine::GetEngine()->setCountElements(compoundCollections[compoundSetIndex]->compounds[i]->elementsCount); GameEngine::GetEngine()->setCountBonds(compoundCollections[compoundSetIndex]->compounds[i]->bondsCount); editingIndex=i; GameEngine::GetEngine()->setCountCompounds(compoundCollections[compoundSetIndex]->countCompounds); return true; } } for(i=0;icountCompounds;j++){ if(compoundCollections[i]->compounds[j]->selected){ MessageBox(GameEngine::GetEngine()->GetWindow(),"You can edit only last set of results","Information",MB_OK); return false; } } } MessageBox(GameEngine::GetEngine()->GetWindow(),"Please select a compound to edit","Information",MB_OK); return false; } bool CCanvas::addInorgCompound(int iType) { if(compoundCollections[compoundSetIndex]->countCompounds==0){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Please add organic compounds first","Info",MB_OK); return false; } int i=0; for(;icountInorgCompounds;i++){ if(compoundCollections[compoundSetIndex]->inorgCompounds[i]->iType==iType){ //already exist remove that free(compoundCollections[compoundSetIndex]->inorgCompounds[i]); compoundCollections[compoundSetIndex]->countInorgCompounds--; int j=i; for(;jcountInorgCompounds;j++){ compoundCollections[compoundSetIndex]->inorgCompounds[j]=compoundCollections[compoundSetIndex]->inorgCompounds[j+1]; } compoundCollections[compoundSetIndex]->inorgCompounds=(Entity**)realloc(compoundCollections[compoundSetIndex]->inorgCompounds,sizeof(Entity*)*compoundCollections[compoundSetIndex]->countInorgCompounds); placeInorgNConditions(compoundCollections[compoundSetIndex]); return false; } } if(compoundCollections[compoundSetIndex]->countInorgCompounds==0){ compoundCollections[compoundSetIndex]->inorgCompounds=(Entity**)malloc(sizeof(Entity*)); }else{ compoundCollections[compoundSetIndex]->inorgCompounds=(Entity**)realloc(compoundCollections[compoundSetIndex]->inorgCompounds,sizeof(Entity*)*(compoundCollections[compoundSetIndex]->countInorgCompounds+1)); } compoundCollections[compoundSetIndex]->inorgCompounds[compoundCollections[compoundSetIndex]->countInorgCompounds]=(Entity*)malloc(sizeof(Entity)); compoundCollections[compoundSetIndex]->inorgCompounds[compoundCollections[compoundSetIndex]->countInorgCompounds]->iType=iType; compoundCollections[compoundSetIndex]->countInorgCompounds++; placeInorgNConditions(compoundCollections[compoundSetIndex]); return true; } bool CCanvas::addCondition(int iType) { if(compoundCollections[compoundSetIndex]->countCompounds==0){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Please add organic compounds first","Info",MB_OK); return false; } int i=0; for(;icountConditions;i++){ if(compoundCollections[compoundSetIndex]->conditions[i]->iType==iType){ //already exist remove that free(compoundCollections[compoundSetIndex]->conditions[i]); compoundCollections[compoundSetIndex]->countConditions--; int j=i; for(;jcountConditions;j++){ compoundCollections[compoundSetIndex]->conditions[j]=compoundCollections[compoundSetIndex]->conditions[j+1]; } compoundCollections[compoundSetIndex]->conditions=(Entity**)realloc(compoundCollections[compoundSetIndex]->conditions,sizeof(Entity*)*compoundCollections[compoundSetIndex]->countConditions); placeInorgNConditions(compoundCollections[compoundSetIndex]); return false; } } if(compoundCollections[compoundSetIndex]->countConditions==0){ compoundCollections[compoundSetIndex]->conditions=(Entity**)malloc(sizeof(Entity*)); }else{ compoundCollections[compoundSetIndex]->conditions=(Entity**)realloc(compoundCollections[compoundSetIndex]->conditions,sizeof(Entity*)*(compoundCollections[compoundSetIndex]->countConditions+1)); } compoundCollections[compoundSetIndex]->conditions[compoundCollections[compoundSetIndex]->countConditions]=(Entity*)malloc(sizeof(Entity)); compoundCollections[compoundSetIndex]->conditions[compoundCollections[compoundSetIndex]->countConditions]->iType=iType; compoundCollections[compoundSetIndex]->countConditions++; placeInorgNConditions(compoundCollections[compoundSetIndex]); return true; } void CCanvas::placeInorgNConditions(CompoundCollection * collection) { int middle=GetWidth()/2; collection->arrow.ptStart.x=middle; collection->arrow.ptStart.y=collection->iCursor-10; int elementHeight=inorgElements[0]->GetHeight(); int arrowLength=elementHeight*(((collection->countInorgCompounds>collection->countConditions)?collection->countInorgCompounds:collection->countConditions)+2); int nextY=collection->arrow.ptStart.y+arrowLength/2-collection->countInorgCompounds*(elementHeight)/2; int i=0; for(;icountInorgCompounds;i++){ collection->inorgCompounds[i]->x=middle-inorgElements[0]->GetWidth(); collection->inorgCompounds[i]->y=nextY; nextY+=elementHeight; } nextY=collection->arrow.ptStart.y+arrowLength/2-collection->countConditions*(elementHeight)/2; for(i=0;icountConditions;i++){ collection->conditions[i]->x=middle+10; collection->conditions[i]->y=nextY; nextY+=elementHeight; } collection->arrow.ptEnd.x=middle; collection->arrow.ptEnd.y=collection->arrow.ptStart.y+arrowLength; if(/*countCompoundCollections>1 && */collection->countInorgCompounds==0 && collection->countConditions==0 && collection==compoundCollections[countCompoundCollections-1]){ collection->arrow.ptEnd.y=collection->arrow.ptStart.y; } collection->length.y=collection->arrow.ptEnd.y+20; collection->end.y=collection->start.y+collection->length.y; } void CCanvas::startReaction() { if(compoundCollections[compoundSetIndex]->countCompounds==0){ MessageBox(GameEngine::GetEngine()->GetWindow(),"Please add organic compounds first","Info",MB_OK); return ; } int i=0; for(;icountCompounds;i++){ compoundCollections[compoundSetIndex]->compounds[i]->selected=false; } Entity **inorgCompounds=compoundCollections[compoundSetIndex]->inorgCompounds; int countInorgCompounds=compoundCollections[compoundSetIndex]->countInorgCompounds; compoundCollections[compoundSetIndex]->countInorgCompounds=0; Entity **conditions=compoundCollections[compoundSetIndex]->conditions; int countConditions=compoundCollections[compoundSetIndex]->countConditions; compoundCollections[compoundSetIndex]->countConditions=0; int *alreadyDone=(int*)malloc(sizeof(int)); alreadyDone[0]=1; reactionsParser->startReaction(&compoundCollections,&countCompoundCollections, inorgCompounds,countInorgCompounds,conditions,countConditions,alreadyDone); free(alreadyDone); for(i=0;icountInorgCompounds=0; compoundCollections[countCompoundCollections-1]->countConditions=0; placeInorgNConditions(compoundCollections[compoundSetIndex]); int total_processing,current_processing; total_processing=current_processing=0; for(i=compoundSetIndex+1;icountCompounds; } HDC _hLayerDC; _hLayerDC=GetDC(GameEngine::GetEngine()->GetWindow()); DrawSlider(_hLayerDC,current_processing,total_processing); for(i=compoundSetIndex+1;icountCompounds;j++){ takeCareCompound(compoundCollections[i]->compounds[j],i); current_processing++; DrawSlider(_hLayerDC,current_processing,total_processing); } } ReleaseDC(GameEngine::GetEngine()->GetWindow(),_hLayerDC); if(countCompoundCollections==compoundSetIndex+1){ MessageBox(GameEngine::GetEngine()->GetWindow(),"No clear reactions!\nPlease check reactants & conditions again..","Info",MB_OK); isEditable=true; //compoundCollections[0]->countInorgCompounds=countInorgCompounds; //compoundCollections[0]->countCompounds=countConditions; } compoundSetIndex=countCompoundCollections-1; placeCompoundCollection(); GameEngine::GetEngine()->setCountCompounds(compoundCollections[countCompoundCollections-1]->countCompounds); } bool CCanvas::IsEditable() { return isEditable; } void CCanvas::placeCompoundCollection() { int iCursor=0; int i=0; for(;istart.x=0; compoundCollections[i]->start.y=iCursor; compoundCollections[i]->end.x=compoundCollections[i]->length.x; compoundCollections[i]->end.y=iCursor+compoundCollections[i]->length.y; iCursor=compoundCollections[i]->end.y; if(iLayerWidthend.x){ iLayerWidth=compoundCollections[i]->end.x; if(iLayerHeight==GetHeight()) iLayerHeight++; } } } bool CCanvas::deleteSelected() { int i=0; for(;icountCompounds;i++){ if(compoundCollections[compoundSetIndex]->compounds[i]->selected){ OrgCompound *compound=compoundCollections[compoundSetIndex]->compounds[i]; int j=0; for(;jelementsCount;j++){ free(compound->elements[j]->bonds); free(compound->elements[j]); } for(j=0;jbondsCount;j++){ free(compound->bonds[j]); } free(compound->elements); free(compound->bonds); free(compound); compoundCollections[compoundSetIndex]->countCompounds--; for(j=i;jcountCompounds;j++){ compoundCollections[compoundSetIndex]->compounds[j]=compoundCollections[compoundSetIndex]->compounds[j+1]; } for(i=compoundSetIndex;icountCompounds;j++){ placeCompoundsPosition(compoundCollections[i],true); } } placeCompoundCollection(); int i=GameEngine::GetEngine()->getCountCompounds(); GameEngine::GetEngine()->setCountCompounds(i-1); return true; } } for(i=0;icountCompounds;j++){ if(compoundCollections[i]->compounds[j]->selected){ MessageBox(GameEngine::GetEngine()->GetWindow(),"You can delete only from last set of results","Information",MB_OK); return false; } } } MessageBox(GameEngine::GetEngine()->GetWindow(),"Please select a compound to delete","Information",MB_OK); return false; } bool CCanvas::clearAll() { if(countCompoundCollections==1 && compoundCollections[0]->countCompounds==0) return false; int i=0; for(;icountCompounds=compoundCollections[0]->countInorgCompounds=compoundCollections[0]->countConditions=0; countCompoundCollections=1; compoundSetIndex=0; iLayerY=0; GameEngine::GetEngine()->setCountCompounds(0); return true; } void CCanvas::clearLastResult() { if(countCompoundCollections==1){ return; } countCompoundCollections--; clearCollection(compoundCollections[countCompoundCollections]); free(compoundCollections[countCompoundCollections]); compoundCollections=(CompoundCollection**)realloc(compoundCollections,sizeof(CompoundCollection*)*countCompoundCollections); int lastIndex=countCompoundCollections-1; int i=0; for(;icountInorgCompounds;i++){ free(compoundCollections[lastIndex]->inorgCompounds[i]); } for(i=0;icountConditions;i++){ free(compoundCollections[lastIndex]->conditions[i]); } if(compoundCollections[lastIndex]->countInorgCompounds)free(compoundCollections[lastIndex]->inorgCompounds); if(compoundCollections[lastIndex]->countConditions)free(compoundCollections[lastIndex]->conditions); compoundCollections[lastIndex]->countInorgCompounds=0; compoundCollections[lastIndex]->countConditions=0; compoundSetIndex=lastIndex; iLayerY=0; placeInorgNConditions(compoundCollections[lastIndex]); GameEngine::GetEngine()->setCountCompounds(compoundCollections[countCompoundCollections-1]->countCompounds); } void CCanvas::clearResults() { int i=1; for(;icountInorgCompounds;i++){ free(compoundCollections[0]->inorgCompounds[i]); } for(i=0;icountConditions;i++){ free(compoundCollections[0]->conditions[i]); } if(compoundCollections[0]->countInorgCompounds)free(compoundCollections[0]->inorgCompounds); if(compoundCollections[0]->countConditions)free(compoundCollections[0]->conditions); compoundCollections[0]->countInorgCompounds=0; compoundCollections[0]->countConditions=0; compoundSetIndex=0; iLayerY=0; placeInorgNConditions(compoundCollections[0]); GameEngine::GetEngine()->setCountCompounds(compoundCollections[0]->countCompounds); } void CCanvas::clearCollection(CompoundCollection* collections) { //first clear already build results int i=0; for(;icountCompounds;i++){ int j=0; for(;jcompounds[i]->bondsCount;j++){ free(collections->compounds[i]->bonds[j]); } free(collections->compounds[i]->bonds); for(j=0;jcompounds[i]->elementsCount;j++){ free(collections->compounds[i]->elements[j]->bonds); free(collections->compounds[i]->elements[j]); } free(collections->compounds[i]->elements); free(collections->compounds[i]); } free(collections->compounds); } void CCanvas::takeCareCompound(OrgCompound *compound,int collectionIndex) { int i=0; for(;ielementsCount;i++){ if(compound->elements[i]->iType==0){ //if it is a c element int connectedC=0; int j=0; for(;jelements[i]->bondsCount;j++){ if(compound->elements[getConnectedElementIndex(i,compound->elements[i]->bonds[j])]->iType==0){ connectedC++; } } if(connectedC>1) //it is not a corner element continue; countChains=0; orgChains=(OrgChain**)malloc(sizeof(OrgChain*)); //create a new chain and assign it to array OrgChain * chain=(OrgChain*)malloc(sizeof(OrgChain)); orgChains[countChains]=chain; chain->countBonds=0; chain->countChains=0; chain->countElements=0; chain->startElementIndex=-1; chain->endElementIndex=i; countChains++; fillToChains(i,chain,-1,compound); assignLevels(compound,orgChains,countChains); break; } } countMainChains=0; FillMainChains(compound); assingnNumbersForLevelsOfMainChains(compound); FilterTheMain(); compound->mainChain=mainChains[0]; free(mainChains); countMainChains=0; for(i=0;istart.x=0l; compound->start.y=0l; compound->end.x=0; compound->end.y=0; //initializing to zero for(i=0;ielementsCount;i++){ if(compound->elements[i]==compound->mainChain->elements[0]){ break; } } numberOfPoints=0; occupiedPoints=(POINT*)malloc(sizeof(POINT)*compound->elementsCount); givePosition(compound,i,NULL,-10,-10,1,0,1); free(occupiedPoints); for(i=0;ielementsCount;i++){ compound->elements[i]->x-=compound->start.x+28; compound->elements[i]->y-=compound->start.y; } for(i=0;ibondsCount;i++){ compound->bonds[i]->start.x-=compound->start.x+28; compound->bonds[i]->start.y-=compound->start.y; compound->bonds[i]->end.x-=compound->start.x+28; compound->bonds[i]->end.y-=compound->start.y; } compound->end.x-=compound->start.x+28; compound->end.y-=compound->start.y; compound->length.x=compound->end.x; compound->length.y=compound->end.y; //(collectionIndex==0 || collectionIndextotal_processing) return; HPEN hGridPen = CreatePen(PS_SOLID, 1, RGB(150,150,225));//(255, 204, 153)); HPEN hPen = (HPEN)SelectObject(_hLayerDC, hGridPen); HBRUSH hBBrush = CreateSolidBrush(RGB(225, 225, 255)); HBRUSH hBrush = (HBRUSH)SelectObject(_hLayerDC, hBBrush); Rectangle(_hLayerDC,SLIDE_START_X,SLIDE_START_Y,SLIDE_START_X+200,SLIDE_START_Y+70); Rectangle(_hLayerDC,SLIDE_START_X+10,SLIDE_START_Y+10,SLIDE_START_X+190,SLIDE_START_Y+30); HBRUSH hDarkBrush = CreateSolidBrush(RGB(100, 100, 255)); SelectObject(_hLayerDC, hDarkBrush); DeleteObject(hBBrush); Rectangle(_hLayerDC,SLIDE_START_X+10,SLIDE_START_Y+10,SLIDE_START_X+10+(180*current_processing)/total_processing,SLIDE_START_Y+30); SelectObject(_hLayerDC,hPen); DeleteObject(hGridPen); SelectObject(_hLayerDC, hBrush); DeleteObject(hDarkBrush); COLORREF clfOld=(COLORREF)SetTextColor(_hLayerDC,RGB(100,100,255)); int iBackMode=SetBkMode(_hLayerDC,TRANSPARENT); HFONT fntRoman=CreateFont(20, 0, 0,0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Times New Roman"); HFONT fnt=(HFONT)SelectObject(_hLayerDC,fntRoman); char percentage[128]; wsprintf(percentage,"%d%% is completed..",(100*current_processing)/total_processing); TextOut(_hLayerDC,SLIDE_START_X+30,SLIDE_START_Y+40, percentage,strlen(percentage)); SelectObject(_hLayerDC,fnt); DeleteObject(fntRoman); SetBkMode(_hLayerDC,iBackMode); SetTextColor(_hLayerDC,clfOld); }