View Single Post
  #15  
Unread 09-27-2010, 11:17 AM
Olenn's Avatar
Olenn Olenn is offline
The Undying
Interface Author - Click to view interfaces
 
Join Date: Sep 2010
Posts: 47
You guys are awesome, but I think I am starting to get all of your advice mixed up. I am going to post both Lua scripts and see what you think. I am getting an error;

attempt to index Data a nil value

main.lua

Code:

import "Turbine.UI";
import "Turbine.UI.Lotro";
import "Turbine.Debug";

import "Olenn.Satchel";

satchelWindow = Olenn.Satchel.SatchelWindow();
satchelWindow:SetVisible( true );

settingsWindow = Olenn.Satchel.SettingsWindow();
settingsWindow:SetVisible( false );

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 );

satchelCommand = Turbine.ShellCommand();

function satchelCommand:Execute(cmd, args)
	if ( args == "show" ) then
		SatchelWindow:SetVisible( true );
	elseif ( args == "hide" ) then
		SatchelWindow:SetVisible( false );
	elseif ( args == "toggle" ) then
		SatchelWindow:SetVisible( not SatchelWindow:IsVisible() );
	elseif ( args ~= nil ) then
		satchelCommand:GetHelp();
	end
	
	pluginName="Satchel";
	window = Turbine.UI.Window();
	window:SetWantsUpdates(true);
	window.Update = function(sender,args)
	if (Plugins[pluginName] ~= nil) then
		Plugins[pluginName].Unload = function(self,sender,args)
			UnloadPlugin();
			Turbine.PluginData.Save( Turbine.DataScope.Character, "Satchel", Data );
		end
		window:SetWantsUpdates(false);
	end
end
end




function satchelCommand:GetHelp()
	Turbine.Shell.WriteLine( "------------Satchel Commands------------");
	Turbine.Shell.WriteLine( "/satchel show: Display Satchel Window");
	Turbine.Shell.WriteLine( "/satchel hide: Hide Satchel Window");
	Turbine.Shell.WriteLine( "/satchel toggle: Toggle Satchel Window");
end

Turbine.Shell.AddCommand( "satchel", satchelCommand );
SatchelWindow.lua

Code:
import "Turbine";
import "Turbine.Gameplay";
import "Turbine.UI";
import "Turbine.UI.Extensions";
import "Turbine.UI.Lotro";
import "Turbine.Utils";
import "Olenn.Satchel"

SatchelWindow = class( Turbine.UI.Window );

function SatchelWindow:Constructor()
----CREATE THE WINDOW----
	Turbine.UI.Lotro.Window.Constructor( self );
	self:SetText("Satchel");
	self:SetSize( 325, 300 );
----LOAD PRIOR POSITION----
	self.Data = Turbine.PluginData.Load( Turbine.DataScope.Character,  "SatchelData")
    if (self.Data==nil)then 
		self:SetPosition(100 , 100);
    else
		self:SetPosition(self.Data.x,self.Data.y)
    end
----SAVE CURENT POSITION WHEN MOVED---
	self.PositionChanged=function(sender,args)
		self.Data.x,self.Data.y=self:GetPosition()
		Turbine.PluginData.Save( Turbine.DataScope.Character, "Satchel", self.Data );
	end
----SATCHEL CLOSES WHEN ESC IS PRESSED----
	self:SetWantsKeyEvents( true );
	self.KeyDown = function( sender, args )
		if ( args.Action == Turbine.UI.Lotro.Action.Escape ) then
			sender:SetVisible( false ) 
		end
		
----MAKE BAG BUTTONS ON THE TOOLBAR OPEN SATCHEL----
		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
			sender:SetVisible( not sender:IsVisible() ) 
		end
	end
	
	local satchel = self;
	local mainWindow = self;
	
	self.itemListBoxScrollBar = Turbine.UI.Lotro.ScrollBar();
	self.itemListBoxScrollBar:SetOrientation( Turbine.UI.Orientation.Vertical );
	self.itemListBoxScrollBar:SetParent( self );

	self.itemListBox = Turbine.UI.ListBox();
	self.itemListBox:SetParent( self );
	self.itemListBox:SetOrientation( Turbine.UI.Orientation.Horizontal );
	self.itemListBox:SetVerticalScrollBar( self.itemListBoxScrollBar );
	self.itemListBox:SetAllowDrop( true );
	
	self.itemListBox.DragDrop = function( sender, args )
		local shortcut = args.DragDropInfo:GetShortcut();
		if ( shortcut ~= nil ) then
		  local destinationItemControl = self.itemListBox:GetItemAt( args.X, args.Y );
		  local destinationIndex = self.itemListBox:IndexOfItem( destinationItemControl );
		  self.backpack:PerformShortcutDrop( shortcut, destinationIndex, Turbine.UI.Control.IsShiftKeyDown() );
		end
	end
