lotrointerface.com
Search Downloads

LoTROInterface SVN SortPack

[/] [trunk/] [JackdawPlugins/] [SortPack/] [Bag.lua] - Rev 111

Go to most recent revision | Compare with Previous | Blame | View Log

--Get ready for the SortBag data.
if SB==nil then

        SB={}

        SB.x=100
        SB.y=100
        SB.icx=4
        SB.icy=3
        SB.override=false       --Override default bags?
        SB.LeftClick="bag"
        SB.labelwidth=80        --Width of the text label for the group.
        SB.font=Turbine.UI.Lotro.Font.TrajanPro14

end
if SB.font==nil then SB.font="TrajanPro14" end

pad={top=30,side=15,bottom=15}

SB.width=SB.labelwidth+36*SB.icx+2*pad.side
SB.maxheight=Turbine.UI.Display.GetHeight()                     --Legacy here of how it used to be set; 36*SB.icy+pad.top+pad.bottom
SB.fontsize=string.sub(SB.font,-2)
if SB.fontsize=="ed" then SB.fontsize=13 else SB.fontsize=tonumber(SB.fontsize) end


SortBag = Turbine.UI.Lotro.Window()
        SortBag:SetPosition( SB.x,SB.y )
        SortBag:SetSize(SB.width,SB.maxheight)
        SortBag:SetText("SortBag")      

SortBag.ListBox = Turbine.UI.ListBox()
        SortBag.ListBox:SetParent( SortBag )
        SortBag.ListBox:SetPosition( pad.side, pad.top )
        SortBag.ListBox:SetMaxItemsPerLine(2)
        SortBag.ListBox:SetOrientation( Turbine.UI.Orientation.Horizontal )
        
        SortBag.ScrollBar=Turbine.UI.Lotro.ScrollBar()
                SortBag.ScrollBar:SetOrientation( Turbine.UI.Orientation.Vertical )
                SortBag.ListBox:SetVerticalScrollBar( SortBag.ScrollBar )
                SortBag.ScrollBar:SetParent(SortBag)
                SortBag.ScrollBar:SetPosition(SB.labelwidth+SB.icx*36+10,pad.top+5)
                SortBag.ScrollBar:SetWidth(10)
        
        SortBag.Resize=Turbine.UI.Control()
                SortBag.Resize:SetParent(SortBag)
                SortBag.Resize:SetZOrder(99)
                
                SortBag.ResizeBack=Turbine.UI.Control()
                SortBag.ResizeBack:SetParent(SortBag.Resize)
                SortBag.ResizeBack:SetBackColor(Turbine.UI.Color(0.7,0.3,0.3,1))
                SortBag.ResizeBack:SetVisible(false)
                SortBag.ResizeBack:SetMouseVisible(false)
                
                SortBag.Resize.MouseEnter=function() SortBag.ResizeBack:SetVisible(true) end
                SortBag.Resize.MouseLeave=function() SortBag.ResizeBack:SetVisible(false)  end
                
                SortBag.Resize.MouseDown=function ()
                        draggingr=true
                end
                SortBag.Resize.MouseUp=function ()
                        draggingr=false
                end
                SortBag.Resize.MouseMove=function(sender,args)
                        if draggingr then
                                local oldwidth=SB.width
                                SB.width,dummy=SortBag:GetMousePosition() --SB.width,SB.maxheight=SortBag:GetMousePosition()    Legacy - height used to be set-able too
                                
                                if SortBag:IsAltKeyDown() then
                                        SB.labelwidth=SB.width-oldwidth+SB.labelwidth
                                        if SB.labelwidth<30 then SB.labelwidth=30 end
                                else
                                        SB.icx=math.floor((SB.width-2*pad.side-SB.labelwidth)/36)
                                        SB.icy=math.floor((SB.maxheight-(pad.top+pad.bottom))/36)
                                        if SB.icx<3 then SB.icx=3 end
                                        if SB.icy<1 then SB.icy=1 end
                                end
                                        SB.width=36*SB.icx+2*pad.side+SB.labelwidth
                                        --SB.maxheight=36*SB.icy+pad.top+pad.bottom Legacy - height used to be set-able too
                                        SortBag:SetSize(SB.width,SB.maxheight)
                        end
                end     
                
        SortBag.SizeChanged=function()
        if SortBag:IsVisible() then
                SortBagSetSize()
                SetGroupBoxSize()
        end
