vanilla-wow-addons – Blame information for rev 1

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