Thread Tools Display Modes
08-21-14, 04:50 PM   #1
melanorion
A Defias Bandit
Join Date: Aug 2014
Posts: 2
New in AddOn Developpment - First shot

Hello,
first, english is not my favorite language, sorry for mistakes .

I'm here to increase my addOn developpment skills, and i try a first shot. I think the code is awful, but i didn't find best practice to do some stuff.

The idea is simple : do a complete suite like elvui (for example), so i can see all sides of developpment.

At the beginning, i only want to modify general tooltip to add the mouseover ilevel. I made different search, look at different addon (SimpleILevel, AiL, InspectEquip-2.0.6, draiks-broker-ilevel-r56, EquippedAverageItemLevelTooltip-2.1.1) but no way to do something good.

Here is the code, next my different questions .

Code:
local E, L, V, P, G = unpack(select(2, ...)); --Inport: Engine, Locales, PrivateDB, ProfileDB, GlobalDB

local TT = E:NewModule('Tooltip', 'AceTimer-3.0', 'AceHook-3.0', 'AceEvent-3.0')
local ItemUpgradeInfo = LibStub("LibItemUpgradeInfo-1.0")
local gameToolTip = CreateFrame("Frame", "gameToolTip", Tooltip);

local showIlevel = true;

local slots = { "HeadSlot", "NeckSlot", "ShoulderSlot", "BackSlot", "ChestSlot",
    "WristSlot", "HandsSlot", "WaistSlot", "LegsSlot", "FeetSlot",
    "Finger0Slot", "Finger1Slot", "Trinket0Slot", "Trinket1Slot", "MainHandSlot",
    "SecondaryHandSlot" }

function TT:OnInitialize()

    E:Print("Module Tooltip initialization");

    gameToolTip:SetScript("OnEvent", Tooltip_OnEvent);
    gameToolTip:RegisterEvent("PLAYER_REGEN_ENABLED");
    gameToolTip:RegisterEvent("PLAYER_REGEN_DISABLED");

    self:HookScript(GameTooltip, "OnTooltipSetUnit", "Tooltip_HookSetUnit");
    --GameTooltip:HookScript("OnTooltipSetItem", Tooltip_HookSetItem);
    --ShoppingTooltip1:HookScript("OnTooltipSetItem", Tooltip_HookCompareItem);
    --ShoppingTooltip2:HookScript("OnTooltipSetItem", Tooltip_HookCompareItem2);
    --ItemRefTooltip:HookScript("OnTooltipSetItem", Tooltip_HookRefItem);

end

function TT:Tooltip_OnEvent(self, event, ...)
    if(event == "PLAYER_REGEN_ENABLED") then
        showIlevel = true;
    end
    if(event == "PLAYER_REGEN_DISABLED") then
        showIlevel = false;
    end
end

function TT:Tooltip_HookSetUnit()
    if(showIlevel) then
        local avgItemLevelEquipped = self:getAverageIlevel();
        if(avgItemLevelEquipped) then
            GameTooltip:AddLine("Ilevel : "..avgItemLevelEquipped,255,255,255);
        end
    end
end

function TT:getAverageIlevel()
    local unit = "mouseover";

    if unit and CanInspect(unit) and CheckInteractDistance(unit, 1) then
        NotifyInspect(unit);
        local playerName = UnitName(unit);

        local sum, count=0,0;

        for _,slot in pairs(slots) do
            local idSlot = GetInventorySlotInfo(slot);
            local itemId = GetInventoryItemID("mouseover", idSlot);
            if itemId then
                local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType,
                itemStackCount, itemEquipLoc, itemTexture, itemSellPrice = GetItemInfo(itemId);

                if itemLink then
                    local ilvl = ItemUpgradeInfo:GetUpgradedItemLevel(itemLink);
                    count = count + 1;
                    sum = sum + ilvl;
                end

            end
        end
        ClearInspectPlayer();
        if (sum or 0) >= (count or 0) and (count or 0) > 0 then
           return sum/count;
        else
        return 0;
        end
    end
end
It works, but not as better as possible. Transmog break all the result for example (it takes transmog ilevel and not original item).

I think here isn't any best practice, like when / whow declare my registerEvent.
When i over a player character. inspect frame do not show correctly, i have to try few times before it shows. Same for items in inspect frame's slots (they don't load).

I see hook, but don't find the exact goal.

After the code review, my wish is to add all tooltips to a frame, item's tooltip from bag or inspect with mouse anchor with more infos, and others like player character mouseover at the top of screen. Anyway that's the futur .

Thanks a lot .
  Reply With Quote
08-21-14, 09:53 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
First of all, whatever addon(s) you're looking at for guidance are horribly written, and I suggest you stop using them as examples of how to write Lua code or Wow addons.

Secondly,
Originally Posted by melanorion View Post
The idea is simple : do a complete suite like elvui (for example), so i can see all sides of developpment.
There's nothing simple about this idea, and nothing good about a bloated all-in-one suite like ElvUI. It causes more problems than it's worth unless you're an egomaniac who needs to pee on all the fire hydrants or spray-paint your name on all the walls.

You (and any potential users of your addons) will be be better off if you just write individual addons that each fill a specific purpose -- for example, have one addon that modifies the tooltip, and don't put any code into that addon that isn't related to modifying the tooltip. This way, people who want to use your tooltip addon aren't forced to also use your action bar addon, your unit frames addon, your bag addon, etc. and if a patch breaks your tooltip addon it doesn't also break everything else in your UI.

