1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

Improved UiParser.ParseImage with locks and a callback action

This commit is contained in:
Ell 2024-03-30 11:52:23 +01:00
parent 23103613cd
commit 60af21aff4
2 changed files with 23 additions and 6 deletions

View file

@ -46,6 +46,7 @@ Improvements
- Allow dropdowns to have scrolling panels - Allow dropdowns to have scrolling panels
- Improved Panel performance when adding and removing a lot of children - Improved Panel performance when adding and removing a lot of children
- Don't reset the caret position of a text field when selecting or deselecting it - Don't reset the caret position of a text field when selecting or deselecting it
- Improved UiParser.ParseImage with locks and a callback action
Fixes Fixes
- Fixed panels updating their relevant children too much when the scroll bar is hidden - Fixed panels updating their relevant children too much when the scroll bar is hidden

View file

@ -10,6 +10,7 @@ using MLEM.Ui.Style;
#if NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER #if NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER
using System.Net.Http; using System.Net.Http;
#else #else
using System.Net; using System.Net;
#endif #endif
@ -138,22 +139,32 @@ namespace MLEM.Ui.Parsers {
/// This method invokes an asynchronouns action, meaning the <see cref="Image"/>'s <see cref="Image.Texture"/> will likely not have loaded in when this method returns. /// This method invokes an asynchronouns action, meaning the <see cref="Image"/>'s <see cref="Image.Texture"/> will likely not have loaded in when this method returns.
/// </summary> /// </summary>
/// <param name="path">The absolute, relative or web path to the image.</param> /// <param name="path">The absolute, relative or web path to the image.</param>
/// <param name="onImageFetched">An action that is invoked with the loaded image once it is fetched. Note that this action will be invoked asynchronously.</param>
/// <returns>The loaded image.</returns> /// <returns>The loaded image.</returns>
/// <exception cref="NullReferenceException">Thrown if <see cref="GraphicsDevice"/> is null, or if there is an <see cref="Exception"/> loading the image and <see cref="ImageExceptionHandler"/> is unset.</exception> /// <exception cref="NullReferenceException">Thrown if <see cref="GraphicsDevice"/> is null, or if there is an <see cref="Exception"/> loading the image and <see cref="ImageExceptionHandler"/> is unset.</exception>
protected Image ParseImage(string path) { protected Image ParseImage(string path, Action<TextureRegion> onImageFetched = null) {
if (this.GraphicsDevice == null) if (this.GraphicsDevice == null)
throw new NullReferenceException("A UI parser requires a GraphicsDevice for parsing images"); throw new NullReferenceException("A UI parser requires a GraphicsDevice for parsing images");
var imageLock = new object();
TextureRegion image = null; TextureRegion image = null;
return new Image(Anchor.AutoLeft, Vector2.One, _ => image) { return new Image(Anchor.AutoLeft, Vector2.One, _ => {
lock (imageLock)
return image;
}) {
SetHeightBasedOnAspect = true, SetHeightBasedOnAspect = true,
OnAddedToUi = e => { OnAddedToUi = e => {
if (image == null) bool imageNull;
lock (imageLock)
imageNull = image == null;
if (imageNull)
LoadImageAsync(); LoadImageAsync();
}, },
OnRemovedFromUi = e => { OnRemovedFromUi = e => {
image?.Texture.Dispose(); lock (imageLock) {
image = null; image?.Texture.Dispose();
image = null;
}
} }
}; };
@ -178,7 +189,12 @@ namespace MLEM.Ui.Parsers {
using (var stream = Path.IsPathRooted(path) ? File.OpenRead(path) : TitleContainer.OpenStream(path)) using (var stream = Path.IsPathRooted(path) ? File.OpenRead(path) : TitleContainer.OpenStream(path))
tex = Texture2D.FromStream(this.GraphicsDevice, stream); tex = Texture2D.FromStream(this.GraphicsDevice, stream);
} }
image = new TextureRegion(tex); lock (imageLock) {
if (image == null) {
image = new TextureRegion(tex);
onImageFetched?.Invoke(image);
}
}
} catch (Exception e) { } catch (Exception e) {
if (this.ImageExceptionHandler != null) { if (this.ImageExceptionHandler != null) {
this.ImageExceptionHandler.Invoke(path, e); this.ImageExceptionHandler.Invoke(path, e);