WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Minimap button (https://www.wowinterface.com/forums/showthread.php?t=16339)

hipjipp 05-17-08 08:27 AM

Minimap button
 
Hi again! I'd like to write a minimapbutton in lua that shows/hides my frame depending on the button i pressed. I've seen alot of them made, but never explained for newbs, and trying to steal some code will result in copy-pasta..

Please help!

Slakah 05-17-08 08:58 AM

Code:

CreateFrame('Button', <Frame Name>, Minimap)
And use trig to place the button around the minimap, using :SetPoint(). Maybe take a look at Bongos 3 Minimap code.

hipjipp 05-17-08 11:44 AM

Man i suck at coding at times.. I read, ripped, scratched, petted and mended the code from bongos3's minimap code. But i can't make the damn button appear. I get no errors, but nothing shows. I'll paste the code here. (Yes, i made it orange for easy read).
Code:

local MinimapButton = CreateFrame('Button', "MainMenuBarToggler", Minimap)

function MinimapButton:Load()
    self:SetFrameStrata('HIGH')
    self:SetWidth(31)
    self:SetHeight(31)
    self:SetFrameLevel(8)
    self:RegisterForClicks('anyUp')
    self:SetHighlightTexture('Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight')

    local overlay = self:CreateTexture(nil, 'OVERLAY')
    overlay:SetWidth(53)
    overlay:SetHeight(53)
    overlay:SetTexture('Interface\\Minimap\\MiniMap-TrackingBorder')
    overlay:SetPoint('TOPLEFT')

    local icon = self:CreateTexture(nil, 'BACKGROUND')
    icon:SetWidth(20)
    icon:SetHeight(20)
    icon:SetTexture('Interface\\Icons\\Spell_ChargeNegative')
    icon:SetTexCoord(0.05, 0.95, 0.05, 0.95)
    icon:SetPoint('TOPLEFT', 7, -5)
    self.icon = icon

    self:SetScript('OnClick', self.OnClick)

    self:SetPoint("BOTTOMRIGHT", Minimap, "BOTTOMRIGHT", -2, 2)
end
   
function MinimapButton:OnClick(button)
    if button == 'LeftButton' then
        MainMenuBar:ClearAllPoints()
        MainMenuBar:SetPoint("BOTTOM", UIParent, "BOTTOM", 0, -300)
    elseif button == 'RightButton' then
        MainMenuBar:ClearAllPoints()
        MainMenuBar:SetPoint("BOTTOM", UIParent, "BOTTOM", 0, 10)
    end
end

Edit: Changed the color of the code and cleaned it up a bit for people wanting to steal it...

Slakah 05-17-08 12:15 PM

You will undoubtably kick yourself for this but add
Code:

MinimapButton:Load()
to the end.

Also why have you got
Code:

local B = CreateFrame('Button', "MainMenuBarToggler", Minimap)
local MinimapButton = B

wouldn't
Code:

local MinimapButton = CreateFrame('Button', "MainMenuBarToggler", Minimap)
achieve exactly the same thing?

and you have no drag enter leave etc. functions so arn't all the :SetScript() pointless
Code:

    self:SetScript('OnEnter', self.OnEnter)
    self:SetScript('OnLeave', self.OnLeave)
    self:SetScript('OnClick', self.OnClick)
    self:SetScript('OnDragStart', self.OnDragStart)
    self:SetScript('OnDragStop', self.OnDragStop)
    self:SetScript('OnMouseDown', self.OnMouseDown)
    self:SetScript('OnMouseUp', self.OnMouseUp)

appart from
Code:

    self:SetScript('OnClick', self.OnClick)

and finally you have already set the parent of the button to Minimap in
Code:

CreateFrame('Button', "MainMenuBarToggler", Minimap)
so why do you do it in :Load()
Code:

self:SetParent(Minimap)

hipjipp 05-17-08 12:30 PM

*Slaps forehead* Yeah.. Gonna fix it now.. Really long day, thanks for helping me even though i keep asking newb questions.

Edit: about all the extra unneeded code, i tried some of them since i couldnt find what was causing (or in this case, not causing) the button to not appear. And the extra script code, i just didn't think about it at the time.

Sepioth 05-17-08 02:14 PM

Quote:

Originally Posted by hipjipp (Post 91293)
(Yes, i made it orange for easy read).

FYI ... it's not easy to read on the Light version of the website cause the background is a beige color. Just thought I would let you know :D

hipjipp 05-18-08 09:31 AM

Fixed it, cleaned the code and it will be on the site later tonight for people wanting to use it. =)

