Code refactor, skills and inventory data

Leandro Silva [09-18-18 - 23:54]
Code refactor, skills and inventory data
Filename
LeoAltholic.lua
LeoAltholic.xml
LeoAltholicUI.lua
diff --git a/LeoAltholic.lua b/LeoAltholic.lua
index b5e3015..b187944 100644
--- a/LeoAltholic.lua
+++ b/LeoAltholic.lua
@@ -333,8 +333,17 @@ function LeoAltholic.InitCharsList()
     local bag = SHARED_INVENTORY:GenerateFullSlotData(nil,BAG_WORN,BAG_BACKPACK)
     char.inventory[BAG_WORN] = {}
     char.inventory[BAG_BACKPACK] = {}
+    local first = true
     for _, data in pairs(bag) do
-        char.inventory[data.bagId][data.slotIndex] = GetItemLink(data.bagId, data.slotIndex)
+        if first then
+            d(data)
+            first = false
+        end
+        char.inventory[data.bagId][data.slotIndex] = {
+            link = GetItemLink(data.bagId, data.slotIndex),
+            name = data.name,
+            count = data.stackCount
+        }
     end

     if char.stats == nil then char.stats = {} end
@@ -367,6 +376,21 @@ function LeoAltholic.GetCharacters()
     return playerLines
 end

+function LeoAltholic.GetItems(char)
+    local itemLines =  {}
+    local i = 1
+    for k, v in pairs(char.inventory[BAG_BACKPACK]) do
+        if k == nil then return end
+        itemLines[i] = copy(v)
+        i = i + 1
+    end
+    table.sort(itemLines, function(a, b)
+        return a.name < b.name
+    end)
+
+    return itemLines
+end
+
 function LeoAltholic.ProcessQueue()
     for x,data in pairs(LeoAltholic.timerQueue) do
         if GetDiffBetweenTimeStamps(data.time, GetTimeStamp()) <= 0 then
@@ -483,6 +507,7 @@ function LeoAltholic.Initialize()
     LeoAltholic.DisplayCharacterFrames()
     LeoAltholic.GetTimer()

+    --[[
     SLASH_COMMANDS["/rr"] = function(cmd)
         ReloadUI()
     end
@@ -491,6 +516,7 @@ function LeoAltholic.Initialize()
         LeoAltholic.savedVariables.CharList = {}
         ReloadUI()
     end
+    ]]

     SLASH_COMMANDS["/leoalt"] = function(cmd)
         LeoAltholic:ShowUI()
diff --git a/LeoAltholic.xml b/LeoAltholic.xml
index cfa5e3e..c1bdfbf 100644
--- a/LeoAltholic.xml
+++ b/LeoAltholic.xml
@@ -666,7 +666,7 @@
                 <Button name="$(parent)ListButton" clickSound="Click">
                     <Anchor point="TOPRIGHT" relativeTo="$(parent)" relativePoint="TOPRIGHT" offsetX="-10" offsetY="35"/>
                     <Dimensions x="40" y="40"/>
-                    <OnClicked> LeoAltholic.ShowInventoryUI() </OnClicked>
+                    <!--<OnClicked> LeoAltholic.ShowInventoryUI() </OnClicked>-->
                     <Textures normal="/esoui/art/buttons/large_rightarrow_up.dds" mouseOver="/esoui/art/buttons/large_rightarrow_over.dds" />
                 </Button>
             </Controls>
@@ -834,18 +834,18 @@
         </Control>

         <TopLevelControl name="LeoAltholicInventoryWindow" mouseEnabled="true" movable="false" hidden="true" clampedToScreen="true">
-            <Dimensions x="300" y="700" />
+            <Dimensions x="350" y="700" />
             <Anchor point="TOPLEFT" relativeTo="LeoAltholicWindow" relativePoint="TOPRIGHT" offsetX="4" offsetY="0" />

             <Controls>
                 <Backdrop name="$(parent)BG" centerColor="000000" edgeColor="222222">
                     <Anchor point="TOPLEFT" relativePoint="TOPLEFT" relativeTo="$(parent)"/>
-                    <Dimensions x="300" y="700"/>
+                    <Dimensions x="350" y="700"/>
                     <Edge edgeSize="1"/>
                 </Backdrop>
                 <Backdrop name="$(parent)HeaderBG" centerColor="111111" edgeColor="222222">
                     <Anchor point="TOPLEFT" relativePoint="TOPLEFT" relativeTo="$(parent)" offsetX="8" offsetY="5"/>
-                    <Dimensions x="292" y="40"/>
+                    <Dimensions x="342" y="40"/>
                     <Edge edgeSize="1"/>
                 </Backdrop>
                 <Label name="$(parent)Title" text="Inventory List" color="39B027" font="ZoFontWinH3" wrapMode="ELLIPSIS"
@@ -868,9 +868,9 @@
                     </Controls>
                 </Button>

