using System; using System.Collections.Generic; using System.Runtime.InteropServices; using CIG; using CIG.Extensions; using CIG.Translation; using CIGEnums; using SUISS.Core; using SUISSEngine; using UnityEngine; using UnityEngine.UI; public class BuildDemolishPopupView : PopupBaseView { private void OnDestroy() { this._tabView.TabIndexChangedEvent -= this.OnTabIndexChanged; this.CancelInvoke(new Action(this.SetupTimersAndListData)); if (SingletonMonobehaviour.IsAvailable) { SingletonMonobehaviour.Instance.PopUnlimitedFPSRequest(this); } } public override void Init() { base.Init(); this._tabView.TabIndexChangedEvent += this.OnTabIndexChanged; } public override void Open() { base.Open(); SingletonMonobehaviour.Instance.PushUnlimitedFPSRequest(this); this.InvokeRepeating(new Action(this.SetupTimersAndListData), 0f, 1f, false); } public override void Close() { this._lastScrollPositionsPerTab[this._tabView.ActiveTabIndex] = new BuildDemolishPopupView.CachedScrollPosition(this._scrollRect.content.rect.width, this._scrollRect.horizontalNormalizedPosition); this.CancelInvoke(new Action(this.SetupTimersAndListData)); this.RemoveAllItems(); SingletonMonobehaviour.Instance.PopUnlimitedFPSRequest(this); base.Close(); } public void OnSpeedupClicked() { int num; Currencies cost = this.SpeedupCost(out num); ((BuildDemolishPopupState)this.State).Speedup(cost); } public void OnOpenShopClicked() { ((BuildDemolishPopupState)this.State).OpenCranesShop(); } public void SwitchTab(BuildDemolishTabs buildDemolishTab) { this._tabView.ActiveTabIndex = (int)buildDemolishTab; this._titleLabel.LocalizedString = this._tabView.LocalizedTitle; this.SwitchContent(); } private BuildDemolishTabs CurrentTab { get { return (BuildDemolishTabs)this._tabView.ActiveTabIndex; } } private void SetupTimersAndListData() { this.UpdateSpeedupCost(false); this.UpdateListData(); this.UpdateBadgeCounts(); } private void UpdateSpeedupCost(bool updateActiveState) { int num; Currencies currencies = this.SpeedupCost(out num); if (updateActiveState) { this._speedupButton.gameObject.SetActive(true); } if (currencies.Contains("Gold")) { this._speedupCostIcon.sprite = SingletonMonobehaviour.Instance.GetAsset(UISpriteType.GoldSmall); } else { this._speedupCostIcon.sprite = SingletonMonobehaviour.Instance.GetAsset(UISpriteType.CashSmall); } if (num > 0) { this._speedupButton.Interactable = true; this._speedupCostLabel.LocalizedString = Localization.Integer(Math.Max(currencies.GetValue("Cash"), currencies.GetValue("Gold"))); this._descriptionLabel.LocalizedString = Localization.Format(Localization.Key("time_remaining"), new ILocalizedString[] { Localization.TimeSpan(TimeSpan.FromSeconds((double)num), false) }); } else { this._speedupButton.Interactable = false; this._speedupCostLabel.LocalizedString = Localization.Integer(0m); this._descriptionLabel.LocalizedString = Localization.EmptyLocalizedString; } } private void UpdateListData() { BuildDemolishPopupState buildDemolishPopupState = (BuildDemolishPopupState)this.State; BuildDemolishTabs currentTab = this.CurrentTab; bool flag; if (currentTab != BuildDemolishTabs.Build) { if (currentTab != BuildDemolishTabs.Demolish) { UnityEngine.Debug.LogErrorFormat("Missing UpdateListData implementation for '{0}'", new object[] { this.CurrentTab }); return; } flag = (buildDemolishPopupState.DemolishListCount > 0); } else { flag = (buildDemolishPopupState.BuildListCount > 0); } if (this._items.Count > 0) { while (buildDemolishPopupState.BuildListCount > 0) { if (!buildDemolishPopupState.IsFirstBuildingBuilt()) { break; } if (this.CurrentTab == BuildDemolishTabs.Build) { BuildDemolishItem buildDemolishItem = this._items[0]; UnityEngine.Object.Destroy(buildDemolishItem.gameObject); this._items.Remove(buildDemolishItem); } } while (buildDemolishPopupState.DemolishListCount > 0) { if (!buildDemolishPopupState.IsFirstBuildingDemolished()) { break; } if (this.CurrentTab == BuildDemolishTabs.Demolish) { BuildDemolishItem buildDemolishItem2 = this._items[0]; UnityEngine.Object.Destroy(buildDemolishItem2.gameObject); this._items.Remove(buildDemolishItem2); } } } BuildDemolishTabs currentTab2 = this.CurrentTab; if (currentTab2 != BuildDemolishTabs.Build) { if (currentTab2 == BuildDemolishTabs.Demolish) { if (flag && buildDemolishPopupState.DemolishListCount == 0) { this.SwitchContent(); } } } else if (flag && buildDemolishPopupState.BuildListCount == 0) { this.SwitchContent(); } } private void UpdateBadgeCounts() { BuildDemolishPopupState buildDemolishPopupState = (BuildDemolishPopupState)this.State; this._tabView.UpdateBadge(0, buildDemolishPopupState.BuildListCount); this._tabView.UpdateBadge(1, buildDemolishPopupState.DemolishListCount); } private Currencies SpeedupCost(out int totalTime) { BuildDemolishTabs currentTab = this.CurrentTab; if (currentTab == BuildDemolishTabs.Build) { return ((BuildDemolishPopupState)this.State).BuildQueueSpeedupCost(out totalTime); } if (currentTab != BuildDemolishTabs.Demolish) { UnityEngine.Debug.LogErrorFormat("Missing speedup cost implementation for '{0}'", new object[] { this.CurrentTab }); totalTime = 0; return new Currencies("Cash", 0m); } return ((BuildDemolishPopupState)this.State).DemolishQueueSpeedupCost(out totalTime); } private void RemoveAllItems() { int count = this._items.Count; for (int i = 0; i < count; i++) { UnityEngine.Object.Destroy(this._items[i].gameObject); } this._items.Clear(); } private void OnTabIndexChanged(int oldIndex, int newIndex) { this._lastScrollPositionsPerTab[oldIndex] = new BuildDemolishPopupView.CachedScrollPosition(this._scrollRect.content.rect.width, this._scrollRect.horizontalNormalizedPosition); ((BuildDemolishPopupState)this.State).SwitchTab((BuildDemolishTabs)newIndex); } private void SwitchContent() { BuildDemolishPopupState buildDemolishPopupState = (BuildDemolishPopupState)this.State; this.RemoveAllItems(); BuildDemolishTabs currentTab = this.CurrentTab; if (currentTab != BuildDemolishTabs.Build) { if (currentTab != BuildDemolishTabs.Demolish) { UnityEngine.Debug.LogErrorFormat("Missing SwitchContent implementation for '{0}'", new object[] { this.CurrentTab }); } else { this._cranesContainer.SetActive(false); int num = buildDemolishPopupState.DemolishListCount; if (num > 0) { this._emptyQueueContainer.SetActive(false); this._nonEmptyQueueContainer.SetActive(true); this._speedupContainer.SetActive(true); for (int i = 0; i < num; i++) { BuildDemolishItem buildDemolishItem = UnityEngine.Object.Instantiate(this._itemPrefab, this._itemLayoutGroup.transform); buildDemolishItem.SetBuilding(buildDemolishPopupState.GetDemolishListItem(i), new Action(this.CancelDemolish)); this._items.Add(buildDemolishItem); } } else { this._emptyQueueContainer.SetActive(true); this._nonEmptyQueueContainer.SetActive(false); this._speedupContainer.SetActive(false); this._emptyQueueLabel.LocalizedString = Localization.Key("bulldozer_empty"); } } } else { this._cranesContainer.SetActive(true); CIGBuilderManager instance = SingletonMonobehaviour.Instance; int num = buildDemolishPopupState.BuildListCount; if (num > 0) { this._emptyQueueContainer.SetActive(false); this._nonEmptyQueueContainer.SetActive(true); this._speedupContainer.SetActive(true); for (int j = 0; j < num; j++) { BuildDemolishItem buildDemolishItem2 = UnityEngine.Object.Instantiate(this._itemPrefab, this._itemLayoutGroup.transform); buildDemolishItem2.SetInfo(buildDemolishPopupState.GetBuildListItem(j)); this._items.Add(buildDemolishItem2); } } else { this._emptyQueueContainer.SetActive(true); this._nonEmptyQueueContainer.SetActive(false); this._speedupContainer.SetActive(false); this._emptyQueueLabel.LocalizedString = Localization.Key("crane_empty"); } } float horizontalNormalizedPosition = 0f; BuildDemolishPopupView.CachedScrollPosition cachedScrollPosition; if (this._lastScrollPositionsPerTab.TryGetValue(this._tabView.ActiveTabIndex, out cachedScrollPosition)) { horizontalNormalizedPosition = cachedScrollPosition.ContentWidth / this._scrollRect.content.rect.width * cachedScrollPosition.NormalizedHorizontalPosition; } this._scrollRect.horizontalNormalizedPosition = horizontalNormalizedPosition; this.UpdateSpeedupCost(true); } private void CancelDemolish(BuildDemolishItem buildDemolishItem, Building building) { if (!this._items.Contains(buildDemolishItem)) { UnityEngine.Debug.LogWarningFormat("Can't cancel the demolishment of a non-existing item.", new object[0]); return; } BuildDemolishPopupState buildDemolishPopupState = (BuildDemolishPopupState)this.State; if (buildDemolishPopupState.CancelDemolish(building)) { this._items.Remove(buildDemolishItem); UnityEngine.Object.Destroy(buildDemolishItem.gameObject); if (this._items.Count == 0) { this.SwitchContent(); } else { this.SetupTimersAndListData(); } } } [SerializeField] private LocalizedText _titleLabel; [SerializeField] private TabView _tabView; [SerializeField] private ScrollRect _scrollRect; [SerializeField] private GameObject _emptyQueueContainer; [SerializeField] private GameObject _nonEmptyQueueContainer; [SerializeField] private GameObject _cranesContainer; [SerializeField] private GameObject _speedupContainer; [SerializeField] private LocalizedText _descriptionLabel; [SerializeField] private LocalizedText _emptyQueueLabel; [SerializeField] private BuildDemolishItem _itemPrefab; [SerializeField] private LayoutGroup _itemLayoutGroup; [SerializeField] private InteractableButton _speedupButton; [SerializeField] private Image _speedupCostIcon; [SerializeField] private LocalizedText _speedupCostLabel; private readonly Dictionary _lastScrollPositionsPerTab = new Dictionary(); private List _items = new List(); [StructLayout(LayoutKind.Sequential, Size = 1)] private struct CachedScrollPosition { public CachedScrollPosition(float contentWidth, float normalizedHorizontalPosition) { this.ContentWidth = contentWidth; this.NormalizedHorizontalPosition = normalizedHorizontalPosition; } public float ContentWidth { get; set; } public float NormalizedHorizontalPosition { get; set; } } }