Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Godot;
- using System.Collections.Generic;
- // This is data passed to, and held by a list of Alert Boxes waiting for attention
- public class AlertBoxData(string alert_text, string button_text = null)
- {
- public string Text = alert_text;
- public string ButtonText = button_text;
- }
- public partial class AlertBox : Control
- {
- [Export] private PanelContainer _panelContainer;
- [Export] private Button _button;
- [Export] private RichTextLabel _text;
- private List<AlertBoxData> _alertBoxes = []; // A list of alert boxes
- private bool _isActive;
- private double _openFraction;
- private static double OPEN_TIME = 0.5f;
- public void AddAlertBoxToList(string alert_text, string button_text = null)
- {
- _alertBoxes.Add(new AlertBoxData(alert_text, button_text));
- Open(_alertBoxes[_alertBoxes.Count - 1]);
- }
- public override void _Ready()
- {
- base._Ready();
- _isActive = false;
- Hide();
- }
- public void Open(AlertBoxData data)
- {
- _isActive = true;
- _text.Text = data.Text;
- Show();
- }
- public void Close()
- {
- _isActive = false;
- }
- public override void _Process(double delta)
- {
- base._Process(delta);
- if (_isActive)
- {
- _panelContainer.SelfModulate = Game.Instance.PrimaryColour;
- _button.SelfModulate = Game.Instance.PrimaryColour;
- _openFraction = Mathf.Min(1, _openFraction + delta / OPEN_TIME);
- }
- else
- {
- double old_frac = _openFraction;
- _openFraction = Mathf.Max(0, _openFraction - delta / OPEN_TIME);
- if (old_frac > 0 && _openFraction <= 0)
- {
- Hide();
- UpdateToNextAlertBoxInList();
- }
- }
- }
- public void ClearAlertBoxes()
- {
- _alertBoxes.Clear();
- }
- private void UpdateToNextAlertBoxInList()
- {
- RemoveCurrentAlertBox();
- if (_alertBoxes.Count > 0)
- {
- Open(_alertBoxes[0]);
- }
- }
- public void RemoveCurrentAlertBox()
- {
- if (_alertBoxes.Count > 0)
- _alertBoxes.RemoveAt(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement