Skip to content

Commit

Permalink
Use indexers instead of ConcurrentDictionary (due to being non existe…
Browse files Browse the repository at this point in the history
…nt in FiveM and RageMP, closes #87)
  • Loading branch information
justalemon committed Oct 3, 2022
1 parent 76207ee commit 5ee8493
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 52 deletions.
4 changes: 3 additions & 1 deletion LemonUI/Menus/NativeColorPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,10 @@ public void Remove(NativeColorData color)
/// <param name="func"></param>
public void Remove(Func<NativeColorData, bool> func)
{
foreach (NativeColorData color in new List<NativeColorData>(Colors))
for (int i = 0; i < Colors.Count; i++)
{
NativeColorData color = Colors[i];

if (func(color))
{
Colors.Remove(color);
Expand Down
21 changes: 9 additions & 12 deletions LemonUI/Menus/NativeMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,14 +1295,12 @@ private void Draw()
{
NativeItem selected = SelectedItem;

// Let's start with the basics
// Draw the banner image and text
if (bannerImage != null)
{
bannerImage.Draw();
Title?.Draw();
}
// And then the subtitle with text and item count

if (ShouldDrawSubtitleBackground)
{
subtitleImage.Draw();
Expand All @@ -1312,25 +1310,24 @@ private void Draw()
countText.Draw();
}
}
// If there is some description text, draw the text and background

if (!string.IsNullOrWhiteSpace(descriptionText.Text))
{
descriptionRect.Draw();
descriptionText.Draw();
}

// Time for the items!
// If there are none, return and do nothing
if (Items.Count == 0)
{
return;
}

// Otherwise, start with the background
backgroundImage?.Draw();
// Then, draw all of the items with the exception of the one selected
foreach (NativeItem item in visibleItems)

for (int i = 0; i < Items.Count; i++)
{
NativeItem item = Items[i];

if (item == selected)
{
continue;
Expand Down Expand Up @@ -1467,10 +1464,10 @@ public void Remove(NativeItem item)
/// <param name="pred">The function to use as a check.</param>
public void Remove(Func<NativeItem, bool> pred)
{
List<NativeItem> items = new List<NativeItem>(Items);

foreach (NativeItem item in items)
for (int i = 0; i < Items.Count; i++)
{
NativeItem item = Items[i];

if (!pred(item))
{
continue;
Expand Down
44 changes: 17 additions & 27 deletions LemonUI/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using GTA.UI;
#endif
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;

namespace LemonUI
Expand Down Expand Up @@ -106,7 +106,7 @@ public class ObjectPool
/// <summary>
/// The list of processable objects.
/// </summary>
private readonly ConcurrentDictionary<int, IProcessable> objects = new ConcurrentDictionary<int, IProcessable>();
private readonly List<IProcessable> objects = new List<IProcessable>();

#endregion

Expand All @@ -119,16 +119,13 @@ public bool AreAnyVisible
{
get
{
// Iterate over the objects
foreach (IProcessable obj in objects.Values)
for (int i = 0; i < objects.Count; i++)
{
// If is visible return true
if (obj.Visible)
if (objects[i].Visible)
{
return true;
}
}
// If none were visible return false
return false;
}
}
Expand Down Expand Up @@ -215,38 +212,33 @@ private void DetectSafezoneChanges()
/// <param name="obj">The object to add.</param>
public void Add(IProcessable obj)
{
// Make sure that the object is not null
if (obj == null)
{
throw new ArgumentNullException(nameof(obj));
}

int key = obj.GetHashCode();
// Otherwise, add it to the general pool
if (objects.ContainsKey(key))
if (objects.Contains(obj))
{
throw new InvalidOperationException("The object is already part of this pool.");
}
objects.TryAdd(key, obj);

objects.Add(obj);
}
/// <summary>
/// Removes the object from the pool.
/// </summary>
/// <param name="obj">The object to remove.</param>
public void Remove(IProcessable obj)
{
objects.TryRemove(obj.GetHashCode(), out _);
}
public void Remove(IProcessable obj) => objects.Remove(obj);
/// <summary>
/// Performs the specified action on each element that matches T.
/// </summary>
/// <typeparam name="T">The type to match.</typeparam>
/// <param name="action">The action delegate to perform on each T.</param>
public void ForEach<T>(Action<T> action)
{
foreach (IProcessable obj in objects.Values)
for (int i = 0; i < objects.Count; i++)
{
if (obj is T conv)
if (objects[i] is T conv)
{
action(conv);
}
Expand All @@ -257,10 +249,9 @@ public void ForEach<T>(Action<T> action)
/// </summary>
public void RefreshAll()
{
// Iterate over the objects and recalculate those possible
foreach (IProcessable obj in objects.Values)
for (int i = 0; i < objects.Count; i++)
{
if (obj is IRecalculable recal)
if (objects[i] is IRecalculable recal)
{
recal.Recalculate();
}
Expand All @@ -271,9 +262,9 @@ public void RefreshAll()
/// </summary>
public void HideAll()
{
foreach (IProcessable obj in objects.Values)
for (int i = 0; i < objects.Count; i++)
{
obj.Visible = false;
objects[i].Visible = false;
}
}
/// <summary>
Expand All @@ -282,13 +273,12 @@ public void HideAll()
/// </summary>
public void Process()
{
// See if there are resolution or safezone changes
DetectResolutionChanges();
DetectSafezoneChanges();
// And process the objects in the pool
foreach (IProcessable obj in objects.Values)

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

Expand Down
20 changes: 8 additions & 12 deletions LemonUI/TimerBars/TimerBarCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public void Remove(TimerBar bar)
/// <param name="func">The function to check the <see cref="TimerBar"/>.</param>
public void Remove(Func<TimerBar, bool> func)
{
// Iterate over the timer bars
foreach (TimerBar bar in new List<TimerBar>(TimerBars))
for (int i = 0; i < TimerBars.Count; i++)
{
// If it matches the function, remove it
TimerBar bar = TimerBars[i];

if (func(bar))
{
TimerBars.Remove(bar);
Expand All @@ -118,18 +118,15 @@ public void Remove(Func<TimerBar, bool> func)
/// </summary>
public void Recalculate()
{
// Get the position of 0,0 while staying safe zone aware
Screen.SetElementAlignment(GFXAlignment.Right, GFXAlignment.Bottom);
PointF pos = Screen.GetRealPosition(PointF.Empty);
Screen.ResetElementAlignment();

// Iterate over the existing timer bars and save the count
int count = 0;
foreach (TimerBar timerBar in TimerBars)

for (int i = 0; i < TimerBars.Count; i++)
{
// And send them to the timer bar
timerBar.Recalculate(new PointF(pos.X - TimerBar.backgroundWidth, pos.Y - (TimerBar.backgroundHeight * (TimerBars.Count - count)) - (TimerBar.separation * (TimerBars.Count - count - 1))));
// Finish by increasing the total count of items
TimerBars[i].Recalculate(new PointF(pos.X - TimerBar.backgroundWidth, pos.Y - (TimerBar.backgroundHeight * (TimerBars.Count - count)) - (TimerBar.separation * (TimerBars.Count - count - 1))));
count++;
}
}
Expand Down Expand Up @@ -162,10 +159,9 @@ public void Process()
Hud.HideComponentThisFrame(HudComponent.StreetName);
Hud.HideComponentThisFrame(HudComponent.VehicleName);
#endif
// Draw the existing timer bars
foreach (TimerBar timerBar in TimerBars)
for (int i = 0; i < TimerBars.Count; i++)
{
timerBar.Draw();
TimerBars[i].Draw();
}
}

Expand Down

0 comments on commit 5ee8493

Please sign in to comment.