Commit 517a8dcba88a6fe6d580032fd24a93ac3dfb9347

Authored by DarkGod
2 parents 083f6152 3d86911b

Merge branch 'hotkey_extract' into 'master'

Pulls the actual parsing of a hotkey into an icon out into its own function

Modifies `engines\default\engine\HotkeysIconDisplay.lua` to better support addons. The current call to `display()` draws every hotkey over the course of a nested iteration, which means that if someone wishes to modify this behavior (e.g. have flagged talents use a different border color, or support multiple talent icons to denote different states of a talent), the entire `display()` function must be entirely replaced. This isn't ideal as these modifications are mutually exclusive.

This MR splits the actual parsing of a hotkey into a `displayHotkey()` function (with appropriate arguments) so that addon developers can safely superload the hotkey drawing behavior.

**This MR is save-safe.**

See merge request !691
... ... @@ -129,6 +129,9 @@ local frames_colors = {
129 129 disabled = {0.65, 0.65, 0.65},
130 130 }
131 131
  132 +-- Store it so addons can play with it.
  133 +_M.frames_colors = frames_colors
  134 +
132 135 -- Displays the hotkeys, keybinds & cooldowns
133 136 function _M:display()
134 137 local a = self.actor
... ... @@ -152,140 +155,156 @@ function _M:display()
152 155 self.items = {}
153 156 local w, h = self.frames.w, self.frames.h
154 157
155   - for page = bpage, #page_to_hotkey do for i = 1, 12 do
156   - local ts = nil
157   - local bi = i
158   - local j = i + (12 * (page - 1))
159   - if a.hotkey[j] and a.hotkey[j][1] == "talent" then
160   - ts = {a.hotkey[j][2], j, "talent", i, page, i + (12 * (page - bpage))}
161   - elseif a.hotkey[j] and a.hotkey[j][1] == "inventory" then
162   - ts = {a.hotkey[j][2], j, "inventory", i, page, i + (12 * (page - bpage))}
163   - end
  158 + for page = bpage, #page_to_hotkey do
  159 + for i = 1, 12 do
  160 + local ts = nil
  161 + local bi = i
  162 + local j = i + (12 * (page - 1))
  163 + if a.hotkey[j] and a.hotkey[j][1] == "talent" then
  164 + ts = {a.hotkey[j][2], j, "talent", i, page, i + (12 * (page - bpage))}
  165 + elseif a.hotkey[j] and a.hotkey[j][1] == "inventory" then
  166 + ts = {a.hotkey[j][2], j, "inventory", i, page, i + (12 * (page - bpage))}
  167 + end
