Skip to content

Commit

Permalink
Fix buttons for generic popup
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostya Stankevych committed Mar 12, 2016
1 parent e03bf32 commit 229bd89
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 24 deletions.
10 changes: 1 addition & 9 deletions UserInterface/Data/UIGenericPopupData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 10,6 @@ public struct UIGenericPopupData : IUIScreenData
private string _message;
private float _width;
private float _timeout;
private bool _skipInQueue;
private UIGenericPopupButtonData[] _buttons;
#endregion

Expand All @@ -21,17 20,15 @@ public UIGenericPopupData(string header, string message, params UIGenericPopupBu
_message = message;
_width = 0;
_timeout = 0;
_skipInQueue = false;
_buttons = buttons;
}

public UIGenericPopupData(string header, string message, bool skipInQueue = false, float timeout = 0, float width = 0, params UIGenericPopupButtonData[] buttons)
public UIGenericPopupData(string header, string message, float timeout, float width = 0, params UIGenericPopupButtonData[] buttons)
{
_header = header;
_message = message;
_width = width;
_timeout = timeout;
_skipInQueue = skipInQueue;
_buttons = buttons;
}

Expand Down Expand Up @@ -70,11 67,6 @@ public float Timeout
get { return _timeout; }
}

public bool SkipInQueue
{
get { return _skipInQueue; }
}

public UIGenericPopupButtonData[] Buttons
{
get { return _buttons; }
Expand Down
73 changes: 58 additions & 15 deletions UserInterface/Popups/UIGenericPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 16,12 @@ public class UIGenericPopup : UIPopup
[SerializeField]
private Text _message;
[SerializeField]
private UIGenericPopupButton _button;
private UIGenericPopupButton[] _originalButtons;
#endregion

#region Properties
private List<UIGenericPopupButton> _buttons;
private IEnumerator _closingRoutine = null;
#endregion

#region Initialization
Expand All @@ -29,11 30,15 @@ public override void Init()
base.Init();

_buttons = new List<UIGenericPopupButton>();
if (_button != null)
if (_originalButtons != null)
{
_button.ClosePopup = OnClosePopupRequest;
_button.Init();
_buttons.Add(_button);
foreach (UIGenericPopupButton button in _originalButtons)
{
button.ClosePopup = OnClosePopupRequest;
button.Init();
}

_buttons.AddRange(_originalButtons);
}
}

Expand All @@ -42,16 47,25 @@ protected override void Cleanup()
base.Cleanup();

CleanUpButtons();
if (_button != null)
if (_originalButtons != null)
{
_button.ClosePopup -= OnClosePopupRequest;
foreach (UIGenericPopupButton button in _originalButtons)
{
button.ClosePopup -= OnClosePopupRequest;
}
}
}
#endregion

#region Controls
override protected void ProcessCustomData(IUIScreenData customData)
{
if (_closingRoutine != null)
{
StopCoroutine(_closingRoutine);
_closingRoutine = null;
}

base.ProcessCustomData(customData);

bool dataExists = customData != null;
Expand All @@ -60,7 74,11 @@ override protected void ProcessCustomData(IUIScreenData customData)

_header.gameObject.SetActive(dataExists);
_message.gameObject.SetActive(dataExists);
_button.gameObject.SetActive(dataExists);

foreach (UIGenericPopupButton button in _originalButtons)
{
button.gameObject.SetActive(dataExists);
}

if (dataExists)
{
Expand All @@ -79,6 97,11 @@ override protected void ProcessCustomData(IUIScreenData customData)
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, data.Width);
}
}

if (data.Timeout > 0f)
{
CloseWithDelay(data.Timeout);
}
}
}
#endregion
Expand All @@ -104,16 127,18 @@ private void SetText(Text text, string content)

private void SetupButtons(UIGenericPopupButtonData[] buttons)
{
_button.gameObject.SetActive(buttons.Length > 0);

if (buttons.Length > 0)
for (int i = 0; i < _originalButtons.Length; i )
{
_button.Reset();
_originalButtons[i].Reset();
_originalButtons[i].gameObject.SetActive(buttons.Length > i);
}

if (buttons.Length > 0 && _originalButtons.Length > 0)
{
while (_buttons.Count < buttons.Length)
{
UIGenericPopupButton newButton = Instantiate<UIGenericPopupButton>(_button);
newButton.transform.SetParent(_button.transform.parent, false);
UIGenericPopupButton newButton = Instantiate<UIGenericPopupButton>(_originalButtons[0]);
newButton.transform.SetParent(_originalButtons[0].transform.parent, false);
newButton.transform.localScale = Vector3.one;
newButton.Init();
newButton.Reset();
Expand All @@ -130,7 155,7 @@ private void SetupButtons(UIGenericPopupButtonData[] buttons)

private void CleanUpButtons()
{
for (int i = _buttons.Count - 1; i > 0; i--)
for (int i = _buttons.Count - 1; i > _originalButtons.Length - 1; i--)
{
_buttons[i].ClosePopup -= OnClosePopupRequest;

Expand All @@ -140,6 165,24 @@ private void CleanUpButtons()
_buttons.RemoveAt(i);
}
}

private void CloseWithDelay(float delay)
{
if (_closingRoutine != null)
{
StopCoroutine(_closingRoutine);
_closingRoutine = null;
}

_closingRoutine = CloseWithDelayRoutine(delay);
StartCoroutine(_closingRoutine);
}

private IEnumerator CloseWithDelayRoutine(float delay)
{
yield return new WaitForSeconds(delay);
Close();
}
#endregion
}
}

0 comments on commit 229bd89

Please sign in to comment.