Advertisement
Madrayken

Untitled

Apr 15th, 2025
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | Source Code | 0 0
  1. using Godot;
  2. using System.Collections.Generic;
  3.  
  4. // This is data passed to, and held by a list of Alert Boxes waiting for attention
  5. public class AlertBoxData(string alert_text, string button_text = null)
  6. {
  7.     public string Text = alert_text;
  8.     public string ButtonText = button_text;
  9. }
  10.  
  11. public partial class AlertBox : Control
  12. {
  13.     [Export] private PanelContainer _panelContainer;
  14.     [Export] private Button _button;
  15.     [Export] private RichTextLabel _text;
  16.  
  17.     private List<AlertBoxData> _alertBoxes = []; // A list of alert boxes
  18.  
  19.     private bool _isActive;
  20.     private double _openFraction;
  21.     private static double OPEN_TIME = 0.5f;
  22.  
  23.     public void AddAlertBoxToList(string alert_text, string button_text = null)
  24.     {
  25.         _alertBoxes.Add(new AlertBoxData(alert_text, button_text));
  26.         Open(_alertBoxes[_alertBoxes.Count - 1]);
  27.     }
  28.  
  29.     public override void _Ready()
  30.     {
  31.         base._Ready();
  32.         _isActive = false;
  33.         Hide();
  34.     }
  35.  
  36.     public void Open(AlertBoxData data)
  37.     {
  38.         _isActive = true;
  39.         _text.Text = data.Text;
  40.         Show();
  41.     }
  42.  
  43.     public void Close()
  44.     {
  45.         _isActive = false;
  46.     }
  47.  
  48.     public override void _Process(double delta)
  49.     {
  50.         base._Process(delta);
  51.  
  52.         if (_isActive)
  53.         {
  54.             _panelContainer.SelfModulate = Game.Instance.PrimaryColour;
  55.             _button.SelfModulate = Game.Instance.PrimaryColour;
  56.             _openFraction = Mathf.Min(1, _openFraction + delta / OPEN_TIME);
  57.         }
  58.         else
  59.         {
  60.             double old_frac = _openFraction;
  61.             _openFraction = Mathf.Max(0, _openFraction - delta / OPEN_TIME);
  62.             if (old_frac > 0 && _openFraction <= 0)
  63.             {
  64.                 Hide();
  65.                 UpdateToNextAlertBoxInList();
  66.             }
  67.         }
  68.     }
  69.  
  70.     public void ClearAlertBoxes()
  71.     {
  72.         _alertBoxes.Clear();
  73.     }
  74.  
  75.     private void UpdateToNextAlertBoxInList()
  76.     {
  77.         RemoveCurrentAlertBox();
  78.  
  79.         if (_alertBoxes.Count > 0)
  80.         {
  81.             Open(_alertBoxes[0]);
  82.         }
  83.     }
  84.  
  85.     public void RemoveCurrentAlertBox()
  86.     {
  87.         if (_alertBoxes.Count > 0)
  88.             _alertBoxes.RemoveAt(0);
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement