• 0

    posted a message on Dynamic minimap icons! (Better code than what is usually used)
    I spent quite a while trying to figure this out, first ditching the code I found (which was using math.deg(math.atan2(ypos,xpos)) or something to that extent) and then writing something from scratch.

    I'm not the kind of person who can just live with a static minimap frame (and any other addon developer worth their salt should feel the same).

    I'm also not the kind of person who can just use somebody else's code... so in place of the regular function, I wrote my own.

    Anyway, the code I wrote was this:
    MoveMinimapIcon = function(self)
    	local x1,y1 = GetCursorPosition();
    	local x2,y2 = Minimap:GetCenter();
    	local dist = (Minimap:GetCenter()) - Minimap:GetLeft() + 10;
    	local hyp = sqrt((x1-x2)^2 + (y1-y2)^2);
    	x1 = (x1 - x2) / hyp * dist;
    	y1 = (y1 - y2) / hyp * dist;

    In my button definition (I use Lua), I have the following (which can be put in your <OnLoad> script handler if you're using XML):
    self:SetScript("OnDragStart", function(self) self.Moving = true; end);
    self:SetScript("OnDragStop", function(self) self.Moving = false; end);
    self:SetScript("OnUpdate", function(self)
    	if self.Moving then

    I haven't benchmarked it against the usual code, but what I'm doing seems less convoluted (and less confusing) than the usual method:
    MyMod_Settings = {
    	MinimapPos = 45 -- default position of the minimap icon in degrees
    function MyMod_MinimapButton_Reposition()
    function MyMod_MinimapButton_DraggingFrame_OnUpdate()
    	local xpos,ypos = GetCursorPosition()
    	local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom()
    	xpos = xmin-xpos/UIParent:GetScale()+70
    	-- get coordinates as differences from the center of the minimap
    	ypos = ypos/UIParent:GetScale()-ymin-70
    	MyMod_Settings.MinimapPos = math.deg(math.atan2(ypos,xpos))
    	-- save the degrees we are relative to the minimap center
    	MyMod_MinimapButton_Reposition() -- move the button

    My first experience working with frames, no less, and I'm already rewriting the basics! :P

    I love feedback, so please - tell me what you think. If you decide to use this, recognition wouldn't go astray but I don't mind too much - I made this because I was unsatisfied with what we use currently, but I shared it for the benefit of all. At the very least, drop me a line saying that you've used it in your addon so I can check it out. :)

    Have a good day all, happy developing!

    Posted in: Project Discussion
  • To post a comment, please or register a new account.