//********************************** Banshee Engine (www.banshee3d.com) **************************************************// //**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************// using System; using System.Runtime.CompilerServices; using bs; namespace bs.Editor { /** @addtogroup GUI-Editor * @{ */ /// /// Editor GUI element that displays a list box with user-specified elements and an optional label. List box can be /// a standard list-box that allows a single element to be selected, or a multi-select list box where any number of /// elements can be selected at the same time. /// public sealed class GUIListBoxField : GUIElement { public delegate void OnSelectionChangedDelegate(int index); /// /// Triggered whenever user selects a new element in the list box. Returned index maps to the element in the /// elements array that the list box was initialized with. /// public event OnSelectionChangedDelegate OnSelectionChanged; /// /// Index of the list box entry currently selected. Returns -1 if nothing is selected. /// public int Index { get { return Internal_GetValue(mCachedPtr); } set { Internal_SetValue(mCachedPtr, value); } } /// /// States of all element in the list box (enabled or disabled). /// public bool[] States { get { return Internal_GetElementStates(mCachedPtr); } set { Internal_SetElementStates(mCachedPtr, value); } } /// /// Creates a new list box with the specified elements and a label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Determines should the listbox allow multiple elements to be selected or just one. /// /// Content to display on the label. /// Width of the title label in pixels. /// Optional style to use for the element. Style controls the look of the element, as well as /// default layout options. Style will be retrieved from the active GUISkin. If not specified /// default element style is used. /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(LocString[] elements, bool multiselect, GUIContent title, int titleWidth, string style = "", params GUIOption[] options) { Internal_CreateInstance(this, elements, multiselect, ref title, titleWidth, style, options, true); } /// /// Creates a new list box with the specified elements and a label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Determines should the listbox allow multiple elements to be selected or just one. /// /// Content to display on the label. /// Width of the title label in pixels. /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(LocString[] elements, bool multiselect, GUIContent title, int titleWidth = 100, params GUIOption[] options) { Internal_CreateInstance(this, elements, multiselect, ref title, titleWidth, "", options, true); } /// /// Creates a new single-selection list box with the specified elements and a label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Content to display on the label. /// Width of the title label in pixels. /// Optional style to use for the element. Style controls the look of the element, as well as /// default layout options. Style will be retrieved from the active GUISkin. If not specified /// default element style is used. /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(LocString[] elements, GUIContent title, int titleWidth, string style = "", params GUIOption[] options) { Internal_CreateInstance(this, elements, false, ref title, titleWidth, style, options, true); } /// /// Creates a new single-selection list box with the specified elements and a label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Content to display on the label. /// Width of the title label in pixels. /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(LocString[] elements, GUIContent title, int titleWidth = 100, params GUIOption[] options) { Internal_CreateInstance(this, elements, false, ref title, titleWidth, "", options, true); } /// /// Creates a new list box with the specified elements and no label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Determines should the listbox allow multiple elements to be selected or just one. /// /// Optional style to use for the element. Style controls the look of the element, as well as /// default layout options. Style will be retrieved from the active GUISkin. If not specified /// default element style is used. /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(LocString[] elements, bool multiselect = false, string style = "", params GUIOption[] options) { GUIContent emptyContent = new GUIContent(); Internal_CreateInstance(this, elements, multiselect, ref emptyContent, 0, style, options, false); } /// /// Creates a new list box with the specified elements and no label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Determines should the listbox allow multiple elements to be selected or just one. /// /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(LocString[] elements, bool multiselect = false, params GUIOption[] options) { GUIContent emptyContent = new GUIContent(); Internal_CreateInstance(this, elements, multiselect, ref emptyContent, 0, "", options, false); } /// /// Creates a new list box with the specified elements and a label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Determines should the listbox allow multiple elements to be selected or just one. /// /// Content to display on the label. /// Width of the title label in pixels. /// Optional style to use for the element. Style controls the look of the element, as well as /// default layout options. Style will be retrieved from the active GUISkin. If not specified /// default element style is used. /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(string[] elements, bool multiselect, GUIContent title, int titleWidth, string style = "", params GUIOption[] options) { Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, ref title, titleWidth, style, options, true); } /// /// Creates a new list box with the specified elements and a label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Determines should the listbox allow multiple elements to be selected or just one. /// /// Content to display on the label. /// Width of the title label in pixels. /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(string[] elements, bool multiselect, GUIContent title, int titleWidth = 100, params GUIOption[] options) { Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, ref title, titleWidth, "", options, true); } /// /// Creates a new single-selection list box with the specified elements and a label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Content to display on the label. /// Width of the title label in pixels. /// Optional style to use for the element. Style controls the look of the element, as well as /// default layout options. Style will be retrieved from the active GUISkin. If not specified /// default element style is used. /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(string[] elements, GUIContent title, int titleWidth, string style = "", params GUIOption[] options) { Internal_CreateInstance(this, ToLocalizedElements(elements), false, ref title, titleWidth, style, options, true); } /// /// Creates a new single-selection list box with the specified elements and a label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Content to display on the label. /// Width of the title label in pixels. /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(string[] elements, GUIContent title, int titleWidth = 100, params GUIOption[] options) { Internal_CreateInstance(this, ToLocalizedElements(elements), false, ref title, titleWidth, "", options, true); } /// /// Creates a new list box with the specified elements and no label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Determines should the listbox allow multiple elements to be selected or just one. /// /// Optional style to use for the element. Style controls the look of the element, as well as /// default layout options. Style will be retrieved from the active GUISkin. If not specified /// default element style is used. /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(string[] elements, bool multiselect = false, string style = "", params GUIOption[] options) { GUIContent emptyContent = new GUIContent(); Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, ref emptyContent, 0, style, options, false); } /// /// Creates a new list box with the specified elements and no label. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. /// Determines should the listbox allow multiple elements to be selected or just one. /// /// Options that allow you to control how is the element positioned and sized. This will /// override any similar options set by style. public GUIListBoxField(string[] elements, bool multiselect = false, params GUIOption[] options) { GUIContent emptyContent = new GUIContent(); Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, ref emptyContent, 0, "", options, false); } /// /// Updates the list box with a new set of elements. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. public void SetElements(LocString[] elements) { Internal_SetElements(mCachedPtr, elements); } /// /// Updates the list box with a new set of elements. /// /// Array of elements to display in the list box. Elements will be displayed in the same /// order as in the array. public void SetElements(string[] elements) { Internal_SetElements(mCachedPtr, ToLocalizedElements(elements)); } /// /// Makes the element with the specified index selected. /// /// Sequential index in the elements array provided on construction. public void SelectElement(int idx) { Internal_SelectElement(mCachedPtr, idx); } /// /// Deselect element the element with the specified index. Only relevant for multi-select list boxes. /// /// Sequential index in the elements array provided on construction. public void DeselectElement(int idx) { Internal_DeselectElement(mCachedPtr, idx); } /// /// Colors the element with a specific tint. /// /// Tint to apply to the element. public void SetTint(Color color) { Internal_SetTint(mCachedPtr, ref color); } /// /// Converts a set of normal strings to a set of localized strings. /// /// Strings to convert. /// Localized strings created from input strings. private LocString[] ToLocalizedElements(string[] elements) { if (elements == null) return null; LocString[] locElements = new LocString[elements.Length]; for (int i = 0; i < locElements.Length; i++) locElements[i] = elements[i]; return locElements; } /// /// Triggered by the native interop object when a user selects an object in the list. /// private void DoOnSelectionChanged(int index) { if (OnSelectionChanged != null) OnSelectionChanged(index); } [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_CreateInstance(GUIListBoxField instance, LocString[] entries, bool multiselect, ref GUIContent title, int titleWidth, string style, GUIOption[] options, bool withTitle); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_SetElements(IntPtr nativeInstance, LocString[] elements); [MethodImpl(MethodImplOptions.InternalCall)] private static extern int Internal_GetValue(IntPtr nativeInstance); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_SetValue(IntPtr nativeInstance, int value); [MethodImpl(MethodImplOptions.InternalCall)] private static extern bool[] Internal_GetElementStates(IntPtr nativeInstance); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_SetElementStates(IntPtr nativeInstance, bool[] states); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_SelectElement(IntPtr nativeInstance, int idx); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_DeselectElement(IntPtr nativeInstance, int idx); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color); } /** @} */ }