WoWInterface SVN TactikSOD

[/] [branches/] [MyMinimapButton.lua] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 StingerSoft-59681
--[[ MyMinimapButton v0.7
2 StingerSoft-59681
 
3 StingerSoft-59681
        This is an embedded library intended to be used by other mods.
4 StingerSoft-59681
        It's not a standalone mod.
5 StingerSoft-59681
 
6 StingerSoft-59681
        See MyMinimapButton_API_readme.txt for more info.
7 StingerSoft-59681
]]
8 StingerSoft-59681
 
9 StingerSoft-59681
local version = 1.0
10 StingerSoft-59681
 
11 StingerSoft-59681
if not MyMinimapButton or MyMinimapButton.Version<version then
12 StingerSoft-59681
 
13 StingerSoft-59681
  MyMinimapButton = {
14 StingerSoft-59681
 
15 StingerSoft-59681
        Version = version,              -- for version checking
16 StingerSoft-59681
 
17 StingerSoft-59681
        -- Dynamically create a button
18 StingerSoft-59681
        --   modName = name of the button (mandatory)
19 StingerSoft-59681
        --   modSettings = table where SavedVariables are stored for the button (optional)
20 StingerSoft-59681
        --   initSettings = table of default settings (optional)
21 StingerSoft-59681
        Create = function(self,modName,modSettings,initSettings)
22 StingerSoft-59681
                if not modName or getglobal(modName.."MinimapButton") then
23 StingerSoft-59681
                        return
24 StingerSoft-59681
                end
25 StingerSoft-59681
                initSettings = initSettings or {}
26 StingerSoft-59681
                modSettings = modSettings or {}
27 StingerSoft-59681
                self.Buttons = self.Buttons or {}
28 StingerSoft-59681
 
29 StingerSoft-59681
                local frameName = modName.."MinimapButton"
30 StingerSoft-59681
                local frame = CreateFrame("Button",frameName,Minimap)
31 StingerSoft-59681
                frame:SetWidth(31)
32 StingerSoft-59681
                frame:SetHeight(31)
33 StingerSoft-59681
                frame:SetFrameStrata("LOW")
34 StingerSoft-59681
                frame:SetToplevel(1) -- enabled in 1.10.2
35 StingerSoft-59681
                frame:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
36 StingerSoft-59681
                frame:SetPoint("TOPLEFT",Minimap,"TOPLEFT")
37 StingerSoft-59681
                local icon = frame:CreateTexture(frameName.."Icon","BACKGROUND")
38 StingerSoft-59681
                icon:SetTexture(initSettings.icon or "Interface\\Icons\\INV_Misc_QuestionMark")
39 StingerSoft-59681
                icon:SetWidth(20)
40 StingerSoft-59681
                icon:SetHeight(20)
41 StingerSoft-59681
                icon:SetPoint("TOPLEFT",frame,"TOPLEFT",7,-5)
42 StingerSoft-59681
                local overlay = frame:CreateTexture(frameName.."Overlay","OVERLAY")
43 StingerSoft-59681
                overlay:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
44 StingerSoft-59681
                overlay:SetWidth(53)
45 StingerSoft-59681
                overlay:SetHeight(53)
46 StingerSoft-59681
                overlay:SetPoint("TOPLEFT",frame,"TOPLEFT")
47 StingerSoft-59681
 
48 StingerSoft-59681
                frame:RegisterForClicks("LeftButtonUp","RightButtonUp")
49 StingerSoft-59681
                frame:SetScript("OnClick",self.OnClick)
50 StingerSoft-59681
 
51 StingerSoft-59681
                frame:SetScript("OnMouseDown",self.OnMouseDown)
52 StingerSoft-59681
                frame:SetScript("OnMouseUp",self.OnMouseUp)
53 StingerSoft-59681
                frame:SetScript("OnEnter",self.OnEnter)
54 StingerSoft-59681
                frame:SetScript("OnLeave",self.OnLeave)
55 StingerSoft-59681
 
56 StingerSoft-59681
                frame:RegisterForDrag("LeftButton")
57 StingerSoft-59681
                frame:SetScript("OnDragStart",self.OnDragStart)
58 StingerSoft-59681
                frame:SetScript("OnDragStop",self.OnDragStop)
59 StingerSoft-59681
 
60 StingerSoft-59681
                frame.tooltipTitle = modName
61 StingerSoft-59681
                frame.leftClick = initSettings.left
62 StingerSoft-59681
                frame.rightClick = initSettings.right
63 StingerSoft-59681
                frame.tooltipText = initSettings.tooltip
64 StingerSoft-59681
 
65 StingerSoft-59681
                local firstUse = 1
66 StingerSoft-59681
                for i in pairs(modSettings) do
67 StingerSoft-59681
                        firstUse = nil -- modSettings has been populated before
68 StingerSoft-59681
                end
69 StingerSoft-59681
                if firstUse then
70 StingerSoft-59681
                        -- define modSettings from initSettings or default
71 StingerSoft-59681
                        modSettings.drag = initSettings.drag or "CIRCLE"
72 StingerSoft-59681
                        modSettings.enabled = initSettings.enabled or 1
73 StingerSoft-59681
                        modSettings.position = initSettings.position or self:GetDefaultPosition()
74 StingerSoft-59681
                        modSettings.locked = initSettings.locked or nil
75 StingerSoft-59681
                end
76 StingerSoft-59681
                frame.modSettings = modSettings
77 StingerSoft-59681
 
78 StingerSoft-59681
                table.insert(self.Buttons,modName)
79 StingerSoft-59681
                self:SetEnable(modName,modSettings.enabled)
80 StingerSoft-59681
        end,
81 StingerSoft-59681
 
82 StingerSoft-59681
        -- Changes the icon of the button.
83 StingerSoft-59681
        --   value = texture path, ie: "Interface\\AddOn\\MyMod\\MyModIcon"
84 StingerSoft-59681
        SetIcon = function(self,modName,value)
85 StingerSoft-59681
                if value and getglobal(modName.."MinimapButton") then
86 StingerSoft-59681
                        getglobal(modName.."MinimapButtonIcon"):SetTexture(value)
87 StingerSoft-59681
                end
88 StingerSoft-59681
        end,
89 StingerSoft-59681
 
90 StingerSoft-59681
        -- Sets the left-click function.
91 StingerSoft-59681
        --   value = function
92 StingerSoft-59681
        SetLeftClick = function(self,modName,value)
93 StingerSoft-59681
                if value and getglobal(modName.."MinimapButton") then
94 StingerSoft-59681
                        getglobal(modName.."MinimapButton").leftClick = value
95 StingerSoft-59681
                end
96 StingerSoft-59681
        end,
97 StingerSoft-59681
 
98 StingerSoft-59681
        -- Sets the right-click function.
99 StingerSoft-59681
        --  value = function
100 StingerSoft-59681
        SetRightClick = function(self,modName,value)
101 StingerSoft-59681
                if value and getglobal(modName.."MinimapButton") then
102 StingerSoft-59681
                        getglobal(modName.."MinimapButton").rightClick = value
103 StingerSoft-59681
                end
104 StingerSoft-59681
        end,
105 StingerSoft-59681
 
106 StingerSoft-59681
        -- Sets the drag route.
107 StingerSoft-59681
        --   value = "CIRCLE" or "SQUARE"
108 StingerSoft-59681
        SetDrag = function(self,modName,value)
109 StingerSoft-59681
                local button = getglobal(modName.."MinimapButton")
110 StingerSoft-59681
                if button and (value=="CIRCLE" or value=="SQUARE") then
111 StingerSoft-59681
                        button.modSettings.drag = value
112 StingerSoft-59681
                        self:Move(modName)
113 StingerSoft-59681
                end
114 StingerSoft-59681
        end,
115 StingerSoft-59681
 
116 StingerSoft-59681
        -- Locks minimap button from moving
117 StingerSoft-59681
        --   value = 0/nil/false or 1/non-nil/true
118 StingerSoft-59681
        SetLock = function(self,modName,value)
119 StingerSoft-59681
                local button = getglobal(modName.."MinimapButton")
120 StingerSoft-59681
                if value==0 then value = nil end
121 StingerSoft-59681
                if button then
122 StingerSoft-59681
                        button.modSettings.locked = (value and 1) or nil
123 StingerSoft-59681
                end
124 StingerSoft-59681
        end,
125 StingerSoft-59681
 
126 StingerSoft-59681
        -- Enables or disables the minimap button
127 StingerSoft-59681
        --    value = 0/nil/false or 1/non-nil/true
128 StingerSoft-59681
        SetEnable = function(self,modName,value)
129 StingerSoft-59681
                local button = getglobal(modName.."MinimapButton")
130 StingerSoft-59681
                if value==0 then value = nil end
131 StingerSoft-59681
                if button then
132 StingerSoft-59681
                        button.modSettings.enabled = (value and 1) or nil
133 StingerSoft-59681
                        if value then
134 StingerSoft-59681
                                button:Show()
135 StingerSoft-59681
                                self:Move(modName,nil,1)
136 StingerSoft-59681
                        else
137 StingerSoft-59681
                                button:Hide()
138 StingerSoft-59681
                        end
139 StingerSoft-59681
                end
140 StingerSoft-59681
        end,
141 StingerSoft-59681
 
142 StingerSoft-59681
        -- Returns a setting of this minimap button
143 StingerSoft-59681
        --   setting = "LOCKED", "ENABLED", "DRAG" or "POSITION"
144 StingerSoft-59681
        GetSetting = function(self,modName,setting)
145 StingerSoft-59681
                local button = getglobal(modName.."MinimapButton")
146 StingerSoft-59681
                setting = string.lower(setting or "")
147 StingerSoft-59681
                if button and button.modSettings[setting] then
148 StingerSoft-59681
                        return button.modSettings[setting]
149 StingerSoft-59681
                end
150 StingerSoft-59681
        end,
151 StingerSoft-59681
 
152 StingerSoft-59681
        -- Sets the tooltip text.
153 StingerSoft-59681
        --   value = string (can include \n)
154 StingerSoft-59681
        SetTooltip = function(self,modName,value)
155 StingerSoft-59681
                local button = getglobal(modName.."MinimapButton")
156 StingerSoft-59681
                if button and value then
157 StingerSoft-59681
                        button.tooltipText = value
158 StingerSoft-59681
                end
159 StingerSoft-59681
        end,
160 StingerSoft-59681
 
161 StingerSoft-59681
        -- Moves the button.
162 StingerSoft-59681
        --  newPosition = degree angle to display the button (optional)
163 StingerSoft-59681
        --  force = force move irregardless of locked status
164 StingerSoft-59681
        Move = function(self,modName,newPosition,force)
165 StingerSoft-59681
                local button = getglobal(modName.."MinimapButton")
166 StingerSoft-59681
                if button and (not button.modSettings.locked or force) then
167 StingerSoft-59681
                        button.modSettings.position = newPosition or button.modSettings.position
168 StingerSoft-59681
                        local xpos,ypos
169 StingerSoft-59681
                        local angle = button.modSettings.position
170 StingerSoft-59681
                        if button.modSettings.drag=="SQUARE" then
171 StingerSoft-59681
                                xpos = 110 * cos(angle)
172 StingerSoft-59681
                                ypos = 110 * sin(angle)
173 StingerSoft-59681
                                xpos = math.max(-82,math.min(xpos,84))
174 StingerSoft-59681
                                ypos = math.max(-86,math.min(ypos,82))
175 StingerSoft-59681
                        else
176 StingerSoft-59681
                                xpos = 80 * cos(angle)
177 StingerSoft-59681
                                ypos = 80 * sin(angle)
178 StingerSoft-59681
                        end
179 StingerSoft-59681
                        button:SetPoint("TOPLEFT","Minimap","TOPLEFT",54-xpos,ypos-54)
180 StingerSoft-59681
                end
181 StingerSoft-59681
        end,
182 StingerSoft-59681
 
183 StingerSoft-59681
        -- Debug. Use no parameters to list all created buttons.
184 StingerSoft-59681
        Debug = function(self,modName)
185 StingerSoft-59681
                DEFAULT_CHAT_FRAME:AddMessage("MyMinimapButton version = "..self.Version)
186 StingerSoft-59681
                if modName then
187 StingerSoft-59681
                        DEFAULT_CHAT_FRAME:AddMessage("Button: \""..modName.."\"")
188 StingerSoft-59681
                        local button = getglobal(modName.."MinimapButton")
189 StingerSoft-59681
                        if button then
190 StingerSoft-59681
                                DEFAULT_CHAT_FRAME:AddMessage("positon = "..tostring(button.modSettings.position))
191 StingerSoft-59681
                                DEFAULT_CHAT_FRAME:AddMessage("enabled = "..tostring(button.modSettings.enabled))
192 StingerSoft-59681
                                DEFAULT_CHAT_FRAME:AddMessage("drag = "..tostring(button.modSettings.drag))
193 StingerSoft-59681
                                DEFAULT_CHAT_FRAME:AddMessage("locked = "..tostring(button.modSettings.locked))
194 StingerSoft-59681
                        else
195 StingerSoft-59681
                                DEFAULT_CHAT_FRAME:AddMessage("button not defined")
196 StingerSoft-59681
                        end
197 StingerSoft-59681
                else
198 StingerSoft-59681
                        DEFAULT_CHAT_FRAME:AddMessage("Buttons created:")
199 StingerSoft-59681
                        for i=1,table.getn(self.Buttons) do
200 StingerSoft-59681
                                DEFAULT_CHAT_FRAME:AddMessage("\""..self.Buttons[i].."\"")
201 StingerSoft-59681
                        end
202 StingerSoft-59681
                end
203 StingerSoft-59681
        end,
204 StingerSoft-59681
 
205 StingerSoft-59681
        --[[ Internal functions: do not call anything below here ]]
206 StingerSoft-59681
 
207 StingerSoft-59681
        -- this gets a new default position by increments of 20 degrees
208 StingerSoft-59681
        GetDefaultPosition = function(self)
209 StingerSoft-59681
                local position,found = 0,1,0
210 StingerSoft-59681
 
211 StingerSoft-59681
                while found do
212 StingerSoft-59681
                        found = nil
213 StingerSoft-59681
                        for i=1,table.getn(self.Buttons) do
214 StingerSoft-59681
                                if getglobal(self.Buttons[i].."MinimapButton").modSettings.position==position then
215 StingerSoft-59681
                                        position = position + 20
216 StingerSoft-59681
                                        found = 1
217 StingerSoft-59681
                                end
218 StingerSoft-59681
                        end
219 StingerSoft-59681
                        found = (position>340) and 1 or found -- leave if we've done full circle
220 StingerSoft-59681
                end
221 StingerSoft-59681
                return position
222 StingerSoft-59681
        end,
223 StingerSoft-59681
 
224 StingerSoft-59681
        OnMouseDown = function()
225 StingerSoft-59681
                getglobal(this:GetName().."Icon"):SetTexCoord(.1,.9,.1,.9)
226 StingerSoft-59681
        end,
227 StingerSoft-59681
 
228 StingerSoft-59681
        OnMouseUp = function()
229 StingerSoft-59681
                getglobal(this:GetName().."Icon"):SetTexCoord(0,1,0,1)
230 StingerSoft-59681
        end,
231 StingerSoft-59681
 
232 StingerSoft-59681
        OnEnter = function()
233 StingerSoft-59681
                GameTooltip_SetDefaultAnchor(GameTooltip,UIParent)
234 StingerSoft-59681
                GameTooltip:AddLine(this.tooltipTitle)
235 StingerSoft-59681
                GameTooltip:AddLine(this.tooltipText,.8,.8,.8,1)
236 StingerSoft-59681
                GameTooltip:Show()
237 StingerSoft-59681
        end,
238 StingerSoft-59681
 
239 StingerSoft-59681
        OnLeave = function()
240 StingerSoft-59681
                GameTooltip:Hide()
241 StingerSoft-59681
        end,
242 StingerSoft-59681
 
243 StingerSoft-59681
        OnDragStart = function()
244 StingerSoft-59681
                MyMinimapButton:OnMouseDown()
245 StingerSoft-59681
                this:LockHighlight()
246 StingerSoft-59681
                this:SetScript("OnUpdate",MyMinimapButton.OnUpdate)
247 StingerSoft-59681
        end,
248 StingerSoft-59681
 
249 StingerSoft-59681
        OnDragStop = function()
250 StingerSoft-59681
                this:SetScript("OnUpdate",nil)
251 StingerSoft-59681
                this:UnlockHighlight()
252 StingerSoft-59681
                MyMinimapButton:OnMouseUp()
253 StingerSoft-59681
        end,
254 StingerSoft-59681
 
255 StingerSoft-59681
        OnUpdate = function()
256 StingerSoft-59681
                local xpos,ypos = GetCursorPosition()
257 StingerSoft-59681
                local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom()
258 StingerSoft-59681
                xpos = xmin-xpos/Minimap:GetEffectiveScale()+70
259 StingerSoft-59681
                ypos = ypos/Minimap:GetEffectiveScale()-ymin-70
260 StingerSoft-59681
                this.modSettings.position = math.deg(math.atan2(ypos,xpos))
261 StingerSoft-59681
                local modName = string.gsub(this:GetName() or "","MinimapButton$","")
262 StingerSoft-59681
                MyMinimapButton:Move(modName)
263 StingerSoft-59681
        end,
264 StingerSoft-59681
 
265 StingerSoft-59681
        OnClick = function()
266 StingerSoft-59681
                if arg1=="LeftButton" and this.leftClick then
267 StingerSoft-59681
                        this.leftClick()
268 StingerSoft-59681
                elseif arg1=="RightButton" and this.rightClick then
269 StingerSoft-59681
                        this.rightClick()
270 StingerSoft-59681
                end
271 StingerSoft-59681
        end
272 StingerSoft-59681
 
273 StingerSoft-59681
  }
274 StingerSoft-59681
 
275 StingerSoft-59681
end