lotrointerface.com
Search Downloads

LoTROInterface SVN SortPack

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

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

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

SB.width=SB.labelwidth+36*SB.icx+2*pad.side
SB.height=36*SB.icy+pad.top+pad.bottom

SortBag=Turbine.UI.Control()

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.Window: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.Window:IsVisible() then
                                        SortBag.Window: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()

SortBag.Window = Turbine.UI.Lotro.Window()
        SortBag.Window:SetPosition( SB.x,SB.y )
        SortBag.Window:SetSize(SB.width,SB.height)
        SortBag.Window:SetText("SortBag")
        SortBag.Window.PositionChanged=function() SB.x,SB.y=SortBag.Window:GetPosition() end            

SortBag.ListBox = Turbine.UI.ListBox()
        SortBag.ListBox:SetParent( SortBag.Window )
        SortBag.ListBox:SetPosition( pad.side, pad.top )
        SortBag.ListBox:SetSize( SB.labelwidth+SB.icx*36, SB.icy*36)
        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.Window)
                SortBag.ScrollBar:SetPosition(SB.labelwidth+SB.icx*36+10,pad.top+5)
                SortBag.ScrollBar:SetHeight(SB.icy*36-10)
                SortBag.ScrollBar:SetWidth(10)
        
        SortBag.Resize=Turbine.UI.Control()
                SortBag.Resize:SetParent(SortBag.Window)
                SortBag.Resize:SetZOrder(99)
                SortBag.Resize:SetSize(10,10)
                SortBag.Resize:SetPosition(SB.width-10,SB.height-10)
                
                SortBag.ResizeBack=Turbine.UI.Control()
                SortBag.ResizeBack:SetParent(SortBag.Resize)
                SortBag.ResizeBack:SetSize(10,10)
                SortBag.ResizeBack:SetBackColor(Turbine.UI.Color(1,1,1,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,SB.height=SortBag.Window:GetMousePosition()
                                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.height-(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.height=36*SB.icy+pad.top+pad.bottom
                                        SortBag.Window:SetSize(SB.width,SB.height)
                                        SortBag.ListBox:SetSize( SB.labelwidth+SB.icx*36, SB.icy*36)
                                        SortBag.ScrollBar:SetPosition(SB.labelwidth+SB.icx*36+10,pad.top+5)
                                        SortBag.ScrollBar:SetHeight(SB.icy*36-10)
                                        SortBag.Resize:SetPosition(SB.width-10,SB.height-10)
                                        SetGroupBoxSize()
                        end
                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()
                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(16) 
                        else 
                                GroupBox[j].label:SetHeight(0) 
                        end
                end
        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             
                
                --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.Window.Closed=function() 
                backpack.ItemMoved = nil
                backpack.ItemAdded = nil
                backpack.ItemRemoved = nil
        end
        
        SortBag.Window: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.TrajanPro14)
        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     

Go to most recent revision | Compare with Previous | Blame


All times are GMT -5. The time now is 08:03 AM.


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