13 | function VehicleHudUtils.loadHud(vehicle, xmlFile, name, baseName, index) |
14 | baseName = Utils.getNoNil(baseName, "vehicle.indoorHud") |
15 | |
16 | local i = Utils.getNoNil(index, 0) |
17 | local huds = {} |
18 | while true do |
19 | local key = string.format(baseName.."."..name.."(%d)", i) |
20 | if not hasXMLProperty(xmlFile, key) then |
21 | break |
22 | end |
23 | |
24 | local entry = {} |
25 | entry.numbers = I3DUtil.indexToObject(vehicle.components, getXMLString(xmlFile, key.."#numbers"), vehicle.i3dMappings) |
26 | |
27 | colorStr = getXMLString(xmlFile, key.."#numberColor") |
28 | entry.numberColor = g_brandColorManager:getBrandColorByName(colorStr) |
29 | if entry.numberColor == nil then |
30 | entry.numberColor = StringUtil.getVectorNFromString(colorStr, 4) |
31 | end |
32 | if entry.numbers ~= nil and entry.numberColor ~= nil then |
33 | for node, _ in pairs(I3DUtil.getNodesByShaderParam(entry.numbers, "numberColor", nodes)) do |
34 | setShaderParameter(node, "numberColor", entry.numberColor[1], entry.numberColor[2], entry.numberColor[3], 1, false) |
35 | end |
36 | end |
37 | |
38 | entry.animName = getXMLString(xmlFile, key.."#animName") |
39 | entry.visibleFunc = getXMLString(xmlFile, key.."#visibleFunc") |
40 | entry.inverseVisibleFunc = Utils.getNoNil(getXMLBool(xmlFile, key.."#inverseVisibleFunc"), false) |
41 | |
42 | if entry.numbers ~= nil or entry.animName ~= nil then |
43 | entry.minValueAnim = getXMLFloat(xmlFile, key.."#minValueAnim") |
44 | entry.maxValueAnim = getXMLFloat(xmlFile, key.."#maxValueAnim") |
45 | if entry.numbers ~= nil then |
46 | entry.precision = Utils.getNoNil(getXMLInt(xmlFile, key.."#precision"), 1) |
47 | entry.numChilds = getNumOfChildren(entry.numbers) |
48 | if entry.numChilds-entry.precision <= 0 then |
49 | g_logManager:xmlWarning(self.configFileName, "Not enough number meshes for vehicle hud '%s'", key) |
50 | end |
51 | entry.numChilds = entry.numChilds - entry.precision |
52 | entry.maxValue = (10 ^ (entry.numChilds)) - 1/(10^entry.precision) -- e.g. max with 2 childs and 1 float -> 10^2 - 1/10 -> 99.9 -> makes sure that display doesn't show 00.0 if value is 100 |
53 | end |
54 | |
55 | table.insert(huds, entry) |
56 | end |
57 | |
58 | if index ~= nil then |
59 | break |
60 | end |
61 | |
62 | i = i + 1 |
63 | end |
64 | |
65 | if table.getn(huds) > 0 then |
66 | VehicleHudUtils.setHudValue(vehicle, huds, 0, 1) |
67 | return huds |
68 | end |
69 | |
70 | return nil |
71 | end |
75 | function VehicleHudUtils.setHudValue(vehicle, huds, value, maxValue) |
76 | for _, hud in ipairs(huds) do |
77 | local showZero = true |
78 | local newValue = value |
79 | if vehicle[hud.visibleFunc] ~= nil then |
80 | local ret = vehicle[hud.visibleFunc](vehicle) |
81 | if ret == hud.inverseVisibleFunc then |
82 | newValue = 0 |
83 | showZero = false |
84 | end |
85 | end |
86 | |
87 | if hud.numbers ~= nil then |
88 | local displayedValue = math.min(hud.maxValue, math.max(0, newValue)) |
89 | local num = tonumber(string.format("%."..hud.precision.."f", displayedValue)) |
90 | I3DUtil.setNumberShaderByValue(hud.numbers, num, hud.precision, showZero) |
91 | end |
92 | if hud.animName ~= nil and vehicle.setAnimationTime ~= nil then |
93 | if vehicle:getAnimationExists(hud.animName) then |
94 | local normValue = 0 |
95 | if maxValue ~= 0 then |
96 | if hud.minValueAnim ~= nil and hud.maxValueAnim ~= nil then |
97 | newValue = MathUtil.clamp(newValue, hud.minValueAnim, hud.maxValueAnim) |
98 | normValue = MathUtil.round((newValue - hud.minValueAnim) / (hud.maxValueAnim-hud.minValueAnim) , 3) |
99 | else |
100 | normValue = MathUtil.round(newValue/maxValue, 3) |
101 | end |
102 | end |
103 | vehicle:setAnimationTime(hud.animName, normValue, true) |
104 | else |
105 | g_logManager:xmlWarning(vehicle.configFileName, "Unknown animation name '%s' for indoor hud!", hud.animName) |
106 | end |
107 | end |
108 | end |
109 | end |