Blizzard_MapCanvas/MapCanvas_DataProviderBase.lua

Blizzard_MapCanvas/MapCanvas_DataProviderBase.lua (9.0.2.36710; unchanged since 9.0.2.36665)
  1. -- Provides a basic interface for something that manages the adding, updating, and removing of data like icons, blobs or text to the map canvas
  2. MapCanvasDataProviderMixin = {};
  3. function MapCanvasDataProviderMixin:OnAdded(owningMap)
  4. -- Optionally override in your mixin, called when this provider is added to a map canvas
  5. self.owningMap = owningMap;
  6. end
  7. function MapCanvasDataProviderMixin:OnRemoved(owningMap)
  8. -- Optionally override in your mixin, called when this provider is removed from a map canvas
  9. assert(owningMap == self.owningMap);
  10. self.owningMap = nil;
  11. if self.registeredEvents then
  12. for event in pairs(self.registeredEvents) do
  13. owningMap:UnregisterEvent(event);
  14. end
  15. self.registeredEvents = nil;
  16. end
  17. end
  18. function MapCanvasDataProviderMixin:RemoveAllData()
  19. -- Override in your mixin, this method should remove everything that has been added to the map
  20. end
  21. function MapCanvasDataProviderMixin:RefreshAllData(fromOnShow)
  22. -- Override in your mixin, this method should assume the map is completely blank, and refresh any data necessary on the map
  23. end
  24. function MapCanvasDataProviderMixin:OnShow()
  25. -- Override in your mixin, called when the map canvas is shown
  26. end
  27. function MapCanvasDataProviderMixin:OnHide()
  28. -- Override in your mixin, called when the map canvas is closed
  29. end
  30. function MapCanvasDataProviderMixin:OnMapInsetSizeChanged(mapInsetIndex, expanded)
  31. -- Optionally override in your mixin, called when a map inset changes sizes
  32. end
  33. function MapCanvasDataProviderMixin:OnMapInsetMouseEnter(mapInsetIndex)
  34. -- Optionally override in your mixin, called when a map inset gains mouse focus
  35. end
  36. function MapCanvasDataProviderMixin:OnMapInsetMouseLeave(mapInsetIndex)
  37. -- Optionally override in your mixin, called when a map inset loses mouse focus
  38. end
  39. function MapCanvasDataProviderMixin:OnCanvasScaleChanged()
  40. -- Optionally override in your mixin, called when the canvas scale changes
  41. end
  42. function MapCanvasDataProviderMixin:OnCanvasPanChanged()
  43. -- Optionally override in your mixin, called when the pan location changes
  44. end
  45. function MapCanvasDataProviderMixin:OnCanvasSizeChanged()
  46. -- Optionally override in your mixin, called when the canvas size changes
  47. end
  48. function MapCanvasDataProviderMixin:OnEvent(event, ...)
  49. -- Override in your mixin to accept events register via RegisterEvent
  50. end
  51. function MapCanvasDataProviderMixin:OnGlobalAlphaChanged()
  52. -- Optionally override in your mixin if your data provider obeys global alpha, called when the global alpha changes
  53. end
  54. function MapCanvasDataProviderMixin:GetMap()
  55. return self.owningMap;
  56. end
  57. function MapCanvasDataProviderMixin:OnMapChanged()
  58. -- Optionally override in your mixin, called when map ID changes
  59. self:RefreshAllData();
  60. end
  61. function MapCanvasDataProviderMixin:RegisterEvent(event)
  62. -- Since data providers aren't frames this provides a similar method of event registration, but always calls self:OnEvent(event, ...)
  63. if not self.registeredEvents then
  64. self.registeredEvents = {}
  65. end
  66. if not self.registeredEvents[event] then
  67. self.registeredEvents[event] = true;
  68. self:GetMap():AddDataProviderEvent(event);
  69. end
  70. end
  71. function MapCanvasDataProviderMixin:UnregisterEvent(event)
  72. if self.registeredEvents and self.registeredEvents[event] then
  73. self.registeredEvents[event] = nil;
  74. self:GetMap():RemoveDataProviderEvent(event);
  75. end
  76. end
  77. function MapCanvasDataProviderMixin:SignalEvent(event, ...)
  78. if self.registeredEvents and self.registeredEvents[event] then
  79. self:OnEvent(event, ...);
  80. end
  81. end
  82. function MapCanvasDataProviderMixin:HandleMouseAction(button, action)
  83. local overriden = self:GetMap():ProcessGlobalPinOverrideMouseHandlers(button, action);
  84. return not overriden;
  85. end
  86. -- A base template for data providers that are enabled or disabled with a CVar, e.g. archaeology digsites.
  87. CVarMapCanvasDataProviderMixin = CreateFromMixins(MapCanvasDataProviderMixin);
  88. function CVarMapCanvasDataProviderMixin:Init(cvar, scriptCVar)
  89. self.cvar = cvar;
  90. self.scriptCVar = scriptCVar;
  91. end
  92. function CVarMapCanvasDataProviderMixin:IsCVarSet()
  93. return GetCVarBool(self.cvar);
  94. end
  95. function CVarMapCanvasDataProviderMixin:OnShow()
  96. self:RegisterEvent("CVAR_UPDATE");
  97. end
  98. function CVarMapCanvasDataProviderMixin:OnHide()
  99. self:UnregisterEvent("CVAR_UPDATE");
  100. end
  101. function CVarMapCanvasDataProviderMixin:OnEvent(event, ...)
  102. if event == "CVAR_UPDATE" then
  103. local eventName, value = ...;
  104. if eventName == self.scriptCVar then
  105. self:RefreshAllData();
  106. end
  107. end
  108. end
  109. -- Provides a basic interface for something that is visible on the map canvas, like icons, blobs or text
  110. MapCanvasPinMixin = {};
  111. function MapCanvasPinMixin:OnLoad()
  112. -- Override in your mixin, called when this pin is created
  113. end
  114. function MapCanvasPinMixin:OnAcquired(...) -- the arguments here are anything that are passed into AcquirePin after the pinTemplate
  115. -- Override in your mixin, called when this pin is being acquired by a data provider but before its added to the map
  116. end
  117. function MapCanvasPinMixin:OnReleased()
  118. -- Override in your mixin, called when this pin is being released by a data provider and is no longer on the map
  119. end
  120. function MapCanvasPinMixin:OnClick(...)
  121. if self:GetMap():ProcessGlobalPinMouseActionHandlers(MapCanvasMixin.MouseAction.Click, ...) then
  122. return;
  123. end
  124. if self.OnMouseClickAction then
  125. self:OnMouseClickAction(...);
  126. end
  127. end
  128. function MapCanvasPinMixin:OnMouseEnter()
  129. -- Override in your mixin, called when the mouse enters this pin
  130. end
  131. function MapCanvasPinMixin:OnMouseLeave()
  132. -- Override in your mixin, called when the mouse leaves this pin
  133. end
  134. function MapCanvasPinMixin:OnMouseDown(...)
  135. if self:GetMap():ProcessGlobalPinMouseActionHandlers(MapCanvasMixin.MouseAction.Down, ...) then
  136. return;
  137. end
  138. if self.OnMouseDownAction then
  139. self:OnMouseDownAction(...);
  140. end
  141. end
  142. function MapCanvasPinMixin:OnMouseUp(...)
  143. if self:GetMap():ProcessGlobalPinMouseActionHandlers(MapCanvasMixin.MouseAction.Up, ...) then
  144. return;
  145. end
  146. if self.OnMouseUpAction then
  147. self:OnMouseUpAction(...);
  148. end
  149. end
  150. function MapCanvasPinMixin:OnMapInsetSizeChanged(mapInsetIndex, expanded)
  151. -- Optionally override in your mixin, called when a map inset changes sizes
  152. end
  153. function MapCanvasPinMixin:OnMapInsetMouseEnter(mapInsetIndex)
  154. -- Optionally override in your mixin, called when a map inset gains mouse focus
  155. end
  156. function MapCanvasPinMixin:OnMapInsetMouseLeave(mapInsetIndex)
  157. -- Optionally override in your mixin, called when a map inset loses mouse focus
  158. end
  159. function MapCanvasPinMixin:ClearNudgeSettings()
  160. self.nudgeTargetFactor = nil;
  161. self.nudgeSourceRadius = nil;
  162. self.nudgeSourceZoomedOutMagnitude = nil;
  163. self.nudgeSourceZoomedInMagnitude = nil;
  164. self.zoomedInNudge = nil;
  165. self.zoomedOutNudge = nil;
  166. end
  167. function MapCanvasPinMixin:SetNudgeTargetFactor(newFactor)
  168. self.nudgeTargetFactor = newFactor;
  169. end
  170. function MapCanvasPinMixin:GetNudgeTargetFactor()
  171. return self.nudgeTargetFactor or 0;
  172. end
  173. function MapCanvasPinMixin:SetNudgeSourceRadius(newRadius)
  174. self.nudgeSourceRadius = newRadius;
  175. end
  176. function MapCanvasPinMixin:GetNudgeSourceRadius()
  177. return self.nudgeSourceRadius or 0;
  178. end
  179. function MapCanvasPinMixin:SetNudgeSourceMagnitude(nudgeSourceZoomedOutMagnitude, nudgeSourceZoomedInMagnitude)
  180. self.nudgeSourceZoomedOutMagnitude = nudgeSourceZoomedOutMagnitude;
  181. self.nudgeSourceZoomedInMagnitude = nudgeSourceZoomedInMagnitude;
  182. end
  183. function MapCanvasPinMixin:GetNudgeSourceZoomedOutMagnitude()
  184. return self.nudgeSourceZoomedOutMagnitude;
  185. end
  186. function MapCanvasPinMixin:GetNudgeSourceZoomedInMagnitude()
  187. return self.nudgeSourceZoomedInMagnitude;
  188. end
  189. function MapCanvasPinMixin:SetNudgeZoomedInFactor(newFactor)
  190. self.zoomedInNudge = newFactor;
  191. end
  192. function MapCanvasPinMixin:GetZoomedInNudgeFactor()
  193. return self.zoomedInNudge or 0;
  194. end
  195. function MapCanvasPinMixin:SetNudgeZoomedOutFactor(newFactor)
  196. self.zoomedOutNudge = newFactor;
  197. end
  198. function MapCanvasPinMixin:GetZoomedOutNudgeFactor()
  199. return self.zoomedOutNudge or 0;
  200. end
  201. function MapCanvasPinMixin:IgnoresNudging()
  202. return self.insetIndex or (self:GetNudgeSourceRadius() == 0 and self:GetNudgeTargetFactor() == 0);
  203. end
  204. function MapCanvasPinMixin:GetMap()
  205. return self.owningMap;
  206. end
  207. function MapCanvasPinMixin:GetNudgeVector()
  208. return self.nudgeVectorX, self.nudgeVectorY;
  209. end
  210. function MapCanvasPinMixin:GetNudgeSourcePinZoomedOutNudgeFactor()
  211. return self.nudgeSourcePinZoomedOutNudgeFactor or 0;
  212. end
  213. function MapCanvasPinMixin:GetNudgeSourcePinZoomedInNudgeFactor()
  214. return self.nudgeSourcePinZoomedInNudgeFactor or 0;
  215. end
  216. -- x and y should be a normalized vector.
  217. function MapCanvasPinMixin:SetNudgeVector(sourcePinZoomedOutNudgeFactor, sourcePinZoomedInNudgeFactor, x, y)
  218. self.nudgeSourcePinZoomedOutNudgeFactor = sourcePinZoomedOutNudgeFactor;
  219. self.nudgeSourcePinZoomedInNudgeFactor = sourcePinZoomedInNudgeFactor;
  220. self.nudgeVectorX = x;
  221. self.nudgeVectorY = y;
  222. self:ApplyCurrentPosition();
  223. end
  224. function MapCanvasPinMixin:GetNudgeFactor()
  225. return self.nudgeFactor or 0;
  226. end
  227. function MapCanvasPinMixin:SetNudgeFactor(nudgeFactor)
  228. self.nudgeFactor = nudgeFactor;
  229. self:ApplyCurrentPosition();
  230. end
  231. function MapCanvasPinMixin:GetNudgeZoomFactor()
  232. local zoomPercent = self:GetMap():GetCanvasZoomPercent();
  233. local targetFactor = Lerp(self:GetZoomedOutNudgeFactor(), self:GetZoomedInNudgeFactor(), zoomPercent);
  234. local sourceFactor = Lerp(self:GetNudgeSourcePinZoomedOutNudgeFactor(), self:GetNudgeSourcePinZoomedInNudgeFactor(), zoomPercent);
  235. return targetFactor * sourceFactor;
  236. end
  237. function MapCanvasPinMixin:SetPosition(normalizedX, normalizedY, insetIndex)
  238. self.normalizedX = normalizedX;
  239. self.normalizedY = normalizedY;
  240. self.insetIndex = insetIndex;
  241. self:GetMap():SetPinPosition(self, normalizedX, normalizedY, insetIndex);
  242. end
  243. -- Returns the global position if not part of an inset, otherwise returns local coordinates of that inset
  244. function MapCanvasPinMixin:GetPosition()
  245. return self.normalizedX, self.normalizedY, self.insetIndex;
  246. end
  247. -- Returns the global position, even if part of an inset
  248. function MapCanvasPinMixin:GetGlobalPosition()
  249. if self.insetIndex then
  250. return self:GetMap():GetGlobalPosition(self.normalizedX, self.normalizedY, self.insetIndex);
  251. end
  252. return self.normalizedX, self.normalizedY;
  253. end
  254. function MapCanvasPinMixin:PanTo(normalizedXOffset, normalizedYOffset)
  255. local normalizedX, normalizedY = self:GetGlobalPosition();
  256. self:GetMap():PanTo(normalizedX + (normalizedXOffset or 0), (normalizedY or 0));
  257. end
  258. function MapCanvasPinMixin:PanAndZoomTo(normalizedXOffset, normalizedYOffset)
  259. local normalizedX, normalizedY = self:GetGlobalPosition();
  260. self:GetMap():PanAndZoomTo(normalizedX + (normalizedXOffset or 0), (normalizedY or 0));
  261. end
  262. function MapCanvasPinMixin:OnCanvasScaleChanged()
  263. self:ApplyCurrentScale();
  264. self:ApplyCurrentAlpha();
  265. end
  266. function MapCanvasPinMixin:OnCanvasPanChanged()
  267. -- Optionally override in your mixin, called when the pan location changes
  268. end
  269. function MapCanvasPinMixin:OnCanvasSizeChanged()
  270. -- Optionally override in your mixin, called when the canvas size changes
  271. end
  272. function MapCanvasPinMixin:SetIgnoreGlobalPinScale(ignore)
  273. self.ignoreGlobalPinScale = ignore;
  274. end
  275. function MapCanvasPinMixin:IsIgnoringGlobalPinScale()
  276. return not not self.ignoreGlobalPinScale;
  277. end
  278. function MapCanvasPinMixin:SetScalingLimits(scaleFactor, startScale, endScale)
  279. self.scaleFactor = scaleFactor;
  280. self.startScale = startScale and math.max(startScale, .01) or nil;
  281. self.endScale = endScale and math.max(endScale, .01) or nil;
  282. end
  283. AM_PIN_SCALE_STYLE_VISIBLE_WHEN_ZOOMED_IN = 1;
  284. AM_PIN_SCALE_STYLE_VISIBLE_WHEN_ZOOMED_OUT = 2;
  285. AM_PIN_SCALE_STYLE_WITH_TERRAIN = 3;
  286. function MapCanvasPinMixin:SetScaleStyle(scaleStyle)
  287. if scaleStyle == AM_PIN_SCALE_STYLE_VISIBLE_WHEN_ZOOMED_IN then
  288. self:SetScalingLimits(1.5, 0.0, 2.55);
  289. elseif scaleStyle == AM_PIN_SCALE_STYLE_VISIBLE_WHEN_ZOOMED_OUT then
  290. self:SetScalingLimits(1.5, 0.825, 0.0);
  291. elseif scaleStyle == AM_PIN_SCALE_STYLE_WITH_TERRAIN then
  292. self:SetScalingLimits(nil, nil, nil);
  293. if self:IsIgnoringGlobalPinScale() then
  294. self:SetScale(1.0);
  295. else
  296. self:SetScale(self:GetGlobalPinScale());
  297. end
  298. end
  299. end
  300. function MapCanvasPinMixin:SetAlphaLimits(alphaFactor, startAlpha, endAlpha)
  301. self.alphaFactor = alphaFactor;
  302. self.startAlpha = startAlpha;
  303. self.endAlpha = endAlpha;
  304. end
  305. AM_PIN_ALPHA_STYLE_VISIBLE_WHEN_ZOOMED_IN = 1;
  306. AM_PIN_ALPHA_STYLE_VISIBLE_WHEN_ZOOMED_OUT = 2;
  307. AM_PIN_ALPHA_STYLE_ALWAYS_VISIBLE = 3;
  308. function MapCanvasPinMixin:SetAlphaStyle(alphaStyle)
  309. if alphaStyle == AM_PIN_ALPHA_STYLE_VISIBLE_WHEN_ZOOMED_IN then
  310. self:SetAlphaLimits(2.0, 0.0, 1.0);
  311. elseif alphaStyle == AM_PIN_ALPHA_STYLE_VISIBLE_WHEN_ZOOMED_OUT then
  312. self:SetAlphaLimits(2.0, 1.0, 0.0);
  313. elseif alphaStyle == AM_PIN_ALPHA_STYLE_ALWAYS_VISIBLE then
  314. self:SetAlphaLimits(nil, nil, nil);
  315. end
  316. end
  317. function MapCanvasPinMixin:ApplyCurrentPosition()
  318. self:GetMap():ApplyPinPosition(self, self.normalizedX, self.normalizedY, self.insetIndex);
  319. end
  320. function MapCanvasPinMixin:ApplyCurrentScale()
  321. local scale;
  322. if self.startScale and self.startScale and self.endScale then
  323. local parentScaleFactor = 1.0 / self:GetMap():GetCanvasScale();
  324. scale = parentScaleFactor * Lerp(self.startScale, self.endScale, Saturate(self.scaleFactor * self:GetMap():GetCanvasZoomPercent()));
  325. elseif not self:IsIgnoringGlobalPinScale() then
  326. scale = 1;
  327. end
  328. if scale then
  329. if not self:IsIgnoringGlobalPinScale() then
  330. scale = scale * self:GetMap():GetGlobalPinScale();
  331. end
  332. self:SetScale(scale);
  333. self:ApplyCurrentPosition();
  334. end
  335. end
  336. function MapCanvasPinMixin:ApplyCurrentAlpha()
  337. if self.alphaFactor and self.startAlpha and self.endAlpha then
  338. local alpha = Lerp(self.startAlpha, self.endAlpha, Saturate(self.alphaFactor * self:GetMap():GetCanvasZoomPercent()));
  339. self:SetAlpha(alpha);
  340. self:SetShown(alpha > 0.05);
  341. end
  342. end
  343. function MapCanvasPinMixin:UseFrameLevelType(pinFrameLevelType, index)
  344. self.pinFrameLevelType = pinFrameLevelType;
  345. self.pinFrameLevelIndex = index;
  346. end
  347. function MapCanvasPinMixin:GetFrameLevelType(pinFrameLevelType)
  348. return self.pinFrameLevelType or "PIN_FRAME_LEVEL_DEFAULT";
  349. end
  350. function MapCanvasPinMixin:ApplyFrameLevel()
  351. local frameLevel = self:GetMap():GetPinFrameLevelsManager():GetValidFrameLevel(self.pinFrameLevelType, self.pinFrameLevelIndex);
  352. self:SetFrameLevel(frameLevel);
  353. end