LocationPickerGroup.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.Xml;
  7. namespace JC.Unity.Picker {
  8. public class LocationPickerGroup : MonoBehaviour
  9. {
  10. [SerializeField] TextAsset xmlFileText;
  11. [HideInInspector] public LocationParser locationParser;
  12. public LocationInfo selectLocation;
  13. public List<LocationPicker> locationPickerList;
  14. [SerializeField] Button btnClose;
  15. [SerializeField] Button btnEnter;
  16. public Action<LocationInfo> onEnter;
  17. void Awake()
  18. {
  19. locationParser = new LocationParser(xmlFileText);
  20. selectLocation = new LocationInfo(locationParser);
  21. Init();
  22. }
  23. void Start()
  24. {
  25. if (btnClose) {
  26. btnClose.onClick.AddListener(() => {
  27. Destroy(transform.GetComponentInParent<Canvas>().gameObject);
  28. });
  29. }
  30. if (btnEnter) {
  31. btnEnter.onClick.AddListener(() => {
  32. Destroy(transform.GetComponentInParent<Canvas>().gameObject);
  33. onEnter?.Invoke(selectLocation);
  34. });
  35. }
  36. }
  37. public void Init() {
  38. for (int i = 0; i < locationPickerList.Count; i++) {
  39. locationPickerList[i].myGroup = this;
  40. locationPickerList[i].Init();
  41. }
  42. }
  43. public void onDateUpdate() {
  44. foreach (var item in locationPickerList) {
  45. item.Refresh();
  46. }
  47. }
  48. private float _height = -1;
  49. public float GetHeight() {
  50. if (_height == -1) {
  51. _height = transform.GetComponent<RectTransform>().sizeDelta.y;
  52. }
  53. return _height;
  54. }
  55. // void Test() {
  56. // LocationInfo locationInfo = new LocationInfo(locationParser);
  57. // while (true) {
  58. // if (!locationInfo.IsValid()) {
  59. // break;
  60. // } else {
  61. // while (true) {
  62. // if (!locationInfo.IsValid()) {
  63. // locationInfo.StateIndex = 0;
  64. // break;
  65. // } else {
  66. // while (true) {
  67. // if (!locationInfo.IsValid()) {
  68. // locationInfo.CityIndex = 0;
  69. // break;
  70. // } else {
  71. // Debug.Log(locationInfo.GetCountryRegion().Item1 + "," + locationInfo.GetState().Item1 + "," + locationInfo.GetCity().Item1);
  72. // }
  73. // locationInfo.CityIndex++;
  74. // }
  75. // }
  76. // locationInfo.StateIndex++;
  77. // }
  78. // }
  79. // locationInfo.CountryRegionIndex++;
  80. // }
  81. // }
  82. }
  83. public class LocationParser {
  84. public List<XmlNode> CountryRegionList = new List<XmlNode>();
  85. public LocationParser(TextAsset xmlFileText) {
  86. XmlDocument xmlDoc = new XmlDocument();
  87. xmlDoc.LoadXml(xmlFileText.text);
  88. XmlNode locationNode = xmlDoc.SelectSingleNode("Location");
  89. XmlNodeList xmlNodeList = locationNode.SelectNodes("CountryRegion");
  90. for (int i = 0; i < xmlNodeList.Count; i++) {
  91. CountryRegionList.Add(xmlNodeList[i]);
  92. }
  93. }
  94. }
  95. public class LocationInfo {
  96. private LocationParser locationParser;
  97. public int CountryRegionIndex = 0;
  98. public int StateIndex = 0;
  99. public int CityIndex = 0;
  100. public LocationInfo(LocationParser locationParser,
  101. int CountryRegionIndex = 0, int StateIndex = 0, int CityIndex = 0) {
  102. this.locationParser = locationParser;
  103. this.CountryRegionIndex = CountryRegionIndex;
  104. this.StateIndex = StateIndex;
  105. this.CityIndex = CityIndex;
  106. }
  107. public LocationInfo ChangeCountryRegionIndex(int num) {
  108. return new LocationInfo(locationParser, CountryRegionIndex + num, 0, 0);
  109. }
  110. public LocationInfo ChangeStateIndex(int num) {
  111. return new LocationInfo(locationParser, CountryRegionIndex, StateIndex + num, 0);
  112. }
  113. public LocationInfo ChangeCityIndex(int num) {
  114. return new LocationInfo(locationParser, CountryRegionIndex, StateIndex, CityIndex + num);
  115. }
  116. public Tuple<string, string> GetCountryRegion() {
  117. string Name = locationParser.CountryRegionList[CountryRegionIndex].Attributes["Name"].Value;
  118. string Code = locationParser.CountryRegionList[CountryRegionIndex].Attributes["Code"].Value;
  119. return new Tuple<string, string>(Name, Code);
  120. }
  121. public Tuple<string, string> GetState() {
  122. string Name = "";
  123. string Code = "";
  124. XmlNode countryRegionNode = locationParser.CountryRegionList[CountryRegionIndex];
  125. XmlNodeList stateList = countryRegionNode.SelectNodes("State");
  126. if (stateList.Count != 0) {
  127. XmlNode stateNode = stateList[StateIndex];
  128. XmlAttribute NameAtb = stateNode.Attributes["Name"];
  129. if (NameAtb != null) {
  130. Name = NameAtb.Value;
  131. }
  132. XmlAttribute CodeAtb = stateNode.Attributes["Code"];
  133. if (CodeAtb != null) {
  134. Code = CodeAtb.Value;
  135. }
  136. }
  137. return new Tuple<string, string>(Name, Code);
  138. }
  139. public Tuple<string, string> GetCity() {
  140. string Name = "";
  141. string Code = "";
  142. XmlNode countryRegionNode = locationParser.CountryRegionList[CountryRegionIndex];
  143. XmlNodeList stateList = countryRegionNode.SelectNodes("State");
  144. if (stateList.Count != 0) {
  145. XmlNode stateNode = stateList[StateIndex];
  146. XmlNodeList cityList = stateNode.SelectNodes("City");
  147. if (cityList.Count != 0) {
  148. XmlNode cityNode = cityList[CityIndex];
  149. XmlAttribute NameAtb = cityNode.Attributes["Name"];
  150. if (NameAtb != null) {
  151. Name = NameAtb.Value;
  152. }
  153. XmlAttribute CodeAtb = cityNode.Attributes["Code"];
  154. if (CodeAtb != null) {
  155. Code = CodeAtb.Value;
  156. }
  157. }
  158. }
  159. return new Tuple<string, string>(Name, Code);
  160. }
  161. public bool IsValid() {
  162. if (CountryRegionIndex < 0 || StateIndex < 0 || CityIndex < 0) {
  163. return false;
  164. }
  165. bool res1 = CountryRegionIndex < locationParser.CountryRegionList.Count;
  166. if (!res1) return false;
  167. XmlNode countryRegionNode = locationParser.CountryRegionList[CountryRegionIndex];
  168. XmlNodeList stateList = countryRegionNode.SelectNodes("State");
  169. if (stateList.Count == 0 && StateIndex == 0 && CityIndex == 0) return true;
  170. bool res2 = StateIndex < stateList.Count;
  171. if (!res2) return false;
  172. XmlNode stateNode = stateList[StateIndex];
  173. XmlNodeList cityList = stateNode.SelectNodes("City");
  174. if (cityList.Count == 0 && CityIndex == 0) return true;
  175. bool res3 = CityIndex < cityList.Count;
  176. if (!res3) return false;
  177. return true;
  178. }
  179. }
  180. }