📄 animview_helper.cpp
字号:
// AnimView_Helper.cpp : implementation file
//
#include "stdafx.h"
#include "resourceeditor.h"
#include "AnimView.h"
#include "DirFileInfo.h"
#include "BmpProcessor.h"
#include "Util.h"
#include "RenameDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// 用各菜单项填充TreeCtrl,将菜单信息以树形结构显示出来
// 被填充的菜单项(包括子菜单)
// 被填充的菜单项在树中所对应的父菜单的句柄
void CAnimView::FillTreeCtrl(CDirFileNode * pNode, HTREEITEM hParent/* = TVI_ROOT*/ )
{
if(pNode == NULL)
return;
int nChildCount = m_pInfo->GetChildCount(pNode);
if(nChildCount == 0)
return;
CTreeCtrl & tc = GetTreeCtrl();
for(int i = 0; i < nChildCount; i++)
{
CDirFileNode * pChild = (CDirFileNode *)m_pInfo->GetChild(pNode, i);
RecursiveFillTree(tc, pChild, hParent);
}
}
// 注:只填充当前节点和它的子节点,不填充兄弟节点
void CAnimView::RecursiveFillTree( CTreeCtrl &tc, CDirFileNode * pNode, HTREEITEM hParent )
{
if(pNode == NULL)
return;
pNode->bIsDir = m_pInfo->GetChild(pNode) != NULL;
hParent = tc.InsertItem(pNode->szName, IMG_COLL_IDX, IMG_COLL_SEL_IDX, hParent);
tc.SetItemData(hParent, (DWORD)pNode);
PANIMINFO pAnimInfo = NULL;
if( g_theApp.m_MMIRes.m_mapAnim.Lookup(pNode->szID, pAnimInfo) )
{
CString strName;
CString strFormat;
if( pAnimInfo->nFrameNum > 9 )
strFormat = _T("%02d.bmp");
else
strFormat = _T("%d.bmp");
for( int i = 1; i <= pAnimInfo->nFrameNum; ++i )
{
strName.Format(strFormat, i);
tc.InsertItem(strName, IMG_IDX, IMG_SEL_IDX, hParent);
}
}
CDirFileNode * pCur = (CDirFileNode *)m_pInfo->GetChild(pNode);
for ( ; pCur != NULL; pCur = (CDirFileNode *)m_pInfo->GetNext(pCur))
{
RecursiveFillTree(tc, pCur, hParent);
}
}
BOOL CAnimView::EnumAndFillImgs( CDirFileNode * pParentNode, HTREEITEM &hParentItem, LPCTSTR pszPathName)
{
_ASSERTE( pParentNode != NULL && pszPathName != NULL );
BOOL bRet = FALSE;
CString PathName = pszPathName;
HTREEITEM hOldParent = hParentItem;
CUtil strUtil;
{
PathName.TrimRight(_T('\\'));
PathName += _T("\\*.*");
WIN32_FIND_DATA wfd = { 0 };
HANDLE hFind = ::FindFirstFile(PathName, &wfd);
_ASSERTE( hFind != INVALID_HANDLE_VALUE );
::FindNextFile(hFind, &wfd);
bRet = AddAnimDir(&pParentNode, hParentItem, pszPathName);
hOldParent = hParentItem;
while( ::FindNextFile(hFind, &wfd) && bRet )
{
if( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
PathName = pszPathName;
PathName.TrimRight(_T('\\'));
PathName += _T('\\');
PathName += wfd.cFileName;
strUtil.AddStr(PathName);
}
}
::FindClose(hFind);
}
strUtil.SortStr();
HTREEITEM hItem = hParentItem;
int nCount = strUtil.GetStrSize();
for( int i = 0; i < nCount; ++i )
{
bRet = EnumAndFillImgs( pParentNode, hItem, strUtil.GetStr(i) );
hItem = hParentItem;
}
strUtil.RemoveAll();
hParentItem = hOldParent;
return bRet;
}
BOOL CAnimView::AddAnimDir(CDirFileNode ** ppNode, HTREEITEM & hItem, LPCTSTR pszDirName)
{
_ASSERTE( ppNode != NULL && pszDirName != NULL );
CMMIRes &mmires = g_theApp.m_MMIRes;
CString strID = mmires.MakeAnimID(pszDirName);
CString strNewDir = pszDirName;
int nFileNum = GetAnimFileNum(pszDirName);
int nCount = 0;
CString strDirName = pszDirName;
strDirName += _T("\\*.*");
WIN32_FIND_DATA wfd = { 0 };
HANDLE hFind = ::FindFirstFile( strDirName, &wfd );
::FindNextFile(hFind, &wfd);
while( ::FindNextFile(hFind, &wfd) )
{
CString strFileName =((CString) (wfd.cFileName)).Left(((CString) (wfd.cFileName)).GetLength() - 4);
LPCTSTR pExt = _tcsrchr(wfd.cFileName, _T('.'));
if( pExt != NULL && _tcsicmp(pExt, SZ_FILE_EXT) == 0 && _ttol(strFileName)>=1)
nCount++;
else
break;
}
::FindClose(hFind);
if (nCount != nFileNum)
{
MessageBox(_T("The file name in the directory must be like (1.bmp--9.bmp,01.bmp--99.bmp)!"));
return FALSE;
}
int nFileCount = GetAnimFileNum(strNewDir);
if( nFileCount > 0 )
{
if( !g_theApp.DoCheckAndRename(pszDirName, strID, strNewDir, MMI_RT_ANIM) )
{
return FALSE;
}
//if( !mmires.AddAnim(strID, strNewDir) )
if( !mmires.AddAnim(strID, pszDirName) )
{
AfxMessageBox(mmires.GetErrMsg());
return FALSE;
}
}
CDirFileNode * pNode = (CDirFileNode *)m_pInfo->MallocNode();
if( NULL == pNode )
return FALSE;
strNewDir.TrimRight(_T('\\'));
LPCTSTR pszDir = strNewDir.GetBuffer(0);
LPCTSTR pszFind = _tcsrchr(pszDir, _T('\\'));
if( pszFind == NULL )
pszFind = pszDir;
else
pszFind++;
m_pInfo->ZeroNode(pNode);
pNode->bIsDir = TRUE;
// _tcscpy(pNode->szID, strID);
// _tcscpy(pNode->szName, pszFind);
// strNewDir.ReleaseBuffer();
//xb
CString strReNewName;
if(!DoCheckAndRenameDir(*ppNode,pszFind,strReNewName))
{
strNewDir.ReleaseBuffer();
return FALSE;
}
_tcscpy(pNode->szID, g_theApp.m_MMIRes.MakeAnimID(strReNewName));
_tcscpy(pNode->szName, strReNewName);
strNewDir.ReleaseBuffer();
//xe*/
m_pInfo->AddChild(*ppNode, pNode);
CTreeCtrl &tc = GetTreeCtrl();
HTREEITEM hInsert = tc.InsertItem(strReNewName, IMG_COLL_IDX, IMG_COLL_SEL_IDX, hItem);
//HTREEITEM hInsert = tc.InsertItem(pszFind, IMG_COLL_IDX, IMG_COLL_SEL_IDX, hItem);
tc.SetItemData(hInsert, (DWORD)pNode);
//BOOL bRet = AddAnimFile( hInsert, strNewDir );
BOOL bRet = AddAnimFile( hInsert, pszDirName );
*ppNode = pNode;
hItem = hInsert;
return bRet;
}
BOOL CAnimView::AddAnimFile( HTREEITEM hParent, LPCTSTR pszDirName)
{
_ASSERTE( pszDirName != NULL );
int nCount = GetAnimFileNum(pszDirName);
_ASSERTE( nCount >= 0 && nCount < 100 );
if( nCount == 0 )
return TRUE;
CString strNewFormat, strOldFormat;
if( nCount > 9 )
{
strNewFormat = _T("%s\\%02d%s");
strOldFormat = _T("%s\\%d%s");
}
else
{
strNewFormat = _T("%s\\%d%s");
strOldFormat = _T("%s\\%02d%s");
}
WIN32_FIND_DATA wfd = { 0 };
HANDLE hFind = NULL;
CString strNewFile, strOldFile;
for( int i = 1; i <= nCount; ++i )
{
strNewFile.Format(strNewFormat, pszDirName, i, SZ_FILE_EXT);
hFind = ::FindFirstFile(strNewFile, &wfd);
if( INVALID_HANDLE_VALUE == hFind )
{
strOldFile.Format(strOldFormat, pszDirName, i, SZ_FILE_EXT);
if( !MoveFile(strOldFile, strNewFile) )
{
CString strMsg;
strMsg.Format(_T("%s is a incorrect anim's dir!"), pszDirName);
AfxMessageBox(strMsg);
return FALSE;
}
}
::FindClose(hFind);
}
CTreeCtrl &tc = GetTreeCtrl();
HTREEITEM hInsert = NULL;
strNewFile.Format(strNewFormat, pszDirName, 1, SZ_FILE_EXT);
CSize sizeBmp(0, 0);
GetBmpSize(strNewFile, sizeBmp);
CDirFileNode * pNode = (CDirFileNode *)tc.GetItemData(hParent);
_ASSERTE( pNode != NULL );
pNode->bIsDir = FALSE;
pNode->nWidth = sizeBmp.cx;
pNode->nHeight = sizeBmp.cy;
if( nCount > 9 )
strNewFormat = _T("%02d%s");
else
strNewFormat = _T("%d%s");
for( i = 1; i <= nCount; ++i )
{
strNewFile.Format(strNewFormat, i, SZ_FILE_EXT);
hInsert = tc.InsertItem(strNewFile, IMG_IDX, IMG_SEL_IDX, hParent);
tc.SetItemData(hInsert, NULL);
}
return TRUE;
}
// 说明:如果该目录下既有图片又有子目录,函数的返回值为0;即我们只处理
// 所有的动画全在叶子目录中
int CAnimView::GetAnimFileNum(LPCTSTR pszDirName)
{
_ASSERTE( pszDirName != NULL );
CString strDir = pszDirName;
strDir += _T("\\*.*");
WIN32_FIND_DATA wfd = { 0 };
HANDLE hFind = ::FindFirstFile( strDir, &wfd );
if( INVALID_HANDLE_VALUE == hFind )
{
::FindClose(hFind);
return 0;
}
::FindNextFile(hFind, &wfd);
int nCount = 0;
while( ::FindNextFile(hFind, &wfd) )
{
// 有子目录
if( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
nCount = 0;
break;
}
LPCTSTR pExt = _tcsrchr(wfd.cFileName, _T('.'));
if( pExt != NULL && _tcsicmp(pExt, SZ_FILE_EXT) == 0 )
nCount++;
}
::FindClose(hFind);
return nCount;
}
BOOL CAnimView::RemoveAllRes(CDirFileNode *pNode)
{
_ASSERTE( pNode != NULL );
RecursiveRemoveAllRes(pNode);
m_pInfo->RemoveNode(pNode);
return TRUE;
}
BOOL CAnimView::RecursiveRemoveAllRes(CDirFileNode *pNode)
{
if( pNode == NULL )
return TRUE;
CDirFileNode * pChild = (CDirFileNode *)m_pInfo->GetChild(pNode);
while ( NULL != pChild )
{
CDirFileNode * pNext = (CDirFileNode *)m_pInfo->GetNext(pChild);
RecursiveRemoveAllRes( pChild );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -