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("MEDIUM")
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 if (modSettings.drag==nil) then modSettings.drag=initSettings.drag or "CIRCLE" end
66 if (modSettings.enabled==nil) then modSettings.enabled =initSettings.enabled or 1 end
67 if (modSettings.position==nil) then modSettings.position = initSettings.position or self:GetDefaultPosition() end
68  
69 frame.modSettings = modSettings
70  
71 table.insert(self.Buttons,modName)
72 if frame.modSettings.enabled==1 or frame.modSettings.enabled=="ON" then
73 self:Enable(modName)
74 end
75 frame:Show()
76 end,
77  
78 -- Changes the icon of the button.
79 -- value = texture path, ie: "Interface\\AddOn\\MyMod\\MyModIcon"
80 SetIcon = function(self,modName,value)
81 if value and getglobal(modName.."MinimapButton") then
82 getglobal(modName.."MinimapButtonIcon"):SetTexture(value)
83 end
84 end,
85  
86 -- Sets the left-click function.
87 -- value = function
88 SetLeftClick = function(self,modName,value)
89 if value and getglobal(modName.."MinimapButton") then
90 getglobal(modName.."MinimapButton").leftClick = value
91 end
92 end,
93  
94 -- Sets the right-click function.
95 -- value = function
96 SetRightClick = function(self,modName,value)
97 if value and getglobal(modName.."MinimapButton") then
98 getglobal(modName.."MinimapButton").rightClick = value
99 end
100 end,
101  
102 -- Sets the drag route.
103 -- value = "NONE", "CIRCLE", or "SQUARE"
104 SetDrag = function(self,modName,value)
105 local button = getglobal(modName.."MinimapButton")
106 if button and (value=="NONE" or value=="CIRCLE" or value=="SQUARE") then
107 button.modSettings.drag = value
108 self:Move(modName)
109 end
110 end,
111  
112 -- Sets the tooltip text.
113 -- value = string (can include \n)
114 SetTooltip = function(self,modName,value)
115 local button = getglobal(modName.."MinimapButton")
116 if button and value then
117 button.tooltipText = value
118 end
119 end,
120  
121 -- Enables the button and shows it.
122 Enable = function(self,modName)
123 local button = getglobal(modName.."MinimapButton")
124 if button then
125 button:Show()
126 button.modSettings.enabled = 1
127 self:Move(modName)
128 end
129 end,
130  
131 -- Disables the button and hides it.
132 Disable = function(self,modName)
133 local button = getglobal(modName.."MinimapButton")
134 if button then
135 button:Hide()
136 button.modSettings.enabled =false
137 end
138 end,
139  
140 -- Moves the button.
141 -- newPosition = degree angle to display the button (optional)
142 Move = function(self,modName,newPosition)
143 local button = getglobal(modName.."MinimapButton")
144 if button and button.modSettings.drag~="NONE" then
145 button.modSettings.position = newPosition or button.modSettings.position
146 local xpos,ypos
147 local angle = button.modSettings.position
148 if button.modSettings.drag=="SQUARE" then
149 xpos = 110 * cos(angle)
150 ypos = 110 * sin(angle)
151 xpos = math.max(-82,math.min(xpos,84))
152 ypos = math.max(-86,math.min(ypos,82))
153 else
154 xpos = 80 * cos(angle)
155 ypos = 80 * sin(angle)
156 end
157 button:SetPoint("TOPLEFT","Minimap","TOPLEFT",54-xpos,ypos-54)
158 end
159 end,
160  
161 -- Debug. Use no parameters to list all created buttons.
162 Debug = function(self,modName)
163 DEFAULT_CHAT_FRAME:AddMessage("MyMinimapButton version = "..self.Version)
164 if modName then
165 DEFAULT_CHAT_FRAME:AddMessage("Button: \""..modName.."\"")
166 local button = getglobal(modName.."MinimapButton")
167 if button then
168 DEFAULT_CHAT_FRAME:AddMessage("positon = "..tostring(button.modSettings.position))
169 DEFAULT_CHAT_FRAME:AddMessage("enabled = "..tostring(button.modSettings.enabled))
170 DEFAULT_CHAT_FRAME:AddMessage("drag = "..tostring(button.modSettings.drag))
171 else
172 DEFAULT_CHAT_FRAME:AddMessage("button not defined")
173 end
174 else
175 DEFAULT_CHAT_FRAME:AddMessage("Buttons created:")
176 for i=1,table.getn(self.Buttons) do
177 DEFAULT_CHAT_FRAME:AddMessage("\""..self.Buttons[i].."\"")
178 end
179 end
180 end,
181  
182 --[[ Internal functions: do not call anything below here ]]
183  
184 -- this gets a new default position by increments of 20 degrees
185 GetDefaultPosition = function(self)
186 local position,found = 0,1,0
187  
188 while found do
189 found = nil
190 for i=1,table.getn(self.Buttons) do
191 if getglobal(self.Buttons[i].."MinimapButton").modSettings.position==position then
192 position = position + 20
193 found = 1
194 end
195 end
196 found = (position>340) and 1 or found -- leave if we've done full circle
197 end
198 return position
199 end,
200  
201 OnMouseDown = function()
202 getglobal(this:GetName().."Icon"):SetTexCoord(.1,.9,.1,.9)
203 end,
204  
205 OnMouseUp = function()
206 getglobal(this:GetName().."Icon"):SetTexCoord(0,1,0,1)
207 end,
208  
209 OnEnter = function()
210 local tooltip=getglobal(this.tooltipTitle.."MinimapButton")
211 --GameTooltip_SetDefaultAnchor(GameTooltip,UIParent)
212 GameTooltip:SetOwner(tooltip, "ANCHOR_LEFT");
213 GameTooltip:AddLine(this.tooltipTitle)
214 GameTooltip:AddLine(this.tooltipText,.8,.8,.8,1)
215 GameTooltip:Show()
216 end,
217  
218 OnLeave = function()
219 GameTooltip:Hide()
220 end,
221  
222 OnDragStart = function()
223 MyMinimapButton:OnMouseDown()
224 this:LockHighlight()
225 this:SetScript("OnUpdate",MyMinimapButton.OnUpdate)
226 end,
227  
228 OnDragStop = function()
229 this:SetScript("OnUpdate",nil)
230 this:UnlockHighlight()
231 MyMinimapButton:OnMouseUp()
232 end,
233  
234 OnUpdate = function()
235 local xpos,ypos = GetCursorPosition()
236 local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom()
237 xpos = xmin-xpos/Minimap:GetEffectiveScale()+70
238 ypos = ypos/Minimap:GetEffectiveScale()-ymin-70
239 this.modSettings.position = math.deg(math.atan2(ypos,xpos))
240 local modName = string.gsub(this:GetName() or "","MinimapButton$","")
241 MyMinimapButton:Move(modName)
242 end,
243  
244 OnClick = function()
245 if arg1=="LeftButton" and this.leftClick then
246 this.leftClick()
247 elseif arg1=="RightButton" and this.rightClick then
248 this.rightClick()
249 end
250 end
251  
252 }
253  
254 end