// ToggleButton.cpp: implementation of the CToggleButton class. // ////////////////////////////////////////////////////////////////////// #include "ToggleButton.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CToggleButton::CToggleButton() { HWND hwindow=GameEngine::GetEngine()->GetWindow(); HDC hDC=GetDC(hwindow); HINSTANCE hInstance=GameEngine::GetEngine()->GetInstance(); for(int i=0;i<25;i++){ bmpButton[i]=new Bitmap(hDC,4000+i,hInstance); } bHold=false; ReleaseDC(hwindow,hDC); } CToggleButton::~CToggleButton() { } void CToggleButton::SetImage(char *szImgName,int x,int y,HDC hDC,int iOffSet){ iCurrentImage=17; bHold=false; char szFile[64]; wsprintf(szFile,"Images\\Compounds\\%s.bmp",szImgName); Bitmap * bmpCmpd=new Bitmap(hDC,szFile); ix=x; iy=y; iwidth=bmpCmpd->GetWidth()+20+iOffSet*2; iheight=bmpCmpd->GetHeight()+20; bmpCmpd->Draw(hDC,x+10+iOffSet,y+10,true); delete bmpCmpd; } void CToggleButton::SetCmpd(char *szFileName,int x,int y,HDC hDC,Bitmap **pElements){ iCurrentImage=17; bHold=false; ix=x; iy=y; char szFile[64]; wsprintf(szFile,"Data\\CElements\\%s.txt",szFileName); SetElements(szFile,false,hDC,pElements); SetElements(szFile,true,hDC,pElements); } void CToggleButton::SetElements(char* szFileName,bool bOnlyElements,HDC hDC,Bitmap **pElements){ HANDLE hFileC = CreateFile(TEXT(szFileName), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); char chData; DWORD dwBytesRead; POINT ptSize; char szName[64]; while(true){ ReadFile(hFileC,&chData,1,&dwBytesRead,NULL); if(chData==(char)179){ break; } ReadFile(hFileC,&chData,1,&dwBytesRead,NULL); if(chData=='E'){ ReadFile(hFileC,&chData,1,&dwBytesRead,NULL); //skip one //Read the Element int iElement[3]; char szNumber[8]; for(int i=0;i<3;i++){ int j=0; while(true){ ReadFile(hFileC,&chData,1,&dwBytesRead,NULL); if(chData==':') break; else szNumber[j++]=chData; } szNumber[j]='\0'; iElement[i]=atoi(szNumber); } pElements[iElement[0]]->Draw(hDC,iElement[1]+ix+10,iElement[2]+iy+10,true); } else if(chData=='B'){ ReadFile(hFileC,&chData,1,&dwBytesRead,NULL); //skip one //Read the Bond int iBonds[5]; char szNumber[8]; for(int i=0;i<5;i++){ int j=0; while(true){ ReadFile(hFileC,&chData,1,&dwBytesRead,NULL); if(chData==':') break; else szNumber[j++]=chData; } szNumber[j]='\0'; iBonds[i]=atoi(szNumber); } if(!bOnlyElements){ POINT ptStartLn; POINT ptEndLn; ptStartLn.x=iBonds[1]+ix+10; ptStartLn.y=iBonds[2]+iy+10; ptEndLn.x=iBonds[3]+ix+10; ptEndLn.y=iBonds[4]+iy+10; DrawBond(hDC,iBonds[0],ptStartLn,ptEndLn); } } else if(chData=='S'){ ReadFile(hFileC,&chData,1,&dwBytesRead,NULL); //skip one //Read the size int iSizes[2]; char szSize[8]; for(int i=0;i<2;i++){ int j=0; while(true){ ReadFile(hFileC,&chData,1,&dwBytesRead,NULL); if(chData==':') break; else szSize[j++]=chData; } szSize[j]='\0'; iSizes[i]=atoi(szSize); } ptSize.x=iSizes[0]; ptSize.y=iSizes[1]; int j=0; while(true){ ReadFile(hFileC,&chData,1,&dwBytesRead,NULL); if(chData==':') break; else szName[j++]=chData; } szName[j]='\0'; } } CToggleButton::iwidth=ptSize.x+20; CToggleButton::iheight=ptSize.y+20; CloseHandle(hFileC); } void CToggleButton::DrawBond(HDC hDC,int iBondType,POINT ptStart,POINT ptEnd){ HPEN hGridPen = CreatePen(PS_SOLID, 1, RGB(255,0,0));//(255, 204, 153)); HPEN hPen = (HPEN)SelectObject(hDC, hGridPen); if(iBondType==1 || iBondType==4){ 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 CToggleButton::SetPlace(int x,int y){ ix=x; iy=y; } bool CToggleButton::MouseMove(int x,int y){ if(!(x>ix && xiy && yix && xiy && yGetWindow(); HDC hDC=GetDC(hWnd); DrawScreen(hDC); ReleaseDC(hWnd,hDC);*/ return true; } bool CToggleButton::MouseButtonUp(int x,int y,BOOL bLeft){ if(!(x>ix && xiy && yDraw(hDC,ix+iwidth-14,iy-1,true); bmpButton[2+iCurrentImage]->Draw(hDC,ix+iwidth-14,iy+iheight-14,true); bmpButton[4+iCurrentImage]->Draw(hDC,ix-1,iy+iheight-14,true); bmpButton[6+iCurrentImage]->Draw(hDC,ix-1,iy-1,true); for(int yPos=iy+14;yPos<=iy+iheight-30;yPos+=15){ bmpButton[1+iCurrentImage]->Draw(hDC,ix+iwidth-14,yPos,true); bmpButton[5+iCurrentImage]->Draw(hDC,ix-1,yPos,true); } bmpButton[1+iCurrentImage]->Draw(hDC,ix+iwidth-14,iy+iheight-29,true);; bmpButton[5+iCurrentImage]->Draw(hDC,ix-1,iy+iheight-29,true); for(int xPos=ix+14;xPos<=ix+iwidth-30;xPos+=15){ bmpButton[3+iCurrentImage]->Draw(hDC,xPos,iy+iheight-14,true); bmpButton[7+iCurrentImage]->Draw(hDC,xPos,iy-1,true); } bmpButton[3+iCurrentImage]->Draw(hDC,ix+iwidth-29,iy+iheight-14,true); bmpButton[7+iCurrentImage]->Draw(hDC,ix+iwidth-29,iy-1,true); }