DlgCreateAnimatedTexture.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. // DlgCreateAnimatedTexture.cpp : implementation file
  13. //
  14. #include "StdH.h"
  15. #include "DlgCreateAnimatedTexture.h"
  16. #include <Engine/Templates/Stock_CTextureData.h>
  17. #ifdef _DEBUG
  18. #undef new
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CDlgCreateAnimatedTexture dialog
  25. #define TEMPORARY_SCRIPT_NAME "Temp\\Temp.scr"
  26. #define TEMPORARY_TEXTURE_NAME "Temp\\Temp.tex"
  27. void CDlgCreateAnimatedTexture::ReleaseCreatedTexture(void)
  28. {
  29. // if there is texture obtained, release it
  30. if( m_ptdCreated!= NULL)
  31. {
  32. // free obtained texture
  33. _pTextureStock->Release( m_ptdCreated);
  34. m_ptdCreated = NULL;
  35. m_wndViewCreatedTexture.m_toTexture.SetData( NULL);
  36. }
  37. }
  38. void CDlgCreateAnimatedTexture::InitAnimationsCombo(void)
  39. {
  40. m_ctrlAnimationsCombo.ResetContent();
  41. if( m_ptdCreated != NULL)
  42. {
  43. CAnimInfo aiInfo;
  44. for( INDEX iAnim=0; iAnim<m_ptdCreated->GetAnimsCt(); iAnim++)
  45. {
  46. m_ptdCreated->GetAnimInfo( iAnim, aiInfo);
  47. m_ctrlAnimationsCombo.AddString(CString(aiInfo.ai_AnimName));
  48. }
  49. }
  50. else
  51. {
  52. m_ctrlAnimationsCombo.AddString( L"None");
  53. }
  54. m_ctrlAnimationsCombo.SetCurSel( 0);
  55. }
  56. void CDlgCreateAnimatedTexture::OnSelchangeTextureAnimations()
  57. {
  58. if( m_ptdCreated != NULL)
  59. {
  60. // set selected animation
  61. INDEX iAnim = m_ctrlAnimationsCombo.GetCurSel();
  62. m_wndViewCreatedTexture.m_toTexture.SetAnim( iAnim);
  63. }
  64. }
  65. void CDlgCreateAnimatedTexture::RefreshTexture(void)
  66. {
  67. // refresh script string from edit control
  68. UpdateData( TRUE);
  69. // prepare names for temporary script and texture
  70. CTFileName fnTempScript = CTString(TEMPORARY_SCRIPT_NAME);
  71. CTFileName fnTemptexture = CTString(TEMPORARY_TEXTURE_NAME);
  72. try
  73. {
  74. // write context of edit ctrl to temporary script file
  75. CTFileStream fileScript;
  76. fileScript.Create_t( fnTempScript);
  77. CTString strEditScript = CStringA(m_strEditScript);
  78. char *pScript = (char *) AllocMemory( strlen(strEditScript)+1);
  79. strcpy( pScript, strEditScript);
  80. fileScript.WriteRawChunk_t( pScript, strlen(strEditScript)+1);
  81. fileScript.Close();
  82. FreeMemory( pScript);
  83. // process script
  84. ProcessScript_t( fnTempScript);
  85. // release old texture if it exists and obtain new texture
  86. ReleaseCreatedTexture();
  87. // obtain newly created texture
  88. m_ptdCreated = _pTextureStock->Obtain_t( fnTemptexture);
  89. m_ptdCreated->Reload();
  90. // set texture data to texture preview window so it could display texture
  91. m_wndViewCreatedTexture.m_toTexture.SetData( m_ptdCreated);
  92. char achrSize[64];
  93. sprintf( achrSize, "%d x %d",
  94. m_ptdCreated->td_mexWidth>>m_ptdCreated->td_iFirstMipLevel,
  95. m_ptdCreated->td_mexHeight>>m_ptdCreated->td_iFirstMipLevel);
  96. m_strSizeInPixels = achrSize;
  97. UpdateData( FALSE);
  98. // init animations combo
  99. InitAnimationsCombo();
  100. }
  101. catch(char *err_str)
  102. {
  103. AfxMessageBox( CString(err_str));
  104. }
  105. }
  106. CDlgCreateAnimatedTexture::CDlgCreateAnimatedTexture(
  107. CDynamicArray<CTFileName> &afnPictures, CWnd* pParent /*=NULL*/)
  108. : CDialog(CDlgCreateAnimatedTexture::IDD, pParent)
  109. {
  110. //{{AFX_DATA_INIT(CDlgCreateAnimatedTexture)
  111. m_strEditScript = _T("");
  112. m_strSizeInPixels = _T("");
  113. m_strCreatedTextureName = _T("");
  114. //}}AFX_DATA_INIT
  115. // remember array of selected frames
  116. m_pafnPictures = &afnPictures;
  117. // set first frame as input file name
  118. afnPictures.Lock();
  119. CTFileName fnInputFile = afnPictures[0];
  120. afnPictures.Unlock();
  121. if( (fnInputFile != "") &&
  122. ((fnInputFile.FileExt() == ".tex") || (fnInputFile.FileExt() == ".scr")) )
  123. {
  124. m_strCreatedTextureName = fnInputFile.FileDir() + fnInputFile.FileName() + ".tex";
  125. }
  126. else
  127. {
  128. m_strCreatedTextureName = "Unnamed";
  129. }
  130. m_bPreviewWindowsCreated = FALSE;
  131. m_ptdCreated = NULL;
  132. m_pixSourceWidth = -1;
  133. m_pixSourceHeight = -1;
  134. // remember source and destination file names
  135. m_fnSourceFileName = fnInputFile;
  136. m_fnCreatedFileName = fnInputFile.FileDir()+fnInputFile.FileName()+".tex";
  137. }
  138. CDlgCreateAnimatedTexture::~CDlgCreateAnimatedTexture()
  139. {
  140. ReleaseCreatedTexture();
  141. }
  142. void CDlgCreateAnimatedTexture::DoDataExchange(CDataExchange* pDX)
  143. {
  144. CDialog::DoDataExchange(pDX);
  145. // if dialog is recieving data
  146. if(pDX->m_bSaveAndValidate == FALSE)
  147. {
  148. }
  149. //{{AFX_DATA_MAP(CDlgCreateAnimatedTexture)
  150. DDX_Control(pDX, IDC_CHEQUERED_ALPHA, m_ctrlCheckButton);
  151. DDX_Control(pDX, IDC_TEXTURE_ANIMATIONS, m_ctrlAnimationsCombo);
  152. DDX_Text(pDX, IDC_EDIT_SCRIPT, m_strEditScript);
  153. DDX_Text(pDX, IDC_SIZE_IN_PIXELS, m_strSizeInPixels);
  154. DDX_Text(pDX, IDC_TEXTURE_NAME, m_strCreatedTextureName);
  155. //}}AFX_DATA_MAP
  156. // if dialog is giving data
  157. if(pDX->m_bSaveAndValidate != FALSE)
  158. {
  159. }
  160. }
  161. BEGIN_MESSAGE_MAP(CDlgCreateAnimatedTexture, CDialog)
  162. //{{AFX_MSG_MAP(CDlgCreateAnimatedTexture)
  163. ON_WM_PAINT()
  164. ON_BN_CLICKED(IDC_CHEQUERED_ALPHA, OnChequeredAlpha)
  165. ON_BN_CLICKED(ID_CREATE_TEXTURE, OnCreateTexture)
  166. ON_BN_CLICKED(ID_REFRESH_TEXTURE, OnRefreshTexture)
  167. ON_CBN_SELCHANGE(IDC_TEXTURE_ANIMATIONS, OnSelchangeTextureAnimations)
  168. //}}AFX_MSG_MAP
  169. END_MESSAGE_MAP()
  170. /////////////////////////////////////////////////////////////////////////////
  171. // CDlgCreateAnimatedTexture message handlers
  172. void CDlgCreateAnimatedTexture::OnPaint()
  173. {
  174. CPaintDC dc(this); // device context for painting
  175. // if texture preview windows are not yet created
  176. if( !m_bPreviewWindowsCreated)
  177. {
  178. // ---------------- Create custom window that will show how created texture will look like
  179. CWnd *pWndCreatedTexturePreview = GetDlgItem(IDC_TEXTURE_PREVIEW_WINDOW);
  180. ASSERT(pWndCreatedTexturePreview != NULL);
  181. CRect rectPreviewCreatedTextureWnd;
  182. // get rectangle occupied by preview texture window
  183. pWndCreatedTexturePreview->GetWindowRect( &rectPreviewCreatedTextureWnd);
  184. ScreenToClient( &rectPreviewCreatedTextureWnd);
  185. // create window for for showing created texture
  186. m_wndViewCreatedTexture.Create( NULL, NULL, WS_BORDER|WS_VISIBLE, rectPreviewCreatedTextureWnd,
  187. this, IDW_VIEW_CREATED_TEXTURE);
  188. // mark that custom windows are created
  189. m_bPreviewWindowsCreated = TRUE;
  190. }
  191. }
  192. void CDlgCreateAnimatedTexture::OnChequeredAlpha()
  193. {
  194. // toggle chequered alpha on/off
  195. m_wndViewCreatedTexture.m_bChequeredAlpha = !m_wndViewCreatedTexture.m_bChequeredAlpha;
  196. }
  197. void CDlgCreateAnimatedTexture::OnRefreshTexture()
  198. {
  199. RefreshTexture();
  200. }
  201. void CDlgCreateAnimatedTexture::OnCreateTexture()
  202. {
  203. // refresh (recreate) texture in temporary directory
  204. RefreshTexture();
  205. // prepare names for temporary script and texture
  206. CTFileName fnFullTempTexture = _fnmApplicationPath+CTString(TEMPORARY_TEXTURE_NAME);
  207. CTFileName fnFullTempScript = _fnmApplicationPath+CTString(TEMPORARY_SCRIPT_NAME);
  208. // and for supposed final texture name
  209. CTFileName fnFullFinalTexture = _fnmApplicationPath+m_fnCreatedFileName;
  210. CTFileName fnSaveName;
  211. if( m_strCreatedTextureName == "Unnamed")
  212. {
  213. // extract last sub directory name
  214. char achrLastSubDir[ 256];
  215. strcpy( achrLastSubDir, m_fnSourceFileName.FileDir());
  216. achrLastSubDir[ strlen(achrLastSubDir)-1]=0; // remove last '\'
  217. CTString strLastSubDir = CTFileName(CTString(achrLastSubDir)).FileName();
  218. // call save texture requester
  219. fnSaveName = _EngineGUI.BrowseTexture(
  220. strLastSubDir+".tex", // default name
  221. KEY_NAME_CREATE_ANIMATED_TEXTURE_DIR, "Choose texture name",
  222. FALSE/* bOpenFileRequester*/);
  223. if( fnSaveName == "") return;
  224. }
  225. else
  226. {
  227. fnSaveName = CTString( CStringA(m_strCreatedTextureName));
  228. }
  229. // set newly picked names for final script and texture
  230. fnFullFinalTexture = _fnmApplicationPath+fnSaveName;
  231. CTFileName fnFullFinalScript =
  232. fnFullFinalTexture.FileDir()+fnFullFinalTexture.FileName()+".scr";
  233. // copy temporary script and texture files into real their place
  234. CopyFileA( fnFullTempScript, fnFullFinalScript, FALSE);
  235. CopyFileA( fnFullTempTexture, fnFullFinalTexture, FALSE);
  236. m_fnCreatedFileName =fnSaveName;
  237. // end dialog
  238. EndDialog( IDOK);
  239. }
  240. BOOL CDlgCreateAnimatedTexture::OnInitDialog()
  241. {
  242. CDialog::OnInitDialog();
  243. // if we received script as input
  244. if( m_fnSourceFileName.FileExt() == ".scr")
  245. {
  246. // load script file into edit control
  247. try
  248. {
  249. CTFileStream fileScript;
  250. fileScript.Open_t( m_fnSourceFileName);
  251. // get size of script file
  252. ULONG ulScriptFileSize = fileScript.GetStreamSize();
  253. char *pchrFile = new char[ ulScriptFileSize+1];
  254. // set eol character
  255. pchrFile[ ulScriptFileSize] = 0;
  256. fileScript.Read_t( pchrFile, ulScriptFileSize);
  257. // copy script to edit ctrl
  258. m_strEditScript = CTString( pchrFile);
  259. delete pchrFile;
  260. }
  261. // catch errors
  262. catch( char *strError)
  263. {
  264. // and do nothing
  265. (void) strError;
  266. }
  267. }
  268. // we will create temporary script
  269. else
  270. {
  271. try
  272. {
  273. // if can't get picture file information
  274. CImageInfo iiImageInfo;
  275. if (iiImageInfo.GetGfxFileInfo_t(m_fnSourceFileName)==UNSUPPORTED_FILE)
  276. {
  277. // throw error
  278. ThrowF_t("File '%s' has unsupported file format",
  279. (CTString&)(_fnmApplicationPath+m_fnSourceFileName));
  280. }
  281. // get dimensions
  282. m_pixSourceWidth = iiImageInfo.ii_Width;
  283. m_pixSourceHeight = iiImageInfo.ii_Height;
  284. }
  285. catch(char *err_str)
  286. {
  287. AfxMessageBox( CString(err_str));
  288. }
  289. // allocate 16k for script
  290. char achrDefaultScript[ 16384];
  291. // default script into edit control
  292. sprintf( achrDefaultScript,
  293. ";* Texture description\r\n"
  294. "TEXTURE_WIDTH %.4f\r\n"
  295. "TEXTURE_MIPMAPS 8\r\n"
  296. "ANIM_START\r\n"
  297. "DIRECTORY %s\r\n\r\n"
  298. ";* Animations\r\n"
  299. "ANIMATION Default_Animation\r\n"
  300. "SPEED 0.1\r\n"
  301. "FRAMES %d\r\n",
  302. METERS_MEX(m_pixSourceWidth * (1 << 5) ),
  303. (CTString&)m_fnCreatedFileName.FileDir(),
  304. m_pafnPictures->Count());
  305. // add name for each frame
  306. FOREACHINDYNAMICARRAY( *m_pafnPictures, CTFileName, itPicture)
  307. {
  308. CTFileName &fn=*itPicture;
  309. CTString strName=fn.FileName();
  310. CTString strExt=fn.FileExt();
  311. // add finishing part of script
  312. sprintf( achrDefaultScript, "%s %s%s\r\n", achrDefaultScript, strName, strExt);
  313. }
  314. // add finishing part of script
  315. sprintf( achrDefaultScript, "%sANIM_END\r\nEND\r\n", achrDefaultScript);
  316. // copy default script into edit ctrl
  317. m_strEditScript = achrDefaultScript;
  318. }
  319. CTFileName fnTexFileName = m_fnSourceFileName.FileDir() + m_fnSourceFileName.FileName() + ".tex";
  320. // try to
  321. try
  322. {
  323. // obtain texture with the same name (if exists)
  324. CTextureData *pTD = _pTextureStock->Obtain_t( fnTexFileName);
  325. pTD->Reload();
  326. // release texture
  327. _pTextureStock->Release( pTD);
  328. }
  329. // if texture can't be obtained
  330. catch( char *err_str)
  331. {
  332. // never mind
  333. (void) err_str;
  334. }
  335. m_ctrlCheckButton.SetCheck( 1);
  336. // force edit script control to pick up default script string
  337. UpdateData( FALSE);
  338. // and refresh (recreate) texture in temporary directory
  339. RefreshTexture();
  340. return TRUE;
  341. }