View Single Post
07-11-19, 08:59 AM   #9
Terenna
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 105
I modified it ever so slightly to this:
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.     x, y = UnitPosition(token)
  10.     if not x then return end
  11.     if mapid then
  12.         rects = MapRects[mapid]
  13.         if not rects then
  14.             rects = {}
  15.             _, topleft = GetWorldPosFromMapPos(mapid, CreateVector2D(0,0))
  16.             _, bottomright = GetWorldPosFromMapPos(mapid, CreateVector2D(1,1))
  17.  
  18.             bottomright:Subtract(topleft)
  19.             rects = { topleft.x, topleft.y, bottomright.x, bottomright.y }
  20.             MapRects[mapid] = rects
  21.         end
  22.        
  23.         x, y = x - rects[1], y - rects[2]
  24.         return y / rects[4], x / rects[3]
  25.     end
  26. end
  27.  
  28. local function OnUpdate(self, elapsed, x, y)
  29.     self.elapsed = self.elapsed + elapsed
  30.     while self.elapsed > 0.5 do --only update the coords 2/second
  31.         if mapID then
  32.             x, y = GetPlayerMapPosition(mapID, 'player')
  33.             if x then
  34.                 self.text:SetFormattedText('%.1f, %.1f', x * 100, y * 100)
  35.             else
  36.                 self.text:SetText('')
  37.             end
  38.         end
  39.  
  40.         self.elapsed = self.elapsed - 0.5
  41.     end
  42. end
  43.  
  44. tMinimapCoordsFrame:SetScript('OnEvent', function(self, event, ...)
  45.     if event == 'PLAYER_STOPPED_MOVING' then
  46.         mapID = nil
  47.         tMinimapCoordsFrame:SetScript('OnUpdate', nil)
  48.     elseif event == 'PLAYER_STARTED_MOVING' then
  49.         mapID = GetBestMapForUnit('player')
  50.         tMinimapCoordsFrame:SetScript('OnUpdate', OnUpdate)
  51.     elseif event == 'ZONE_CHANGED_NEW_AREA' then
  52.         if IsInInstance() then
  53.             self:UnregisterEvent('PLAYER_STARTED_MOVING')
  54.             self:UnregisterEvent('PLAYER_STOPPED_MOVING')
  55.             self:UnregisterEvent('ZONE_CHANGED')
  56.             self:UnregisterEvent('ZONE_CHANGED_INDOORS')
  57.         else
  58.             self:RegisterEvent('PLAYER_STOPPED_MOVING')
  59.             self:RegisterEvent('PLAYER_STARTED_MOVING')
  60.             self:RegisterEvent('ZONE_CHANGED')
  61.             self:RegisterEvent('ZONE_CHANGED_INDOORS')
  62.         end
  63.     else
  64.         mapID = GetBestMapForUnit('player')
  65.         OnUpdate(self, 1) --force it to update the coords exactly once as we log in
  66.         self:UnregisterEvent('PLAYER_ENTERING_WORLD')
  67.         if IsInInstance() then
  68.             self:UnregisterEvent('PLAYER_STARTED_MOVING')
  69.             self:UnregisterEvent('PLAYER_STOPPED_MOVING')
  70.             self:UnregisterEvent('ZONE_CHANGED')
  71.             self:UnregisterEvent('ZONE_CHANGED_INDOORS')
  72.         end
  73.     end
  74. end)

I reworked the OnEvent handler to unregister and register the movement and zone changes if the player is in an instance so it won't be running in there where there are no coordinates.

Last edited by Terenna : 07-11-19 at 09:13 AM.
  Reply With Quote