StageView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "StageView.h"
  23. IMPLEMENT_DYNCREATE(StageView, ToggleListView)
  24. BEGIN_MESSAGE_MAP(StageView, ToggleListView)
  25. ON_WM_CREATE()
  26. ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnLvnItemchanged)
  27. ON_NOTIFY_REFLECT(LVN_DELETEALLITEMS, OnLvnDeleteallitems)
  28. ON_NOTIFY_REFLECT(LVN_BEGINDRAG, OnLvnBegindrag)
  29. ON_WM_LBUTTONUP()
  30. ON_WM_MOUSEMOVE()
  31. ON_NOTIFY_REFLECT(NM_RCLICK, OnNMRclick)
  32. ON_COMMAND(ID_STAGEPOPUP_RENAMESTAGE, OnRenameStage)
  33. ON_COMMAND(ID_STAGEPOPUP_DELETESTAGE, OnDeleteStage)
  34. ON_COMMAND(ID_STAGEPOPUP_DELETEALLSTAGES, OnDeleteAllStages)
  35. ON_COMMAND(ID_STAGEPOPUP_ADDSTAGE, OnAddStage)
  36. ON_COMMAND(ID_STAGEPOPUP_ADDBUMPMAP, OnAddBumpmapStage)
  37. ON_COMMAND(ID_STAGEPOPUP_ADDDIFFUSEMAP, OnAddDiffuseStage)
  38. ON_COMMAND(ID_STAGEPOPUP_ADDSPECULAR, OnAddSpecualarStage)
  39. ON_COMMAND(ID_STAGEPOPUP_COPY, OnCopy)
  40. ON_COMMAND(ID_STAGEPOPUP_PASTE, OnPaste)
  41. ON_NOTIFY_REFLECT(LVN_BEGINLABELEDIT, OnLvnBeginlabeledit)
  42. ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnLvnEndlabeledit)
  43. ON_WM_CHAR()
  44. END_MESSAGE_MAP()
  45. /**
  46. * Constructor for StageView.
  47. */
  48. StageView::StageView() {
  49. currentMaterial = NULL;
  50. bDragging = false;
  51. internalChange = false;
  52. }
  53. /**
  54. * Destructor for StageView.
  55. */
  56. StageView::~StageView() {
  57. }
  58. /**
  59. * Called when the selected material has changed.
  60. * @param pMaterial The newly selected material.
  61. */
  62. void StageView::MV_OnMaterialSelectionChange(MaterialDoc* pMaterial) {
  63. currentMaterial = pMaterial;
  64. RefreshStageList();
  65. }
  66. /**
  67. * Called when the material changes have been saved.
  68. * @param pMaterial The saved material.
  69. */
  70. void StageView::MV_OnMaterialSaved(MaterialDoc* pMaterial) {
  71. CListCtrl& list = GetListCtrl();
  72. //Saving a material reenables all of the stages
  73. if(pMaterial == currentMaterial) {
  74. for(int i = 1; i < list.GetItemCount(); i++) {
  75. SetToggleState(i, ToggleListView::TOGGLE_STATE_ON);
  76. }
  77. }
  78. }
  79. /**
  80. * Called when a stage is added
  81. * @param pMaterial The material that was affected.
  82. * @param stageNum The index of the stage that was added
  83. */
  84. void StageView::MV_OnMaterialStageAdd(MaterialDoc* pMaterial, int stageNum) {
  85. CListCtrl& list = GetListCtrl();
  86. idStr name = pMaterial->GetAttribute(stageNum, "name");
  87. int index = list.InsertItem(stageNum+1, name.c_str());
  88. SetToggleState(index, ToggleListView::TOGGLE_STATE_ON);
  89. }
  90. /**
  91. * Called when a stage is deleted
  92. * @param pMaterial The material that was affected.
  93. * @param stageNum The index of the stage that was deleted
  94. */
  95. void StageView::MV_OnMaterialStageDelete(MaterialDoc* pMaterial, int stageNum) {
  96. CListCtrl& list = GetListCtrl();
  97. list.DeleteItem(stageNum+1);
  98. }
  99. /**
  100. * Called when a stage is moved
  101. * @param pMaterial The material that was deleted.
  102. * @param from The from index
  103. * @param to The to index
  104. */
  105. void StageView::MV_OnMaterialStageMove(MaterialDoc* pMaterial, int from, int to) {
  106. if(!internalChange) {
  107. from++;
  108. to++;
  109. CListCtrl& list = GetListCtrl();
  110. char szLabel[256];
  111. LV_ITEM lvi;
  112. ZeroMemory(&lvi, sizeof(LV_ITEM));
  113. lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_PARAM;
  114. lvi.stateMask = LVIS_DROPHILITED | LVIS_FOCUSED | LVIS_SELECTED;
  115. lvi.pszText = szLabel;
  116. lvi.iItem = from;
  117. lvi.cchTextMax = 255;
  118. list.GetItem(&lvi);
  119. //Delete the original item
  120. list.DeleteItem(from);
  121. //Insert the item
  122. lvi.iItem = to;
  123. list.InsertItem(&lvi);
  124. int type = -1;
  125. int stageType = currentMaterial->GetAttributeInt(to-1, "stagetype");
  126. switch(stageType) {
  127. case MaterialDoc::STAGE_TYPE_NORMAL:
  128. type = MaterialDefManager::MATERIAL_DEF_STAGE;
  129. break;
  130. case MaterialDoc::STAGE_TYPE_SPECIALMAP:
  131. type = MaterialDefManager::MATERIAL_DEF_SPECIAL_STAGE;
  132. break;
  133. }
  134. m_propView->SetPropertyListType(type, to-1);
  135. Invalidate();
  136. }
  137. }
  138. /**
  139. * Called when an attribute is changed
  140. * @param pMaterial The material that was deleted.
  141. * @param stage The stage that contains the change.
  142. * @param attribName The attribute that has changed.
  143. */
  144. void StageView::MV_OnMaterialAttributeChanged(MaterialDoc* pMaterial, int stage, const char* attribName) {
  145. //Refresh this stage list if a material name has changed
  146. if(!internalChange && currentMaterial == pMaterial && stage >= 0 && attribName && !strcmp(attribName, "name") ) {
  147. CListCtrl& list = GetListCtrl();
  148. list.SetItemText(stage+1, 0, currentMaterial->GetAttribute(stage, attribName));
  149. }
  150. }
  151. /**
  152. * Returns true if the current state of the stage view will allow a copy operation
  153. */
  154. bool StageView::CanCopy() {
  155. CListCtrl& list = GetListCtrl();
  156. POSITION pos = list.GetFirstSelectedItemPosition();
  157. int nItem = -1;
  158. if(pos)
  159. nItem = list.GetNextSelectedItem(pos);
  160. if(nItem > 0) {
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. }
  166. /**
  167. * Returns true if the current state of the stage view will allow a paste operation
  168. */
  169. bool StageView::CanPaste() {
  170. return materialDocManager->IsCopyStage();
  171. }
  172. /**
  173. * Cut is not supported for stages.
  174. */
  175. bool StageView::CanCut() {
  176. //No cut for stages
  177. return false;
  178. }
  179. /**
  180. * Returns true if the current state of the stage view will allow a delete operation
  181. */
  182. bool StageView::CanDelete() {
  183. CListCtrl& list = GetListCtrl();
  184. POSITION pos = list.GetFirstSelectedItemPosition();
  185. int nItem = -1;
  186. if(pos) {
  187. nItem = list.GetNextSelectedItem(pos);
  188. }
  189. if(nItem > 0)
  190. return true;
  191. return false;
  192. }
  193. /**
  194. * Returns true if the current state of the stage view will allow a rename operation
  195. */
  196. bool StageView::CanRename() {
  197. CListCtrl& list = GetListCtrl();
  198. POSITION pos = list.GetFirstSelectedItemPosition();
  199. int nItem = -1;
  200. if(pos) {
  201. nItem = list.GetNextSelectedItem(pos);
  202. }
  203. if(nItem > 0) {
  204. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  205. if(nItem > 0 && material->GetAttributeInt(nItem-1, "stagetype") == MaterialDoc::STAGE_TYPE_NORMAL) {
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. /**
  212. * Rebuilds the list of stages based on the currently selected material
  213. */
  214. void StageView::RefreshStageList() {
  215. CListCtrl& list = GetListCtrl();
  216. POSITION pos = list.GetFirstSelectedItemPosition();
  217. int selectedItem = -1;
  218. if(pos)
  219. selectedItem = list.GetNextSelectedItem(pos);
  220. list.DeleteAllItems();
  221. if(currentMaterial) {
  222. //Always add the material item for the main material properties
  223. list.InsertItem(0, "Material");
  224. SetToggleState(0, ToggleListView::TOGGLE_STATE_DISABLED);
  225. //Get the stage info
  226. int stageCount = currentMaterial->GetStageCount();
  227. for(int i = 0; i < stageCount; i++) {
  228. const char* name = currentMaterial->GetAttribute(i, "name");
  229. int itemNum = list.InsertItem(list.GetItemCount(), name);
  230. if(currentMaterial->IsStageEnabled(i)) {
  231. SetToggleState(itemNum, ToggleListView::TOGGLE_STATE_ON);
  232. } else {
  233. SetToggleState(itemNum, ToggleListView::TOGGLE_STATE_OFF);
  234. }
  235. }
  236. if(selectedItem < 0) {
  237. //Select the material
  238. list.SetItemState(0, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
  239. } else {
  240. list.SetItemState(selectedItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
  241. }
  242. }
  243. }
  244. /**
  245. * Called by the MFC framework when the view is being created.
  246. */
  247. int StageView::OnCreate(LPCREATESTRUCT lpCreateStruct) {
  248. if (ToggleListView::OnCreate(lpCreateStruct) == -1)
  249. return -1;
  250. SetToggleIcons(MAKEINTRESOURCE(IDI_ME_DISABLED_ICON), MAKEINTRESOURCE(IDI_ME_ON_ICON), MAKEINTRESOURCE(IDI_ME_OFF_ICON));
  251. return 0;
  252. }
  253. /**
  254. * Called when the user changes the selection in the list box. This method will notify the
  255. * property view of the change so that it can display the appropriate properties.
  256. */
  257. void StageView::OnLvnItemchanged(NMHDR *pNMHDR, LRESULT *pResult) {
  258. LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
  259. if(!bDragging) {
  260. //The state has changed and changed to selected
  261. if(pNMLV->uChanged && LVIF_STATE && pNMLV->uNewState & LVIS_SELECTED) {
  262. int type = -1;
  263. if(pNMLV->iItem >= 0) {
  264. if(pNMLV->iItem == 0)
  265. type = MaterialDefManager::MATERIAL_DEF_MATERIAL;
  266. else {
  267. int stageType = currentMaterial->GetAttributeInt(pNMLV->iItem-1, "stagetype");
  268. switch(stageType) {
  269. case MaterialDoc::STAGE_TYPE_NORMAL:
  270. type = MaterialDefManager::MATERIAL_DEF_STAGE;
  271. break;
  272. case MaterialDoc::STAGE_TYPE_SPECIALMAP:
  273. type = MaterialDefManager::MATERIAL_DEF_SPECIAL_STAGE;
  274. break;
  275. }
  276. }
  277. }
  278. m_propView->SetPropertyListType(type, pNMLV->iItem-1);
  279. }
  280. if(pNMLV->uChanged && LVIF_STATE && pNMLV->uOldState & LVIS_SELECTED && !(pNMLV->uNewState & LVIS_SELECTED)) {
  281. //This item was deselected.
  282. //If there is no item selected then clear the prop list
  283. CListCtrl& list = GetListCtrl();
  284. POSITION pos = list.GetFirstSelectedItemPosition();
  285. if(!pos)
  286. m_propView->SetPropertyListType(-1);
  287. }
  288. }
  289. *pResult = 0;
  290. }
  291. /**
  292. * Notifies the property view that all stages have been removed.
  293. */
  294. void StageView::OnLvnDeleteallitems(NMHDR *pNMHDR, LRESULT *pResult) {
  295. LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
  296. //The list has been cleared so clear the prop view
  297. m_propView->SetPropertyListType(-1);
  298. *pResult = 0;
  299. }
  300. /**
  301. * Starts the stage drag operation.
  302. */
  303. void StageView::OnLvnBegindrag(NMHDR *pNMHDR, LRESULT *pResult) {
  304. LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
  305. CListCtrl& list = GetListCtrl();
  306. //Start a drag if the item isn't the material
  307. if(pNMLV->iItem > 0) {
  308. dragIndex = pNMLV->iItem;
  309. //Trun off ownerdrawn to create the drag image correctly
  310. list.ModifyStyle(LVS_OWNERDRAWFIXED, 0);
  311. //Create the drag image
  312. POINT pt;
  313. pt.x = 8;
  314. pt.y = 8;
  315. dragImage = list.CreateDragImage(dragIndex, &pt);
  316. dragImage->BeginDrag(0, CPoint (8, 8));
  317. dragImage->DragEnter(GetDesktopWindow(), pNMLV->ptAction);
  318. //Turn the owner draw back on
  319. list.ModifyStyle(0, LVS_OWNERDRAWFIXED);
  320. //Drag is in progress
  321. bDragging = true;
  322. dropIndex = -1;
  323. dropWnd = &list;
  324. //Capture the messages
  325. SetCapture();
  326. }
  327. *pResult = 0;
  328. }
  329. /**
  330. * Finishes a stage drag operation of the user was dragging a stage.
  331. */
  332. void StageView::OnLButtonUp(UINT nFlags, CPoint point) {
  333. if( bDragging ) {
  334. //Release mouse capture
  335. ReleaseCapture();
  336. //Delete the drag image
  337. dragImage->DragLeave(GetDesktopWindow());
  338. dragImage->EndDrag();
  339. //Where did we drop
  340. CPoint pt(point);
  341. ClientToScreen(&pt);
  342. dropWnd = WindowFromPoint(pt);
  343. if( dropWnd->IsKindOf(RUNTIME_CLASS(StageView)) )
  344. DropItemOnList();
  345. bDragging = false;
  346. }
  347. ToggleListView::OnLButtonUp(nFlags, point);
  348. }
  349. /**
  350. * Handles drawing the drag image when a user is draging a stage.
  351. */
  352. void StageView::OnMouseMove(UINT nFlags, CPoint point) {
  353. if( bDragging ) {
  354. dropPoint = point;
  355. ClientToScreen(&dropPoint);
  356. //Move the drag image
  357. dragImage->DragMove(dropPoint);
  358. dragImage->DragShowNolock(FALSE);
  359. dropWnd = WindowFromPoint(dropPoint);
  360. dropWnd->ScreenToClient(&dropPoint);
  361. dragImage->DragShowNolock(TRUE);
  362. }
  363. ToggleListView::OnMouseMove(nFlags, point);
  364. }
  365. /**
  366. * Displays the popup menu when the user performs a right mouse click.
  367. */
  368. void StageView::OnNMRclick(NMHDR *pNMHDR, LRESULT *pResult) {
  369. if(materialDocManager->GetCurrentMaterialDoc()) {
  370. CListCtrl& list = GetListCtrl();
  371. DWORD dwPos = GetMessagePos();
  372. CPoint pt( LOWORD( dwPos ), HIWORD ( dwPos ) );
  373. CPoint spt = pt;
  374. list.ScreenToClient( &spt );
  375. PopupMenu(&spt);
  376. }
  377. *pResult = 0;
  378. }
  379. /**
  380. * Begins a label edit when the user selects the rename menu option.
  381. */
  382. void StageView::OnRenameStage() {
  383. CListCtrl& list = GetListCtrl();
  384. POSITION pos = list.GetFirstSelectedItemPosition();
  385. int nItem = -1;
  386. if(pos) {
  387. nItem = list.GetNextSelectedItem(pos);
  388. list.EditLabel(nItem);
  389. }
  390. }
  391. /**
  392. * Deletes the selected stage when the user selects the delete menu option.
  393. */
  394. void StageView::OnDeleteStage() {
  395. CListCtrl& list = GetListCtrl();
  396. POSITION pos = list.GetFirstSelectedItemPosition();
  397. int nItem = -1;
  398. if(pos) {
  399. nItem = list.GetNextSelectedItem(pos);
  400. if(nItem > 0) {
  401. int result = MessageBox("Are you sure you want to delete this stage?", "Delete?", MB_ICONQUESTION | MB_YESNO);
  402. if(result == IDYES) {
  403. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  404. material->RemoveStage(nItem-1);
  405. }
  406. }
  407. }
  408. }
  409. /**
  410. * Conforms the user wants to delete all stages and then performs the operation.
  411. */
  412. void StageView::OnDeleteAllStages() {
  413. int result = MessageBox("Are you sure you want to delete all stages?", "Delete?", MB_ICONQUESTION | MB_YESNO);
  414. if(result == IDYES) {
  415. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  416. material->ClearStages();
  417. }
  418. }
  419. /**
  420. * Adds a new stage when the user selects the menu option.
  421. */
  422. void StageView::OnAddStage() {
  423. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  424. idStr name = va("Stage %d", material->GetStageCount()+1);
  425. material->AddStage(MaterialDoc::STAGE_TYPE_NORMAL, name.c_str());
  426. }
  427. /**
  428. * Adds a new bumpmap stage when the user selects the menu option.
  429. */
  430. void StageView::OnAddBumpmapStage() {
  431. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  432. material->AddStage(MaterialDoc::STAGE_TYPE_SPECIALMAP, "bumpmap");
  433. }
  434. /**
  435. * Adds a new diffusemap stage when the user selects the menu option.
  436. */
  437. void StageView::OnAddDiffuseStage() {
  438. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  439. material->AddStage(MaterialDoc::STAGE_TYPE_SPECIALMAP, "diffusemap");
  440. }
  441. /**
  442. * Adds a new specularmap stage when the user selects the menu option.
  443. */
  444. void StageView::OnAddSpecualarStage() {
  445. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  446. material->AddStage(MaterialDoc::STAGE_TYPE_SPECIALMAP, "specularmap");
  447. }
  448. /**
  449. * Performs a copy operation when the user selects the menu option.
  450. */
  451. void StageView::OnCopy() {
  452. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  453. CListCtrl& list = GetListCtrl();
  454. POSITION pos = list.GetFirstSelectedItemPosition();
  455. int nItem = -1;
  456. if(pos)
  457. nItem = list.GetNextSelectedItem(pos);
  458. if(nItem > 0) {
  459. materialDocManager->CopyStage(material, nItem-1);
  460. }
  461. }
  462. /**
  463. * Performs a paste operation when the user selects the menu option.
  464. */
  465. void StageView::OnPaste() {
  466. if(materialDocManager->IsCopyStage()) {
  467. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  468. int type;
  469. idStr name;
  470. materialDocManager->GetCopyStageInfo(type, name);
  471. int existingIndex = material->FindStage(type, name);
  472. if(type != MaterialDoc::STAGE_TYPE_SPECIALMAP || existingIndex == -1) {
  473. materialDocManager->PasteStage(material);
  474. } else {
  475. if(MessageBox(va("Do you want to replace '%s' stage?", name.c_str()), "Replace?", MB_ICONQUESTION | MB_YESNO) == IDYES) {
  476. material->RemoveStage(existingIndex);
  477. materialDocManager->PasteStage(material);
  478. }
  479. }
  480. }
  481. }
  482. /**
  483. * Determines is a label edit can be performed on the selected stage.
  484. */
  485. void StageView::OnLvnBeginlabeledit(NMHDR *pNMHDR, LRESULT *pResult) {
  486. NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
  487. //if this is a special stage then don't allow edit
  488. int index = pDispInfo->item.iItem;
  489. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  490. if(index <= 0 || material->GetAttributeInt(index-1, "stagetype") != MaterialDoc::STAGE_TYPE_NORMAL)
  491. {
  492. *pResult = 1;
  493. return;
  494. }
  495. //ToDo: Can we move the edit box
  496. /*HWND edit = ListView_GetEditControl(m_hWnd);
  497. CWnd* editWnd = CWnd::FromHandle(edit);
  498. CRect rect;
  499. editWnd->GetWindowRect(rect);
  500. rect.left += 22;
  501. rect.right += 22;
  502. editWnd->MoveWindow(rect);*/
  503. *pResult = 0;
  504. }
  505. /**
  506. * Performs the stage name change after the label edit is done.
  507. */
  508. void StageView::OnLvnEndlabeledit(NMHDR *pNMHDR, LRESULT *pResult) {
  509. NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
  510. if(pDispInfo->item.pszText) {
  511. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  512. internalChange = true;
  513. material->SetAttribute(pDispInfo->item.iItem-1, "name", pDispInfo->item.pszText);
  514. internalChange = false;
  515. *pResult = 1;
  516. } else {
  517. *pResult = 0;
  518. }
  519. }
  520. /**
  521. * Handles keyboard shortcuts for copy and paste operations.
  522. */
  523. void StageView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) {
  524. if(nChar == 3 && GetKeyState(VK_CONTROL)) {
  525. OnCopy();
  526. }
  527. if(nChar == 22 && GetKeyState(VK_CONTROL)) {
  528. OnPaste();
  529. }
  530. ToggleListView::OnChar(nChar, nRepCnt, nFlags);
  531. }
  532. /**
  533. * Handles keyboard shortcut for the delete operations.
  534. */
  535. BOOL StageView::PreTranslateMessage(MSG* pMsg) {
  536. CListCtrl& list = GetListCtrl();
  537. if (pMsg->hwnd == list.GetSafeHwnd()) {
  538. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DELETE) {
  539. OnDeleteStage();
  540. return TRUE;
  541. }
  542. }
  543. return FALSE;
  544. }
  545. /**
  546. * Sets window styles before the window is created.
  547. */
  548. BOOL StageView::PreCreateWindow(CREATESTRUCT& cs) {
  549. cs.style &= ~LVS_TYPEMASK;
  550. cs.style |= LVS_SINGLESEL | LVS_EDITLABELS;
  551. return ToggleListView::PreCreateWindow(cs);
  552. }
  553. /**
  554. * Called by the ToggleListView when the toggle state has changed.
  555. */
  556. void StageView::OnStateChanged(int index, int toggleState) {
  557. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  558. if(material && index > 0) {
  559. if (toggleState == ToggleListView::TOGGLE_STATE_ON) {
  560. material->EnableStage(index-1, true);
  561. } else if (toggleState == ToggleListView::TOGGLE_STATE_OFF) {
  562. material->EnableStage(index-1, false);
  563. }
  564. }
  565. }
  566. /**
  567. * Dispalys the popup menu with the appropriate menu items enabled.
  568. */
  569. void StageView::PopupMenu(CPoint* pt) {
  570. //Determine the type of object clicked on
  571. CListCtrl& list = GetListCtrl();
  572. ClientToScreen (pt);
  573. CMenu FloatingMenu;
  574. VERIFY(FloatingMenu.LoadMenu(IDR_ME_STAGELIST_POPUP));
  575. CMenu* pPopupMenu = FloatingMenu.GetSubMenu (0);
  576. ASSERT(pPopupMenu != NULL);
  577. POSITION pos = list.GetFirstSelectedItemPosition();
  578. int nItem = -1;
  579. if(pos)
  580. nItem = list.GetNextSelectedItem(pos);
  581. if(nItem <= 0) {
  582. pPopupMenu->EnableMenuItem(ID_STAGEPOPUP_RENAMESTAGE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  583. pPopupMenu->EnableMenuItem(ID_STAGEPOPUP_DELETESTAGE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  584. pPopupMenu->EnableMenuItem(ID_STAGEPOPUP_CUT, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  585. pPopupMenu->EnableMenuItem(ID_STAGEPOPUP_COPY, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  586. } else {
  587. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  588. if(material->GetAttributeInt(nItem-1, "stagetype") != MaterialDoc::STAGE_TYPE_NORMAL) {
  589. pPopupMenu->EnableMenuItem(ID_STAGEPOPUP_RENAMESTAGE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  590. }
  591. }
  592. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  593. if(material->FindStage(MaterialDoc::STAGE_TYPE_SPECIALMAP, "bumpmap") >= 0) {
  594. pPopupMenu->EnableMenuItem(ID_STAGEPOPUP_ADDBUMPMAP, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  595. }
  596. if(material->FindStage(MaterialDoc::STAGE_TYPE_SPECIALMAP, "diffusemap") >= 0) {
  597. pPopupMenu->EnableMenuItem(ID_STAGEPOPUP_ADDDIFFUSEMAP, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  598. }
  599. if(material->FindStage(MaterialDoc::STAGE_TYPE_SPECIALMAP, "specularmap") >= 0) {
  600. pPopupMenu->EnableMenuItem(ID_STAGEPOPUP_ADDSPECULAR, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  601. }
  602. if(materialDocManager->IsCopyStage()) {
  603. pPopupMenu->EnableMenuItem(ID_STAGEPOPUP_PASTE, MF_BYCOMMAND | MF_ENABLED);
  604. } else {
  605. pPopupMenu->EnableMenuItem(ID_STAGEPOPUP_PASTE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  606. }
  607. pPopupMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt->x, pt->y, &list);
  608. }
  609. /**
  610. * Performs the stage move when the user has dragged and dropped a stage.
  611. */
  612. void StageView::DropItemOnList() {
  613. CListCtrl& list = GetListCtrl();
  614. int toStage;
  615. //Get and adjust the drop index based on the direction of the move
  616. dropIndex = list.HitTest(dropPoint);
  617. if(dropIndex < 0) dropIndex = list.GetItemCount()-1;
  618. //Ignore the drop if the index is the same or they are droping on the material
  619. if(dropIndex == dragIndex || dropIndex == 0)
  620. return;
  621. //Move the stage data
  622. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  623. internalChange = true;
  624. toStage = dropIndex-1;
  625. material->MoveStage(dragIndex-1, dropIndex-1);
  626. internalChange = false;
  627. if(dragIndex < dropIndex) {
  628. dropIndex++;
  629. }
  630. //Get the item
  631. char szLabel[256];
  632. LV_ITEM lvi;
  633. ZeroMemory(&lvi, sizeof(LV_ITEM));
  634. lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_PARAM;
  635. lvi.stateMask = LVIS_DROPHILITED | LVIS_FOCUSED | LVIS_SELECTED;
  636. lvi.pszText = szLabel;
  637. lvi.iItem = dragIndex;
  638. lvi.cchTextMax = 255;
  639. list.GetItem(&lvi);
  640. //Insert the item
  641. lvi.iItem = dropIndex;
  642. list.InsertItem(&lvi);
  643. //Adjust the drag index if the move was up in the list
  644. if(dragIndex > dropIndex) {
  645. dragIndex++;
  646. }
  647. //Delete the original item
  648. list.DeleteItem(dragIndex);
  649. int type = -1;
  650. int stageType = currentMaterial->GetAttributeInt(toStage, "stagetype");
  651. switch(stageType) {
  652. case MaterialDoc::STAGE_TYPE_NORMAL:
  653. type = MaterialDefManager::MATERIAL_DEF_STAGE;
  654. break;
  655. case MaterialDoc::STAGE_TYPE_SPECIALMAP:
  656. type = MaterialDefManager::MATERIAL_DEF_SPECIAL_STAGE;
  657. break;
  658. }
  659. m_propView->SetPropertyListType(type, toStage);
  660. }