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 "tools/edit_gui_common.h"
29
30
31 #include "MaterialDef.h"
32
33
34 /**
35 * Constructor.
36 */
MaterialDef(void)37 MaterialDef::MaterialDef(void) {
38 type = 0;
39 quotes = false;
40 }
41
42 /**
43 * Destructor.
44 */
~MaterialDef(void)45 MaterialDef::~MaterialDef(void) {
46 }
47
48 /**
49 * Returns view specific data associated with the material definition.
50 */
GetViewData(const char * viewName)51 DWORD MaterialDef::GetViewData(const char* viewName) {
52 DWORD* value = NULL;
53 viewData.Get(viewName, &value);
54 return *value;
55 }
56
57 /**
58 * Sets view specific data for the material definition.
59 */
SetViewData(const char * viewName,DWORD value)60 void MaterialDef::SetViewData(const char* viewName, DWORD value) {
61 viewData.Set(viewName, value);
62 }
63
64 #define MATERIAL_DEF_FILE "MaterialEditorDefs.med"
65
66 MaterialDefList MaterialDefManager::materialDefs[MaterialDefManager::MATERIAL_DEF_NUM];
67
68
69 /**
70 * Loads the material definition file instatiates MaterialDef objects for each definition
71 * and groups the definitions.
72 */
InitializeMaterialDefLists()73 void MaterialDefManager::InitializeMaterialDefLists() {
74
75 char *buffer;
76 int length = fileSystem->ReadFile( MATERIAL_DEF_FILE, (void **)&buffer);
77
78 if ( length == -1 ) {
79 common->Error( "Couldn't load material editor definition: %s", MATERIAL_DEF_FILE );
80 return;
81 }
82
83 idLexer src;
84 if ( !src.LoadMemory( buffer, length, MATERIAL_DEF_FILE ) ) {
85 common->Error( "Couldn't parse %s", MATERIAL_DEF_FILE );
86 fileSystem->FreeFile(buffer);
87 }
88
89
90 InitializeMaterialDefList(&src, "materialprops", &materialDefs[MATERIAL_DEF_MATERIAL]);
91 InitializeMaterialDefList(&src, "stageprops", &materialDefs[MATERIAL_DEF_STAGE]);
92 InitializeMaterialDefList(&src, "specialmapstageprops", &materialDefs[MATERIAL_DEF_SPECIAL_STAGE]);
93
94 fileSystem->FreeFile(buffer);
95 }
96
97 /**
98 * Loads a single type of material attributes and adds them to the supplied MaterialDefList object.
99 * @param src The idLexer object that contains the file.
100 * @param typeName The name of the attribute grouping to search for in the file.
101 * @param list The MaterialDefList object to append the MaterialDef instances to.
102 */
InitializeMaterialDefList(idLexer * src,const char * typeName,MaterialDefList * list)103 void MaterialDefManager::InitializeMaterialDefList(idLexer* src, const char* typeName, MaterialDefList* list) {
104
105 idToken token;
106
107 src->Reset();
108 src->SkipUntilString(typeName);
109 src->SkipUntilString("{");
110
111 while(1) {
112 if ( !src->ExpectAnyToken( &token ) ) {
113 //Todo: Add some error checking here
114 return;
115 }
116
117 if ( token == "}" ) {
118 break;
119 }
120
121 MaterialDef* newProp = new MaterialDef();
122
123 if(!token.Icmp("TYPE_GROUP")) {
124 newProp->type = MaterialDef::MATERIAL_DEF_TYPE_GROUP;
125 } else if(!token.Icmp("TYPE_BOOL")) {
126 newProp->type = MaterialDef::MATERIAL_DEF_TYPE_BOOL;
127 } else if(!token.Icmp("TYPE_STRING")) {
128 newProp->type = MaterialDef::MATERIAL_DEF_TYPE_STRING;
129 } else if(!token.Icmp("TYPE_FLOAT")) {
130 newProp->type = MaterialDef::MATERIAL_DEF_TYPE_FLOAT;
131 } else if(!token.Icmp("TYPE_INT")) {
132 newProp->type = MaterialDef::MATERIAL_DEF_TYPE_INT;
133 }
134
135 //Skip the ,
136 src->ReadToken(&token);
137
138 //Read Dict Name
139 src->ReadToken(&token);
140 newProp->dictName = token;
141
142 //Skip the ,
143 src->ReadToken(&token);
144
145 //Read Display Name
146 src->ReadToken(&token);
147 newProp->displayName = token;
148
149 //Skip the ,
150 src->ReadToken(&token);
151
152 //Read Display Info
153 src->ReadToken(&token);
154 newProp->displayInfo = token;
155
156 //Type Specific Data
157 if(newProp->type == MaterialDef::MATERIAL_DEF_TYPE_STRING) {
158
159 newProp->quotes = false;
160
161 //Skip the ,
162 src->ReadToken(&token);
163
164 //Read validate flag
165 src->ReadToken(&token);
166 if(token == "1") {
167 newProp->quotes = true;
168 }
169 }
170
171 src->SkipRestOfLine();
172
173 list->Append(newProp);
174 }
175 }
176
177 /**
178 * Destroys all MaterialDef instances and clears the material attribute grouping lists.
179 */
DestroyMaterialDefLists()180 void MaterialDefManager::DestroyMaterialDefLists() {
181
182 for(int i = 0; i < MATERIAL_DEF_NUM; i++) {
183 for(int j = 0; j < materialDefs[i].Num(); j++) {
184 delete materialDefs[i][j];
185 }
186 materialDefs[i].Clear();
187 }
188 }
189
190 /**
191 * Returns the MaterialDefList for the specified attribute grouping.
192 * @param type The attribute grouping for which to retreive the attribute list.
193 */
GetMaterialDefs(int type)194 MaterialDefList* MaterialDefManager::GetMaterialDefs(int type) {
195 if(type >= 0 && type < MATERIAL_DEF_NUM) {
196 return &materialDefs[type];
197 }
198 return NULL;
199 }