From c38f79ce21007bb731185837440a48a46db6fe63 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Mon, 18 Dec 2023 20:03:35 -0300 Subject: [PATCH 01/17] Added basic class for ytd based animations --- LemonUI/Elements/ScaledAnim.cs | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 LemonUI/Elements/ScaledAnim.cs diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs new file mode 100644 index 0000000..8387ae0 --- /dev/null +++ b/LemonUI/Elements/ScaledAnim.cs @@ -0,0 +1,45 @@ +using System.Drawing; + +namespace LemonUI.Elements +{ + /// + /// A scaled animation using YTD files with all of the frames. + /// + public class ScaledAnim : BaseElement + { + #region Properties + + /// + /// The dictionary that contains the textures. + /// + public string Dict { get; set; } + + #endregion + + #region Constructors + + /// + /// Creates a new dictionary based animation. + /// + /// The texture dictionary (YTD) to use. + /// The position of the animation. + /// The size of the animation. + public ScaledAnim(string dict, PointF pos, SizeF size) : base(pos, size) + { + Dict = dict; + } + + #endregion + + #region Functions + + /// + /// Draws the animation. + /// + public override void Draw() + { + } + + #endregion + } +} From 62f6d7c068e2f50afc59aee5c6decb9c1a64459a Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Mon, 18 Dec 2023 20:07:26 -0300 Subject: [PATCH 02/17] Make the BaseElement properties virtual Fixes #150 --- LemonUI/Elements/BaseElement.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/LemonUI/Elements/BaseElement.cs b/LemonUI/Elements/BaseElement.cs index 208911b..d68def8 100644 --- a/LemonUI/Elements/BaseElement.cs +++ b/LemonUI/Elements/BaseElement.cs @@ -34,12 +34,9 @@ public abstract class BaseElement : I2Dimensional /// /// The Position of the drawable. /// - public PointF Position + public virtual PointF Position { - get - { - return literalPosition; - } + get => literalPosition; set { literalPosition = value; @@ -49,12 +46,9 @@ public PointF Position /// /// The Size of the drawable. /// - public SizeF Size + public virtual SizeF Size { - get - { - return literalSize; - } + get => literalSize; set { literalSize = value; @@ -64,11 +58,11 @@ public SizeF Size /// /// The Color of the drawable. /// - public Color Color { get; set; } = Color.FromArgb(255, 255, 255, 255); + public virtual Color Color { get; set; } = Color.FromArgb(255, 255, 255, 255); /// /// The rotation of the drawable. /// - public float Heading { get; set; } = 0; + public virtual float Heading { get; set; } = 0; #endregion From b2becb91ecccaa3e273f12348a3aee46ace57a54 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Mon, 18 Dec 2023 22:38:39 -0300 Subject: [PATCH 03/17] Added texture field --- LemonUI/Elements/ScaledAnim.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index 8387ae0..c31d673 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -7,6 +7,12 @@ namespace LemonUI.Elements /// public class ScaledAnim : BaseElement { + #region Fields + + private ScaledTexture texture = new ScaledTexture(string.Empty, string.Empty); + + #endregion + #region Properties /// From cb2c2c574fdf107dcebdc461ecef93795e3ab1d8 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Mon, 18 Dec 2023 22:39:16 -0300 Subject: [PATCH 04/17] Make the dict property use the texture --- LemonUI/Elements/ScaledAnim.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index c31d673..b4d92f2 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -1,3 +1,4 @@ +using System; using System.Drawing; namespace LemonUI.Elements @@ -18,7 +19,11 @@ public class ScaledAnim : BaseElement /// /// The dictionary that contains the textures. /// - public string Dict { get; set; } + public string Dict + { + get => texture.Dictionary; + set => texture.Dictionary = value ?? throw new ArgumentNullException(nameof(value)); + } #endregion @@ -32,7 +37,7 @@ public class ScaledAnim : BaseElement /// The size of the animation. public ScaledAnim(string dict, PointF pos, SizeF size) : base(pos, size) { - Dict = dict; + texture.Dictionary = dict ?? throw new ArgumentNullException(nameof(dict)); } #endregion From ce98f5425b197d64852bbbee22123f2018b4b471 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Mon, 18 Dec 2023 22:39:41 -0300 Subject: [PATCH 05/17] Make the texture readonly --- LemonUI/Elements/ScaledAnim.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index b4d92f2..5e68106 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -10,7 +10,7 @@ public class ScaledAnim : BaseElement { #region Fields - private ScaledTexture texture = new ScaledTexture(string.Empty, string.Empty); + private readonly ScaledTexture texture = new ScaledTexture(string.Empty, string.Empty); #endregion From 5661453272f14298de103a89577e9432467a769a Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Mon, 18 Dec 2023 22:41:34 -0300 Subject: [PATCH 06/17] Renamed Dict to Dictionary --- LemonUI/Elements/ScaledAnim.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index 5e68106..04021e6 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -19,7 +19,7 @@ public class ScaledAnim : BaseElement /// /// The dictionary that contains the textures. /// - public string Dict + public string Dictionary { get => texture.Dictionary; set => texture.Dictionary = value ?? throw new ArgumentNullException(nameof(value)); From c099cda5136b0583c402019c6c48688e31cc8807 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Mon, 18 Dec 2023 23:17:49 -0300 Subject: [PATCH 07/17] Added property to set the framerate and frame time --- LemonUI/Elements/ScaledAnim.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index 04021e6..fbe6584 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -11,6 +11,8 @@ public class ScaledAnim : BaseElement #region Fields private readonly ScaledTexture texture = new ScaledTexture(string.Empty, string.Empty); + private float frameRate; + private int frameTime; #endregion @@ -24,6 +26,18 @@ public string Dictionary get => texture.Dictionary; set => texture.Dictionary = value ?? throw new ArgumentNullException(nameof(value)); } + /// + /// The total number of frames per second. + /// + public float FrameRate + { + get => frameRate; + set + { + frameRate = value; + frameTime = (int)((1.0f / value) * 1000); + } + } #endregion From 1a2b9d960315b49f110db2999e4f661dc24d1dc2 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Tue, 19 Dec 2023 01:47:54 -0300 Subject: [PATCH 08/17] Removed the frame time calculation --- LemonUI/Elements/ScaledAnim.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index fbe6584..72ab65b 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -1,3 +1,14 @@ +#if ALTV +using AltV.Net.Client; +#elif FIVEM +using CitizenFX.Core; +#elif RAGEMP +using RAGE.Game; +#elif RPH +using Rage.Native; +#elif SHVDN3 || SHVDNC +using GTA; +#endif using System; using System.Drawing; @@ -12,7 +23,6 @@ public class ScaledAnim : BaseElement private readonly ScaledTexture texture = new ScaledTexture(string.Empty, string.Empty); private float frameRate; - private int frameTime; #endregion @@ -32,11 +42,7 @@ public string Dictionary public float FrameRate { get => frameRate; - set - { - frameRate = value; - frameTime = (int)((1.0f / value) * 1000); - } + set => frameRate = value; } #endregion From f00b67fe031ebeba433ed1e7eb4fb7d2bbb3f56c Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Tue, 19 Dec 2023 01:49:12 -0300 Subject: [PATCH 09/17] Added property to set the duration --- LemonUI/Elements/ScaledAnim.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index 72ab65b..472171f 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -23,6 +23,7 @@ public class ScaledAnim : BaseElement private readonly ScaledTexture texture = new ScaledTexture(string.Empty, string.Empty); private float frameRate; + private int duration; #endregion @@ -44,6 +45,22 @@ public float FrameRate get => frameRate; set => frameRate = value; } + /// + /// The duration of the animation in milliseconds. + /// + public int Duration + { + get => duration; + set + { + if (value < 0) + { + throw new ArgumentOutOfRangeException(nameof(value), "The duration can't be under zero."); + } + + duration = value; + } + } #endregion From 84fb739910c8ea6a767bcf7167727b4be26d2882 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Tue, 19 Dec 2023 01:50:43 -0300 Subject: [PATCH 10/17] Added calculation of the current frame --- LemonUI/Elements/ScaledAnim.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index 472171f..73c8fa7 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -23,6 +23,7 @@ public class ScaledAnim : BaseElement private readonly ScaledTexture texture = new ScaledTexture(string.Empty, string.Empty); private float frameRate; + private int start = 0; private int duration; #endregion @@ -86,6 +87,32 @@ public ScaledAnim(string dict, PointF pos, SizeF size) : base(pos, size) /// public override void Draw() { + if (Duration <= 0) + { + return; + } + +#if ALTV + int time = Alt.Natives.GetGameTimer(); +#elif RAGEMP + int time = Misc.GetGameTimer(); +#elif RPH + int time = NativeFunction.CallByHash(0x9CD27B0045628463); +#elif FIVEM || SHVDN3 || SHVDNC + int time = Game.GameTime; +#endif + + int end = start + Duration; + + if (start == 0 || end <= time) + { + start = time; + } + + float progress = (time - (float)start) / Duration; + int totalFrames = (int)((duration / 1000.0f) * frameRate); + int currentFrame = (int)(totalFrames * progress); + texture.Texture = currentFrame.ToString(); } #endregion From f52abb4cc74f52a9bdd60183608e822a272ce15f Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Tue, 19 Dec 2023 01:51:02 -0300 Subject: [PATCH 11/17] Added texture drawing --- LemonUI/Elements/ScaledAnim.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index 73c8fa7..4071fda 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -113,6 +113,8 @@ public override void Draw() int totalFrames = (int)((duration / 1000.0f) * frameRate); int currentFrame = (int)(totalFrames * progress); texture.Texture = currentFrame.ToString(); + + texture.Draw(); } #endregion From 587f76184d3fce41b4dcb4603bdc5b6f2467b4f9 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Tue, 19 Dec 2023 01:54:58 -0300 Subject: [PATCH 12/17] Added properties for Position, Size, Color and Heading --- LemonUI/Elements/ScaledAnim.cs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index 4071fda..d8b7734 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -30,6 +30,38 @@ public class ScaledAnim : BaseElement #region Properties + /// + /// The position of this animation. + /// + public override PointF Position + { + get => texture.Position; + set => texture.Position = value; + } + /// + /// The size of this animation. + /// + public override SizeF Size + { + get => texture.Size; + set => texture.Size = value; + } + /// + /// The color of this animation. + /// + public override Color Color + { + get => texture.Color; + set => texture.Color = value; + } + /// + /// The rotation of this animation. + /// + public override float Heading + { + get => texture.Heading; + set => texture.Heading = value; + } /// /// The dictionary that contains the textures. /// From 5cf4e43b0edabcaa024be30cbf33c2ac8f3d3c46 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Tue, 19 Dec 2023 01:56:08 -0300 Subject: [PATCH 13/17] Make sure that the framerate does not goes under zero --- LemonUI/Elements/ScaledAnim.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index d8b7734..3a4a120 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -76,7 +76,15 @@ public string Dictionary public float FrameRate { get => frameRate; - set => frameRate = value; + set + { + if (frameRate <= 0) + { + throw new ArgumentOutOfRangeException(nameof(value), "The Frame Rate can't be equal or lower to zero."); + } + + frameRate = value; + } } /// /// The duration of the animation in milliseconds. From 9565d63e513efa20778da97e857f3be039499e35 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Tue, 19 Dec 2023 01:58:28 -0300 Subject: [PATCH 14/17] Added simpler ScaledAnim constructors --- LemonUI/Elements/ScaledAnim.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index 3a4a120..41b3f76 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -107,6 +107,22 @@ public int Duration #region Constructors + /// + /// Creates a new dictionary based animation. + /// + /// The texture dictionary (YTD) to use. + public ScaledAnim(string dict) : this(dict, PointF.Empty, SizeF.Empty) + { + } + /// + /// Creates a new dictionary based animation. + /// + /// The texture dictionary (YTD) to use. + /// The size of the animation. + public ScaledAnim(string dict, SizeF size) : this(dict, PointF.Empty, size) + { + texture.Dictionary = dict ?? throw new ArgumentNullException(nameof(dict)); + } /// /// Creates a new dictionary based animation. /// From 328fbffda8230823d07f69805d083ec3aacbaaa8 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Tue, 19 Dec 2023 02:09:45 -0300 Subject: [PATCH 15/17] Fixed FrameRate check --- LemonUI/Elements/ScaledAnim.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index 41b3f76..a27a5b3 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -78,7 +78,7 @@ public float FrameRate get => frameRate; set { - if (frameRate <= 0) + if (value <= 0) { throw new ArgumentOutOfRangeException(nameof(value), "The Frame Rate can't be equal or lower to zero."); } From a18842ece17af269a6ea1c589bd250f97a9025ac Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Tue, 19 Dec 2023 02:34:41 -0300 Subject: [PATCH 16/17] Inherit from a regular ScaledTexture To prevent further issues down the line --- LemonUI/Elements/ScaledAnim.cs | 52 ++++------------------------------ 1 file changed, 5 insertions(+), 47 deletions(-) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index a27a5b3..5706760 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -17,11 +17,10 @@ namespace LemonUI.Elements /// /// A scaled animation using YTD files with all of the frames. /// - public class ScaledAnim : BaseElement + public class ScaledAnim : ScaledTexture { #region Fields - private readonly ScaledTexture texture = new ScaledTexture(string.Empty, string.Empty); private float frameRate; private int start = 0; private int duration; @@ -30,46 +29,6 @@ public class ScaledAnim : BaseElement #region Properties - /// - /// The position of this animation. - /// - public override PointF Position - { - get => texture.Position; - set => texture.Position = value; - } - /// - /// The size of this animation. - /// - public override SizeF Size - { - get => texture.Size; - set => texture.Size = value; - } - /// - /// The color of this animation. - /// - public override Color Color - { - get => texture.Color; - set => texture.Color = value; - } - /// - /// The rotation of this animation. - /// - public override float Heading - { - get => texture.Heading; - set => texture.Heading = value; - } - /// - /// The dictionary that contains the textures. - /// - public string Dictionary - { - get => texture.Dictionary; - set => texture.Dictionary = value ?? throw new ArgumentNullException(nameof(value)); - } /// /// The total number of frames per second. /// @@ -121,7 +80,6 @@ public ScaledAnim(string dict) : this(dict, PointF.Empty, SizeF.Empty) /// The size of the animation. public ScaledAnim(string dict, SizeF size) : this(dict, PointF.Empty, size) { - texture.Dictionary = dict ?? throw new ArgumentNullException(nameof(dict)); } /// /// Creates a new dictionary based animation. @@ -129,9 +87,9 @@ public ScaledAnim(string dict, SizeF size) : this(dict, PointF.Empty, size) /// The texture dictionary (YTD) to use. /// The position of the animation. /// The size of the animation. - public ScaledAnim(string dict, PointF pos, SizeF size) : base(pos, size) + public ScaledAnim(string dict, PointF pos, SizeF size) : base(pos, size, dict, string.Empty) { - texture.Dictionary = dict ?? throw new ArgumentNullException(nameof(dict)); + Dictionary = dict ?? throw new ArgumentNullException(nameof(dict)); } #endregion @@ -168,9 +126,9 @@ public override void Draw() float progress = (time - (float)start) / Duration; int totalFrames = (int)((duration / 1000.0f) * frameRate); int currentFrame = (int)(totalFrames * progress); - texture.Texture = currentFrame.ToString(); + Texture = currentFrame.ToString(); - texture.Draw(); + base.Draw(); } #endregion From 5af8c5f99f53205afe89837a4f9bbaab107997a3 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Tue, 19 Dec 2023 02:56:01 -0300 Subject: [PATCH 17/17] Make sure that no frames over or under the total are played --- LemonUI/Elements/ScaledAnim.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/LemonUI/Elements/ScaledAnim.cs b/LemonUI/Elements/ScaledAnim.cs index 5706760..0a46989 100644 --- a/LemonUI/Elements/ScaledAnim.cs +++ b/LemonUI/Elements/ScaledAnim.cs @@ -125,7 +125,19 @@ public override void Draw() float progress = (time - (float)start) / Duration; int totalFrames = (int)((duration / 1000.0f) * frameRate); - int currentFrame = (int)(totalFrames * progress); + int currentFrame = (int)(totalFrames * progress) + 1; + + if (progress < 0) + { + currentFrame = 1; + start = time; + } + else if (currentFrame >= totalFrames) + { + currentFrame = totalFrames; + start = time; + } + Texture = currentFrame.ToString(); base.Draw();