end

--Set the sizes of all the areas of the bag.
function SortBagSetSize()
        SortBag.ListBox:SetSize( SB.labelwidth+SB.icx*36, SortBag:GetHeight()-(pad.top+pad.bottom))
        SortBag.ScrollBar:SetPosition(SB.labelwidth+SB.icx*36+10,pad.top+5)
        SortBag.ScrollBar:SetHeight(SortBag:GetHeight()-(pad.top+pad.bottom)-10)
        SortBag.Resize:SetPosition(SB.width-10,pad.top)
        SortBag.Resize:SetSize(10,SortBag:GetHeight()-2*pad.top)
        SortBag.ResizeBack:SetSize(10,SortBag:GetHeight()-pad.top-pad.bottom)
end

SortBagSetSize()

SortBag.PositionChanged=function()
        local x,y=SortBag:GetPosition()
                if x<0 then x=0 end
                if y<0 then y=0 end
                SortBag:SetPosition(x,y)
                SB.x,SB.y=x,y   
end
                
function openbag()

        SortBag.ListBox:ClearItems()
        local GroupBox={}
        
        --Build VirtPack 
        
        VirtPack=analysepack()
        
        ---Build a box for this group of items. Box 0 is for empty slots.
        for i=0,#Groups do
                GroupBox[i] = Turbine.UI.ListBox()
                GroupBox[i]:SetOrientation( Turbine.UI.Orientation.Horizontal )
                
                GroupBox[i]:SetAllowDrop( true );       --I DID NOT KNOW ABOUT THIS SETTING! How I have suffered...
                GroupBox[i].DragDrop = function( sender, args )
                        --Empty slots allow drag-drop. Hopefully!
                        local shortcut = args.DragDropInfo:GetShortcut();

                        if ( shortcut ~= nil ) then
                                local item = GroupBox[i]:GetItemAt( args.X, args.Y );
                                local index = item.id
                                
                                backpack:PerformShortcutDrop( shortcut, index, Turbine.UI.Control.IsShiftKeyDown() )
                        end
                end
        end
        
        BackpackSlots=nil
        BackpackSlots={}
        
        --Build the backpack Slots.
        for i=1,size do
                local item = backpack:GetItem(i)
                BackpackSlots[i]=Turbine.UI.Lotro.ItemControl( item )   
                BackpackSlots[i].id=i --so it can be passed for item swopping.
                BackpackSlots[i]:SetBackground( "JackdawPlugins/SortPack/Back.tga" )
                BackpackSlots[i]:SetBlendMode( Turbine.UI.BlendMode.Overlay )
        end
        
        --Move the items into the boxes
        for i=1,#VirtPack do
                BackpackSlots[VirtPack[i].id].Group=VirtPack[i].Group
                GroupBox[VirtPack[i].Group]:AddItem(BackpackSlots[VirtPack[i].id])
        end
        
        --Add the groups to the backpack
        for j=0,#Groups do
                --Build a label
                GroupBox[j].label=label(Groups[j].Name)
                if j~=0 then 
                        --Add the label and GroupBox, but not for empty slots
                        SortBag.ListBox:AddItem(GroupBox[j].label)                      
                        SortBag.ListBox:AddItem(GroupBox[j])
                end
                GroupBox[j].label.MouseClick=function()
                        --Clicking the label collapses this group
                        Groups[j].hide=not(Groups[j].hide)
                        SetGroupBoxSize()
                end
        end
                --Empty Slots group added last.
                SortBag.ListBox:AddItem(GroupBox[0].label) 
                SortBag.ListBox:AddItem(GroupBox[0])
        
        --Function refresh the heights of the group boxes
        function SetGroupBoxSize()
                local height=0
                for j=0,#Groups do
                
                        --Adjust GroupBox Widths
                        GroupBox[j]:SetMaxItemsPerLine(SB.icx)
                        GroupBox[j]:SetWidth(SB.icx*36)
                        
                        --Hide the slot, or adjust its height
                        if Groups[j].hide then 
                                GroupBox[j]:SetHeight(0) 
                                GroupBox[j].label:SetBackColor(Turbine.UI.Color( 1,0.4,0,0 )) 
                        else 
                                GroupBox[j]:SetHeight( math.ceil(GroupBox[j]:GetItemCount()/SB.icx)*36) 
                                GroupBox[j].label:SetBackColor(Turbine.UI.Color( 1,0,0,0.4 ))
                        end
                        
                        --Adjust Label Width,Height
                        GroupBox[j].label:SetWidth(SB.labelwidth)
                        if GroupBox[j]:GetItemCount()>0 then 
                                GroupBox[j].label:SetHeight(SB.fontsize) 
                        else 
                                GroupBox[j].label:SetHeight(0) 
                        end
                        
                        --Add to the height variable
                        height=height+math.max(GroupBox[j]:GetHeight(),GroupBox[j].label:GetHeight())
                        
                        if height>SB.maxheight-(pad.top+pad.bottom) then height=SB.maxheight-(pad.top+pad.bottom) end
                        
                end
                        --Resize the window to fit the new height
                        SortBag:SetHeight(height+pad.top+pad.bottom)
                        
                        --Change the title to show how many empty slots are left
                        SortBag:SetText(GroupBox[0]:GetItemCount().." empty slots left")
        end
        
        SetGroupBoxSize()
        
        --Next three items update the bag if items are added or removed.

        backpack.ItemAdded = function( sender, args )
                --args.Index    = Bag slot
                --args.Item             = Item object

                --Remove the slot from the group it is in.
                
                for i=0,#Groups do
                        if GroupBox[i]:ContainsItem(BackpackSlots[args.Index]) then GroupBox[i]:RemoveItem(BackpackSlots[args.Index]) end
                end
                --Determine which group it belongs to
                local destination=ItemGroup[ItemCategory[args.Item:GetCategory()]]
                --Insert the item in that group
                BackpackSlots[args.Index]:SetItem(args.Item)
                BackpackSlots[args.Index].Group=destination
                
                GroupBox[destination]:InsertItem(position,BackpackSlots[args.Index])
                
                --Remove every item from that groupbox one at a time and resort them.
                local temp={}
                --cycle through the elements checking for weights
                for i=GroupBox[destination]:GetItemCount(),1,-1 do
                        temp[i]=GroupBox[destination]:GetItem(i)
                        temp[i].Weight=weight(temp[i].id)
                        temp[i].Name=GroupBox[destination]:GetItem(i):GetItem():GetName()
                end
                
                --Remove all the items
                GroupBox[destination]:ClearItems()
                
                --Sort the items according to weight (ignoring alpha. I am happy with this compromise)
                table.sort(temp, function (a,b) 
                        if a.Weight == b.Weight then 
                                --return a.Name < b.Name
                                return strip(a.Name)< strip(b.Name)
                        else  
                                return (a.Weight < b.Weight) 
                        end
                 end) 
                
                --Put the items in the group
                for i=1,#temp do
                        GroupBox[destination]:AddItem(temp[i])
                end             
                
                --[[This did not fire as the backpack was often closed!
                --If this group was hidden, then show it.
                        --GroupBox[destination].hide=nil]]
                
                --Refresh heights
                SetGroupBoxSize()
        end

        backpack.ItemRemoved = function( sender, args )
                --Also called if a stack is merged.
                --args.Index    = Bag slot

                --Remove item from group
                GroupBox[BackpackSlots[args.Index].Group]:RemoveItem(BackpackSlots[args.Index])
                --Make the item for that slot nil
                BackpackSlots[args.Index]:SetItem(nil)
                BackpackSlots[args.Index].Group=0
                --Add it to empty slots.
                GroupBox[0]:AddItem(BackpackSlots[args.Index])
                
                --Refesh Heights
                SetGroupBoxSize()
        end
        
        backpack.ItemMoved = function( sender, args )
                --[[
                1) If an item has just been moved to an empty slot then just this fires.
                2) If an item has been swopped with another; Say A in slot n1 with B in slot n2
                                ItemRemoved fires on slot n1
                                ItemMoved fires with B from n2 to n1
                                ItemAdded fires on slot n2 with item A
                
                Essentially, item A (the moved item) gets held "in hand" making the target slot empty.]]
                                
                --args.Item             =Item object of the item that has been moved.
                --args.NewIndex =Target Slot
                --args.OldIndex =Destination Slot
                
                BackpackSlots[args.NewIndex],BackpackSlots[args.OldIndex]=BackpackSlots[args.OldIndex],BackpackSlots[args.NewIndex]
        end
        
        --Clear these events on closing the plugin
        SortBag.Closed=function() 
                backpack.ItemMoved = nil
                backpack.ItemAdded = nil
                backpack.ItemRemoved = nil
        end
        
        SortBag:SetVisible(true)
