-- A button with a DropDown menu.
import "Turbine";
--------------------------------------------------------------------------
--- DropDownBox
--
-- A button control that contains a drop-down menu.
-- Just pass the list of items to populate the menu.
-- Can recieve notification of changes (SelectionChanged)
-- Can query for index or value of selected item.
--
DropDownBox = class( Turbine.UI.Control );
function DropDownBox:Constructor(args)
Turbine.UI.Control.Constructor( self );
--Turbine.Shell.WriteLine("DropDownBox:Constructor()");
if( "nil" == type(args.itemList) ) then
error("Missing required itemList parameter",3);
elseif( "table" ~= type(args.itemList) ) then
error("Invalid itemList parameter",3);
end
local itemList = args.itemList;
self.show = false;
self.enabled = true;
self.selectedIndex = 1;
self.itemList=itemList;
self.contextMenu = Turbine.UI.ContextMenu();
local menuItems = self.contextMenu:GetItems();
for idx,value in ipairs(itemList) do
-- Turbine.Shell.WriteLine( string.format(" [%d] [%s]", idx,value) );
menuItems:Add( Turbine.UI.MenuItem( value ) );
end
-- Register event handlers with all menu items.
local idx;
for idx = 1, menuItems:GetCount() do
local menuItem = menuItems:Get( idx );
local itemText = menuItem:GetText();
if ( idx == self.selectedIndex ) then
menuItem:SetChecked( true );
menuItem:SetEnabled( false );
end
menuItem.Click = function( sender, args )
local selectedIndex = menuItems:IndexOf(sender);
--Turbine.Shell.WriteLine( string.format(
-- "DropDownBox.menuItem:Click [%d] [%s]",
-- selectedIndex,
-- itemText) );
self:SetSelectedIndex(selectedIndex);
end
end
-- A custom function to refresh the selected item
self.contextMenu.refreshSelection = function( sender, args )
--Turbine.Shell.WriteLine( "DropDownBox.contextMenu.refreshSelection." );
-- Update other context menu items enable state
for i = 1, menuItems:GetCount() do
local menuItem = menuItems:Get( i );
if ( i == self.selectedIndex ) then
menuItem:SetChecked( true );
menuItem:SetEnabled( false );
else
menuItem:SetChecked( false );
menuItem:SetEnabled( true );
end
end
end
local button = args.button or Turbine.UI.Button();
self.button = button;
--self.button = Turbine.UI.Lotro.Button();
self.button:SetBlendMode( Turbine.UI.BlendMode.Overlay );
self.button:SetParent( self );
self.button:SetSize( self:GetSize() ); -- width,height
--self.buttonColor:SetPosition( self:GetPosition()); -- left,top
self.button:SetPosition( 0, 0 ); -- left,top
self.button:SetText( "DropDown" );
self.button:SetVisible( true );
--self.button:SetBackColor(Turbine.UI.Color.Gray);
self.button:SetTextAlignment( Turbine.UI.ContentAlignment.MiddleCenter );
self.button.Click = function( sender, args )
--Turbine.Shell.WriteLine( "color = " .. self.color );
self.contextMenu:ShowMenu();
end
self.GetSelectedIndex = function()
--Turbine.Shell.WriteLine( "DropDownBox:GetSelectedIndex" );
return self.selectedIndex;
end
self.GetSelectedValue = function()
--Turbine.Shell.WriteLine( "DropDownBox:GetSelectedValue" );
return self.itemList[self.selectedIndex];
end
self.SetSelectedIndex = function(self, index)
--Turbine.Shell.WriteLine( "DropDownBox:SetSelectedIndex [" .. index .. "]" );
if( (index < 1) or (index > #self.itemList) ) then
error( "Index Out of range [" .. index .. "]", 2);
end
if( self.selectedIndex ~= index ) then
self.selectedIndex = index;
self.contextMenu:refreshSelection();
self.button:SetText( self.GetSelectedValue() );
self:SelectionChanged(index);
end
end
self.SetSelectedValue = function(self, value)
--Turbine.Shell.WriteLine( "DropDownBox:SetSelectedValue [" .. value .. "]" );
local selectedIndex = nil;
-- search list of items for value
for idx,v in ipairs(self.itemList) do
if( v == value ) then
selectedIndex = idx;
end
end
if( nil == selectedIndex ) then
error( "Invalid value supplied [" .. tostring(value) .. "]", 2);
else
self:SetSelectedIndex(selectedIndex);
end
end
self.SetDropDownList = function(self, itemList)
--Turbine.Shell.WriteLine( "DropDownBox:SetDropDownList" );
if( "table" ~= type(itemList) ) then
error( "Invalid value supplied [" .. itemList .. "]", 2);
end
self.itemList=itemList;
-- Clear old items from list
local menuItems = self.contextMenu:GetItems();
menuItems:Clear();
-- Build new list of items
for idx,value in ipairs(itemList) do
--Turbine.Shell.WriteLine( string.format(" [%d] [%s]", idx,value) );
menuItems:Add( Turbine.UI.MenuItem( value ) );
end
-- Register event handlers with all menu items.
local idx;
for idx = 1, menuItems:GetCount() do
local menuItem = menuItems:Get( idx );
local itemText = menuItem:GetText();
if ( idx == self.selectedIndex ) then
menuItem:SetChecked( true );
menuItem:SetEnabled( false );
end
menuItem.Click = function( sender, args )
local selectedIndex = menuItems:IndexOf(sender);
--Turbine.Shell.WriteLine( string.format(
-- "DropDownBox.menuItem:Click [%d] [%s]",
-- selectedIndex,
-- itemText) );
self:SetSelectedIndex(selectedIndex);
end
end
end
self.SizeChanged = function( sender, args )
--Turbine.Shell.WriteLine( "DropDownBox:SizeChanged" );
self.button:SetSize(self:GetSize());
end
-- notification of selection change
self.SelectionChanged = function( sender, args )
-- intent is for user to override for notification of change
end
self.SetBackColor = function(self, value)
--Turbine.Shell.WriteLine( "DropDownBox:SetBackColor " .. tostring(value) );
self.button:SetBackColor(value);
end
self.SetForeColor = function(self, value)
--Turbine.Shell.WriteLine( "DropDownBox:SetForeColor " .. tostring(value) );
self.button:SetForeColor(value);
end
self.SetText = function(self, value)
--Turbine.Shell.WriteLine( "DropDownBox:SetText " .. tostring(value) );
self.button:SetText(value);
end
self.SetTextAlignment = function(self, value)
--Turbine.Shell.WriteLine( "DropDownBox:SetTextAlignment " .. tostring(value) );
self.button:SetTextAlignment(value);
end
self.SetFont = function(self, value)
--Turbine.Shell.WriteLine( "DropDownBox:SetFont " .. tostring(value) );
self.button:SetFont(value);
end
self.SetFontStyle = function(self, value)
--Turbine.Shell.WriteLine( "DropDownBox:SetFontStyle " .. tostring(value) );
self.button:SetFontStyle(value);
end
self.SetOutlineColor = function(self, value)
--Turbine.Shell.WriteLine( "DropDownBox:SetOutlineColor " .. tostring(value) );
self.button:SetOutlineColor(value);
end
end