Quantcast WoWInterface - View Single Post - Minimap Coordinates Memory Usage Optimization
View Single Post
07-11-19, 11:46 AM   #10
Lybrial
A Cliff Giant
AddOn Compiler - Click to view compilations
Join Date: Jan 2010
Posts: 77
I use AceTimer-3.0 for updating my coords frame:

Lua Code:
  1. local CORE, LIBS = LybrialUI:GetCore();
  2. local LOCALE = LIBS.LOCALE;
  3.  
  4. local Texts = LybrialMiniMap:NewModule("Texts", "AceTimer-3.0");
  5.  
  6. Texts.displayName = LOCALE["TEXTS"];
  7. Texts.desc = LOCALE["TEXTS_DESC"];
  8. Texts.zone = nil;
  9. Texts.coords = nil;
  10. Texts.date = nil;
  11. Texts.clock = nil;
  12.  
  13. local _G = _G;
  14. local C_Map = C_Map;
  15. local CreateFrame = CreateFrame;
  16. local CreateVector2D = CreateVector2D;
  17. local GetMinimapZoneText = GetMinimapZoneText;
  18. local Minimap = Minimap;
  19. local UnitPosition = UnitPosition;
  20. local strsub = strsub;
  21. local unpack = unpack;
  22.  
  23. function Texts:OnEnableCoords()
  24.     if (not self.coords) then
  25.         self.coords = CreateFrame("Frame", "LybrialUITextsCoords", Minimap);
  26.         self.coords.text = self.coords:CreateFontString(nil, "OVERLAY");
  27.         self.coords.MapRects = {};
  28.         self.coords:SetScript("OnEvent", function(self)
  29.             self.mapId = C_Map.GetBestMapForUnit("player");
  30.         end);
  31.         self.coords:RegisterEvent("PLAYER_ENTERING_WORLD");
  32.         self.coords:RegisterEvent("ZONE_CHANGED");
  33.         self.coords:RegisterEvent("ZONE_CHANGED_INDOORS");
  34.         self.coords:RegisterEvent("ZONE_CHANGED_NEW_AREA");
  35.         self:ScheduleRepeatingTimer(self.UpdatePlayerMapPosition, 1);
  36.     end
  37. end
  38.  
  39. function Texts:OnUpdateCoords()
  40.     local show = self.db.profile.texts.coords.show;
  41.     local anchor = self.db.profile.texts.coords.position.anchor;
  42.     local offsetX = self.db.profile.texts.coords.position.offset.x;
  43.     local offsetY = self.db.profile.texts.coords.position.offset.y;
  44.     local font = self.db.profile.texts.coords.font.font;
  45.     local fontSize = self.db.profile.texts.coords.font.size;
  46.     local fontOutline = self.db.profile.texts.coords.font.outline;
  47.     local fontColor = self.db.profile.texts.coords.font.color;
  48.  
  49.     self.coords:SetSize(120, 30);
  50.     self.coords:ClearAllPoints();
  51.     self.coords:SetPoint(anchor, Minimap, anchor, offsetX, offsetY);
  52.     self.coords.text:ClearAllPoints();
  53.     self.coords.text:SetPoint("CENTER");
  54.     self.coords.text:SetFont(LIBS.LSM:Fetch("font", font), fontSize, fontOutline);
  55.     self.coords.text:SetTextColor(unpack(fontColor));
  56.  
  57.     if (show) then
  58.         self.coords:Show();
  59.     else
  60.         self.coords:Hide();
  61.     end
  62. end
  63.  
  64. function Texts:UpdatePlayerMapPosition()
  65.     local coords = Texts.coords;
  66.  
  67.     if (not coords:IsShown()) then
  68.         return ;
  69.     end
  70.  
  71.     if (not coords.mapId) then
  72.         return ;
  73.     end
  74.  
  75.     local rects = coords.MapRects[coords.mapId];
  76.  
  77.     if (not rects) then
  78.         rects = {};
  79.  
  80.         local _, topLeft = C_Map.GetWorldPosFromMapPos(coords.mapId, CreateVector2D(0, 0));
  81.         local _, bottomRight = C_Map.GetWorldPosFromMapPos(coords.mapId, CreateVector2D(1, 1));
  82.  
  83.         bottomRight:Subtract(topLeft);
  84.  
  85.         rects = { topLeft.x, topLeft.y, bottomRight.x, bottomRight.y };
  86.  
  87.         coords.MapRects[coords.mapId] = rects;
  88.     end
  89.  
  90.     local x, y = UnitPosition("player");
  91.  
  92.     if (x) then
  93.         x = x - rects[1];
  94.         y = y - rects[2];
  95.         x = x / rects[3];
  96.         y = y / rects[4];
  97.         y = y * 100;
  98.         x = x * 100;
  99.  
  100.         coords.text:SetFormattedText("%.1f, %.1f", y, x);
  101.     end
  102. end
  Reply With Quote