mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-25 05:58:35 +01:00
Removed LINQ Any and All usage in various methods to improve memory usage
This commit is contained in:
parent
374d936be2
commit
abac738123
9 changed files with 111 additions and 23 deletions
|
@ -15,6 +15,7 @@ Improvements
|
||||||
- Moved sound-related classes into Sound namespace
|
- 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
|
- Added ColorExtensions.Invert and made ColorHelper.Invert obsolete
|
||||||
|
- Removed LINQ Any and All usage in various methods to improve memory usage
|
||||||
|
|
||||||
Fixes
|
Fixes
|
||||||
- Set default values for InputHandler held and pressed keys to avoid an exception if buttons are held in the very first frame
|
- 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
|
Additions
|
||||||
- Added a masking character to TextField to allow for password-style text fields
|
- 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
|
Fixes
|
||||||
- Fixed a crash if a paragraph has a link formatting code, but no font
|
- Fixed a crash if a paragraph has a link formatting code, but no font
|
||||||
|
|
||||||
|
|
|
@ -118,8 +118,10 @@ namespace MLEM.Data {
|
||||||
|
|
||||||
if (dict.ContainsKey(value))
|
if (dict.ContainsKey(value))
|
||||||
throw new ArgumentException($"Duplicate value {value}", nameof(value));
|
throw new ArgumentException($"Duplicate value {value}", nameof(value));
|
||||||
if (dict.Values.Any(v => v.name == name))
|
foreach (var v in dict.Values) {
|
||||||
|
if (v.name == name)
|
||||||
throw new ArgumentException($"Duplicate name {name}", nameof(name));
|
throw new ArgumentException($"Duplicate name {name}", nameof(name));
|
||||||
|
}
|
||||||
|
|
||||||
var ret = Construct(typeof(T), name, value);
|
var ret = Construct(typeof(T), name, value);
|
||||||
dict.Add(value, ret);
|
dict.Add(value, ret);
|
||||||
|
|
|
@ -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>
|
/// <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>
|
/// <returns>An enumerable of collision infos for that area</returns>
|
||||||
public IEnumerable<TileCollisionInfo> GetCollidingTiles(RectangleF area, Func<TileCollisionInfo, bool> included = null) {
|
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 minX = Math.Max(0, area.Left.Floor());
|
||||||
var maxX = Math.Min(this.map.Width - 1, area.Right.Floor());
|
var maxX = Math.Min(this.map.Width - 1, area.Right.Floor());
|
||||||
var minY = Math.Max(0, area.Top.Floor());
|
var minY = Math.Max(0, area.Top.Floor());
|
||||||
|
|
|
@ -24,19 +24,43 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A <see cref="Rule"/> that allows any visible character and spaces
|
/// A <see cref="Rule"/> that allows any visible character and spaces
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// A <see cref="Rule"/> that only allows letters
|
/// A <see cref="Rule"/> that only allows letters
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// A <see cref="Rule"/> that only allows numerals
|
/// A <see cref="Rule"/> that only allows numerals
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// A <see cref="Rule"/> that only allows letters and numerals
|
/// A <see cref="Rule"/> that only allows letters and numerals
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// A <see cref="Rule"/> that only allows characters not contained in <see cref="Path.GetInvalidPathChars"/>
|
/// A <see cref="Rule"/> that only allows characters not contained in <see cref="Path.GetInvalidPathChars"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -129,8 +129,12 @@ namespace MLEM.Ui.Elements {
|
||||||
public void AddToElement(Element elementToHover) {
|
public void AddToElement(Element elementToHover) {
|
||||||
elementToHover.OnMouseEnter += element => {
|
elementToHover.OnMouseEnter += element => {
|
||||||
// only display the tooltip if there is anything in it
|
// only display the tooltip if there is anything in it
|
||||||
if (this.Children.Any(c => !c.IsHidden))
|
foreach (var c in this.Children) {
|
||||||
|
if (!c.IsHidden) {
|
||||||
this.Display(element.System, element.GetType().Name + "Tooltip");
|
this.Display(element.System, element.GetType().Name + "Tooltip");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
elementToHover.OnMouseExit += element => this.Remove();
|
elementToHover.OnMouseExit += element => this.Remove();
|
||||||
}
|
}
|
||||||
|
|
|
@ -465,8 +465,14 @@ namespace MLEM.Ui {
|
||||||
this.CanSelectContent = true;
|
this.CanSelectContent = true;
|
||||||
};
|
};
|
||||||
this.OnElementRemoved += e => {
|
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;
|
this.CanSelectContent = false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -100,7 +100,13 @@ namespace MLEM.Formatting {
|
||||||
/// <param name="scale">The scale that the string is drawn at</param>
|
/// <param name="scale">The scale that the string is drawn at</param>
|
||||||
/// <returns>The token under the target position</returns>
|
/// <returns>The token under the target position</returns>
|
||||||
public Token GetTokenUnderPos(Vector2 stringPos, Vector2 target, float scale) {
|
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)"/>
|
/// <inheritdoc cref="GenericFont.DrawString(SpriteBatch,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
|
||||||
|
|
|
@ -363,7 +363,11 @@ namespace MLEM.Input {
|
||||||
/// <param name="modifier">The modifier key</param>
|
/// <param name="modifier">The modifier key</param>
|
||||||
/// <returns>If the modifier key is down</returns>
|
/// <returns>If the modifier key is down</returns>
|
||||||
public bool IsModifierKeyDown(ModifierKey modifier) {
|
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>
|
/// <summary>
|
||||||
|
@ -570,18 +574,30 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IsDown"/>
|
/// <inheritdoc cref="IsDown"/>
|
||||||
public bool IsAnyDown(params GenericInput[] control) {
|
public bool IsAnyDown(params GenericInput[] controls) {
|
||||||
return control.Any(c => this.IsDown(c));
|
foreach (var control in controls) {
|
||||||
|
if (this.IsDown(control))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IsUp"/>
|
/// <inheritdoc cref="IsUp"/>
|
||||||
public bool IsAnyUp(params GenericInput[] control) {
|
public bool IsAnyUp(params GenericInput[] controls) {
|
||||||
return control.Any(c => this.IsUp(c));
|
foreach (var control in controls) {
|
||||||
|
if (this.IsUp(control))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IsPressed"/>
|
/// <inheritdoc cref="IsPressed"/>
|
||||||
public bool IsAnyPressed(params GenericInput[] control) {
|
public bool IsAnyPressed(params GenericInput[] controls) {
|
||||||
return control.Any(c => this.IsPressed(c));
|
foreach (var control in controls) {
|
||||||
|
if (this.IsPressed(control))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -89,7 +89,11 @@ namespace MLEM.Input {
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
/// <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>
|
/// <returns>Whether this keybind is considered to be down</returns>
|
||||||
public bool IsDown(InputHandler handler, int gamepadIndex = -1) {
|
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>
|
/// <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>
|
/// <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>
|
/// <returns>Whether this keybind is considered to be pressed</returns>
|
||||||
public bool IsPressed(InputHandler handler, int gamepadIndex = -1) {
|
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>
|
/// <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>
|
/// <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>
|
/// <returns>Whether any of this keyboard's modifier keys are down</returns>
|
||||||
public bool IsModifierDown(InputHandler handler, int gamepadIndex = -1) {
|
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>
|
/// <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>
|
/// <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>
|
/// <returns>Whether this combination's modifiers are down</returns>
|
||||||
public bool IsModifierDown(InputHandler handler, int gamepadIndex = -1) {
|
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>
|
/// <summary>
|
||||||
|
|
Loading…
Reference in a new issue