Skip to content

File ObjectPool.cs

File List > LemonUI > LemonUI > ObjectPool.cs

Go to the documentation of this file


#if FIVEM
using CitizenFX.Core.Native;
#elif RAGEMP
using RAGE.Game;
using RAGE.NUI;
#elif RPH
using Rage;
using Rage.Native;
#elif SHVDN3 || SHVDNC
using GTA.Native;
#elif ALTV
using AltV.Net.Client;
#endif
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using LemonUI.Tools;

namespace LemonUI
{
    public class ObjectPool : IEnumerable<IProcessable>
    {
        #region Fields

        private SizeF lastKnownResolution = GameScreen.AbsoluteResolution;
        private float lastKnownSafezone = SafeZone.Size;
        private readonly List<IProcessable> objects = new List<IProcessable>();

        #endregion

        #region Properties

        public bool AreAnyVisible
        {
            get
            {
                for (int i = 0; i < objects.Count; i++)
                {
                    if (objects[i].Visible)
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        #endregion

        #region Events

        public event ResolutionChangedEventHandler ResolutionChanged;
        public event SafeZoneChangedEventHandler SafezoneChanged;

        #endregion

        #region Tools

        private void DetectResolutionChanges()
        {
            // Get the current resolution
            SizeF resolution = GameScreen.AbsoluteResolution;
            // If the old res does not matches the current one
            if (lastKnownResolution != resolution)
            {
                // Trigger the event
                ResolutionChanged?.Invoke(this, new ResolutionChangedEventArgs(lastKnownResolution, resolution));
                // Refresh everything
                RefreshAll();
                // And save the new resolution
                lastKnownResolution = resolution;
            }
        }
        private void DetectSafezoneChanges()
        {
            // Get the current Safezone size
            float safezone = SafeZone.Size;
            // If is not the same as the last one
            if (lastKnownSafezone != safezone)
            {
                // Trigger the event
                SafezoneChanged?.Invoke(this, new SafeZoneChangedEventArgs(lastKnownSafezone, safezone));
                // Refresh everything
                RefreshAll();
                // And save the new safezone
                lastKnownSafezone = safezone;
            }
        }

        #endregion

        #region Functions

        public IEnumerator<IProcessable> GetEnumerator() => objects.GetEnumerator();
        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
        public void Add(IProcessable obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (objects.Contains(obj))
            {
                throw new InvalidOperationException("The object is already part of this pool.");
            }

            objects.Add(obj);
        }
        public void Remove(IProcessable obj) => objects.Remove(obj);
        public void ForEach<T>(Action<T> action)
        {
            for (int i = 0; i < objects.Count; i++)
            {
                if (objects[i] is T conv)
                {
                    action(conv);
                }
            }
        }
        public void RefreshAll()
        {
            for (int i = 0; i < objects.Count; i++)
            {
                if (objects[i] is IRecalculable recal)
                {
                    recal.Recalculate();
                }
            }
        }
        public void HideAll()
        {
            for (int i = 0; i < objects.Count; i++)
            {
                objects[i].Visible = false;
            }
        }
        public void Process()
        {
            DetectResolutionChanges();
            DetectSafezoneChanges();

            for (int i = 0; i < objects.Count; i++)
            {
                objects[i].Process();
            }
        }

        #endregion
    }
}