91 | function ConfigurationUtil.applyDesign(object, xmlFile, configDesignId) |
92 | local designKey = string.format("vehicle.designConfigurations.designConfiguration(%d)", configDesignId-1) |
93 | if not hasXMLProperty(xmlFile, designKey) then |
94 | print("Warning: Invalid design configuration " .. configDesignId); |
95 | return |
96 | end |
97 | local i = 0 |
98 | while true do |
99 | local materialKey = string.format(designKey..".material(%d)", i) |
100 | if not hasXMLProperty(xmlFile, materialKey) then |
101 | break |
102 | end |
103 | local baseMaterialNode = I3DUtil.indexToObject(object.components, getXMLString(xmlFile, materialKey.."#node"), object.i3dMappings); |
104 | local refMaterialNode = I3DUtil.indexToObject(object.components, getXMLString(xmlFile, materialKey.."#refNode"), object.i3dMappings); |
105 | if baseMaterialNode ~= nil and refMaterialNode ~= nil then |
106 | local oldMaterial = getMaterial(baseMaterialNode, 0) |
107 | local newMaterial = getMaterial(refMaterialNode, 0) |
108 | for _, component in pairs(object.components) do |
109 | ConfigurationUtil.replaceMaterialRec(object, component.node, oldMaterial, newMaterial); |
110 | end; |
111 | end |
112 | |
113 | local materialName = getXMLString(xmlFile, materialKey .. "#name") |
114 | if materialName ~= nil then |
115 | local shaderParameterName = getXMLString(xmlFile, materialKey .. "#shaderParameter") |
116 | if shaderParameterName ~= nil then |
117 | local colorStr = getXMLString(xmlFile, materialKey.."#color") |
118 | if colorStr ~= nil then |
119 | local color = g_brandColorManager:getBrandColorByName(colorStr) |
120 | if color == nil then |
121 | color = ConfigurationUtil.getColorFromString(colorStr) |
122 | end |
123 | if color ~= nil then |
124 | local materialId = getXMLInt(xmlFile, materialKey.."#materialId") |
125 | if object.setBaseMaterialColor ~= nil then |
126 | object:setBaseMaterialColor(materialName, shaderParameterName, color, materialId) |
127 | end |
128 | end |
129 | end |
130 | end |
131 | end |
132 | |
133 | i = i + 1 |
134 | end |
135 | ObjectChangeUtil.updateObjectChanges(xmlFile, "vehicle.designConfigurations.designConfiguration", configDesignId, object.components, object); |
136 | end |
55 | function ConfigurationUtil.getColorByConfigId(object, configName, configId) |
56 | local configId = object.configurations[configName] |
57 | if configId ~= nil then |
58 | local item = g_storeManager:getItemByXMLFilename(object.configFileName) |
59 | local config = item.configurations[configName][configId] |
60 | if config ~= nil then |
61 | local r, g, b = unpack(config.color) |
62 | return {r, g, b, config.material} |
63 | end |
64 | end |
65 | |
66 | return nil |
67 | end |
280 | function ConfigurationUtil.getConfigColorPostLoad(xmlFile, baseKey, baseDir, customEnvironment, isMod, configurationItems, storeItem) |
281 | local defaultColorIndex = getXMLInt(xmlFile, baseKey.."#defaultColorIndex") |
282 | |
283 | if Utils.getNoNil(getXMLBool(xmlFile, baseKey.."#useDefaultColors"), false) then |
284 | local price = Utils.getNoNil(getXMLInt(xmlFile, baseKey.."#price"), 1000); |
285 | |
286 | for i, color in pairs(g_vehicleColors) do |
287 | local configItem = StoreItemUtil.addConfigurationItem(configurationItems, "", "", price, 0, false) |
288 | if color.r ~= nil and color.g ~= nil and color.b ~= nil then |
289 | configItem.color = {color.r, color.g, color.b, 1} |
290 | elseif color.brandColor ~= nil then |
291 | configItem.color = g_brandColorManager:getBrandColorByName(color.brandColor) |
292 | |
293 | if configItem.color == nil then |
294 | configItem.color = {1, 1, 1, 1} |
295 | g_logManager:warning("Unable to find brandColor '%s' in g_vehicleColors", color.brandColor) |
296 | end |
297 | end |
298 | |
299 | configItem.name = g_i18n:convertText(color.name) |
300 | |
301 | if i == defaultColorIndex then |
302 | configItem.isDefault = true |
303 | configItem.price = 0 |
304 | end |
305 | end |
306 | end; |
307 | |
308 | if defaultColorIndex == nil then |
309 | local defaultIsDefined = false |
310 | for _, item in ipairs(configurationItems) do |
311 | if item.isDefault ~= nil and item.isDefault then |
312 | defaultIsDefined = true |
313 | end |
314 | end |
315 | |
316 | if not defaultIsDefined then |
317 | if #configurationItems > 0 then |
318 | configurationItems[1].isDefault = true |
319 | configurationItems[1].price = 0 |
320 | end |
321 | end |
322 | end |
323 | end |
258 | function ConfigurationUtil.getConfigColorSingleItemLoad(xmlFile, baseXMLName, baseDir, customEnvironment, isMod, configItem) |
259 | |
260 | local colorStr = Utils.getNoNil(getXMLString(xmlFile, baseXMLName.."#color"), "1 1 1 1") |
261 | local color = g_brandColorManager:getBrandColorByName(colorStr) |
262 | if color == nil then |
263 | color = ConfigurationUtil.getColorFromString(colorStr) |
264 | end |
265 | |
266 | configItem.color = color |
267 | configItem.material = getXMLInt(xmlFile, baseXMLName.."#material") |
268 | |
269 | configItem.name = XMLUtil.getXMLI18NValue(xmlFile, baseXMLName.."#name", getXMLString, "", "", customEnvironment, false) |
270 | end |
206 | function ConfigurationUtil.getConfigurationValue(xmlFile, key, subKey, param, xmlFunc, defaultValue, fallbackConfigKey, fallbackOldKey) |
207 | if type(subKey) == "table" then |
208 | printCallstack(); |
209 | end |
210 | local value = nil; |
211 | if key ~= nil then |
212 | value = xmlFunc(xmlFile, key..subKey..param); |
213 | end; |
214 | |
215 | if value == nil and fallbackConfigKey ~= nil then |
216 | value = xmlFunc(xmlFile, fallbackConfigKey..subKey..param); -- Check for default configuration (xml index 0) |
217 | end; |
218 | if value == nil and fallbackOldKey ~= nil then |
219 | value = xmlFunc(xmlFile, fallbackOldKey..subKey..param); -- Fallback to old xml setup |
220 | end; |
221 | return Utils.getNoNil(value, defaultValue); -- using default value |
222 | end |
233 | function ConfigurationUtil.getXMLConfigurationKey(xmlFile, index, key, defaultKey, configurationKey) |
234 | local configIndex = Utils.getNoNil(index, 1); |
235 | local configKey = string.format(key.."(%d)", configIndex-1); |
236 | if index ~= nil and not hasXMLProperty(xmlFile, configKey) then |
237 | print("Warning: Invalid "..configurationKey.." index '"..tostring(index).."' in '"..key.."'. Using default "..configurationKey.." settings instead!"); |
238 | end; |
239 | |
240 | if not hasXMLProperty(xmlFile, configKey) then |
241 | configKey = key.."(0)"; |
242 | end; |
243 | if not hasXMLProperty(xmlFile, configKey) then |
244 | configKey = defaultKey; |
245 | end; |
246 | |
247 | return configKey, configIndex; |
248 | end |
143 | function ConfigurationUtil.replaceMaterialRec(object, node, oldMaterial, newMaterial) |
144 | if getHasClassId(node, ClassIds.SHAPE) then |
145 | local nodeMaterial = getMaterial(node, 0); |
146 | if nodeMaterial == oldMaterial then |
147 | setMaterial(node, newMaterial, 0) |
148 | end |
149 | end; |
150 | |
151 | local numChildren = getNumOfChildren(node); |
152 | if numChildren > 0 then |
153 | for i=0, numChildren-1 do |
154 | ConfigurationUtil.replaceMaterialRec(object, getChildAt(node, i), oldMaterial, newMaterial) |
155 | end; |
156 | end; |
157 | end |
164 | function ConfigurationUtil.setColor(object, xmlFile, configName, configColorId) |
165 | local color = ConfigurationUtil.getColorByConfigId(object, configName, configColorId) |
166 | if color ~= nil then |
167 | local r,g,b,mat = unpack(color); |
168 | local i = 0; |
169 | while true do |
170 | local colorKey = string.format("vehicle.%sConfigurations.colorNode(%d)", configName, i) |
171 | if not hasXMLProperty(xmlFile, colorKey) then |
172 | break; |
173 | end |
174 | |
175 | local node = I3DUtil.indexToObject(object.components, getXMLString(xmlFile, colorKey .. "#node"), object.i3dMappings); |
176 | if node ~= nil then |
177 | if getHasClassId(node, ClassIds.SHAPE) then |
178 | if mat == nil then |
179 | _,_,_,mat = getShaderParameter(node, "colorScale") |
180 | end |
181 | if Utils.getNoNil(getXMLBool(xmlFile, colorKey .. "#recursive"), false) then |
182 | I3DUtil.setShaderParameterRec(node, "colorScale", r, g, b, mat); |
183 | else |
184 | setShaderParameter(node, "colorScale", r, g, b, mat, false); |
185 | end |
186 | else |
187 | print("Warning: Could not set vehicle color to '"..getName(node).."' because node is not a shape!") |
188 | end |
189 | end |
190 | i = i + 1; |
191 | end |
192 | end |
193 | end |