import "Turbine"
import "Turbine.UI";
import "Turbine.UI.Lotro";
import "Turbine.Gameplay";
--[[To do:
Variable columns.
Over-ride default bags
Close on ESC
Make *optional*
Release]]
if SB==nil then
SB={}
SB.x=100
SB.y=100
SB.icx=4
SB.icy=3
SB.columns=2
end
pad={top=30,side=15,bottom=15}
SB.width=36*SB.icx+2*pad.side
SB.height=36*SB.icy+pad.top+pad.bottom
SortBag=Turbine.UI.Control()
SortBag.Window = Turbine.UI.Lotro.Window()
SortBag.Window:SetPosition( SB.x,SB.y )
SortBag.Window:SetSize(SB.width,SB.height)
SortBag.Window:SetText("SortBag Slots")
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.icx*36, SB.icy*36)
SortBag.ListBox:SetMaxItemsPerLine(1)--SB.icx)
SortBag.ListBox:SetOrientation( Turbine.UI.Orientation.Horizontal )
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()
if draggingr then
SB.width,SB.height=SortBag.Window:GetMousePosition()
SB.icx=math.floor((SB.width-2*pad.side)/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
SB.width=36*SB.icx+2*pad.side
SB.height=36*SB.icy+pad.top+pad.bottom
SortBag.Window:SetSize(SB.width,SB.height)
SortBag.ListBox:SetSize( 36*SB.icx, 36*SB.icy)
SortBag.ListBox:SetMaxItemsPerLine(1)--SB.icx)
SortBag.Resize:SetPosition(SB.width-10,SB.height-10)
for i=1,SortBag.ListBox:GetItemCount() do
local temp=SortBag.ListBox:GetItem(i)
if temp.label then
temp:SetWidth(36*SB.icx)
else
temp:SetMaxItemsPerLine(SB.icx)
temp:SetSize( SB.icx*36, math.ceil(temp:GetItemCount()/SB.icx)*36)
end
end
end
end
function openbag()
local height=0
--Build VirtPack
VirtPack=analysepack()
--Next two items update the bag if items are added or removed. Ugly, but it works.
backpack.ItemAdded = function( sender, args )
openbag()
end
backpack.ItemRemoved = function( sender, args )
openbag()
end
backpack.ItemMoved = function( sender, args )
openbag()
end
SortBag.Window.Closed=function()
backpack.ItemAdded = nil
backpack.ItemRemoved = nil
end
SortBag.ListBox:ClearItems()
local GroupBox={}
local function makegroupbox(i)
--Make the box for the current group
GroupBox[i] = Turbine.UI.ListBox()
GroupBox[i]:SetMaxItemsPerLine(SB.icx)
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
openbag()
end
end
--Box for empty slots...
makegroupbox(0)
---Build a box for this group of items
for j=1,#Groups do
makegroupbox(j)
end
--Move the items in
for i=1,#VirtPack do
local item = backpack:GetItem(VirtPack[i].id)
local temp=Turbine.UI.Lotro.ItemControl( item )
temp.id=VirtPack[i].id --so it can be passed for item swopping.
if item~=nil then
GroupBox[VirtPack[i].Group]:AddItem(temp)
end
if item==nil then
temp:SetBackground( "JackdawPlugins/SortPack/Empty.tga" )
temp:SetBlendMode( Turbine.UI.BlendMode.Overlay )
GroupBox[0]:AddItem(temp)
end
end
--Add the groups to the backpack
for j=1,#Groups do
if GroupBox[j]:GetItemCount()>0 then
SortBag.ListBox:AddItem(label(Groups[j].Name))
GroupBox[j]:SetSize( SB.icx*36, math.ceil(GroupBox[j]:GetItemCount()/SB.icx)*36)
SortBag.ListBox:AddItem(GroupBox[j])
end
end
SortBag.ListBox:AddItem(label("Empty Slots"))
SortBag.ListBox:AddItem(GroupBox[0])
GroupBox[0]:SetSize( SB.icx*36, math.ceil(GroupBox[0]:GetItemCount()/SB.icx)*36)
--[[Here for an idea for adjusting the window height
for i=1,SortBag.ListBox:GetItemCount() do
height=height+SortBag.ListBox:GetItem(i):GetHeight()
end
Turbine.Shell.WriteLine(height)
]]
SortBag.Window:SetVisible(true)
end
--Simple label function
label=class(Turbine.UI.Label)
function label:Constructor (text,colour)
if colour==nil then colour=Turbine.UI.Color( 1,0,0,0.4 ) end
Turbine.UI.Label.Constructor(self)
self:SetSize(36*SB.icx,20)
self:SetBackColor( colour );
self:SetFont(Turbine.UI.Lotro.Font.TrajanPro16)
self:SetTextAlignment( Turbine.UI.ContentAlignment.MiddleCenter )
self:SetText( text );
self:SetMouseVisible (false)
self.label=true
end
SortBagCommand = Turbine.ShellCommand()
function SortBagCommand:Execute(command, i)
openbag()
end
Turbine.Shell.AddCommand('openbag', SortBagCommand)