Nuckin 02-03-09 08:43 AM

I hate to bump an old thread, but how well does this script work? achieve basic minimap button functionality in 30000+ interface?

If so I would love to integrate this into my addon

Arabeth 02-03-09 03:48 PM

I looked at this same thread just this week for exactly the same reason. Other than me modifying it to handle right clicks only and changing the background texture, the code works a treat.

Edit:Not that modifying it caused a problem, just that's what I did to the code.

eliljey 03-01-09 05:49 PM

I'm still coming up to speed on all this stuff and am not able to get this to work. I can only assume that this is because the code is taken out of context so I'm putting it in the wrong place or handling it wrong.

First, I get the CreateFrame line but where does that go? Anywhere in the body of the addon or does it need to live inside of a specific function?

Second, what calls the :Load() function for the button frame? Do I need to register an event for the frame to cause it to trigger the load?

DreamStorm 03-01-09 06:20 PM

In hipjipp's example, because the variable assigned to the frame has been declared 'local', I would place it at the top of the *.lua file; that is, outside any other function declaration or whatever. That way it can be referenced by any function/variable in that lua file. But only in that lua file.

If you declared it like this:

Code:

MinimapButton = CreateFrame('Button', "MainMenuBarToggler", Minimap);
then 'MinimapButton' would be a global variable and could effectively be seen by your and (technically) any other addon you were running in your current WoW session. In that case it actually wouldn't mater where you put the CreateFrame. It is a matter of what is called scope. Reading this will give you a good idea.

As to the second part, you are correct in thinking you need an event to fire the :Load() method. Something like this:

Code:

MinimapButton:RegisterEvent("PLAYER_ENTERING_WORLD");

MinimapButton:SetScript("OnEvent", function (self, event, addon)

    if(event == "PLAYER_ENTERING_WORLD") then
            MinimapButton:Load();
            MinimapButton:UnregisterEvent("PLAYER_ENTERING_WORLD");
            OnEvent = nil;
    end

end);

In the example above, you don't strictly need the conditional that checks for the type of event as there is only one registered, but you could have more events registered, in which case you would need a series of conditionals to allow the right code to fire from the appropriate event. Hope that helps.

eliljey 03-02-09 03:26 AM

Thanks for all the help. I wasn't able to get it to work using PLAYER_ENTERING_WORLD but ADDON_LOADED worked. Close enough. Now to figure out how to let the user move it around the minimap....

DreamStorm 03-02-09 11:24 AM

Quote:

Originally Posted by eliljey (Post 118783)
Thanks for all the help. I wasn't able to get it to work using PLAYER_ENTERING_WORLD but ADDON_LOADED worked. Close enough. Now to figure out how to let the user move it around the minimap....

Hmmm. I was wondering why that would be as it works for me; until I looked at my own code and I realised I had copy pasted the wrong OnEvent handler function (I modified it after pasting but not the args). My actual button code is this:

Code:

--Create the button
dStormButton = CreateFrame("Button", "dStormButton", UIParent, "SecureActionButtonTemplate");

--Register the appropriate event
dStormButton:RegisterEvent("PLAYER_ENTERING_WORLD");

--Set the functionality for the event
dStormButton:SetScript("OnEvent", function()
    dStormButton:initialiseButton();
    dStormButton:UnregisterEvent("PLAYER_ENTERING_WORLD");
    OnEvent = nil;
end);

Can anyone tell me if the fact that the above code has no arguments to the scripted function (compared to the code I posted last) would make a difference in whether or not it would run under PLAYER_ENTERING_WORLD?

Thanks in advance.

P.S. Sorry for any duff advice! :-(


All times are GMT -6. The time now is 02:29 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI