MaterialEditView.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "MaterialEditView.h"
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #endif
  26. #define EDIT_HEIGHT 25
  27. #define EDIT_TAB_CONTROL 0x2006
  28. #define NAME_CONTROL 0x2007
  29. IMPLEMENT_DYNCREATE(MaterialEditView, CFormView)
  30. BEGIN_MESSAGE_MAP(MaterialEditView, CFormView)
  31. ON_WM_SIZE()
  32. ON_WM_CREATE()
  33. ON_NOTIFY(TCN_SELCHANGE, EDIT_TAB_CONTROL, OnTcnSelChange)
  34. ON_NOTIFY(EN_CHANGE, IDC_MATERIALEDITOR_EDIT_TEXT, OnEnChangeEdit)
  35. END_MESSAGE_MAP()
  36. /**
  37. * Constructor for MaterialEditView.
  38. */
  39. MaterialEditView::MaterialEditView()
  40. : CFormView(MaterialEditView::IDD) {
  41. initHack = false;
  42. sourceInit = false;
  43. sourceChanged = false;
  44. }
  45. /**
  46. * Destructor for MaterialEditView.
  47. */
  48. MaterialEditView::~MaterialEditView() {
  49. }
  50. /**
  51. * Called when the selected material has changed.
  52. * @param pMaterial The newly selected material.
  53. */
  54. void MaterialEditView::MV_OnMaterialSelectionChange(MaterialDoc* pMaterial) {
  55. //Apply any text changes that have been made
  56. ApplyMaterialSource();
  57. if(pMaterial) {
  58. m_nameEdit.SetWindowText(pMaterial->name);
  59. m_textView.SetReadOnly(false);
  60. //If the edit tab is selected then get the source
  61. int sel = m_tabs.GetCurSel();
  62. if (sel == 1) {
  63. GetMaterialSource();
  64. }
  65. currentMaterialName = pMaterial->name;
  66. } else {
  67. m_nameEdit.SetWindowText("");
  68. GetMaterialSource();
  69. m_textView.SetReadOnly(true);
  70. currentMaterialName = "";
  71. }
  72. }
  73. void MaterialEditView::MV_OnMaterialNameChanged(MaterialDoc* pMaterial, const char* oldName) {
  74. if(!currentMaterialName.Icmp(oldName)) {
  75. currentMaterialName = pMaterial->name;
  76. }
  77. }
  78. /**
  79. * Returns the current source text in the source edit control.
  80. */
  81. idStr MaterialEditView::GetSourceText() {
  82. idStr text;
  83. m_textView.GetText(text);
  84. text.Replace( "\n", "" );
  85. text.Replace( "\r", "\r\n" );
  86. text.Replace( "\v", "\r\n" );
  87. text.StripLeading( "\r\n" );
  88. text.Insert( "\r\n\r\n", 0 );
  89. text.StripTrailing( "\r\n" );
  90. return text;
  91. }
  92. /**
  93. * Gets the source of the current document and populates the
  94. * source edit control.
  95. */
  96. void MaterialEditView::GetMaterialSource() {
  97. //Clear it
  98. sourceInit = true;
  99. m_textView.SetText("");
  100. sourceInit = false;
  101. if(materialDocManager) {
  102. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  103. if(material) {
  104. idStr text = material->GetEditSourceText();
  105. // clean up new-line crapola
  106. text.Replace( "\r", "" );
  107. text.Replace( "\n", "\r" );
  108. text.Replace( "\v", "\r" );
  109. text.StripLeading( '\r' );
  110. text.Append( "\r" );
  111. sourceInit = true;
  112. m_textView.SetText(text);
  113. sourceInit = false;
  114. }
  115. }
  116. }
  117. /**
  118. * Takes the source out of the edit control and applies it
  119. * to the material.
  120. */
  121. void MaterialEditView::ApplyMaterialSource() {
  122. if(!sourceChanged)
  123. return;
  124. MaterialDoc* material = materialDocManager->CreateMaterialDoc(currentMaterialName);
  125. if(material) {
  126. idStr text = GetSourceText();
  127. material->ApplySourceModify(text);
  128. }
  129. sourceChanged = false;
  130. }
  131. /**
  132. * Transfers data to and from the controls in the console.
  133. */
  134. void MaterialEditView::DoDataExchange(CDataExchange* pDX) {
  135. CFormView::DoDataExchange(pDX);
  136. DDX_Control(pDX, IDC_MATERIALEDITOR_EDIT_TEXT, m_textView);
  137. }
  138. /**
  139. * Called by the MFC framework when the view is being created.
  140. */
  141. void MaterialEditView::OnInitialUpdate() {
  142. CFormView::OnInitialUpdate();
  143. if(!initHack) {
  144. initHack = true;
  145. m_textView.Init();
  146. m_textView.LoadKeyWordsFromFile( "editors/material.def" );
  147. m_textView.ShowWindow(SW_HIDE);
  148. m_textView.SetText("");
  149. m_textView.SetReadOnly(true);
  150. }
  151. }
  152. /**
  153. * Called by the MFC framework when the view is being created.
  154. */
  155. int MaterialEditView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  156. {
  157. if (CFormView::OnCreate(lpCreateStruct) == -1)
  158. return -1;
  159. m_nameEdit.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_READONLY, CRect(0,0,0,0), this, NAME_CONTROL);
  160. m_editSplitter.CreateStatic(this, 1, 2);
  161. if(!m_editSplitter.CreateView(0, 0, RUNTIME_CLASS(StageView), CSize(200, 200), NULL)) {
  162. TRACE0("Failed to create stage property pane\n");
  163. return -1;
  164. }
  165. if(!m_editSplitter.CreateView(0, 1, RUNTIME_CLASS(MaterialPropTreeView), CSize(500, 200), NULL)) {
  166. TRACE0("Failed to create property pane\n");
  167. return -1;
  168. }
  169. m_nameEdit.SetFont(materialEditorFont);
  170. m_stageView = (StageView*)m_editSplitter.GetPane(0, 0);
  171. m_materialPropertyView = (MaterialPropTreeView*)m_editSplitter.GetPane(0, 1);
  172. m_tabs.Create(TCS_BOTTOM | TCS_FLATBUTTONS | WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), this, EDIT_TAB_CONTROL);
  173. m_tabs.InsertItem(0, "Properties");
  174. m_tabs.InsertItem(1, "Text");
  175. m_tabs.SetFont(materialEditorFont);
  176. return 0;
  177. }
  178. /**
  179. * Windows message called when the window is resized.
  180. */
  181. void MaterialEditView::OnSize(UINT nType, int cx, int cy) {
  182. CFormView::OnSize(nType, cx, cy);
  183. CRect tabRect;
  184. m_tabs.GetItemRect(0, tabRect);
  185. int tabHeight = tabRect.Height()+5;
  186. //Hardcode the edit window height
  187. if(m_nameEdit.GetSafeHwnd()) {
  188. m_nameEdit.MoveWindow(1,1, cx-2, 20);
  189. }
  190. if(m_tabs.GetSafeHwnd()) {
  191. m_tabs.MoveWindow(0, cy-tabHeight, cx, tabHeight);
  192. }
  193. if(m_editSplitter.GetSafeHwnd()) {
  194. m_editSplitter.MoveWindow(1, 22, cx-2, cy-tabHeight-22);
  195. }
  196. if(m_textView.GetSafeHwnd()) {
  197. m_textView.MoveWindow(1, 22, cx-2, cy-tabHeight-22);
  198. }
  199. }
  200. /**
  201. * Called when the user changes the properties/text tab selection. This methods shows and hides
  202. * the appropriate windows.
  203. */
  204. void MaterialEditView::OnTcnSelChange(NMHDR *pNMHDR, LRESULT *pResult) {
  205. int sel = m_tabs.GetCurSel();
  206. switch(sel) {
  207. case 0:
  208. m_editSplitter.ShowWindow(SW_SHOW);
  209. m_textView.ShowWindow(SW_HIDE);
  210. ApplyMaterialSource();
  211. m_stageView->RefreshStageList();
  212. break;
  213. case 1:
  214. m_editSplitter.ShowWindow(SW_HIDE);
  215. m_textView.ShowWindow(SW_SHOW);
  216. GetMaterialSource();
  217. m_textView.SetReadOnly(false);
  218. }
  219. }
  220. /**
  221. * Called when the user changes text in the edit control
  222. */
  223. void MaterialEditView::OnEnChangeEdit( NMHDR *pNMHDR, LRESULT *pResult ) {
  224. if(materialDocManager && !sourceInit) {
  225. MaterialDoc* material = materialDocManager->GetCurrentMaterialDoc();
  226. if(material && !material->IsSourceModified()) {
  227. sourceChanged = true;
  228. material->SourceModify(this);
  229. }
  230. }
  231. }