Originally Posted by melanorion View Post
local gameToolTip = CreateFrame("Frame", "gameToolTip", Tooltip)
If you're trying to add information to the existing tooltip, this won't achieve that goal. This creates a new tooltip frame. Even if you did want to create another tooltip frame, you should definitely not name it "gameToolTip" or anything else that's a near-exact match for an existing Blizzard frame name.

Code:
    gameToolTip:RegisterEvent("PLAYER_REGEN_ENABLED");
    gameToolTip:RegisterEvent("PLAYER_REGEN_DISABLED");
You don't need to create your own frame to receive events -- your AceAddon object already inherits AceEvent, so you can just register events directly on your addon object:

Code:
    self:RegisterEvent("PLAYER_REGEN_ENABLED")
    self:RegisterEvent("PLAYER_REGEN_DISABLED")
And this doesn't actually do anything:

Code:
    gameToolTip:SetScript("OnEvent", Tooltip_OnEvent);
... because it's looking for a function named Tooltip_OnEvent which doesn't exist anywhere in your code. You do have a method on your addon object with that name, but you'd need to refer to it as self.Tooltip_OnEvent here if you wanted to use it. However, since you should be getting rid of this confusing tooltip-like frame and registering events directly on your addon object, you can also get rid of this method.

Actually, on further investigation, you can get rid of those events, too, since all you're using them for is to toggle a boolean value, and instead of consulting that boolean, you can just check if the player is in combat directly when you're actually doing stuff:

Code:
function TT:Tooltip_HookSetUnit()
    if UnitAffectingCombat("player") then return end

    -- do things here
end
As for the rest, it probably won't work at all for units other than yourself, and if it does, it won't work consistently, since inspecting is not instant. For the player unit, you can just use GetAverageItemLevel to get your own average item level, and not worry about the inspection API. For other players, though, after you ask the game to inspect someone with NotifyInspect you need to wait until the INSPECT_READY event fires to signify that inspection data is available before you scan their items. There's also the problem of inspections being throttled, so if other addons are also calling NotifyInspect then some requests may be dropped.

bTooltip is a small tooltip addon that does inspect stuff, and its code didn't make me want to pour bleach into my eye sockets, so you may want to look at that as an example.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-22-14, 02:46 AM   #3
melanorion
A Defias Bandit
Join Date: Aug 2014
Posts: 2
Thx for reply .
i didn't think that do a complete suite is less powerful. Welcome to my garvage collector !

Can you link addons (you know) wich have good practice in they code ? I'll go analyze bToolTip, but if there is no best practices topic, if not many addons are good, how can we start coding ? i find the list of ace3 lib and example of using it.

Is there a topic which explain a generic skeleton addon ?
- How to init my addon
- Which event should i catch, why
- should i save data (savedVariable?), how, why
- it's an ui addon (skada for example), how manage frame / elements in frame

There is some questions i havn't an idea of solution ^^

In your signature are links i'll follow.

Thank's again.
  Reply With Quote
08-22-14, 05:57 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by melanorion View Post
Can you link addons (you know) wich have good practice in they code ? I'll go analyze bToolTip, but if there is no best practices topic, if not many addons are good, how can we start coding ? i find the list of ace3 lib and example of using it.
Haste, Tekkub, Industrial, Adirelle, Rabbit, Ammo all generally write good clean code, and obviously I'd recommend myself as well. :P Stick to smaller addons for general examples, as they'll be easier to understand and it'll be easier to ignore anything irrelevant. If you see a lot of select calls and semicolons and tables being created in functions, stay away.

I'd recommend learning how to write addons without using libraries first. That way you'll have a better understanding of how addons actually work, and then later you can make an informed decision about whether to use a library or not.

Originally Posted by melanorion View Post
Is there a topic which explain a generic skeleton addon ?
- How to init my addon
- Which event should i catch, why
- should i save data (savedVariable?), how, why
- it's an ui addon (skada for example), how manage frame / elements in frame
Use the ADDON_LOADED and/or PLAYER_LOGIN events to initialize your addon. When ADDON_LOADED fires with your addon's name as its first (and only) argument, that means that all of your addons files have been read, including its saved variables file(s). When PLAYER_LOGIN fires, that means that all information about your character is now available, including your GUID, talents, etc.

Whether or not you "should" save data seems like a pretty straightforward question to answer. If you want to save any data -- such as the position of a frame, the user's preferred font, a count of how many murlocs the player has killed, or any other information -- between sessions, save it. If you don't have any data to save, then you don't.

As for "managing frames" that's a pretty open-ended question. It really depends on what you want to do. If you want to show or hide a frame, show or hide a frame. If you want to show some text, create a font string and tell it what text to show. etc.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-22-14, 08:49 AM   #5
morpheusxeno
A Flamescale Wyrmkin
 
morpheusxeno's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 126
Originally Posted by Phanx View Post
unless you're an egomaniac who needs to pee on all the fire hydrants or spray-paint your name on all the walls.
AHHHHH HAHAHA I died laughing. X.X
__________________
My mother-in-law fell down a wishing well. I was amazed; I never knew they worked...
  Reply With Quote
08-22-14, 05:47 PM   #6
JDoubleU00
A Firelord
 
JDoubleU00's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 463
Originally Posted by morpheusxeno View Post
AHHHHH HAHAHA I died laughing. X.X
Glad I wan't drinking anything at the time.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » New in AddOn Developpment - First shot

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off