using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace BansheeEngine
{
///
/// Base class for all GUI elements. Every GUI element can at least be positioned in it's parent layout/panel and be
/// hidden/visible, focused/unfocused and assigned a context menu.
///
public abstract class GUIElement : ScriptObject
{
///
/// Triggered when a GUI element receives keyboard focus.
///
public Action OnFocusGained;
///
/// Triggered when a GUI element loses keyboard focus.
///
public Action OnFocusLost;
///
/// Returns the layout this element belongs to, if any.
///
public GUILayout Parent
{
get { return Internal_GetParent(mCachedPtr); }
}
///
/// Name of the style that determines the appearance of this GUI element.
///
public string Style
{
get { return Internal_GetStyle(mCachedPtr); }
set { Internal_SetStyle(mCachedPtr, value); }
}
///
/// Gets or sets non-clipped bounds of the GUI element. Relative to a parent GUI panel.
///
public Rect2I Bounds
{
get
{
Rect2I bounds;
Internal_GetBounds(mCachedPtr, out bounds);
return bounds;
}
set { Internal_SetBounds(mCachedPtr, ref value); }
}
///
/// Gets or sets non-clipped bounds of the GUI element including the margins. Relative to a parent GUI panel.
///
public Rect2I VisualBounds
{
get
{
Rect2I bounds;
Internal_GetVisualBounds(mCachedPtr, out bounds);
return bounds;
}
}
///
/// Makes the element hidden or visible. This will not affect the layout as the room for the element will still
/// be reserved in the parent layout, use if you need to affect the layout as well.
///
public bool Visible
{
set { Internal_SetVisible(mCachedPtr, value); }
get { return Internal_GetVisible(mCachedPtr); }
}
///
/// Activates or deactivates the element, making it hidden or visible. When disabled it is essentially removed from
/// the parent achieving the similar effect as if the element was destroyed.
///
public bool Active
{
set { Internal_SetActive(mCachedPtr, value); }
get { return Internal_GetActive(mCachedPtr); }
}
///
/// Disables or enables the element. Disabled elements cannot be interacted with and have a faded out appearance.
///
public bool Disabled
{
set { Internal_SetDisabled(mCachedPtr, value); }
get { return Internal_GetDisabled(mCachedPtr); }
}
///
/// Assigns or removes keyboard focus on this element.
///
public bool Focus
{
set { Internal_SetFocus(mCachedPtr, value); }
}
///
/// Destroys this element and all its children. Removes the element from parent layout/panel.
///
///
/// Calling methods on a destroyed element is a no-operation. Destroyed elements aren't allowed to be assigned as
/// children of other elements, or be parents of other elements.
///
public virtual void Destroy()
{
Internal_Destroy(mCachedPtr);
}
///
/// Sets element position relative to parent GUI panel.
///
/// X position of the element in pixels, relative to parent GUI panel.
/// Y position of the element in pixels, relative to parent GUI panel.
///
/// Be aware that this value will be ignored if GUI element is part of a layout because the layout controls placement
/// of child elements.
///
public void SetPosition(int x, int y)
{
Internal_SetPosition(mCachedPtr, x, y);
}
///
/// Sets a fixed element width.
///
/// Width in pixels.
public void SetWidth(int width)
{
Internal_SetWidth(mCachedPtr, width);
}
///
/// Sets a flexible element width. Element will be resized according to its contents and parent layout but will
/// always stay within the provided range.
///
/// Minimum width in pixels. Element will never be smaller than this width.
/// Maximum width in pixels. Element will never be larger than this width. Specify zero for
/// unlimited width.
public void SetFlexibleWidth(int minWidth, int maxWidth)
{
Internal_SetFlexibleWidth(mCachedPtr, minWidth, maxWidth);
}
///
/// Sets a fixed element height.
///
/// Height in pixels.
public void SetHeight(int height)
{
Internal_SetHeight(mCachedPtr, height);
}
///
/// Sets a flexible element height. Element will be resized according to its contents and parent layout but will
/// always stay within the provided range.
///
/// Minimum height in pixels. Element will never be smaller than this height.
/// Maximum height in pixels. Element will never be larger than this height. Specify zero
/// for unlimited height.
public void SetFlexibleHeight(int minHeight, int maxHeight)
{
Internal_SetFlexibleHeight(mCachedPtr, minHeight, maxHeight);
}
///
/// Resets element bounds to their initial values dictated by the element's style.
///
public void ResetDimensions()
{
Internal_ResetDimensions(mCachedPtr);
}
///
/// Assigns a new context menu that will be opened when the element is right clicked.
///
/// Object containing context menu contents. Can be null if no menu is wanted.
public void SetContextMenu(ContextMenu menu)
{
IntPtr menuPtr = IntPtr.Zero;
if (menu != null)
menuPtr = menu.GetCachedPtr();
Internal_SetContextMenu(mCachedPtr, menuPtr);
}
///
/// Triggered by the native interop object when the element gains keyboard focus.
///
private void Internal_OnFocusGained()
{
if (OnFocusGained != null)
OnFocusGained();
}
///
/// Triggered by the native interop object when the element loses keyboard focus.
///
private void Internal_OnFocusLost()
{
if (OnFocusLost != null)
OnFocusLost();
}
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool Internal_GetVisible(IntPtr nativeInstance);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetVisible(IntPtr nativeInstance, bool visible);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool Internal_GetActive(IntPtr nativeInstance);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetActive(IntPtr nativeInstance, bool enabled);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool Internal_GetDisabled(IntPtr nativeInstance);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetDisabled(IntPtr nativeInstance, bool disabled);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetFocus(IntPtr nativeInstance, bool focus);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern GUILayout Internal_GetParent(IntPtr nativeInstance);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetPosition(IntPtr nativeInstance, int x, int y);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetWidth(IntPtr nativeInstance, int width);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetFlexibleWidth(IntPtr nativeInstance, int minWidth, int maxWidth);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetHeight(IntPtr nativeInstance, int height);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetFlexibleHeight(IntPtr nativeInstance, int minHeight, int maxHeight);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_ResetDimensions(IntPtr nativeInstance);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_GetBounds(IntPtr nativeInstance, out Rect2I value);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetBounds(IntPtr nativeInstance, ref Rect2I value);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_GetVisualBounds(IntPtr nativeInstance, out Rect2I value);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetContextMenu(IntPtr nativeInstance, IntPtr contextMenu);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern string Internal_GetStyle(IntPtr nativeInstance);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetStyle(IntPtr nativeInstance, string style);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_Destroy(IntPtr nativeInstance);
}
}