Skip to content

Commit

Permalink
Merge pull request #145 from LemonUIbyLemon/feat/toolsandmath
Browse files Browse the repository at this point in the history
Refactored all tools
  • Loading branch information
justalemon authored Dec 16, 2023
2 parents ca82d8d + ef38f74 commit 4793173
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 257 deletions.
2 changes: 1 addition & 1 deletion LemonUI/Elements/BaseElement.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using LemonUI.Extensions;
using System.Drawing;
using LemonUI.Tools;

namespace LemonUI.Elements
{
Expand Down
2 changes: 1 addition & 1 deletion LemonUI/Elements/ScaledText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using LemonUI.Extensions;
using LemonUI.Tools;

namespace LemonUI.Elements
{
Expand Down
31 changes: 7 additions & 24 deletions LemonUI/Extensions/FloatExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,36 @@
using System;

namespace LemonUI.Extensions
{
/// <summary>
/// Extensions for the float class.
/// </summary>
[Obsolete("Please use LemonUI.Tools.Extensions instead.", true)]
public static class FloatExtensions
{
#region Extensions

/// <summary>
/// Converts an absolute X or Width float to a relative one.
/// </summary>
/// <param name="fin">The float to convert.</param>
/// <returns>A relative float between 0 and 1.</returns>
public static float ToXRelative(this float fin)
{
Screen.ToRelative(fin, 0, out float fout, out _);
return fout;
}
public static float ToXRelative(this float fin) => Tools.Extensions.ToXRelative(fin);
/// <summary>
/// Converts an absolute Y or Height float to a relative one.
/// </summary>
/// <param name="fin">The float to convert.</param>
/// <returns>A relative float between 0 and 1.</returns>
public static float ToYRelative(this float fin)
{
Screen.ToRelative(0, fin, out _, out float fout);
return fout;
}
public static float ToYRelative(this float fin) => Tools.Extensions.ToYRelative(fin);
/// <summary>
/// Converts an relative X or Width float to an absolute one.
/// </summary>
/// <param name="fin">The float to convert.</param>
/// <returns>An absolute float.</returns>
public static float ToXAbsolute(this float fin)
{
Screen.ToAbsolute(fin, 0, out float fout, out _);
return fout;
}
public static float ToXAbsolute(this float fin) => Tools.Extensions.ToXAbsolute(fin);
/// <summary>
/// Converts an relative Y or Height float to an absolute one.
/// </summary>
/// <param name="fin">The float to convert.</param>
/// <returns>An absolute float.</returns>
public static float ToYAbsolute(this float fin)
{
Screen.ToAbsolute(0, fin, out _, out float fout);
return fout;
}

#endregion
public static float ToYAbsolute(this float fin) => Tools.Extensions.ToYAbsolute(fin);
}
}
18 changes: 4 additions & 14 deletions LemonUI/Extensions/PointExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
using System;
using System.Drawing;

namespace LemonUI.Extensions
{
/// <summary>
/// Extensions for the Point and PointF classes.
/// </summary>
[Obsolete("Please use LemonUI.Tools.Extensions instead.", true)]
public static class PointExtensions
{
#region Extensions

/// <summary>
/// Converts an absolute 1080-based position into a relative one.
/// </summary>
/// <param name="point">The absolute PointF.</param>
/// <returns>A new PointF with relative values.</returns>
public static PointF ToRelative(this PointF point)
{
Screen.ToRelative(point.X, point.Y, out float x, out float y);
return new PointF(x, y);
}
public static PointF ToRelative(this PointF point) => Tools.Extensions.ToRelative(point);
/// <summary>
/// Converts a normalized 0-1 position into an absolute one.
/// </summary>
/// <param name="point">The relative PointF.</param>
/// <returns>A new PointF with absolute values.</returns>
public static PointF ToAbsolute(this PointF point)
{
Screen.ToAbsolute(point.X, point.Y, out float x, out float y);
return new PointF(x, y);
}

#endregion
public static PointF ToAbsolute(this PointF point) => Tools.Extensions.ToAbsolute(point);
}
}
18 changes: 4 additions & 14 deletions LemonUI/Extensions/SizeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
using System;
using System.Drawing;

namespace LemonUI.Extensions
{
/// <summary>
/// Extensions for the Size and SizeF classes.
/// </summary>
[Obsolete("Please use LemonUI.Tools.Extensions instead.", true)]
public static class SizeExtensions
{
#region Extensions

/// <summary>
/// Converts an absolute 1080-based size into a relative one.
/// </summary>
/// <param name="size">The absolute SizeF.</param>
/// <returns>A new SizeF with relative values.</returns>
public static SizeF ToRelative(this SizeF size)
{
Screen.ToRelative(size.Width, size.Height, out float width, out float height);
return new SizeF(width, height);
}
public static SizeF ToRelative(this SizeF size) => Tools.Extensions.ToRelative(size);
/// <summary>
/// Converts a normalized 0-1 size into an absolute one.
/// </summary>
/// <param name="size">The relative SizeF.</param>
/// <returns>A new SizeF with absolute values.</returns>
public static SizeF ToAbsolute(this SizeF size)
{
Screen.ToAbsolute(size.Width, size.Height, out float width, out float height);
return new SizeF(width, height);
}

#endregion
public static SizeF ToAbsolute(this SizeF size) => Tools.Extensions.ToAbsolute(size);
}
}
15 changes: 7 additions & 8 deletions LemonUI/Menus/NativeGridPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
using Control = Rage.GameControl;
#elif SHVDN3 || SHVDNC
using GTA;
using GTA.Native;
using GTA.UI;
#endif
using LemonUI.Elements;
using LemonUI.Extensions;
using System;
using System.Drawing;
using LemonUI.Tools;

namespace LemonUI.Menus
{
Expand Down Expand Up @@ -290,16 +289,16 @@ public override void Process()

if (!Controls.IsUsingController)
{
if (Screen.IsCursorInArea(grid.Position, grid.Size) && Controls.IsPressed(Control.CursorAccept))
if (GameScreen.IsCursorInArea(grid.Position, grid.Size) && Controls.IsPressed(Control.CursorAccept))
{
PointF cursor = Screen.CursorPositionRelative;
PointF pos = innerPosition.ToRelative();
PointF cursor = GameScreen.Cursor;
PointF pos = innerPosition;

PointF start = new PointF(cursor.X - pos.X, cursor.Y - pos.Y);
SizeF size = innerSize.ToRelative();
SizeF size = innerSize;

x = start.X / size.Width;
y = start.Y / size.Height;
x = (start.X / size.Width).ToXRelative();
y = (start.Y / size.Height).ToYRelative();
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion LemonUI/Menus/NativeItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using LemonUI.Elements;
using System;
using System.Drawing;
using LemonUI.Tools;

namespace LemonUI.Menus
{
Expand Down Expand Up @@ -230,7 +231,7 @@ public ColorSet Colors
/// <summary>
/// If this item is being hovered.
/// </summary>
public bool IsHovered => Screen.IsCursorInArea(background.Position, background.Size);
public bool IsHovered => GameScreen.IsCursorInArea(background.Position, background.Size);

#endregion

Expand Down
38 changes: 15 additions & 23 deletions LemonUI/Menus/NativeMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
using Font = GTA.UI.Font;
#endif
using LemonUI.Elements;
using LemonUI.Extensions;
using LemonUI.Scaleform;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using LemonUI.Tools;

namespace LemonUI.Menus
{
Expand Down Expand Up @@ -950,11 +950,10 @@ public void ResetCursor()
const float extraX = 35;
const float extraY = 325;

// Get the correct desired position of the cursor as relative
PointF pos = PointF.Empty;
if (SafeZoneAware)
{
Screen.SetElementAlignment(Alignment, GFXAlignment.Top);
SafeZone.SetAlignment(Alignment, GFXAlignment.Top);
float x = 0;
switch (Alignment)
{
Expand All @@ -965,8 +964,8 @@ public void ResetCursor()
x = Offset.X - Width - extraX;
break;
}
pos = Screen.GetRealPosition(x, Offset.Y + extraY).ToRelative();
Screen.ResetElementAlignment();
pos = SafeZone.GetSafePosition(x, Offset.Y + extraY).ToRelative();
SafeZone.ResetAlignment();
}
else
{
Expand Down Expand Up @@ -1004,7 +1003,7 @@ private void UpdateItems()
PointF pos;
if (SafeZoneAware)
{
Screen.SetElementAlignment(Alignment, GFXAlignment.Top);
SafeZone.SetAlignment(Alignment, GFXAlignment.Top);
float x = 0;
switch (Alignment)
{
Expand All @@ -1015,8 +1014,8 @@ private void UpdateItems()
x = Offset.X - Width;
break;
}
pos = Screen.GetRealPosition(x, Offset.Y);
Screen.ResetElementAlignment();
pos = SafeZone.GetSafePosition(x, Offset.Y);
SafeZone.ResetAlignment();
}
else
{
Expand Down Expand Up @@ -1208,18 +1207,12 @@ private void ProcessControls()
if (UseMouse && !Controls.IsUsingController)
{
// Enable the mouse cursor
#if FIVEM || SHVDN3 || SHVDNC || ALTV
Screen.ShowCursorThisFrame();
#elif RAGEMP
Invoker.Invoke(Natives.ShowCursorThisFrame);
#elif RPH
NativeFunction.CallByHash<int>(0xAAE7CE1D63167423);
#endif
GameScreen.ShowCursorThisFrame();

// If the camera should be rotated when the cursor is on the left and right sections of the screen, do so
if (RotateCamera)
{
if (Screen.IsCursorInArea(PointF.Empty, searchAreaSize))
if (GameScreen.IsCursorInArea(PointF.Empty, searchAreaSize))
{
#if FIVEM || SHVDN3 || SHVDNC
GameplayCamera.RelativeHeading += 5;
Expand All @@ -1233,7 +1226,7 @@ private void ProcessControls()
Camera.RenderingCamera.Heading += 5;
#endif
}
else if (Screen.IsCursorInArea(searchAreaRight, searchAreaSize))
else if (GameScreen.IsCursorInArea(searchAreaRight, searchAreaSize))
{
#if FIVEM || SHVDN3 || SHVDNC
GameplayCamera.RelativeHeading -= 5;
Expand All @@ -1259,7 +1252,7 @@ private void ProcessControls()
if (item == selectedItem && item is NativeSlidableItem slidable)
{
// If the right arrow was pressed, go to the right
if (Screen.IsCursorInArea(slidable.RightArrow.Position, slidable.RightArrow.Size))
if (GameScreen.IsCursorInArea(slidable.RightArrow.Position, slidable.RightArrow.Size))
{
if (item.Enabled)
{
Expand All @@ -1273,7 +1266,7 @@ private void ProcessControls()
return;
}
// If the user pressed the left arrow, go to the right
else if (Screen.IsCursorInArea(slidable.LeftArrow.Position, slidable.LeftArrow.Size))
else if (GameScreen.IsCursorInArea(slidable.LeftArrow.Position, slidable.LeftArrow.Size))
{
if (item.Enabled)
{
Expand All @@ -1289,7 +1282,7 @@ private void ProcessControls()
}

// If the cursor is inside of the selection rectangle
if (Screen.IsCursorInArea(item.title.Position.X - itemOffsetX, item.title.Position.Y - itemOffsetY, Width, itemHeight))
if (GameScreen.IsCursorInArea(item.title.Position.X - itemOffsetX, item.title.Position.Y - itemOffsetY, Width, itemHeight))
{
// If the item is selected, activate it
if (item == selectedItem)
Expand Down Expand Up @@ -1688,9 +1681,8 @@ public virtual void Recalculate()
x = Offset.X - Width;
break;
}
Screen.SetElementAlignment(Alignment, GFXAlignment.Top);
pos = Screen.GetRealPosition(x, Offset.Y);
Screen.ResetElementAlignment();

pos = SafeZone.GetPositionAt(new PointF(x, Offset.Y), Alignment, GFXAlignment.Top);
}
else
{
Expand Down
Loading

0 comments on commit 4793173

Please sign in to comment.