adam@0: local _, AskMrRobot = ... adam@0: adam@0: local unresolvedItemIds = {} adam@0: adam@0: -- Create a new class that inherits from a base class adam@0: function AskMrRobot.inheritsFrom( baseClass ) adam@0: adam@0: -- The following lines are equivalent to the SimpleClass example: adam@0: adam@0: -- Create the table and metatable representing the class. yellowfive@11: local new_class = { } adam@0: adam@0: -- Note that this function uses class_mt as an upvalue, so every instance adam@0: -- of the class will share the same metatable. adam@0: -- adam@0: -- function new_class:create(o) adam@0: -- o = o or {} adam@0: -- setmetatable( o, class_mt ) adam@0: -- return o adam@0: -- end adam@0: adam@0: -- The following is the key to implementing inheritance: adam@0: adam@0: -- The __index member of the new class's metatable references the adam@0: -- base class. This implies that all methods of the base class will adam@0: -- be exposed to the sub-class, and that the sub-class can override adam@0: -- any of these methods. adam@0: -- adam@0: if baseClass then adam@0: setmetatable( new_class, { __index = baseClass } ) adam@0: end adam@0: adam@0: return new_class adam@0: end adam@0: adam@0: local itemInfoFrame = nil; adam@0: adam@0: local function onGetItemInfoReceived(arg1, arg2, arg3) adam@0: -- since wow is awesome, it doesn't tell us *which* item id was just resolved, so we have to look at them all adam@0: for itemId, callbacks in pairs(unresolvedItemIds) do adam@0: -- attempt to get the item info AGAIN adam@0: local a, b, c, d, e, f, g, h, i, j, k = GetItemInfo(itemId) adam@0: -- if we got item info... adam@0: if a then adam@0: -- remove the callbacks from the list adam@0: unresolvedItemIds[itemId] = nil adam@0: adam@0: -- call each callback adam@0: for i = 1, #callbacks do adam@0: callbacks[i](a, b, c, d, e, f, g, h, i, j, k) adam@0: end adam@0: end adam@0: end adam@0: end adam@0: adam@0: adam@0: function AskMrRobot.RegisterItemInfoCallback(itemId, callback) adam@0: if not itemId then adam@0: return adam@0: end adam@0: adam@0: if not itemInfoFrame then adam@0: waitFrame = CreateFrame("Frame","WaitFrame", UIParent); adam@0: waitFrame:RegisterEvent("GET_ITEM_INFO_RECEIVED") adam@0: waitFrame:SetScript("OnEvent", onGetItemInfoReceived); adam@0: end adam@0: adam@0: adam@0: -- get the list of registered callbacks for this particular item adam@0: local list = unresolvedItemIds[itemId] adam@0: -- if there was a list, then just add the callback to the list adam@0: if list then adam@0: tinsert(list, callback) adam@0: else adam@0: -- there wasn't a list, so make a new one with this callback adam@0: unresolvedItemIds[itemId] = { callback } adam@0: end adam@0: end adam@0: adam@0: adam@0: -- initialize the Frame class (inherit from a dummy frame) adam@0: AskMrRobot.Frame = AskMrRobot.inheritsFrom(CreateFrame("Frame")) adam@0: adam@0: -- Frame contructor adam@0: function AskMrRobot.Frame:new(name, parentFrame, inheritsFrame) adam@0: -- create a new frame (if one isn't supplied) adam@0: local o = CreateFrame("Frame", name, parentFrame, inheritsFrame) adam@0: adam@0: -- use the Frame class adam@0: setmetatable(o, { __index = AskMrRobot.Frame }) adam@0: adam@0: -- return the instance of the Frame adam@0: return o adam@0: end