-                <Backdrop name="$(parent)List" tier="1" centerColor="000000" edgeColor="202020" hidden="true" clampedToScreen="true" movable="false" mouseEnabled="true" inherits="ZO_ScrollContainerBase">
-                    <Anchor point="TOPLEFT" relativePoint="TOPLEFT" relativeTo="LeoAltholicWindow" offsetX="0" offsetY="52"/>
-                    <Dimensions x="200" y="450"/>
+                <Backdrop name="$(parent)List" tier="1" centerColor="000000" edgeColor="202020" hidden="false" clampedToScreen="true" movable="false" mouseEnabled="true" inherits="ZO_ScrollContainerBase">
+                    <Anchor point="TOPLEFT" relativePoint="TOPLEFT" relativeTo="LeoAltholicInventoryWindow" offsetX="0" offsetY="52"/>
+                    <Dimensions x="350" y="650"/>
                     <Edge edgeSize="1"/>
                     <OnInitialized>ZO_Scroll_Initialize(self)</OnInitialized>
                     <Controls>
@@ -882,5 +882,22 @@
             </Controls>
         </TopLevelControl>

+        <Control name="LeoAltholicInventoryTemplate" virtual="true" mouseEnabled="true" horizontalAlignment="LEFT" verticalAlignment="CENTER">
+            <Dimensions x="350" y="30" />
+            <Controls>
+                <Texture name="$(parent)BG" inherits="ZO_ThinListBgStrip" />
+                <Label name="$(parent)Item" visible="true" mouseEnabled="true" font="ZoFontWinH4" color="ffffff" inheritAlpha="true" verticalAlignment="CENTER" horizontalAlignment="LEFT" resizeToFitDescendents="true">
+                    <Dimensions x="336" y="30" />
+                    <Anchor point="TOPLEFT" relativeTo="$(parent)" relativePoint="TOPLEFT" offsetX="8" offsetY="2" />
+                </Label>
+            </Controls>
+        </Control>
+
+        <TopLevelControl name="LeoAltholicItemToolTipTopLevel" inherits="TooltipTopLevel">
+            <Controls>
+                <Tooltip name="LeoAltholicItemToolTip" inherits="ZO_ItemIconTooltip" />
+            </Controls>
+        </TopLevelControl>
+
     </Controls>
 </GuiXml>
diff --git a/LeoAltholicUI.lua b/LeoAltholicUI.lua
index 06e4152..cb686de 100644
--- a/LeoAltholicUI.lua
+++ b/LeoAltholicUI.lua
@@ -21,9 +21,45 @@ function LeoAltholic.RestorePosition()
     LeoAltholicWindow:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, left, top)
 end

-function LeoAltholic.ShowInventoryUI()
+function LeoAltholic.ShowInventoryUI(charId)
     SCENE_MANAGER:ShowTopLevel(LeoAltholicInventoryWindow)
     LeoAltholicInventoryWindow:SetHidden(false)
+    local char = LeoAltholic.GetCharacters()[charId]
+    local sc = WINDOW_MANAGER:GetControlByName("LeoAltholicInventoryWindowListScrollChild")
+    sc:SetHidden(false)
+
+    local i = 1
+    for x = 1, sc:GetNumChildren() do
+        sc:GetChild(x):SetHidden(true)
+    end
+    for _, item in pairs(LeoAltholic.GetItems(char)) do
+        local row = WINDOW_MANAGER:GetControlByName("LeoAltholicInventoryWindowRow" .. i)
+        if not row then
+            row = CreateControlFromVirtual("LeoAltholicInventoryWindowRow" .. i, sc, "LeoAltholicInventoryTemplate")
+        end
+        local control = row:GetNamedChild("Item")
+        control:SetHidden(false)
+        local qty = ""
+        if item.count > 1 then
+            qty = " (" .. item.count ..")"
+        end
+        control:SetText(zo_iconFormat(GetItemLinkIcon(item.link), 25, 25) .." ".. item.link .. qty)
+        control:SetHandler("OnMouseEnter",
+                function(self)
+                    InitializeTooltip(LeoAltholicItemToolTip, LeoAltholicInventoryWindow, TOPLEFT, -450, 50, TOPLEFT)
+                    LeoAltholicItemToolTip:SetLink(item.link)
+                end
+        )
+        control:SetHandler("OnMouseExit",
+                function(self)
+                    ClearTooltip(LeoAltholicItemToolTip)
+                end
+        )
+        row:SetAnchor(TOPLEFT, sc, TOPLEFT, 2, (i - 1) * 30)
+        row:SetHidden(false)
+        i = i + 1
+    end
+    sc:SetHeight(i * 30)
 end

 function LeoAltholic.HideInventoryUI()
@@ -93,6 +129,9 @@ function LeoAltholic.InitializeCharacterFrames()
                     label:SetHandler('OnMouseEnter', function(self) LeoAltholic.TooltipChampionSkill(self, true, x, dAttribute, i) end)
                     label:SetHandler('OnMouseExit', function(self) LeoAltholic.TooltipChampionSkill(self, false) end)
                 end
+            elseif panel == "Inventory" then
+                control = row:GetNamedChild("ListButton")
+                control:SetHandler('OnClicked', function() LeoAltholic.ShowInventoryUI(x) end)
             elseif panel == "Research" then
                 for _,craft in pairs(LeoAltholic.craftResearch) do
                     local label = row:GetNamedChild("Craft"..craft.."Label")