1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-25 09:49:23 +01:00

Removed LINQ Any and All usage in various methods to improve memory usage

This commit is contained in:
Ell 2021-07-18 22:18:46 +02:00
parent 374d936be2
commit abac738123
9 changed files with 111 additions and 23 deletions

View file

@ -13,8 +13,9 @@ Additions
Improvements
- Improved NinePatch memory performance
- Moved sound-related classes into Sound namespace
- Added customizable overloads for Keybind, Combination and GenericInput ToString methods
- Added customizable overloads for Keybind, Combination and GenericInput ToString methods
- Added ColorExtensions.Invert and made ColorHelper.Invert obsolete
- Removed LINQ Any and All usage in various methods to improve memory usage
Fixes
- Set default values for InputHandler held and pressed keys to avoid an exception if buttons are held in the very first frame
@ -23,6 +24,9 @@ Fixes
Additions
- Added a masking character to TextField to allow for password-style text fields
Improvements
- Removed LINQ Any and All usage in various methods to improve memory usage
Fixes
- Fixed a crash if a paragraph has a link formatting code, but no font

View file

@ -118,8 +118,10 @@ namespace MLEM.Data {
if (dict.ContainsKey(value))
throw new ArgumentException($"Duplicate value {value}", nameof(value));
if (dict.Values.Any(v => v.name == name))
throw new ArgumentException($"Duplicate name {name}", nameof(name));
foreach (var v in dict.Values) {
if (v.name == name)
throw new ArgumentException($"Duplicate name {name}", nameof(name));
}
var ret = Construct(typeof(T), name, value);
dict.Add(value, ret);

View file

@ -72,7 +72,15 @@ namespace MLEM.Extended.Tiled {
/// <param name="included">A function that determines if a certain info should be included or not</param>
/// <returns>An enumerable of collision infos for that area</returns>
public IEnumerable<TileCollisionInfo> GetCollidingTiles(RectangleF area, Func<TileCollisionInfo, bool> included = null) {
var inclusionFunc = included ?? (tile => tile.Collisions.Any(c => c.Intersects(area)));
bool DefaultInclusion(TileCollisionInfo tile) {
foreach (var c in tile.Collisions) {
if (c.Intersects(area))
return true;
}
return false;
}
var inclusionFunc = included ?? DefaultInclusion;
var minX = Math.Max(0, area.Left.Floor());
var maxX = Math.Min(this.map.Width - 1, area.Right.Floor());
var minY = Math.Max(0, area.Top.Floor());

View file

@ -24,19 +24,43 @@ namespace MLEM.Ui.Elements {
/// <summary>
/// A <see cref="Rule"/> that allows any visible character and spaces
/// </summary>
public static readonly Rule DefaultRule = (field, add) => !add.Any(char.IsControl);
public static readonly Rule DefaultRule = (field, add) => {
foreach (var c in add) {
if (char.IsControl(c))
return false;
}
return true;
};
/// <summary>
/// A <see cref="Rule"/> that only allows letters
/// </summary>
public static readonly Rule OnlyLetters = (field, add) => add.All(char.IsLetter);
public static readonly Rule OnlyLetters = (field, add) => {
foreach (var c in add) {
if (!char.IsLetter(c))
return false;
}
return true;
};
/// <summary>
/// A <see cref="Rule"/> that only allows numerals
/// </summary>
public static readonly Rule OnlyNumbers = (field, add) => add.All(char.IsNumber);
public static readonly Rule OnlyNumbers = (field, add) => {
foreach (var c in add) {
if (!char.IsNumber(c))
return false;
}
return true;
};
/// <summary>
/// A <see cref="Rule"/> that only allows letters and numerals
/// </summary>
public static readonly Rule LettersNumbers = (field, add) => add.All(c => char.IsLetter(c) || char.IsNumber(c));
public static readonly Rule LettersNumbers = (field, add) => {
foreach (var c in add) {
if (!char.IsLetter(c) || !char.IsNumber(c))
return false;
}
return true;
};
/// <summary>
/// A <see cref="Rule"/> that only allows characters not contained in <see cref="Path.GetInvalidPathChars"/>
/// </summary>

View file

@ -129,8 +129,12 @@ namespace MLEM.Ui.Elements {
public void AddToElement(Element elementToHover) {
elementToHover.OnMouseEnter += element => {
// only display the tooltip if there is anything in it
if (this.Children.Any(c => !c.IsHidden))
this.Display(element.System, element.GetType().Name + "Tooltip");
foreach (var c in this.Children) {
if (!c.IsHidden) {
this.Display(element.System, element.GetType().Name + "Tooltip");
break;
}
}
};
elementToHover.OnMouseExit += element => this.Remove();
}

View file

@ -465,8 +465,14 @@ namespace MLEM.Ui {
this.CanSelectContent = true;
};
this.OnElementRemoved += e => {
if (e.CanBeSelected && !this.Element.GetChildren(regardGrandchildren: true).Any(c => c.CanBeSelected))
if (e.CanBeSelected) {
// check if removing this element removed all other selectable elements
foreach (var c in this.Element.GetChildren(regardGrandchildren: true)) {
if (c.CanBeSelected)
return;
}
this.CanSelectContent = false;
}
};
}

View file

@ -100,7 +100,13 @@ namespace MLEM.Formatting {
/// <param name="scale">The scale that the string is drawn at</param>
/// <returns>The token under the target position</returns>
public Token GetTokenUnderPos(Vector2 stringPos, Vector2 target, float scale) {
return this.Tokens.FirstOrDefault(t => t.GetArea(stringPos, scale).Any(r => r.Contains(target)));
foreach (var token in this.Tokens) {
foreach (var rect in token.GetArea(stringPos, scale)) {
if (rect.Contains(target))
return token;
}
}
return null;
}
/// <inheritdoc cref="GenericFont.DrawString(SpriteBatch,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>

View file

@ -363,7 +363,11 @@ namespace MLEM.Input {
/// <param name="modifier">The modifier key</param>
/// <returns>If the modifier key is down</returns>
public bool IsModifierKeyDown(ModifierKey modifier) {
return modifier.GetKeys().Any(this.IsKeyDown);
foreach (var key in modifier.GetKeys()) {
if (this.IsKeyDown(key))
return true;
}
return false;
}
/// <summary>
@ -570,18 +574,30 @@ namespace MLEM.Input {
}
/// <inheritdoc cref="IsDown"/>
public bool IsAnyDown(params GenericInput[] control) {
return control.Any(c => this.IsDown(c));
public bool IsAnyDown(params GenericInput[] controls) {
foreach (var control in controls) {
if (this.IsDown(control))
return true;
}
return false;
}
/// <inheritdoc cref="IsUp"/>
public bool IsAnyUp(params GenericInput[] control) {
return control.Any(c => this.IsUp(c));
public bool IsAnyUp(params GenericInput[] controls) {
foreach (var control in controls) {
if (this.IsUp(control))
return true;
}
return false;
}
/// <inheritdoc cref="IsPressed"/>
public bool IsAnyPressed(params GenericInput[] control) {
return control.Any(c => this.IsPressed(c));
public bool IsAnyPressed(params GenericInput[] controls) {
foreach (var control in controls) {
if (this.IsPressed(control))
return true;
}
return false;
}
/// <summary>

View file

@ -89,7 +89,11 @@ namespace MLEM.Input {
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
/// <returns>Whether this keybind is considered to be down</returns>
public bool IsDown(InputHandler handler, int gamepadIndex = -1) {
return this.combinations.Any(c => c.IsDown(handler, gamepadIndex));
foreach (var combination in this.combinations) {
if (combination.IsDown(handler, gamepadIndex))
return true;
}
return false;
}
/// <summary>
@ -100,7 +104,11 @@ namespace MLEM.Input {
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
/// <returns>Whether this keybind is considered to be pressed</returns>
public bool IsPressed(InputHandler handler, int gamepadIndex = -1) {
return this.combinations.Any(c => c.IsPressed(handler, gamepadIndex));
foreach (var combination in this.combinations) {
if (combination.IsPressed(handler, gamepadIndex))
return true;
}
return false;
}
/// <summary>
@ -111,7 +119,11 @@ namespace MLEM.Input {
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
/// <returns>Whether any of this keyboard's modifier keys are down</returns>
public bool IsModifierDown(InputHandler handler, int gamepadIndex = -1) {
return this.combinations.Any(c => c.IsModifierDown(handler, gamepadIndex));
foreach (var combination in this.combinations) {
if (combination.IsModifierDown(handler, gamepadIndex))
return true;
}
return false;
}
/// <summary>
@ -198,7 +210,13 @@ namespace MLEM.Input {
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
/// <returns>Whether this combination's modifiers are down</returns>
public bool IsModifierDown(InputHandler handler, int gamepadIndex = -1) {
return this.Modifiers.Length <= 0 || this.Modifiers.Any(m => handler.IsDown(m, gamepadIndex));
if (this.Modifiers.Length <= 0)
return true;
foreach (var modifier in this.Modifiers) {
if (handler.IsDown(modifier, gamepadIndex))
return true;
}
return false;
}
/// <summary>