SelectList

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Usage)
(Component code)
Line 7: Line 7:
 
== Component code ==
 
== Component code ==
  
<csharp> public static object SelectList( ICollection list, object selected, GUIStyle defaultStyle, GUIStyle selectedStyle )
+
<csharp> public static object SelectList( ICollection list, object selected, GUIStyle defaultStyle, GUIStyle selectedStyle )
{
+
{
foreach( object item in list )
+
foreach( object item in list )
{
+
if( GUILayout.Button( item.ToString(), ( selected == item ) ? selectedStyle : defaultStyle ) )
+
 
{
 
{
selected = item;
+
if( GUILayout.Button( item.ToString(), ( selected == item ) ? selectedStyle : defaultStyle ) )
 +
{
 +
if( selected == item )
 +
// Clicked an already selected item. Deselect.
 +
{
 +
selected = null;
 +
}
 +
else
 +
{
 +
selected = item;
 +
}
 +
}
 
}
 
}
 +
 +
return selected;
 
}
 
}
 
 
return selected;
+
}
+
 +
public delegate bool OnListItemGUI( object item, bool selected, ICollection list );
 +
 +
 +
 +
public static object SelectList( ICollection list, object selected, OnListItemGUI itemHandler )
 +
{
 +
ArrayList itemList;
 +
 +
itemList = new ArrayList( list );
 +
 +
foreach( object item in itemList )
 +
{
 +
if( itemHandler( item, item == selected, list ) )
 +
{
 +
selected = item;
 +
}
 +
else if( selected == item )
 +
// If we *were* selected, but aren't any more then deselect
 +
{
 +
selected = null;
 +
}
 +
}
 +
 +
return selected;
 +
}
 
</csharp>
 
</csharp>
  

Revision as of 17:47, 10 November 2008

By AngryAnt.

Description

Visualises a list of strings or objects overriding ToString and returns the item selected by the user.

Component code

<csharp> public static object SelectList( ICollection list, object selected, GUIStyle defaultStyle, GUIStyle selectedStyle ) { foreach( object item in list ) { if( GUILayout.Button( item.ToString(), ( selected == item ) ? selectedStyle : defaultStyle ) ) { if( selected == item ) // Clicked an already selected item. Deselect. { selected = null; } else { selected = item; } } }

return selected; }


public delegate bool OnListItemGUI( object item, bool selected, ICollection list );


public static object SelectList( ICollection list, object selected, OnListItemGUI itemHandler ) { ArrayList itemList;

itemList = new ArrayList( list );

foreach( object item in itemList ) { if( itemHandler( item, item == selected, list ) ) { selected = item; } else if( selected == item ) // If we *were* selected, but aren't any more then deselect { selected = null; } }

return selected; } </csharp>

Usage

<csharp> currentSeletion = SelectList( myList, currentSelection, GUI.skin.GetStyle( "Button" ), GUI.skin.GetStyle( "Label" ) ); </csharp>

In this example, myList could be an ArrayList of strings or instances of your own type, returning the desired list label in ToString.

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox