123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565 |
- using System;
- using UnityEngine;
- namespace FairyGUI
- {
- /// <summary>
- ///
- /// </summary>
- public class PopupMenu : EventDispatcher
- {
- protected GComponent _contentPane;
- protected GList _list;
- protected GObject _expandingItem;
- PopupMenu _parentMenu;
- TimerCallback _showSubMenu;
- TimerCallback _closeSubMenu;
- EventListener _onPopup;
- EventListener _onClose;
- public int visibleItemCount;
- public bool hideOnClickItem;
- public bool autoSize;
- const string EVENT_TYPE = "PopupMenuItemClick";
- public PopupMenu()
- {
- Create(null);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="resourceURL"></param>
- public PopupMenu(string resourceURL)
- {
- Create(resourceURL);
- }
- public EventListener onPopup
- {
- get { return _onPopup ?? (_onPopup = new EventListener(this, "onPopup")); }
- }
- public EventListener onClose
- {
- get { return _onClose ?? (_onClose = new EventListener(this, "onClose")); }
- }
- void Create(string resourceURL)
- {
- if (resourceURL == null)
- {
- resourceURL = UIConfig.popupMenu;
- if (resourceURL == null)
- {
- Debug.LogError("FairyGUI: UIConfig.popupMenu not defined");
- return;
- }
- }
- _contentPane = UIPackage.CreateObjectFromURL(resourceURL).asCom;
- _contentPane.onAddedToStage.Add(__addedToStage);
- _contentPane.onRemovedFromStage.Add(__removeFromStage);
- _contentPane.focusable = false;
- _list = _contentPane.GetChild("list").asList;
- _list.RemoveChildrenToPool();
- _list.AddRelation(_contentPane, RelationType.Width);
- _list.RemoveRelation(_contentPane, RelationType.Height);
- _contentPane.AddRelation(_list, RelationType.Height);
- _list.onClickItem.Add(__clickItem);
- hideOnClickItem = true;
- _showSubMenu = __showSubMenu;
- _closeSubMenu = CloseSubMenu;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="caption"></param>
- /// <param name="callback"></param>
- /// <returns></returns>
- public GButton AddItem(string caption, EventCallback0 callback)
- {
- GButton item = CreateItem(caption, callback);
- _list.AddChild(item);
- return item;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="caption"></param>
- /// <param name="callback"></param>
- /// <returns></returns>
- public GButton AddItem(string caption, EventCallback1 callback)
- {
- GButton item = CreateItem(caption, callback);
- _list.AddChild(item);
- return item;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="caption"></param>
- /// <param name="index"></param>
- /// <param name="callback"></param>
- /// <returns></returns>
- public GButton AddItemAt(string caption, int index, EventCallback1 callback)
- {
- GButton item = CreateItem(caption, callback);
- _list.AddChildAt(item, index);
- return item;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="caption"></param>
- /// <param name="index"></param>
- /// <param name="callback"></param>
- /// <returns></returns>
- public GButton AddItemAt(string caption, int index, EventCallback0 callback)
- {
- GButton item = CreateItem(caption, callback);
- _list.AddChildAt(item, index);
- return item;
- }
- GButton CreateItem(string caption, Delegate callback)
- {
- GButton item = _list.GetFromPool(_list.defaultItem).asButton;
- item.title = caption;
- item.grayed = false;
- Controller c = item.GetController("checked");
- if (c != null)
- c.selectedIndex = 0;
- item.RemoveEventListeners(EVENT_TYPE);
- if (callback is EventCallback0)
- item.AddEventListener(EVENT_TYPE, (EventCallback0)callback);
- else
- item.AddEventListener(EVENT_TYPE, (EventCallback1)callback);
- item.onRollOver.Add(__rollOver);
- item.onRollOut.Add(__rollOut);
- return item;
- }
- /// <summary>
- ///
- /// </summary>
- public void AddSeperator()
- {
- AddSeperator(-1);
- }
- /// <summary>
- ///
- /// </summary>
- public void AddSeperator(int index)
- {
- if (UIConfig.popupMenu_seperator == null)
- {
- Debug.LogError("FairyGUI: UIConfig.popupMenu_seperator not defined");
- return;
- }
- if (index == -1)
- _list.AddItemFromPool(UIConfig.popupMenu_seperator);
- else
- {
- GObject item = _list.GetFromPool(UIConfig.popupMenu_seperator);
- _list.AddChildAt(item, index);
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- public string GetItemName(int index)
- {
- GButton item = _list.GetChildAt(index).asButton;
- return item.name;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="name"></param>
- /// <param name="caption"></param>
- public void SetItemText(string name, string caption)
- {
- GButton item = _list.GetChild(name).asButton;
- item.title = caption;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="name"></param>
- /// <param name="visible"></param>
- public void SetItemVisible(string name, bool visible)
- {
- GButton item = _list.GetChild(name).asButton;
- if (item.visible != visible)
- {
- item.visible = visible;
- _list.SetBoundsChangedFlag();
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="name"></param>
- /// <param name="grayed"></param>
- public void SetItemGrayed(string name, bool grayed)
- {
- GButton item = _list.GetChild(name).asButton;
- item.grayed = grayed;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="name"></param>
- /// <param name="checkable"></param>
- public void SetItemCheckable(string name, bool checkable)
- {
- GButton item = _list.GetChild(name).asButton;
- Controller c = item.GetController("checked");
- if (c != null)
- {
- if (checkable)
- {
- if (c.selectedIndex == 0)
- c.selectedIndex = 1;
- }
- else
- c.selectedIndex = 0;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="name"></param>
- /// <param name="check"></param>
- public void SetItemChecked(string name, bool check)
- {
- GButton item = _list.GetChild(name).asButton;
- Controller c = item.GetController("checked");
- if (c != null)
- c.selectedIndex = check ? 2 : 1;
- }
- [Obsolete("Use IsItemChecked instead")]
- public bool isItemChecked(string name)
- {
- return IsItemChecked(name);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public bool IsItemChecked(string name)
- {
- GButton item = _list.GetChild(name).asButton;
- Controller c = item.GetController("checked");
- if (c != null)
- return c.selectedIndex == 2;
- else
- return false;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="name"></param>
- public void RemoveItem(string name)
- {
- GComponent item = _list.GetChild(name).asCom;
- if (item != null)
- {
- item.RemoveEventListeners(EVENT_TYPE);
- if (item.data is PopupMenu)
- {
- ((PopupMenu)item.data).Dispose();
- item.data = null;
- }
- int index = _list.GetChildIndex(item);
- _list.RemoveChildToPoolAt(index);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public void ClearItems()
- {
- _list.RemoveChildrenToPool();
- }
- /// <summary>
- ///
- /// </summary>
- public int itemCount
- {
- get { return _list.numChildren; }
- }
- /// <summary>
- ///
- /// </summary>
- public GComponent contentPane
- {
- get { return _contentPane; }
- }
- /// <summary>
- ///
- /// </summary>
- public GList list
- {
- get { return _list; }
- }
- public void Dispose()
- {
- int cnt = _list.numChildren;
- for (int i = 0; i < cnt; i++)
- {
- GObject obj = _list.GetChildAt(i);
- if (obj.data is PopupMenu)
- ((PopupMenu)obj.data).Dispose();
- }
- _contentPane.Dispose();
- }
- /// <summary>
- ///
- /// </summary>
- public void Show()
- {
- Show(null, PopupDirection.Auto);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="target"></param>
- public void Show(GObject target)
- {
- Show(target, PopupDirection.Auto, null);
- }
- [Obsolete]
- public void Show(GObject target, object downward)
- {
- Show(target, downward == null ? PopupDirection.Auto : ((bool)downward == true ? PopupDirection.Down : PopupDirection.Up), null);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="target"></param>
- /// <param name="dir"></param>
- public void Show(GObject target, PopupDirection dir)
- {
- Show(target, PopupDirection.Auto, null);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="target"></param>
- /// <param name="dir"></param>
- /// <param name="parentMenu"></param>
- public void Show(GObject target, PopupDirection dir, PopupMenu parentMenu)
- {
- GRoot r = target != null ? target.root : GRoot.inst;
- r.ShowPopup(this.contentPane, (target is GRoot) ? null : target, dir);
- _parentMenu = parentMenu;
- }
- public void Hide()
- {
- if (contentPane.parent != null)
- ((GRoot)contentPane.parent).HidePopup(contentPane);
- }
- void ShowSubMenu(GObject item)
- {
- _expandingItem = item;
- PopupMenu popup = item.data as PopupMenu;
- if (item is GButton)
- ((GButton)item).selected = true;
- popup.Show(item, PopupDirection.Auto, this);
- Vector2 pt = contentPane.LocalToRoot(new Vector2(item.x + item.width - 5, item.y - 5), item.root);
- popup.contentPane.position = pt;
- }
- void CloseSubMenu(object param)
- {
- if (contentPane.isDisposed)
- return;
- if (_expandingItem == null)
- return;
- if (_expandingItem is GButton)
- ((GButton)_expandingItem).selected = false;
- PopupMenu popup = (PopupMenu)_expandingItem.data;
- if (popup == null)
- return;
- _expandingItem = null;
- popup.Hide();
- }
- private void __clickItem(EventContext context)
- {
- GButton item = ((GObject)context.data).asButton;
- if (item == null)
- return;
- if (item.grayed)
- {
- _list.selectedIndex = -1;
- return;
- }
- Controller c = item.GetController("checked");
- if (c != null && c.selectedIndex != 0)
- {
- if (c.selectedIndex == 1)
- c.selectedIndex = 2;
- else
- c.selectedIndex = 1;
- }
- if (hideOnClickItem)
- {
- if (_parentMenu != null)
- _parentMenu.Hide();
- Hide();
- }
- item.DispatchEvent(EVENT_TYPE, item); //event data is for backward compatibility
- }
- void __addedToStage()
- {
- DispatchEvent("onPopup", null);
- if (autoSize)
- {
- _list.EnsureBoundsCorrect();
- int cnt = _list.numChildren;
- float maxDelta = -1000;
- for (int i = 0; i < cnt; i++)
- {
- GButton obj = _list.GetChildAt(i).asButton;
- if (obj == null)
- continue;
- GTextField tf = obj.GetTextField();
- if (tf != null)
- {
- float v = tf.textWidth - tf.width;
- if (v > maxDelta)
- maxDelta = v;
- }
- }
- if (contentPane.width + maxDelta > contentPane.initWidth)
- contentPane.width += maxDelta;
- else
- contentPane.width = contentPane.initWidth;
- }
- _list.selectedIndex = -1;
- _list.ResizeToFit(visibleItemCount > 0 ? visibleItemCount : int.MaxValue, 10);
- }
- void __removeFromStage()
- {
- _parentMenu = null;
- if (_expandingItem != null)
- Timers.inst.Add(0, 1, _closeSubMenu);
- DispatchEvent("onClose", null);
- }
- void __rollOver(EventContext context)
- {
- GObject item = (GObject)context.sender;
- if ((item.data is PopupMenu) || _expandingItem != null)
- {
- Timers.inst.Add(0.1f, 1, _showSubMenu, item);
- }
- }
- void __showSubMenu(object param)
- {
- if (contentPane.isDisposed)
- return;
- GObject item = (GObject)param;
- GRoot r = contentPane.root;
- if (r == null)
- return;
- if (_expandingItem != null)
- {
- if (_expandingItem == item)
- return;
- CloseSubMenu(null);
- }
- PopupMenu popup = item.data as PopupMenu;
- if (popup == null)
- return;
- ShowSubMenu(item);
- }
- void __rollOut(EventContext context)
- {
- if (_expandingItem == null)
- return;
- Timers.inst.Remove(_showSubMenu);
- GRoot r = contentPane.root;
- if (r != null)
- {
- PopupMenu popup = (PopupMenu)_expandingItem.data;
- Vector2 pt = popup.contentPane.GlobalToLocal(context.inputEvent.position);
- if (pt.x >= 0 && pt.y >= 0 && pt.x < popup.contentPane.width && pt.y < popup.contentPane.height)
- return;
- }
- CloseSubMenu(null);
- }
- }
- }
|