#define USE_NGUI_2X
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UIGamePanel : MonoBehaviour {
public const float UI_DELAY = 0.75f;
public const float UI_TRAVEL_DIST = 1.6f;
public GameObject content;
public PanelType panelType;
public PanelType openFromPanelOnly;
/**
* Position of the buttons when visible.
*/
protected Vector3 showPosition;
/**
* Position of the buttons when hidden.
*/
protected Vector3 hidePosition;
public static Dictionary <PanelType, UIGamePanel> panels;
public PanelType[] IgnoreTypes;
void Awake() {
if (panels
== null) panels
= new Dictionary
<PanelType, UIGamePanel
> (); if (activePanels
== null) activePanels
= new List
<UIGamePanel
> (0);
panels.Add (panelType, this);
showPosition = content.transform.position;
hidePosition
= content
.transform.position - new Vector3
(0, UI_TRAVEL_DIST,
0); if (panelType == PanelType.DEFAULT) {
activePanels.Add(this);
} else {
content.transform.position = hidePosition;
}
Init ();
}
virtual protected void Init() {
}
virtual public void InitialiseWithBuilding(Building building) {
}
virtual public void Show() {
if (panelType == PanelType.DEFAULT && GridView.Instance != null) GridView.Instance.NormalMode();
if (panelType == PanelType.PLACE_BUILDING && GridView.Instance != null) GridView.Instance.BuildingMode();
if (panelType == PanelType.EDIT_PATHS && GridView.Instance != null) GridView.Instance.PathMode();
if (activePanels.Contains(this)) {
StartCoroutine(DoReShow());
} else if (activePanels.Count == 0 || IsPanelOpenedFromActivePanel () || openFromPanelOnly == PanelType.NONE) {
HideActivePanels ();
gameObject.SendMessage ("OnShow", this, SendMessageOptions.DontRequireReceiver);
StartCoroutine(DoShow());
activePanels.Add (this);
}
}
virtual public void Hide() {
if (activePanels.Contains(this)) {
activePanels.Remove (this);
}
gameObject.SendMessage ("OnHide", this, SendMessageOptions.DontRequireReceiver);
StartCoroutine(DoHide());
}
public static void ShowPanel(PanelType panelType) {
if (panelType == PanelType.DEFAULT) BuildingManager.ActiveBuilding = null;
if (panels.ContainsKey (panelType)) {
//if (!activePanels.Contains(panels[panelType])) {
panels [panelType].Show ();
//}
}
}
public static List<UIGamePanel> activePanels;
/**
* Reshow the panel (i.e. same panel but for a different object/building).
*/
virtual protected IEnumerator DoReShow() {
gameObject.BroadcastMessage ("OnHide", SendMessageOptions.DontRequireReceiver);
iTween.MoveTo(content, hidePosition, UI_DELAY);
yield return new WaitForSeconds
(UI_DELAY
/ 3
.0f
);
gameObject.BroadcastMessage ("OnShow", SendMessageOptions.DontRequireReceiver);
iTween.MoveTo(content, showPosition, UI_DELAY);
}
/**
* Show the panel.
*/
virtual protected IEnumerator DoShow() {
yield return new WaitForSeconds
(UI_DELAY
/ 3
.0f
); content.SetActive (true);
gameObject.BroadcastMessage ("OnShow", SendMessageOptions.DontRequireReceiver);
#if USE_NGUI_3X
yield return true;
GetComponent<UIPanel>().Refresh();
#endif
iTween.MoveTo(content, showPosition, UI_DELAY);
}
/**
* Hide the panel.
*/
virtual protected IEnumerator DoHide() {
gameObject.BroadcastMessage ("OnHide", SendMessageOptions.DontRequireReceiver);
iTween.MoveTo(content, hidePosition, UI_DELAY);
yield return new WaitForSeconds
(UI_DELAY
/ 3
.0f
); content.SetActive (false);
}
protected bool IsPanelOpenedFromActivePanel () {
for (int i = 0; i < activePanels.Count; ++i) {
if (activePanels[i].panelType == openFromPanelOnly) {
return true;
}
}
return false;
}
protected void HideActivePanels () {
if (activePanels != null) {
for (int i = 0; i < activePanels.Count; ++i) {
UIGamePanel panel = activePanels[i];
bool hide = true;
if (IgnoreTypes != null) {
for (int j = 0; j < IgnoreTypes.Length; ++j) {
if (IgnoreTypes[j] == panel.panelType) {
hide = false;
break;
}
}
}
if (hide) {
activePanels.Remove (panel);
panel.Hide ();
--i;
}
}
}
}
}