View Single Post
07-10-19, 10:41 AM   #4
jeruku
A Cobalt Mageweaver
 
jeruku's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 223
May have gone a little overboard but this should increase performance significantly.

Edit: Decided to take out the comments after fixing it up; I'm clearly bad at explaining myself.
Lua Code:
  1. local GetBestMapForUnit = C_Map.GetBestMapForUnit  
  2. local MapRects =  {}
  3. local mapID
  4. local UnitPosition =  UnitPosition
  5. local GetWorldPosFromMapPos =  C_Map.GetWorldPosFromMapPos
  6. local CreateVector2D =  CreateVector2D
  7.  
  8. local function GetPlayerMapPosition(mapid, token, rects, _, x, y)
  9.     if mapid then
  10.         rects = MapRects[mapid]
  11.         if not rects then
  12.             rects = {}
  13.             _, topleft = GetWorldPosFromMapPos(mapid, CreateVector2D(0,0))
  14.             _, bottomright = GetWorldPosFromMapPos(mapid, CreateVector2D(1,1))
  15.  
  16.             bottomright:Subtract(topleft)
  17.             rects = { topleft.x, topleft.y, bottomright.x, bottomright.y }
  18.             MapRects[mapid] = rects
  19.         end
  20.  
  21.         x, y = UnitPosition(token)
  22.         if x then
  23.             x, y = x - rects[1], y - rects[2]
  24.             return y / rects[4], x / rects[3]
  25.         else
  26.             return 0, 0
  27.         end
  28.     end
  29. end
  30.  
  31. local function OnUpdate(self, elapsed, x, y)
  32.     if mapID then
  33.         x, y = GetPlayerMapPosition(mapID, 'player')
  34.         self.text:SetFormattedText('%.1f, %.1f', x*100, y*100)
  35.     end
  36. end
  37. tMinimapCoordsFrame:HookScript('OnUpdate', OnUpdate)
  38.  
  39. tMinimapCoordsFrame:HookScript('OnEvent', function(self, event, ...)
  40.     if event == 'PLAYER_STOPPED_MOVING' then
  41.         mapID = nil
  42.     elseif event ~= 'PLAYER_ENTERING_WORLD' then
  43.         mapID = GetBestMapForUnit('player')
  44.     else
  45.         mapID = GetBestMapForUnit('player')
  46.         OnUpdate(self)
  47.         self:UnregisterEvent('PLAYER_ENTERING_WORLD')
  48.     end
  49. end)
  50. tMinimapCoordsFrame:RegisterEvent('ZONE_CHANGED')
  51. tMinimapCoordsFrame:RegisterEvent('ZONE_CHANGED_INDOORS')
  52. tMinimapCoordsFrame:RegisterEvent('ZONE_CHANGED_NEW_AREA')
  53. tMinimapCoordsFrame:RegisterEvent('PLAYER_ENTERING_WORLD')
  54. tMinimapCoordsFrame:RegisterEvent('PLAYER_STOPPED_MOVING')
  55. tMinimapCoordsFrame:RegisterEvent('PLAYER_STARTED_MOVING')
__________________
"I have not failed, I simply found 10,000 ways that did not work." - Thomas Edison

Last edited by jeruku : 07-10-19 at 08:59 PM. Reason: Okay, totally fixed... hopefully.
  Reply With Quote