]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/tools/materialeditor/MaterialPreviewPropView.cpp
hello world
[icculus/iodoom3.git] / neo / tools / materialeditor / MaterialPreviewPropView.cpp
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 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.
23
24 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.
25
26 ===========================================================================
27 */
28 #include "../../idlib/precompiled.h"
29 #pragma hdrstop
30
31
32 #include "MaterialPreviewPropView.h"
33
34
35 // MaterialPropTreeView
36
37 IMPLEMENT_DYNCREATE(MaterialPreviewPropView, CPropTreeView)
38
39
40 MaterialPreviewPropView::MaterialPreviewPropView() {
41         numLights = 0;
42         materialPreview = NULL;
43 }
44
45 MaterialPreviewPropView::~MaterialPreviewPropView() {
46 }
47
48 BEGIN_MESSAGE_MAP(MaterialPreviewPropView, CPropTreeView)
49         ON_NOTIFY( PTN_ITEMCHANGED, IDC_PROPERTYTREE, OnPropertyChangeNotification )
50         ON_NOTIFY( PTN_ITEMBUTTONCLICK, IDC_PROPERTYTREE, OnPropertyButtonClick )
51 END_MESSAGE_MAP()
52
53
54 void MaterialPreviewPropView::AddLight( void ) {
55         int i, count, lightShaderIndex = 0;
56         const idMaterial *mat;
57
58         CPropTreeItemButton*    pRoot;
59         CPropTreeItemCombo*             pCombo;
60         CPropTreeItemColor*             pColor;
61         CPropTreeItemCheck*             pCheck;
62         CPropTreeItemEdit*              pEdit;
63
64         //Increase the number of lights
65         numLights++;
66
67         pRoot = (CPropTreeItemButton*)m_Tree.InsertItem(new CPropTreeItemButton());
68         pRoot->SetLabelText(_T(va("Light #%d", numLights)));
69         pRoot->SetInfoText(_T(va("Parameters for light number %d.", numLights)));
70         pRoot->SetButtonText( "Remove" );
71         pRoot->SetCtrlID( numLights - 1 );
72         pRoot->Expand();
73
74         pCombo = (CPropTreeItemCombo*)m_Tree.InsertItem(new CPropTreeItemCombo(), pRoot);
75         pCombo->SetLabelText( _T("Shader") );
76         pCombo->SetInfoText( _T("Set the light shader.") );
77         pCombo->SetDropDownHeight( 200 );
78         pCombo->CreateComboBox();
79         // Add all light shaders to the combo box
80         count = declManager->GetNumDecls( DECL_MATERIAL );
81         for (i = 0; i < count; i++) {
82                 mat = declManager->MaterialByIndex(i, false);
83
84                 idStr materialName = mat->GetName();
85                 materialName.ToLower();
86
87                 if ( materialName.Left(7) == "lights/" || materialName.Left(5) == "fogs/" ) {
88                         pCombo->InsertString( lightShaderIndex, materialName );
89                         pCombo->SetItemData( lightShaderIndex, lightShaderIndex );
90
91                         if ( materialName == "lights/defaultpointlight" ) {
92                                 pCombo->SetCurSel( lightShaderIndex );
93                         }
94
95                         lightShaderIndex++;
96                 }
97         }
98
99         pColor = (CPropTreeItemColor*)m_Tree.InsertItem(new CPropTreeItemColor(), pRoot);
100         pColor->SetLabelText(_T("Color"));
101         pColor->SetInfoText(_T("Color of the light."));
102         pColor->SetItemValue((LPARAM)RGB(0xff, 0xff, 0xff)); // default as color white
103
104         pEdit = (CPropTreeItemEdit*)m_Tree.InsertItem( new CPropTreeItemEdit(), pRoot);
105         pEdit->SetLabelText(_T("Radius"));
106         pEdit->SetInfoText(_T("Radius of the light."));
107         pEdit->SetItemValue( (LPARAM)_T("300.0") );
108
109         pCheck = (CPropTreeItemCheck*)m_Tree.InsertItem(new CPropTreeItemCheck(), pRoot);
110         pCheck->SetLabelText(_T("Move light"));
111         pCheck->SetInfoText(_T("When checked, allow light to move."));
112         pCheck->CreateCheckBox();
113         pCheck->SetCheckState( BST_CHECKED );
114
115         if ( materialPreview ) {
116                 materialPreview->OnAddLight();
117         }
118 }
119
120 //Create sample data for the preview properties
121 void MaterialPreviewPropView::InitializePropTree( void ) {
122         int i;
123         CPropTreeItem           *pRoot;
124         CPropTreeItem           *pParmRoot;
125         CPropTreeItemCheck      *pCheck;
126         CPropTreeItemEdit       *pEdit;
127
128         pRoot = m_Tree.InsertItem(new CPropTreeItem());
129         pRoot->SetLabelText(_T("Preview Properties"));
130         pRoot->SetInfoText(_T("Properties for the preview window."));
131         pRoot->Expand(); // have this item expanded by default
132
133         CPropTreeItemCombo* pCombo;
134         pCombo = (CPropTreeItemCombo*)m_Tree.InsertItem(new CPropTreeItemCombo(), pRoot);
135         pCombo->SetLabelText(_T("Model Type"));
136         pCombo->SetInfoText(_T("Select the type of model on which to preview the material."));
137         pCombo->CreateComboBox();
138         pCombo->InsertString( 0, "Cube" );
139         pCombo->InsertString( 1, "Box - 2:1");
140         pCombo->InsertString( 2, "Box - 4:1");
141         pCombo->InsertString( 3, "Box - 1:2");
142         pCombo->InsertString( 4, "Box - 1:4");
143         pCombo->InsertString( 5, "Cylinder - V");
144         pCombo->InsertString( 6, "Cylinder - H");
145         pCombo->InsertString( 7, "Sphere");
146         pCombo->SetItemData( 0, 0 );
147         pCombo->SetItemData( 1, 1 );
148         pCombo->SetItemData( 2, 2 );
149         pCombo->SetItemData( 3, 3 );
150         pCombo->SetItemData( 4, 4 );
151         pCombo->SetItemData( 5, 5 );
152         pCombo->SetItemData( 6, 6 );
153         pCombo->SetItemData( 7, 7 );
154         
155         pCombo->SetCurSel( 0 );
156
157         // Custom model entry
158         /*pEdit = (CPropTreeItemEdit*)m_Tree.InsertItem( new CPropTreeItemEdit(), pRoot );
159         pEdit->SetLabelText(_T("Custom Model"));
160         pEdit->SetInfoText(_T("Specify any model to display the current material."));
161         pEdit->SetItemValue((LPARAM)_T(""));*/
162         CPropTreeItemEditButton *pCutomButton;
163         pCutomButton = (CPropTreeItemEditButton*)m_Tree.InsertItem(new CPropTreeItemEditButton(), pRoot );
164         pCutomButton->SetButtonText(_T("..."));
165         pCutomButton->SetLabelText(_T("Custom Model"));
166         pCutomButton->SetInfoText(_T("Specify any model to display the current material."));
167         pCutomButton->SetItemValue((LPARAM)_T(""));
168
169         // Checkbox for showing debug light spheres
170         pCheck = (CPropTreeItemCheck*)m_Tree.InsertItem( new CPropTreeItemCheck(), pRoot );
171         pCheck->SetLabelText(_T("Show Lights"));
172         pCheck->SetInfoText(_T("Show the light origin sphere and number in the preview."));
173         pCheck->CreateCheckBox();
174         pCheck->SetCheckState( BST_CHECKED );
175
176         // Local and Global shader parms
177         pParmRoot = m_Tree.InsertItem(new CPropTreeItem());
178         pParmRoot->SetLabelText(_T("Local Parms"));
179         pParmRoot->SetInfoText(_T("Local shaderparms for the model being displayed."));
180         pParmRoot->Expand( FALSE ); // have this item NOT expanded by default
181
182         for( i = 0; i < MAX_ENTITY_SHADER_PARMS; i++ ) {
183                 pEdit = (CPropTreeItemEdit*)m_Tree.InsertItem( new CPropTreeItemEdit(), pParmRoot );
184                 pEdit->SetLabelText(_T(va("parm%d", i)));
185                 pEdit->SetInfoText(_T("Set the local shaderparm for the model"));
186                 if ( i < 4 ) {
187                         pEdit->SetItemValue((LPARAM)_T("1"));
188                 } else {
189                         pEdit->SetItemValue((LPARAM)_T("0"));
190                 }
191         }
192
193         pParmRoot = m_Tree.InsertItem(new CPropTreeItem());
194         pParmRoot->SetLabelText(_T("Global Parms"));
195         pParmRoot->SetInfoText(_T("Global shaderparms for the renderworld being displayed."));
196         pParmRoot->Expand( FALSE ); // have this item NOT expanded by default
197
198         for( i = 0; i < MAX_GLOBAL_SHADER_PARMS; i++ ) {
199                 pEdit = (CPropTreeItemEdit*)m_Tree.InsertItem( new CPropTreeItemEdit(), pParmRoot );
200                 pEdit->SetLabelText(_T(va("global%d", i)));
201                 pEdit->SetInfoText(_T("Set the global shaderparm for the renderworld"));
202                 if ( i < 4 ) {
203                         pEdit->SetItemValue((LPARAM)_T("1"));
204                 } else {
205                         pEdit->SetItemValue((LPARAM)_T("0"));
206                 }
207         }
208
209         // Lights
210
211         pRoot = m_Tree.InsertItem(new CPropTreeItem());
212         pRoot->SetLabelText(_T(""));
213         pRoot->SetInfoText(_T(""));
214
215         CPropTreeItemButton *pButton;
216         pButton = (CPropTreeItemButton*)m_Tree.InsertItem(new CPropTreeItemButton());
217         pButton->SetButtonText(_T(" Add Light "));
218         pButton->SetLabelText(_T("Preview Lights"));
219         pButton->SetInfoText(_T("Test the button."));
220
221         pRoot = m_Tree.InsertItem(new CPropTreeItem());
222         pRoot->SetLabelText(_T(""));
223         pRoot->SetInfoText(_T(""));
224
225         AddLight();
226 }
227
228 // MaterialPreviewPropView drawing
229
230 void MaterialPreviewPropView::OnDraw(CDC* pDC)
231 {
232         // TODO: add draw code here
233 }
234
235 // MaterialPreviewPropView diagnostics
236
237 #ifdef _DEBUG
238 void MaterialPreviewPropView::AssertValid() const
239 {
240         CPropTreeView::AssertValid();
241 }
242
243 void MaterialPreviewPropView::Dump(CDumpContext& dc) const
244 {
245         CPropTreeView::Dump(dc);
246 }
247 #endif //_DEBUG
248
249
250 void MaterialPreviewPropView::RegisterPreviewView( MaterialPreviewView *view ) {
251         materialPreview = view;
252 }
253
254 // MaterialPreviewPropView message handlers
255
256 void MaterialPreviewPropView::OnPropertyChangeNotification( NMHDR *nmhdr, LRESULT *lresult ) {
257         idVec3                  testColor;
258         int                             lightId = 0;
259         COLORREF                color;
260         NMPROPTREE              *nmProp;
261         CPropTreeItem   *item;
262         CPropTreeItem   *parent;
263
264         nmProp = (NMPROPTREE *)nmhdr;
265         item = nmProp->pItem;
266
267         // Determine which light this item modifies
268         parent = item->GetParent();
269         if ( parent ) {
270                 lightId = parent->GetCtrlID();
271         }
272
273         idStr   itemLabel = item->GetLabelText();
274         
275         if ( itemLabel == "Model Type" ) {
276                 materialPreview->OnModelChange( item->GetItemValue() );
277
278         } else if ( itemLabel == "Custom Model" ) {
279                 materialPreview->OnCustomModelChange( (const char *)item->GetItemValue() );
280
281         } else if ( itemLabel == "Show Lights" ) {
282                 materialPreview->OnShowLightsChange( item->GetItemValue() ? true : false );
283
284         } else if ( itemLabel == "Shader" ) {
285                 CPropTreeItemCombo      *combo = (CPropTreeItemCombo *)item;
286                 CString materialName;
287
288                 combo->GetLBText( combo->GetCurSel(), materialName );
289
290                 materialPreview->OnLightShaderChange( lightId, materialName.GetBuffer() );
291
292         } else if ( itemLabel == "Radius" ) {
293                 materialPreview->OnLightRadiusChange( lightId, atof( (char *)item->GetItemValue() ) );
294
295         } else if ( itemLabel == "Color" ) {
296                 color = item->GetItemValue();
297
298                 testColor.x = (float)GetRValue( color ) * (float)( 1.f/255.f );
299                 testColor.y = (float)GetGValue( color ) * (float)( 1.f/255.f );
300                 testColor.z = (float)GetBValue( color ) * (float)( 1.f/255.f );
301
302                 materialPreview->OnLightColorChange( lightId, testColor );
303
304         } else if ( itemLabel == "Move light" ) {
305                 materialPreview->OnLightAllowMoveChange( lightId, item->GetItemValue() ? true : false );
306
307         } else if ( itemLabel.Left(4) == "parm" ) {
308                 int index;
309
310                 itemLabel.Strip( "parm" );
311                 index = atoi( itemLabel.c_str() );
312
313                 materialPreview->OnLocalParmChange( index, atof( (char *)item->GetItemValue() ) );
314
315         } else if ( itemLabel.Left(6) == "global" ) {
316                 int index;
317
318                 itemLabel.Strip( "global" );
319                 index = atoi( itemLabel.c_str() );
320
321                 materialPreview->OnGlobalParmChange( index, atof( (char *)item->GetItemValue() ) );
322         }
323 }
324
325 void MaterialPreviewPropView::OnPropertyButtonClick( NMHDR *nmhdr, LRESULT *lresult ) {
326         NMPROPTREE              *nmProp;
327         CPropTreeItem   *item;
328
329         nmProp = (NMPROPTREE *)nmhdr;
330         item = nmProp->pItem;
331
332         idStr   itemLabel = item->GetLabelText();
333
334         if ( itemLabel == "Preview Lights" ) {
335                 AddLight();
336
337         } else if ( itemLabel.Left(5) == "Light" ) {
338                 CPropTreeItem   *light;
339                 int lightId = item->GetCtrlID();
340                 int testLightNum = 0;
341
342                 m_Tree.DeleteItem( item );
343
344                 for( light = m_Tree.GetRootItem()->GetChild(); light != NULL; light = light->GetSibling() ) {
345                         idStr label = light->GetLabelText();
346
347                         if ( label.Left(5) == "Light" ) {
348                                 testLightNum++;
349                                 light->SetLabelText(_T(va("Light #%d", testLightNum)));
350                                 light->SetInfoText(_T(va("Parameters for light number %d.", testLightNum)));
351                                 light->SetCtrlID( testLightNum - 1 );
352                         }
353                 }
354
355                 materialPreview->OnDeleteLight( lightId );
356
357                 numLights--;
358         } else if ( itemLabel == "Custom Model" ) {
359                 CFileDialog dlg(TRUE);
360                 dlg.m_ofn.Flags |= OFN_FILEMUSTEXIST;
361                 item->Check(FALSE);
362                 if( dlg.DoModal()== IDOK) {
363                         item->Check(FALSE);
364                         item->SetItemValue((LPARAM)fileSystem->OSPathToRelativePath(dlg.m_ofn.lpstrFile));
365                         m_Tree.SendNotify(PTN_ITEMCHANGED, item);
366                 }
367         }
368 }