end

--Simple label function
label=class(Turbine.UI.Label)
function label:Constructor (text)
        Turbine.UI.Label.Constructor(self)
        self:SetBackColor( Turbine.UI.Color( 1,0,0,0.4 ) )
        self:SetFont(Turbine.UI.Lotro.Font[SB.font])
        self:SetMultiline(false)
        self:SetTextAlignment( Turbine.UI.ContentAlignment.MiddleCenter )
        self:SetText( text );
        self:SetMouseVisible (true)
        self:SetSize(SB.labelwidth,0)--Initially of height 0
        self.label=true
end     

function SBOverride()
                --Events here replace the default bags with this one.
        if SB.override then SortBag:SetWantsKeyEvents( true );

                SortBag.KeyDown = function( sender, args )
                        if ( args.Action == Turbine.UI.Lotro.Action.Escape ) then
                                SortBag:Close()
                        end

                        if ( args.Action == Turbine.UI.Lotro.Action.ToggleBags or
                                 args.Action == Turbine.UI.Lotro.Action.ToggleBag1 or
                                 args.Action == Turbine.UI.Lotro.Action.ToggleBag2 or
                                 args.Action == Turbine.UI.Lotro.Action.ToggleBag3 or
                                 args.Action == Turbine.UI.Lotro.Action.ToggleBag4 or
                                 args.Action == Turbine.UI.Lotro.Action.ToggleBag5 ) 
                        then
                                if SortBag:IsVisible() then
                                        SortBag:Close()
                                else
                                        openbag()
                                end
                        end
                end
                Turbine.UI.Lotro.LotroUI.SetEnabled( Turbine.UI.Lotro.LotroUIElement.Backpack1, false )
                Turbine.UI.Lotro.LotroUI.SetEnabled( Turbine.UI.Lotro.LotroUIElement.Backpack2, false )
                Turbine.UI.Lotro.LotroUI.SetEnabled( Turbine.UI.Lotro.LotroUIElement.Backpack3, false )
                Turbine.UI.Lotro.LotroUI.SetEnabled( Turbine.UI.Lotro.LotroUIElement.Backpack4, false )
                Turbine.UI.Lotro.LotroUI.SetEnabled( Turbine.UI.Lotro.LotroUIElement.Backpack5, false )
        else
                SortBag:SetWantsKeyEvents( false )
                Turbine.UI.Lotro.LotroUI.SetEnabled( Turbine.UI.Lotro.LotroUIElement.Backpack1, true )
                Turbine.UI.Lotro.LotroUI.SetEnabled( Turbine.UI.Lotro.LotroUIElement.Backpack2, true )
                Turbine.UI.Lotro.LotroUI.SetEnabled( Turbine.UI.Lotro.LotroUIElement.Backpack3, true )
                Turbine.UI.Lotro.LotroUI.SetEnabled( Turbine.UI.Lotro.LotroUIElement.Backpack4, true )
                Turbine.UI.Lotro.LotroUI.SetEnabled( Turbine.UI.Lotro.LotroUIElement.Backpack5, true )
        end
end

SBOverride()

Go to most recent revision | Compare with Previous | Blame


All times are GMT -5. The time now is 06:11 PM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui