📄 animview_helper.cpp
字号:
pChild = pNext;
}
if( !m_pInfo->IsDirNode(pNode) )
{
CMMIRes &mmires = g_theApp.m_MMIRes;
mmires.DeleteAnim(pNode->szID);
}
return TRUE;
}
BOOL CAnimView::ReplaceAnimDir(HTREEITEM hItem, LPCTSTR pszDir, BOOL bCheck /* = FALSE */)
{
_ASSERTE( m_pInfo != NULL );
_ASSERTE( pszDir != NULL );
BOOL bReplaced = FALSE;
CTreeCtrl &tc = GetTreeCtrl();
// 如果选择的是动画目录直接替换,否则需要依次替换
CDirFileNode * pNode = (CDirFileNode *)tc.GetItemData(hItem);
if( m_pInfo->IsDirNode(pNode) )
{
// 依次替换需要匹配目录名
hItem = tc.GetChildItem(hItem);
RecursiveReplaceDir(hItem, pszDir, bReplaced, bCheck);
}
else
{
// 不需匹配目录名
bReplaced = ReplaceAnim(hItem, pszDir, bCheck);
}
return bReplaced;
}
BOOL CAnimView::RecursiveReplaceDir(HTREEITEM hItem, LPCTSTR pszDir, BOOL &bReplaced, BOOL bCheck /*= FALSE*/ )
{
_ASSERTE( m_pInfo != NULL );
_ASSERTE( pszDir != NULL );
if( hItem == NULL )
return TRUE;
CString strDir = pszDir;
BOOL bRet = TRUE;
CTreeCtrl &tc = GetTreeCtrl();
CDirFileNode * pNode = (CDirFileNode *)tc.GetItemData(hItem);
BOOL bContinue = FALSE;
HTREEITEM hSel = tc.GetSelectedItem();
if( hSel != hItem )
{
_ASSERTE(hSel != NULL);
for( HTREEITEM hTemp = hItem; hTemp != NULL && hTemp != hSel; hTemp = tc.GetParentItem(hTemp) )
{
if( tc.GetNextSiblingItem(hTemp) != NULL )
{
bContinue = TRUE;
break;
}
}
}
strDir += _T("\\");
strDir += pNode->szName;
if( m_pInfo->IsDirNode(pNode) )
{
HTREEITEM hChild = tc.GetChildItem(hItem);
//_ASSERTE( hChild != NULL );
if(hChild != NULL)
bRet = RecursiveReplaceDir(hChild, strDir, bReplaced, bCheck);
else
bRet = TRUE;
}
else if( !ReplaceAnim(hItem, strDir, bCheck) )
{
CString strMsg;
strMsg.Format(_T("Replace \"%s\" fail! Continue?"), pNode->szName);
if( bContinue && AfxMessageBox(strMsg, MB_YESNO) == IDNO )
return FALSE;
}
else
{
bReplaced = TRUE;
}
strDir = pszDir;
if( bRet && bContinue )
bRet = RecursiveReplaceDir(tc.GetNextSiblingItem(hItem), strDir, bReplaced, bCheck);
return bRet;
}
BOOL CAnimView::ReplaceAnim(HTREEITEM hItem, LPCTSTR pszDir, BOOL bCheck /* = FALSE */ )
{
_ASSERTE( hItem != NULL && pszDir != NULL );
CTreeCtrl &tc = GetTreeCtrl();
CDirFileNode * pNode = (CDirFileNode *)tc.GetItemData(hItem);
_ASSERTE( pNode != NULL );
CMMIRes &mmires = g_theApp.m_MMIRes;
PANIMINFO pAnimInfo = NULL;
if( !mmires.m_mapAnim.Lookup(pNode->szID, pAnimInfo) )
return TRUE;
int nFileNum = GetAnimFileNum(pszDir);
if( 0 == nFileNum )
{
CString strMsg;
strMsg.Format(_T("\"%s\" not exist!"), pszDir);
AfxMessageBox(strMsg);
return FALSE;
}
CString strFormat;
CString strFile;
if( bCheck )
{
if( nFileNum != pAnimInfo->nFrameNum )
{
// 检查数目
AfxMessageBox(_T("The dircotry's file num incorrect!"));
return FALSE;
}
// 依次检查文件和大小
WIN32_FIND_DATA wfd;
HANDLE hFind = INVALID_HANDLE_VALUE;
strFormat = nFileNum > 9 ? _T("%s\\%02d.bmp") : _T("%s\\%d.bmp");
for( int i = 1; i <= nFileNum ; ++i )
{
strFile.Format(strFormat, pszDir, i);
hFind = ::FindFirstFile(strFile, &wfd);
if( INVALID_HANDLE_VALUE == hFind )
{
CString strMsg;
strMsg.Format(_T("%s not exist!"), strFile);
AfxMessageBox(strMsg);
return FALSE;
}
else
{
CSize sizeBmp;
GetBmpSize(strFile, sizeBmp);
if( bCheck && (sizeBmp.cx != pNode->nWidth || sizeBmp.cy != pNode->nHeight) )
{
CString strMsg;
strMsg.Format(_T("file size incorrect in %s"), pszDir);
AfxMessageBox(strMsg);
::FindClose(hFind);
return FALSE;
}
}
::FindClose(hFind);
}
}
else
{
HTREEITEM hChild = NULL;
while( (hChild = tc.GetChildItem(hItem)) != NULL )
tc.DeleteItem(hChild);
HTREEITEM hInsert = NULL;
for( int i = 1; i <= nFileNum; ++i )
{
strFormat = nFileNum > 9 ? _T("%02d.bmp") : _T("%d.bmp");
strFile.Format(strFormat, i);
hInsert = tc.InsertItem(strFile, IMG_IDX, IMG_SEL_IDX, hItem );
_ASSERTE( hInsert != NULL );
tc.SetItemData(hInsert, NULL);
}
strFormat = nFileNum > 9 ? _T("%s\\%02d.bmp") : _T("%s\\%d.bmp");
strFile.Format( strFormat, pszDir, 1 );
CSize sizeBmp;
GetBmpSize(strFile, sizeBmp);
pNode->nWidth = sizeBmp.cx;
pNode->nHeight = sizeBmp.cy;
}
m_SizeInfo.DeleteByHandle( (DWORD)hItem );
UINT uOld = 0;
UINT uNew = 0;
if( !mmires.ReplaceAnim(pNode->szID, pszDir, bCheck, &uOld, &uNew) )
{
AfxMessageBox(mmires.GetErrMsg());
return FALSE;
}
//@Hongliang Xin 2006-8-14
if( uNew != uOld )
{
m_SizeInfo.SetSizeInfo( (DWORD)hItem, uOld, uNew );
}
return TRUE;
}
BOOL CAnimView::GetBmpSize(LPCTSTR pszFileName, CSize &sizeBmp)
{
_ASSERTE( pszFileName != NULL );
sizeBmp.cx = sizeBmp.cy = 0;
HANDLE hFile = ::CreateFile( pszFileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL );
if( hFile == INVALID_HANDLE_VALUE )
{
CString strMsg;
strMsg.Format(_T("Read file \"%s\" error!"), pszFileName);
AfxMessageBox(strMsg);
return FALSE;
}
SetFilePointer(hFile, sizeof(BITMAPFILEHEADER), NULL, FILE_CURRENT);
DWORD dwRead = 0;
BITMAPINFOHEADER bih = { 0 };
VERIFY( ReadFile(hFile, &bih, sizeof(bih), &dwRead, NULL) );
CloseHandle(hFile);
sizeBmp.cx = bih.biWidth;
sizeBmp.cy = bih.biHeight;
return TRUE;
}
//DEL void CAnimView::ForceConvertBmpFile( LPCTSTR pszFileName )
//DEL {
//DEL _ASSERTE( pszFileName != NULL );
//DEL
//DEL LPBYTE pBmp = NULL;
//DEL int nSize = 0;
//DEL CBmpProcessor bmpProc;
//DEL if( bmpProc.LoadBmpFile(pszFileName, &pBmp, &nSize, TRUE) )
//DEL {
//DEL HANDLE hFile = ::CreateFile( pszFileName, GENERIC_WRITE, 0,
//DEL NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
//DEL NULL );
//DEL _ASSERTE( hFile != INVALID_HANDLE_VALUE );
//DEL
//DEL DWORD dwWritten = 0;
//DEL WriteFile(hFile, pBmp, nSize, &dwWritten, NULL);
//DEL CloseHandle(hFile);
//DEL
//DEL bmpProc.FreeBmpStream( pBmp );
//DEL }
//DEL }
void CAnimView::RemoveSizeInfo( HTREEITEM hItem )
{
if( NULL == hItem )
return;
CTreeCtrl &tc = GetTreeCtrl();
m_SizeInfo.DeleteByHandle( (DWORD)hItem );
for( HTREEITEM hChild = tc.GetChildItem(hItem); hChild != NULL;
hChild = tc.GetNextSiblingItem(hChild) )
{
RemoveSizeInfo(hChild);
}
}
BOOL CAnimView::EnumAndExportAnims( CDirFileNode * pParentNode, LPCTSTR pszPathName)
{
_ASSERTE( pParentNode != NULL && pszPathName != NULL );
CString strPathName = pszPathName;
CDirFileNode *pNode = pParentNode;
CDirFileInfo dfi;
if(pNode->bIsDir)
{
if( !::CreateDirectory(strPathName, NULL) )
{
if( GetLastError() != ERROR_ALREADY_EXISTS )
{
_ASSERTE( 0 );
return FALSE;
}
}
strPathName+="\\";
}
else
{
g_theApp.m_MMIRes.ExportAnim(pNode->szID,strPathName);
return TRUE;
}
for( pNode = (CDirFileNode *)dfi.GetChild(pNode);
pNode != NULL; pNode = (CDirFileNode *)dfi.GetNext(pNode) )
{
CString strNewDir = strPathName;
if(!pNode->bIsDir)
strNewDir+=pNode->szName;
if(!EnumAndExportAnims(pNode, strNewDir))
return FALSE;
}
return TRUE;
}
BOOL CAnimView::RenameDir( CDirFileNode * pParentNode,LPCTSTR pszOldName, CString &strNewName)
{
CString strOldName(pszOldName);
_ASSERTE( !strOldName.IsEmpty() );
CRenameDlg dlg;
dlg.m_strAdded = strOldName;
dlg.m_strAdding = strNewName;
dlg.m_bDirFile = TRUE;
dlg.m_pParentNode = pParentNode;
dlg.m_pDirFileInfo= m_pInfo;
if( dlg.DoModal() == IDOK )
{
strNewName = dlg.m_strNewName;
return TRUE;
}
else
return FALSE;
}
BOOL CAnimView::DoCheckAndRenameDir( CDirFileNode * pParentNode, LPCTSTR pszOldName, CString &strNewName)
{
CDirFileNode * pCNode =(CDirFileNode *)m_pInfo->GetChild(pParentNode);
while(pCNode != NULL)
{
if(_tcscmp(pCNode->szName, pszOldName)==0)
{
if(AfxMessageBox(_T("This directory already exist,\ndo you rename it?"), MB_YESNO)==IDNO)
return FALSE;
return RenameDir(pParentNode,pszOldName,strNewName);
}
pCNode = (CDirFileNode *)m_pInfo->GetNext(pCNode);
}
strNewName = pszOldName;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -