159 | function KeyValueInfoHUDBox:addLine(key, value, accentuate, accentuateColor) |
160 | local line |
161 | local cached = self.cachedLines |
162 | local numCached = #cached |
163 | if numCached > 0 then |
164 | line = self.cachedLines[numCached] |
165 | self.cachedLines[numCached] = nil |
166 | else |
167 | line = {} |
168 | end |
169 | |
170 | line.key = key |
171 | line.value = value or "" |
172 | line.accentuate = accentuate |
173 | line.accentuateColor = accentuateColor |
174 | |
175 | self.activeLines[#self.activeLines + 1] = line |
176 | end |
47 | function KeyValueInfoHUDBox:draw(posX, posY) |
48 | local rightX = posX |
49 | local leftX = posX - self.boxWidth |
50 | local y = posY |
51 | |
52 | local height = 2 * self.listMarginHeight + #self.activeLines * self.rowHeight |
53 | drawFilledRect(leftX, y, self.boxWidth, height, 0, 0, 0, 0.75) |
54 | |
55 | -- Draw title |
56 | setTextAlignment(RenderText.ALIGN_LEFT) |
57 | setTextColor(unpack(KeyValueInfoHUDBox.COLOR.TEXT_DEFAULT)) |
58 | setTextBold(true) |
59 | renderText(leftX + self.labelTextOffsetX, y + height + self.labelTextOffsetY, self.titleTextSize, self.title) |
60 | setTextBold(false) |
61 | |
62 | -- Displayitems |
63 | y = y + self.listMarginHeight |
64 | leftX = leftX + self.leftTextOffsetX + self.listMarginWidth |
65 | rightX = rightX - self.rightTextOffsetX - self.listMarginWidth |
66 | |
67 | local textAreaX = self.boxWidth - self.leftTextOffsetX - self.listMarginWidth - self.rightTextOffsetX - self.listMarginWidth |
68 | |
69 | for i = #self.activeLines, 1, -1 do |
70 | local line = self.activeLines[i] |
71 | |
72 | setTextBold(true) |
73 | if line.accentuate then |
74 | setTextColor(unpack(line.accentuateColor or KeyValueInfoHUDBox.COLOR.TEXT_HIGHLIGHT)) |
75 | end |
76 | |
77 | setTextAlignment(RenderText.ALIGN_LEFT) |
78 | renderText(leftX, y + self.leftTextOffsetY, self.rowTextSize, line.key) |
79 | |
80 | setTextAlignment(RenderText.ALIGN_RIGHT) |
81 | local maxWidth = textAreaX - 0.025 * self.boxWidth - getTextWidth(self.rowTextSize, line.key) |
82 | setTextBold(false) |
83 | |
84 | local text = Utils.limitTextToWidth(line.value, self.rowTextSize, maxWidth, false, "...") |
85 | renderText(rightX, y + self.rightTextOffsetY, self.rowTextSize, text) |
86 | |
87 | if line.accentuate then |
88 | setTextColor(unpack(KeyValueInfoHUDBox.COLOR.TEXT_DEFAULT)) |
89 | end |
90 | |
91 | if i < #self.activeLines then |
92 | drawFilledRect(leftX, y, self.rowWidth, 1 / g_screenHeight, unpack(KeyValueInfoHUDBox.COLOR.SEPARATOR)) |
93 | end |
94 | |
95 | y = y + self.rowHeight |
96 | end |
97 | |
98 | setTextAlignment(RenderText.ALIGN_LEFT) |
99 | |
100 | self.doShowNextFrame = false |
101 | end |
197 | function KeyValueInfoHUDBox:storeScaledValues() |
198 | local scale = self.uiScale |
199 | |
200 | local function normalize(x, y) |
201 | return x * scale * g_aspectScaleX / g_referenceScreenWidth, y * scale * g_aspectScaleY / g_referenceScreenHeight |
202 | end |
203 | |
204 | self.boxWidth = normalize(340, 0) |
205 | |
206 | local _ |
207 | _, self.labelTextSize = normalize(0, HUDElement.TEXT_SIZE.DEFAULT_TITLE) |
208 | _, self.rowTextSize = normalize(0, HUDElement.TEXT_SIZE.DEFAULT_TEXT) |
209 | self.titleTextSize = self.labelTextSize |
210 | |
211 | self.labelTextOffsetX, self.labelTextOffsetY = normalize(0, 3) |
212 | self.leftTextOffsetX, self.leftTextOffsetY = normalize(0, 6) |
213 | self.rightTextOffsetX, self.rightTextOffsetY = normalize(0, 6) |
214 | |
215 | self.rowWidth, self.rowHeight = normalize(308, 26) |
216 | self.listMarginWidth, self.listMarginHeight = normalize(16, 15) |
217 | end |
131 | function KeyValueInfoHUDBox:textSizeToFit(baseSize, text, maxWidth, minSize) |
132 | local size = baseSize |
133 | if minSize == nil then |
134 | minSize = baseSize / 2 |
135 | end |
136 | |
137 | setTextWrapWidth(maxWidth) |
138 | local lengthWithNoLineLimit = getTextLength(size, text, 99999) |
139 | |
140 | while getTextLength(size, text, 1) < lengthWithNoLineLimit do |
141 | size = size - baseSize * 0.05 |
142 | |
143 | -- Limit size. Cut off any extra text |
144 | if size <= baseSize / 2 then |
145 | -- Undo |
146 | size = size + baseSize * 0.05 |
147 | |
148 | break |
149 | end |
150 | end |
151 | |
152 | setTextWrapWidth(0) |
153 | |
154 | return size |
155 | end |