164 168
165   - x = self.frames.w * col
166   - y = self.frames.h * row
167   - self.dragclics[j] = {x,y,w,h}
168   -
169   - if ts then
170   - local s
171   - local i = ts[2]
172   - local lpage = ts[5]
173   - local color, angle, txt = nil, 0, nil
174   - local display_entity = nil
175   - local frame = "ok"
176   - if ts[3] == "talent" then
177   - local tid = ts[1]
178   - local t = a:getTalentFromId(tid)
179   - if t then
180   - display_entity = t.display_entity
181   - if a:isTalentCoolingDown(t) then
182   - if not a:preUseTalent(t, true, true) then
183   - color = {190,190,190}
184   - frame = "disabled"
185   - else
186   - frame = "cooldown"
187   - color = {255,0,0}
188   - angle = 360 * (1 - (a.talents_cd[t.id] / a:getTalentCooldown(t)))
189   - end
190   - txt = tostring(math.ceil(a:isTalentCoolingDown(t)))
191   - elseif a:isTalentActive(t.id) then
192   - color = {255,255,0}
193   - frame = "sustain"
194   - elseif not a:preUseTalent(t, true, true) then
  169 + x = self.frames.w * col
  170 + y = self.frames.h * row
  171 + self.dragclics[j] = {x,y,w,h}
  172 +
  173 + local ind, hktable, cfake = self:displayHotkey(page, i, x, y, ts)
  174 +
  175 + self.items[#self.items+1] = hktable
  176 + self.clics[ind] = {x, y, w, h, fake=cfake}
  177 +
  178 + if orient == "down" or orient == "up" then
  179 + col = col + 1
  180 + if col >= self.max_cols then
  181 + col = 0
  182 + row = row + 1
  183 + if row >= self.max_rows then return end
  184 + end
  185 + elseif orient == "left" or orient == "right" then
  186 + row = row + 1
  187 + if row >= self.max_rows then
  188 + row = 0
  189 + col = col + 1
  190 + if col >= self.max_cols then return end
  191 + end
  192 + end
  193 + end
  194 + end
  195 +end
  196 +
  197 +function _M:displayHotkey(page, i, x, y, ts)
  198 + local bi = i
  199 + local w, h = self.frames.w, self.frames.h
  200 + local a = self.actor
  201 +
  202 + if ts then
  203 + local s
  204 + local i = ts[2]
  205 + local lpage = ts[5]
  206 + local color, angle, txt = nil, 0, nil
  207 + local display_entity = nil
  208 + local frame = "ok"
  209 + if ts[3] == "talent" then
  210 + local tid = ts[1]
  211 + local t = a:getTalentFromId(tid)
  212 + if t then
  213 + display_entity = t.display_entity
  214 + if a:isTalentCoolingDown(t) then
  215 + if not a:preUseTalent(t, true, true) then
195 216 color = {190,190,190}
196 217 frame = "disabled"
  218 + else
  219 + frame = "cooldown"
  220 + color = {255,0,0}
  221 + angle = 360 * (1 - (a.talents_cd[t.id] / a:getTalentCooldown(t)))
197 222 end
198   - end
199   - elseif ts[3] == "inventory" then
200   - local o = a:findInAllInventories(ts[1], {no_add_name=true, force_id=true, no_count=true})
201   - local cnt = 0
202   - if o then cnt = o:getNumber() end
203   - if cnt == 0 then
  223 + txt = tostring(math.ceil(a:isTalentCoolingDown(t)))
  224 + elseif a:isTalentActive(t.id) then
  225 + color = {255,255,0}
  226 + frame = "sustain"
  227 + elseif not a:preUseTalent(t, true, true) then
204 228 color = {190,190,190}
205 229 frame = "disabled"
206 230 end
207   - display_entity = o
208   - if o and o.use_talent and o.use_talent.id then
209   - local t = a:getTalentFromId(o.use_talent.id)
210   - display_entity = t and t.display_entity
211   - end
212   - if o and o.talent_cooldown then
213   - local t = a:getTalentFromId(o.talent_cooldown)
214   - angle = 360
215   - if t and a:isTalentCoolingDown(t) then
216   - color = {255,0,0}
217   - angle = 360 * (1 - (a.talents_cd[t.id] / a:getTalentCooldown(t)))
218   - frame = "cooldown"
219   - txt = tostring(math.ceil(a:isTalentCoolingDown(t)))
220   - end
221   - elseif o and (o.use_talent or o.use_power) then
222   - angle = 360 * ((o.power / o.max_power))
  231 + end
  232 + elseif ts[3] == "inventory" then
  233 + local o = a:findInAllInventories(ts[1], {no_add_name=true, force_id=true, no_count=true})
  234 + local cnt = 0
  235 + if o then cnt = o:getNumber() end
  236 + if cnt == 0 then
  237 + color = {190,190,190}
  238 + frame = "disabled"
  239 + end
  240 + display_entity = o
  241 + if o and o.use_talent and o.use_talent.id then
  242 + local t = a:getTalentFromId(o.use_talent.id)
  243 + display_entity = t and t.display_entity
  244 + end
  245 + if o and o.talent_cooldown then
  246 + local t = a:getTalentFromId(o.talent_cooldown)
  247 + angle = 360
  248 + if t and a:isTalentCoolingDown(t) then
223 249 color = {255,0,0}
224   - local cd = o:getObjectCooldown(a)
225   - if cd and cd > 0 then
226   - frame = "cooldown"
227   - txt = tostring(cd)
228   - elseif not cd then
229   - frame = "disabled"
230   - end
  250 + angle = 360 * (1 - (a.talents_cd[t.id] / a:getTalentCooldown(t)))
  251 + frame = "cooldown"
  252 + txt = tostring(math.ceil(a:isTalentCoolingDown(t)))
231 253 end
232   - if o and o.wielded then
233   - frame = "sustain"
  254 + elseif o and (o.use_talent or o.use_power) then
  255 + angle = 360 * ((o.power / o.max_power))
  256 + color = {255,0,0}
  257 + local cd = o:getObjectCooldown(a)
  258 + if cd and cd > 0 then
  259 + frame = "cooldown"
  260 + txt = tostring(cd)
  261 + elseif not cd then
  262 + frame = "disabled"
234 263 end
235   - if o and o.wielded and o.use_talent and o.use_talent.id then
236   - local t = a:getTalentFromId(o.use_talent.id)
237   - if not a:preUseTalent(t, true, true, true) then
238   - angle = 0
239   - color = {190,190,190}
240   - frame = "disabled"
241   - end
  264 + end
  265 + if o and o.wielded then
  266 + frame = "sustain"
  267 + end
  268 + if o and o.wielded and o.use_talent and o.use_talent.id then
  269 + local t = a:getTalentFromId(o.use_talent.id)
  270 + if not a:preUseTalent(t, true, true, true) then
  271 + angle = 0
  272 + color = {190,190,190}
  273 + frame = "disabled"
242 274 end
243 275 end
  276 + end
244 277
245   - self.font:setStyle("bold")
246   - local ks = game.key:formatKeyString(game.key:findBoundKeys("HOTKEY_"..page_to_hotkey[page]..bi))
247   - local key = self.font:draw(ks, self.font:size(ks), colors.ANTIQUE_WHITE.r, colors.ANTIQUE_WHITE.g, colors.ANTIQUE_WHITE.b, true)[1]
248   - self.font:setStyle("normal")
249   -
250   - local gtxt = nil
251   - if txt then
252   - gtxt = self.fontbig:draw(txt, w, colors.WHITE.r, colors.WHITE.g, colors.WHITE.b, true)[1]
253   - gtxt.fw, gtxt.fh = self.fontbig:size(txt)
254   - end
  278 + self.font:setStyle("bold")
  279 + local ks = game.key:formatKeyString(game.key:findBoundKeys("HOTKEY_"..page_to_hotkey[page]..bi))
  280 + local key = self.font:draw(ks, self.font:size(ks), colors.ANTIQUE_WHITE.r, colors.ANTIQUE_WHITE.g, colors.ANTIQUE_WHITE.b, true)[1]
  281 + self.font:setStyle("normal")
255 282
256   - self.items[#self.items+1] = {i=i, x=x, y=y, e=display_entity or self.default_entity, color=color, angle=angle, key=key, gtxt=gtxt, frame=frame, pagesel=lpage==spage}
257   - self.clics[i] = {x,y,w,h}
258   - else
259   - local i = i + (12 * (page - 1))
260   - local angle = 0
261   - local color = {190,190,190}
262   - local frame = "disabled"
263   -
264   - self.font:setStyle("bold")
265   - local ks = game.key:formatKeyString(game.key:findBoundKeys("HOTKEY_"..page_to_hotkey[page]..bi))
266   - local key = self.font:draw(ks, self.font:size(ks), colors.ANTIQUE_WHITE.r, colors.ANTIQUE_WHITE.g, colors.ANTIQUE_WHITE.b, true)[1]
267   - self.font:setStyle("normal")
268   -
269   - self.items[#self.items+1] = {show_on_drag=true, i=i, x=x, y=y, e=nil, color=color, angle=angle, key=key, gtxt=nil, frame=frame}
270   - self.clics[i] = {x,y,w,h, fake=true}
  283 + local gtxt = nil
  284 + if txt then
  285 + gtxt = self.fontbig:draw(txt, w, colors.WHITE.r, colors.WHITE.g, colors.WHITE.b, true)[1]
  286 + gtxt.fw, gtxt.fh = self.fontbig:size(txt)
271 287 end
272 288
273   - if orient == "down" or orient == "up" then
274   - col = col + 1
275   - if col >= self.max_cols then
276   - col = 0
277   - row = row + 1
278   - if row >= self.max_rows then return end
279   - end
280   - elseif orient == "left" or orient == "right" then
281   - row = row + 1
282   - if row >= self.max_rows then
283   - row = 0
284   - col = col + 1
285   - if col >= self.max_cols then return end
286   - end
287   - end
288   - end end
  289 + --self.items[#self.items+1] = {i=i, x=x, y=y, e=display_entity or self.default_entity, color=color, angle=angle, key=key, gtxt=gtxt, frame=frame, pagesel=lpage==a.hotkey_page}
  290 + --self.clics[i] = {x,y,w,h}
  291 +
  292 + return i, {i=i, x=x, y=y, e=display_entity or self.default_entity, color=color, angle=angle, key=key, gtxt=gtxt, frame=frame, pagesel=lpage==a.hotkey_page}, nil
  293 + else
  294 + local i = i + (12 * (page - 1))
  295 + local angle = 0
  296 + local color = {190,190,190}
  297 + local frame = "disabled"
  298 +
  299 + self.font:setStyle("bold")
  300 + local ks = game.key:formatKeyString(game.key:findBoundKeys("HOTKEY_"..page_to_hotkey[page]..bi))
  301 + local key = self.font:draw(ks, self.font:size(ks), colors.ANTIQUE_WHITE.r, colors.ANTIQUE_WHITE.g, colors.ANTIQUE_WHITE.b, true)[1]
  302 + self.font:setStyle("normal")
  303 +
  304 + --self.items[#self.items+1] = {show_on_drag=true, i=i, x=x, y=y, e=nil, color=color, angle=angle, key=key, gtxt=nil, frame=frame}
  305 + --self.clics[i] = {x,y,w,h, fake=true}
  306 + return i, {show_on_drag=true, i=i, x=x, y=y, e=nil, color=color, angle=angle, key=key, gtxt=nil, frame=frame}, true
  307 + end
289 308 end
290 309
291 310 --- Our toScreen override
... ... @@ -298,7 +317,7 @@ function _M:toScreen()
298 317 if not item.show_on_drag or (game.mouse and game.mouse.drag) and self.cur_sel then
299 318 local key = item.key
300 319 local gtxt = item.gtxt
301   - local frame = frames_colors[item.frame]
  320 + local frame = self.frames_colors[item.frame]
302 321 local pagesel = item.pagesel and 1 or 0.5
303 322
304 323 if item.e then item.e:toScreen(self.tiles, self.display_x + item.x + self.frames.fx, self.display_y + item.y + self.frames.fy, self.icon_w, self.icon_h) end
... ...