• 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;
    
    	self:ClearAllPoints();
    	self:SetPoint("CENTER",Minimap,"CENTER",x1,y1);
    end
    

    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:SetMovable(true);
    self:SetUserPlaced(true);
    self:SetClampedToScreen(true);
    self:RegisterForDrag("RightButton");
    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
    		MoveMinimapIcon(self);
    	end
    end);
    


    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()
    	MyMod_MinimapButton:SetPoint("TOPLEFT","Minimap","TOPLEFT",52-(80*cos(MyMod_Settings.MinimapPos)),(80*sin(MyMod_Settings.MinimapPos))-52)
    end
    
    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
    end
    


    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!


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