-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorld.lua
More file actions
267 lines (228 loc) · 7.62 KB
/
Copy pathHelloWorld.lua
File metadata and controls
267 lines (228 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
--
-- A simple Slash command
-- This can be used to access the Addon functions in-game
-- by the use of a /<command> <parameters> format
--
SLASH_HELLO_WORLD_SHOW1 = "/hwshow"
SLASH_HELLO_WORLD_SHOW2 = "/helloworldshow" -- an alias to /hwshow
SlashCmdList["HELLO_WORLD_SHOW"] = function (msg)
HelloWorld(msg)
end
--
-- References used for the current framework
-- Button Generator - http://wowprogramming.com/snippets/LOL_CLASS_31
-- Quantity Box - http://eu.battle.net/wow/en/forum/topic/1622897526
-- Dropdown Box - http://wowprogramming.com/snippets/Create_UI-styled_dropdown_menu_10
--
local scale = 1
local prevpos = false
local itemList = {};
local timerFrame = nil;
local advertiseInterval = 5.0;
local timeSinceLastAdvertisement = 0
local quantityTypes = {
"Individual", -- ID 1
"Stacks", -- ID 2
}
--
-- Advertise the Item into Trade Chat (Channel 2), when the Icon is clicked
--
local function advertiseItem(itemID, quantity, quantityType)
local index, name = GetChannelName(2) -- It finds Trade is a channel at index 2
local quantityString = (quantityType == 2 and " Stack(s)." or ".")
if (index ~= nil) then
local itemName, itemLink = GetItemInfo(itemID)
SendChatMessage("WTS "..itemLink.." x "..quantity..quantityString, "CHANNEL", nil, index);
end
end
--
-- Avertise a list of items, each item will be displayed one after another
-- in the chat window in Trade Channel
--
local function advertiseItems(itemList)
for itemID in pairs(itemList) do
local editBox = itemID.."-editBox"
local dropdownBox = itemID.."-dropdownBox"
advertiseItem(itemID, _G[editBox]:GetNumber(), UIDropDownMenu_GetSelectedID(_G[dropdownBox]))
end
if (timerFrame == nil) then
timerFrame = CreateFrame("frame")
advertiseInterval = 5.0;
timerFrame:SetScript("OnUpdate", function(self,elapsed) advertiseItemsPeriodic(self,elapsed,itemList) end)
end
end
--
-- OnUpdate Callback for a timer frame, which advertises the items in the trade
-- channel in a fixed periodic interval
--
function advertiseItemsPeriodic(self, elapsed, itemList)
timeSinceLastAdvertisement = timeSinceLastAdvertisement + elapsed;
if(itemList ~= nil) then
if (timeSinceLastAdvertisement > advertiseInterval) then
advertiseItems(itemList)
timeSinceLastAdvertisement = 0;
end
end
end
--
-- Stub function to stop advertisement
-- Currently just sets a huge time interval to prevent advertisement spam
--
function stopAdvertise()
advertiseInterval = 50000000000.0;
end
--
-- Callback for whenever the drop down changes
--
local function dropdownBoxOnClick(self, frame)
UIDropDownMenu_SetSelectedID(frame, self:GetID())
end
--
-- Initialization of the dropdown and filling up with values
--
local function initializeDropdownBox(self, level)
local info = UIDropDownMenu_CreateInfo()
for k,v in pairs(quantityTypes) do
info = UIDropDownMenu_CreateInfo()
info.text = v
info.value = v
info.func = function(this) dropdownBoxOnClick(this, self) end
UIDropDownMenu_AddButton(info, level)
end
end
--
-- Generates a button containing the icon of the tooltip data captured
-- and also a Quantity Box to enter quantities
--
-- TODO:
-- 1. Better alignment of generated rows of Buttons and Boxes
-- 2. Table to store and retrieve Data after logging off
-- 3. Implement basic trading logic (yet to be split into micro tasks)
--
local function generateButton(itemIcon, itemID)
if itemList[itemID] == true then
return;
end
local button = CreateFrame("Button", itemID.."-button", HelloWorldForm, "ActionButtonTemplate")
local editBox = CreateFrame("EditBox", itemID.."-editBox", HelloWorldForm, "InputBoxTemplate")
local dropdownBox = CreateFrame("Button", itemID.."-dropdownBox", HelloWorldForm, "UIDropDownMenuTemplate")
-- Initialize each of the UI elements
button:SetScale(scale)
editBox:SetWidth(35)
editBox:SetHeight(50)
editBox:SetAutoFocus(false)
editBox:SetNumeric(true)
editBox:SetNumber(1)
dropdownBox:ClearAllPoints()
dropdownBox:Show()
UIDropDownMenu_Initialize(dropdownBox, initializeDropdownBox)
UIDropDownMenu_SetWidth(dropdownBox, 100);
UIDropDownMenu_SetButtonWidth(dropdownBox, 124)
UIDropDownMenu_SetSelectedID(dropdownBox, 1)
UIDropDownMenu_JustifyText(dropdownBox, "LEFT")
if not prevpos then
button:SetPoint("TOPLEFT",HelloWorldForm,"TOPLEFT",17,-30)
editBox:SetPoint("TOPLEFT",HelloWorldForm,"TOPLEFT",73,-23.5)
dropdownBox:SetPoint("TOPLEFT",HelloWorldForm,"TOPLEFT",130,-30)
else
button:SetPoint("TOP",prevpos,"BOTTOM",0,-4)
--editBox:SetPoint("TOP",prevposBox,"BOTTOM",0, 2)
editBox:SetPoint("TOPLEFT",prevpos,"TOPLEFT", 55, -29)
dropdownBox:SetPoint("TOP",prevposDropdownBox,"BOTTOM",0,-8)
end
_G[button:GetName().."Icon"]:SetTexture(itemIcon)
_G[button:GetName().."Icon"]:SetTexCoord(0, 1, 0, 1)
button:SetScript("OnClick", function() advertiseItem(itemID, editBox:GetNumber(), UIDropDownMenu_GetSelectedID(dropdownBox)) end)
editBox:SetScript("OnEnterPressed", function (self)
editBox:ClearFocus(); -- clears focus from editbox, (unlocks key bindings, so pressing W makes your character go forward.
end );
editBox:Show()
prevpos = itemID.."-button"
prevposBox = itemID.."-editBox"
prevposDropdownBox = itemID.."-dropdownBox"
itemList[itemID] = true
advertiseButton:SetScript("OnClick", function() advertiseItems(itemList) end)
stopAdvertiseButton:SetScript("OnClick", stopAdvertise)
end
--
-- Stub code for the trade chat parser
--
local function tradeChatParser(self, event, ...)
local message, author = ...
local channelName = select(4, ...)
local channelNo = select(8, ...)
--print(author.." "..message.." "..channelName.." "..channelNo)
end
--
-- Initializes the hooks and callbacks for watching tradechat, the callback
-- is a parser to extract potential trading information
--
local function tradeChatWatcher()
local frame = CreateFrame("Frame");
frame:RegisterEvent("CHAT_MSG_CHANNEL")
frame:SetScript("OnEvent", tradeChatParser)
end
--
-- Captures the data from the tooltip
--
local function showTooltip(self, linkData)
local linkType, itemID = string.split(":", linkData)
if(linkType == 'item') then
itemIcon = GetItemIcon(itemID);
generateButton(itemIcon, itemID);
end
end
--
-- Not used a stub function
--
local function hideTooltip(...)
-- print(...)
end
--
-- Initializes all the events and handlers for a given frame
--
local function setOrHookHandler(frame, event, handler)
if frame:GetScript(event) then -- Checks if the event has a handler
frame:HookScript(event, handler) -- if not we hook out handler
else
frame:SetScript(event, handler) -- else set our function has the handler
end
end
--
-- Initializes the hooks and handlers for capturing information
-- from an Item Hyperlink
--
local function hoverToolTip()
-- Creates copy of "General Chat Frame"
local frame = getglobal("ChatFrame"..1);
if frame then
setOrHookHandler(frame,"onHyperlinkClick", showTooltip)
setOrHookHandler(frame,"OnHyperlinkLeave", hideTooltip)
end
end
--
-- A simple function
--
function HelloWorld(msg)
local str = str or msg;
-- print("Hello, Universe!"); -- Simple Console Message
-- message("Hello, Universe!"); -- Simple PopUp Box
-- Controls the display of the frame based on the parameter passed to the
-- slash command
if(str == "show") then
HelloWorldForm:Show();
else if(str == "hide") then
HelloWorldForm:Hide();
end
end
-- Display a message if the string passed is not empty
if (str ~= "") then
print("You passed " .. msg);
end
-- Initialize the Tooltip Information Capture
hoverToolTip()
-- Sets the backdrop to be transparent
HelloWorldForm:SetBackdrop(StaticPopup1:GetBackdrop())
--- Initialize the trade chat watcher
tradeChatWatcher()
end