----MAKE WINDOW RESIZABLE----
	self.resizeHandle = Turbine.UI.Control();
	self.resizeHandle:SetParent( self );
	self.resizeHandle:SetZOrder( 100 );
	self.resizeHandle:SetSize( 20, 20 );
	self.resizeHandle:SetPosition( self:GetWidth() - self.resizeHandle:GetWidth(), self:GetHeight() - self.resizeHandle:GetHeight() );

	self.resizeHandle.MouseDown = function( sender, args )
		sender.dragStartX = args.X;
		sender.dragStartY = args.Y;
		sender.dragging = true;
	end

	self.resizeHandle.MouseMove = function( sender, args )
		local width, height = mainWindow:GetSize();

		if ( sender.dragging ) then
			mainWindow:SetSize( width + ( args.X - sender.dragStartX ), height + ( args.Y - sender.dragStartY ) );
			sender:SetPosition( mainWindow:GetWidth() - sender:GetWidth(), mainWindow:GetHeight() - sender:GetHeight() )
			satchel:PerformLayout()
		end
	end

	self.resizeHandle.MouseUp = function( sender, args )
		sender.dragging = false;
	end
----CREATE CONAINER FOR ITEMS----
	self.items = { };

	local player = Turbine.Gameplay.LocalPlayer();
	self.backpack = player:GetBackpack();

	self.backpack.SizeChanged = function( sender, args )
		satchel:Refresh();
	end

	self.backpack.ItemAdded = function( sender, args )
		satchel.items[args.Index]:SetItem( satchel.backpack:GetItem( args.Index ) );
	end

	self.backpack.ItemRemoved = function( sender, args )
		satchel.items[args.Index]:SetItem( satchel.backpack:GetItem( args.Index ) );
	end

	self.backpack.ItemMoved = function( sender, args )
		satchel.items[args.OldIndex]:SetItem( satchel.backpack:GetItem( args.OldIndex ) );
		satchel.items[args.NewIndex]:SetItem( satchel.backpack:GetItem( args.NewIndex ) );
	end

	self:Refresh();
	
----CREATE SETTINGS BUTTON----
	self.settingsButton = Turbine.UI.Lotro.Button();
	self.settingsButton:SetParent( self );

	self.settingsButton:SetSize(70,30);
	self.settingsButton:SetPosition(34, 37);
	self.settingsButton:SetFont(Turbine.UI.Lotro.Font.Verdana14);
	self.settingsButton:SetText("Settings");	
	self.settingsButton.Click = function(sender, args)
		settingsWindow:SetVisible( not SettingsWindow:IsVisible() );
	end
	
end

----FUNCTION TO REFRESH BAG CONTENTS----
function SatchelWindow:Refresh()
	local backpackSize = self.backpack:GetSize();

	for i = 1, backpackSize, 1 do
		if ( self.items[i] ) then
			self.items[i]:SetParent( nil );
		end
		
		self.items[i] = Turbine.UI.Lotro.ItemControl( self.backpack:GetItem( i ) );
		self.itemListBox:AddItem( self.items[i] );
	end

	self:PerformLayout();
end

function SatchelWindow:PerformLayout()
	self:Layout( { } )
end

function SatchelWindow:Layout( args )
	local width, height = self:GetSize();
	
	local itemWidth = 40;
	
	if ( self.items[1] ~= nil ) then
		itemWidth = self.items[1]:GetWidth()
	end

	local listWidth = width - 40;
	local listHeight = height - 75;
	local itemsPerRow = listWidth / itemWidth;

	self.itemListBox:SetPosition( 15, 60 );
	self.itemListBox:SetSize( listWidth, listHeight );
	self.itemListBox:SetMaxItemsPerLine( itemsPerRow );
	
	self.itemListBoxScrollBar:SetPosition( width - 25, 55 );
	self.itemListBoxScrollBar:SetSize( 10, listHeight );

end
Thanks for all of you help guys!

I have attached the plugin in its current state for Lua help only, it is not advised for use by the common user.
Attached Files
File Type: zip Satchel2.0_Alpha.zip (3.8 KB, 615 views)
Reply With Quote