123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using FairyGUI.Utils;
- namespace FairyGUI
- {
- /// <summary>
- /// GComboBox class.
- /// </summary>
- public class GComboBox : GComponent
- {
- /// <summary>
- /// Visible item count of the drop down list.
- /// </summary>
- public int visibleItemCount;
- /// <summary>
- ///
- /// </summary>
- public GComponent dropdown;
- /// <summary>
- /// Play sound when button is clicked.
- /// </summary>
- public NAudioClip sound;
- /// <summary>
- /// Volume of the click sound. (0-1)
- /// </summary>
- public float soundVolumeScale;
- protected GObject _titleObject;
- protected GObject _iconObject;
- protected GList _list;
- protected List<string> _items;
- protected List<string> _icons;
- protected List<string> _values;
- protected PopupDirection _popupDirection;
- protected Controller _selectionController;
- bool _itemsUpdated;
- int _selectedIndex;
- Controller _buttonController;
- bool _down;
- bool _over;
- EventListener _onChanged;
- public GComboBox()
- {
- visibleItemCount = UIConfig.defaultComboBoxVisibleItemCount;
- _itemsUpdated = true;
- _selectedIndex = -1;
- _items = new List<string>();
- _values = new List<string>();
- _popupDirection = PopupDirection.Auto;
- soundVolumeScale = 1;
- }
- /// <summary>
- /// Dispatched when selection was changed.
- /// </summary>
- public EventListener onChanged
- {
- get { return _onChanged ?? (_onChanged = new EventListener(this, "onChanged")); }
- }
- /// <summary>
- /// Icon of the combobox.
- /// </summary>
- override public string icon
- {
- get
- {
- if (_iconObject != null)
- return _iconObject.icon;
- else
- return null;
- }
- set
- {
- if (_iconObject != null)
- _iconObject.icon = value;
- UpdateGear(7);
- }
- }
- /// <summary>
- /// Title of the combobox.
- /// </summary>
- public string title
- {
- get
- {
- if (_titleObject != null)
- return _titleObject.text;
- else
- return null;
- }
- set
- {
- if (_titleObject != null)
- _titleObject.text = value;
- UpdateGear(6);
- }
- }
- /// <summary>
- /// Same of the title.
- /// </summary>
- override public string text
- {
- get { return this.title; }
- set { this.title = value; }
- }
- /// <summary>
- /// Text color
- /// </summary>
- public Color titleColor
- {
- get
- {
- GTextField tf = GetTextField();
- if (tf != null)
- return tf.color;
- else
- return Color.black;
- }
- set
- {
- GTextField tf = GetTextField();
- if (tf != null)
- tf.color = value;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public int titleFontSize
- {
- get
- {
- GTextField tf = GetTextField();
- if (tf != null)
- return tf.textFormat.size;
- else
- return 0;
- }
- set
- {
- GTextField tf = GetTextField();
- if (tf != null)
- {
- TextFormat format = tf.textFormat;
- format.size = value;
- tf.textFormat = format;
- }
- }
- }
- /// <summary>
- /// Items to build up drop down list.
- /// </summary>
- public string[] items
- {
- get
- {
- return _items.ToArray();
- }
- set
- {
- _items.Clear();
- if (value != null)
- _items.AddRange(value);
- ApplyListChange();
- }
- }
- /// <summary>
- ///
- /// </summary>
- public string[] icons
- {
- get { return _icons != null ? _icons.ToArray() : null; }
- set
- {
- this.iconList.Clear();
- if (value != null)
- _icons.AddRange(value);
- ApplyListChange();
- }
- }
- /// <summary>
- /// Values, should be same size of the items.
- /// </summary>
- public string[] values
- {
- get { return _values.ToArray(); }
- set
- {
- _values.Clear();
- if (value != null)
- _values.AddRange(value);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public List<string> itemList
- {
- get { return _items; }
- }
- /// <summary>
- ///
- /// </summary>
- public List<string> valueList
- {
- get { return _values; }
- }
- /// <summary>
- ///
- /// </summary>
- public List<string> iconList
- {
- get { return _icons ?? (_icons = new List<string>()); }
- }
- /// <summary>
- /// Call this method after you made changes on itemList or iconList
- /// </summary>
- public void ApplyListChange()
- {
- if (_items.Count > 0)
- {
- if (_selectedIndex >= _items.Count)
- _selectedIndex = _items.Count - 1;
- else if (_selectedIndex == -1)
- _selectedIndex = 0;
- this.text = _items[_selectedIndex];
- if (_icons != null && _selectedIndex < _icons.Count)
- this.icon = _icons[_selectedIndex];
- }
- else
- {
- this.text = string.Empty;
- if (_icons != null)
- this.icon = null;
- _selectedIndex = -1;
- }
- _itemsUpdated = true;
- }
- /// <summary>
- /// Selected index.
- /// </summary>
- public int selectedIndex
- {
- get
- {
- return _selectedIndex;
- }
- set
- {
- if (_selectedIndex == value)
- return;
- _selectedIndex = value;
- if (_selectedIndex >= 0 && _selectedIndex < _items.Count)
- {
- this.text = (string)_items[_selectedIndex];
- if (_icons != null && _selectedIndex < _icons.Count)
- this.icon = _icons[_selectedIndex];
- }
- else
- {
- this.text = string.Empty;
- if (_icons != null)
- this.icon = null;
- }
- UpdateSelectionController();
- }
- }
- /// <summary>
- ///
- /// </summary>
- public Controller selectionController
- {
- get { return _selectionController; }
- set { _selectionController = value; }
- }
- /// <summary>
- /// Selected value.
- /// </summary>
- public string value
- {
- get
- {
- if (_selectedIndex >= 0 && _selectedIndex < _values.Count)
- return _values[_selectedIndex];
- else
- return null;
- }
- set
- {
- int index = _values.IndexOf(value);
- if (index == -1 && value == null)
- index = _values.IndexOf(string.Empty);
- if (index == -1)
- index = 0;
- this.selectedIndex = index;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public PopupDirection popupDirection
- {
- get { return _popupDirection; }
- set { _popupDirection = value; }
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public GTextField GetTextField()
- {
- if (_titleObject is GTextField)
- return (GTextField)_titleObject;
- else if (_titleObject is GLabel)
- return ((GLabel)_titleObject).GetTextField();
- else if (_titleObject is GButton)
- return ((GButton)_titleObject).GetTextField();
- else
- return null;
- }
- protected void SetState(string value)
- {
- if (_buttonController != null)
- _buttonController.selectedPage = value;
- }
- protected void SetCurrentState()
- {
- if (this.grayed && _buttonController != null && _buttonController.HasPage(GButton.DISABLED))
- SetState(GButton.DISABLED);
- else if (dropdown != null && dropdown.parent != null)
- SetState(GButton.DOWN);
- else
- SetState(_over ? GButton.OVER : GButton.UP);
- }
- override protected void HandleGrayedChanged()
- {
- if (_buttonController != null && _buttonController.HasPage(GButton.DISABLED))
- {
- if (this.grayed)
- SetState(GButton.DISABLED);
- else
- SetState(GButton.UP);
- }
- else
- base.HandleGrayedChanged();
- }
- override public void HandleControllerChanged(Controller c)
- {
- base.HandleControllerChanged(c);
- if (_selectionController == c)
- this.selectedIndex = c.selectedIndex;
- }
- void UpdateSelectionController()
- {
- if (_selectionController != null && !_selectionController.changing
- && _selectedIndex < _selectionController.pageCount)
- {
- Controller c = _selectionController;
- _selectionController = null;
- c.selectedIndex = _selectedIndex;
- _selectionController = c;
- }
- }
- public override void Dispose()
- {
- if (dropdown != null)
- {
- dropdown.Dispose();
- dropdown = null;
- }
- _selectionController = null;
- base.Dispose();
- }
- override protected void ConstructExtension(ByteBuffer buffer)
- {
- buffer.Seek(0, 6);
- _buttonController = GetController("button");
- _titleObject = GetChild("title");
- _iconObject = GetChild("icon");
- string str = buffer.ReadS();
- if (str != null)
- {
- dropdown = UIPackage.CreateObjectFromURL(str) as GComponent;
- if (dropdown == null)
- {
- Debug.LogWarning("FairyGUI: " + this.resourceURL + " should be a component.");
- return;
- }
- _list = dropdown.GetChild("list") as GList;
- if (_list == null)
- {
- Debug.LogWarning("FairyGUI: " + this.resourceURL + ": should container a list component named list.");
- return;
- }
- _list.onClickItem.Add(__clickItem);
- _list.AddRelation(dropdown, RelationType.Width);
- _list.RemoveRelation(dropdown, RelationType.Height);
- dropdown.AddRelation(_list, RelationType.Height);
- dropdown.RemoveRelation(_list, RelationType.Width);
- dropdown.SetHome(this);
- }
- displayObject.onRollOver.Add(__rollover);
- displayObject.onRollOut.Add(__rollout);
- displayObject.onTouchBegin.Add(__touchBegin);
- displayObject.onTouchEnd.Add(__touchEnd);
- displayObject.onClick.Add(__click);
- }
- override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
- {
- base.Setup_AfterAdd(buffer, beginPos);
- if (!buffer.Seek(beginPos, 6))
- return;
- if ((ObjectType)buffer.ReadByte() != packageItem.objectType)
- return;
- string str;
- int itemCount = buffer.ReadShort();
- for (int i = 0; i < itemCount; i++)
- {
- int nextPos = buffer.ReadUshort();
- nextPos += buffer.position;
- _items.Add(buffer.ReadS());
- _values.Add(buffer.ReadS());
- str = buffer.ReadS();
- if (str != null)
- {
- if (_icons == null)
- _icons = new List<string>();
- _icons.Add(str);
- }
- buffer.position = nextPos;
- }
- str = buffer.ReadS();
- if (str != null)
- {
- this.text = str;
- _selectedIndex = _items.IndexOf(str);
- }
- else if (_items.Count > 0)
- {
- _selectedIndex = 0;
- this.text = _items[0];
- }
- else
- _selectedIndex = -1;
- str = buffer.ReadS();
- if (str != null)
- this.icon = str;
- if (buffer.ReadBool())
- this.titleColor = buffer.ReadColor();
- int iv = buffer.ReadInt();
- if (iv > 0)
- visibleItemCount = iv;
- _popupDirection = (PopupDirection)buffer.ReadByte();
- iv = buffer.ReadShort();
- if (iv >= 0)
- _selectionController = parent.GetControllerAt(iv);
- if (buffer.version >= 5)
- {
- str = buffer.ReadS();
- if (str != null)
- sound = UIPackage.GetItemAssetByURL(str) as NAudioClip;
- soundVolumeScale = buffer.ReadFloat();
- }
- }
- public void UpdateDropdownList()
- {
- if (_itemsUpdated)
- {
- _itemsUpdated = false;
- RenderDropdownList();
- _list.ResizeToFit(visibleItemCount);
- }
- }
- protected void ShowDropdown()
- {
- UpdateDropdownList();
- if (_list.selectionMode == ListSelectionMode.Single)
- _list.selectedIndex = -1;
- dropdown.width = this.width;
- _list.EnsureBoundsCorrect(); //avoid flicker
- this.root.TogglePopup(dropdown, this, _popupDirection);
- if (dropdown.parent != null)
- {
- dropdown.displayObject.onRemovedFromStage.Add(__popupWinClosed);
- SetState(GButton.DOWN);
- }
- }
- virtual protected void RenderDropdownList()
- {
- _list.RemoveChildrenToPool();
- int cnt = _items.Count;
- for (int i = 0; i < cnt; i++)
- {
- GObject item = _list.AddItemFromPool();
- item.text = _items[i];
- item.icon = (_icons != null && i < _icons.Count) ? _icons[i] : null;
- item.name = i < _values.Count ? _values[i] : string.Empty;
- }
- }
- private void __popupWinClosed(object obj)
- {
- dropdown.displayObject.onRemovedFromStage.Remove(__popupWinClosed);
- SetCurrentState();
- RequestFocus();
- }
- private void __clickItem(EventContext context)
- {
- if (dropdown.parent is GRoot)
- ((GRoot)dropdown.parent).HidePopup(dropdown);
- _selectedIndex = int.MinValue;
- this.selectedIndex = _list.GetChildIndex((GObject)context.data);
- DispatchEvent("onChanged", null);
- }
- private void __rollover()
- {
- _over = true;
- if (_down || dropdown != null && dropdown.parent != null)
- return;
- SetCurrentState();
- }
- private void __rollout()
- {
- _over = false;
- if (_down || dropdown != null && dropdown.parent != null)
- return;
- SetCurrentState();
- }
- private void __touchBegin(EventContext context)
- {
- if (context.initiator is InputTextField)
- return;
- _down = true;
- if (dropdown != null)
- ShowDropdown();
- context.CaptureTouch();
- }
- private void __touchEnd(EventContext context)
- {
- if (_down)
- {
- _down = false;
- if (dropdown != null && dropdown.parent != null)
- SetCurrentState();
- }
- }
- private void __click()
- {
- if (sound != null && sound.nativeClip != null)
- Stage.inst.PlayOneShotSound(sound.nativeClip, soundVolumeScale);
- }
- }
- }
|