ImprovedSelectionList

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (inspiration)
m (Description: Re-arranged description)
Line 3: Line 3:
 
== Description ==
 
== Description ==
  
Visualises an array of strings or GUIContents and returns the index selected by the user. '''Important''': this script depends on your current GUISkin having a custom style called "List Item" with an appropriate OnNormal look. If you don't have this in your skin, you will have to use the custom style versions of the function.
+
Visualises an array of strings or GUIContents and returns the index selected by the user. A delegate can be provided to capture double-clicking list items. '''Important''': this script depends on your current GUISkin having a custom style called "List Item" with an appropriate OnNormal look. If you don't have this in your skin, you will have to use the custom style versions of the function.
 
+
A delegate can be provided to capture double-clicking list items.
+
  
 
== Component code ==
 
== Component code ==

Revision as of 01:54, 6 April 2011

By Daniel Brauer, inspired by AngryAnt

Description

Visualises an array of strings or GUIContents and returns the index selected by the user. A delegate can be provided to capture double-clicking list items. Important: this script depends on your current GUISkin having a custom style called "List Item" with an appropriate OnNormal look. If you don't have this in your skin, you will have to use the custom style versions of the function.

Component code

<csharp>using UnityEngine;

public class GUILayoutx {

public delegate void DoubleClickCallback(int index);

public static int SelectionList(int selected, GUIContent[] list) { return SelectionList(selected, list, "List Item", null); }

public static int SelectionList(int selected, GUIContent[] list, GUIStyle elementStyle) { return SelectionList(selected, list, elementStyle, null); }

public static int SelectionList(int selected, GUIContent[] list, DoubleClickCallback callback) { return SelectionList(selected, list, "List Item", callback); }

public static int SelectionList(int selected, GUIContent[] list, GUIStyle elementStyle, DoubleClickCallback callback) { for (int i = 0; i < list.Length; ++i) { Rect elementRect = GUILayoutUtility.GetRect(list[i], elementStyle); bool hover = elementRect.Contains(Event.current.mousePosition); if (hover && Event.current.type == EventType.MouseDown) { selected = i; Event.current.Use(); } else if (hover && callback != null && Event.current.type == EventType.MouseUp && Event.current.clickCount == 2) { callback(i); Event.current.Use(); } else if (Event.current.type == EventType.repaint) { elementStyle.Draw(elementRect, list[i], hover, false, i == selected, false); } } return selected; }

public static int SelectionList(int selected, string[] list) { return SelectionList(selected, list, "List Item", null); }

public static int SelectionList(int selected, string[] list, GUIStyle elementStyle) { return SelectionList(selected, list, elementStyle, null); }

public static int SelectionList(int selected, string[] list, DoubleClickCallback callback) { return SelectionList(selected, list, "List Item", callback); }

public static int SelectionList(int selected, string[] list, GUIStyle elementStyle, DoubleClickCallback callback) { for (int i = 0; i < list.Length; ++i) { Rect elementRect = GUILayoutUtility.GetRect(new GUIContent(list[i]), elementStyle); bool hover = elementRect.Contains(Event.current.mousePosition); if (hover && Event.current.type == EventType.MouseDown) { selected = i; Event.current.Use(); } else if (hover && callback != null && Event.current.type == EventType.MouseUp && Event.current.clickCount == 2) { callback(i); Event.current.Use(); } else if (Event.current.type == EventType.repaint) { elementStyle.Draw(elementRect, list[i], hover, false, i == selected, false); } } return selected; }

}</csharp>

Usage

Providing a double-click delegate:

<csharp> int currentSelection; string[] myList = new string[] {"First", "Second", "Third"};

protected void OnGUI() { currentSelection = SelectionList(myList, currentSelection, DoubleClickItem); }

protected void DoubleClickItem(int index) { Debug.Log("Clicked " + myList[index]; } </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox