Skip to content

Commit

Permalink
Merge remote-tracking branch 'thorium-cfx/feat/mono_rt2_gui_types'
Browse files Browse the repository at this point in the history
  • Loading branch information
blattersturm committed Jul 5, 2023
2 parents cace99d + 23042c1 commit 585cd7e
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 124 deletions.
40 changes: 37 additions & 3 deletions code/client/clrcore-v2/Interop/Types/CString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public byte this[uint index]
/// <param name="startIndex">start character index</param>
/// <returns>CString with the requested part of characters of this string</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> &lt; 0 or &gt;= Length</exception>
public CString SubString(int startIndex) => SubString(unchecked((uint)startIndex));
public CString Substring(int startIndex) => Substring(unchecked((uint)startIndex));

/// <summary>
/// Retrieves a substring from this instance. The substring starts at a specified character position.
Expand All @@ -170,7 +170,7 @@ public byte this[uint index]
/// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> &lt; 0 or &gt;= Length</exception>

[SecuritySafeCritical]
public CString SubString(uint startIndex)
public CString Substring(uint startIndex)
{
uint maxLength = (uint)Length;
if (startIndex < maxLength)
Expand Down Expand Up @@ -210,7 +210,7 @@ public CString Substring(uint startIndex, uint length)
uint maxLength = (uint)Length;
if (startIndex < maxLength)
{
uint copyLength = Math.Min(startIndex + length, maxLength);
uint copyLength = Math.Min(length, maxLength - startIndex);

byte[] bytes = new byte[copyLength + 1];
Buffer.BlockCopy(value, (int)startIndex, bytes, 0, (int)copyLength);
Expand All @@ -221,6 +221,40 @@ public CString Substring(uint startIndex, uint length)
throw new ArgumentOutOfRangeException();
}

/// <summary>
/// Joins/merges all given strings
/// </summary>
/// <param name="strings">Strings to join</param>
/// <returns><see cref="CString"/> containing all given strings concatenated</returns>
[SecuritySafeCritical]
public static CString Concat(CString[] strings)
{
if (strings != null)
{
int size = 1; // null terminator
for (int i = 0; i < strings.Length; ++i)
{
size += strings[i].Length;
}

byte[] array = new byte[size];
int offset = 0;

for (int i = 0; i < strings.Length; ++i)
{
var src = strings[i];

size = src.Length;
Buffer.BlockCopy(src.value, 0, array, offset, size);
offset += size;
}

return new CString(array);
}

return null;
}

[SecuritySafeCritical]
public static CString operator +(CString left, CString right)
{
Expand Down
42 changes: 31 additions & 11 deletions code/client/clrcore/External/Element.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#if !MONO_V2

using System;
using System.Drawing;
using System.Collections.Generic;
using CitizenFX.Core.Native;

#if MONO_V2
using CitizenFX.Core;
using PointF = CitizenFX.Core.Vector2;
using SizeF = CitizenFX.Core.Vector2;
using API = CitizenFX.FiveM.Native.Natives;

namespace CitizenFX.FiveM.GUI
#else
namespace CitizenFX.Core.UI
#endif
{
public interface IElement
{
Expand Down Expand Up @@ -173,7 +180,7 @@ public virtual void Draw()
/// <param name="offset">The offset to shift the draw position of this <see cref="Rectangle" /> using a 1280*720 pixel base.</param>
public virtual void Draw(SizeF offset)
{
InternalDraw(offset, Screen.Width, Screen.Height);
InternalDraw(offset, 1.0f / Screen.Width, 1.0f / Screen.Height);
}

/// <summary>
Expand All @@ -190,20 +197,27 @@ public virtual void ScaledDraw()
/// <param name="offset">The offset to shift the draw position of this <see cref="Rectangle" /> using a <see cref="Screen.ScaledWidth" />*720 pixel base.</param>
public virtual void ScaledDraw(SizeF offset)
{
InternalDraw(offset, Screen.ScaledWidth, Screen.Height);
InternalDraw(offset, 1.0f / Screen.ScaledWidth, 1.0f / Screen.Height);
}

void InternalDraw(SizeF offset, float screenWidth, float screenHeight)
void InternalDraw(SizeF offset, float inverseScreenWidth, float inverseScreenHeight)
{
if (!Enabled)
{
return;
}

float w = Size.Width / screenWidth;
float h = Size.Height / screenHeight;
float x = (Position.X + offset.Width) / screenWidth;
float y = (Position.Y + offset.Height) / screenHeight;
#if MONO_V2
float w = Size.X * inverseScreenWidth;
float h = Size.Y * inverseScreenHeight;
float x = (Position.X + offset.X) * inverseScreenWidth;
float y = (Position.Y + offset.Y) * inverseScreenHeight;
#else
float w = Size.Width * inverseScreenWidth;
float h = Size.Height * inverseScreenHeight;
float x = (Position.X + offset.Width) * inverseScreenWidth;
float y = (Position.Y + offset.Height) * inverseScreenHeight;
#endif

if (!Centered)
{
Expand Down Expand Up @@ -285,7 +299,11 @@ public override void Draw(SizeF offset)

if (Centered)
{
#if MONO_V2
offset -= Size * 0.5f;
#else
offset -= new SizeF(Size.Width * 0.5f, Size.Height * 0.5f);
#endif
}

foreach (var item in Items)
Expand Down Expand Up @@ -319,7 +337,11 @@ public override void ScaledDraw(SizeF offset)

if (Centered)
{
#if MONO_V2
offset -= Size * 0.5f;
#else
offset -= new SizeF(Size.Width * 0.5f, Size.Height * 0.5f);
#endif
}

foreach (var item in Items)
Expand All @@ -329,5 +351,3 @@ public override void ScaledDraw(SizeF offset)
}
}
}

#endif
2 changes: 1 addition & 1 deletion code/client/clrcore/External/Font.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#if MONO_V2
namespace CitizenFX.FiveM.UI
namespace CitizenFX.FiveM.GUI
#else
namespace CitizenFX.Core.UI
#endif
Expand Down
15 changes: 10 additions & 5 deletions code/client/clrcore/External/Scaleform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#if MONO_V2
using CitizenFX.Core;
using CitizenFX.FiveM.GUI;
using API = CitizenFX.FiveM.Native.Natives;
using INativeValue = CitizenFX.Core.Native.Input.Primitive;
using PointF = CitizenFX.Core.Vector2;
Expand All @@ -10,6 +11,7 @@ namespace CitizenFX.FiveM
#else
using CitizenFX.Core.Native;
using System.Drawing;
using CitizenFX.Core.UI;

namespace CitizenFX.Core
#endif
Expand Down Expand Up @@ -130,12 +132,15 @@ public void Render2D()
}
public void Render2DScreenSpace(PointF location, PointF size)
{
float x = location.X / UI.Screen.Width;
float y = location.Y / UI.Screen.Height;
float width = size.X / UI.Screen.Width;
float height = size.Y / UI.Screen.Height;
const float screenWidthInverse = 1.0f / Screen.Width;
const float screenHeightInverse = 1.0f / Screen.Height;

API.DrawScaleformMovie(Handle, x + (width / 2.0f), y + (height / 2.0f), width, height, 255, 255, 255, 255, 0);
float x = location.X * screenWidthInverse;
float y = location.Y * screenHeightInverse;
float width = size.X * screenWidthInverse;
float height = size.Y * screenHeightInverse;

API.DrawScaleformMovie(Handle, x + (width * .5f), y + (height * .5f), width, height, 255, 255, 255, 255, 0);
}
public void Render3D(Vector3 position, Vector3 rotation, Vector3 scale)
{
Expand Down
63 changes: 30 additions & 33 deletions code/client/clrcore/External/Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
using API = CitizenFX.FiveM.Native.Natives;
using PointF = CitizenFX.Core.Vector2;
using Size = CitizenFX.Core.Size2;
using String = CitizenFX.Core.CString;

namespace CitizenFX.FiveM.UI
namespace CitizenFX.FiveM.GUI
#else
using CitizenFX.Core.Native;
using System;
using System.Drawing;

namespace CitizenFX.Core.UI
Expand Down Expand Up @@ -199,23 +201,10 @@ public void Hide()

public static class Screen
{
/// <summary>
/// Converts the inputString into a string[] (array) containing strings each 99 or less characters long.
/// </summary>
/// <param name="inputString">The string to convert.</param>
/// <returns>string[] containing strings each 99 or less characters long.</returns>
public static string[] StringToArray(string inputString)
{
int stringsNeeded = (inputString.Length % 99 == 0) ? (inputString.Length / 99) : ((inputString.Length / 99) + 1);

string[] outputString = new string[stringsNeeded];
for (int i = 0; i < stringsNeeded; i++)
{
outputString[i] = inputString.Substring(i * 99, MathUtil.Clamp(inputString.Substring(i * 99).Length, 0, 99));
}

return outputString;
}
#if !MONO_V2
/// <inheritdoc cref="Text.SplitString(String)"/>
public static String[] StringToArray(String inputString) => Text.SplitString(inputString);
#endif

/// <summary>
/// The base width of the screen used for all UI Calculations, unless ScaledDraw is used
Expand Down Expand Up @@ -263,15 +252,18 @@ public static Size Resolution
/// </summary>
/// <param name="message">The message to display.</param>
/// <param name="duration">The duration to display the subtitle in milliseconds.</param>
public static void ShowSubtitle(string message, int duration = 2500)
{
string[] strings = StringToArray(message);
public static void ShowSubtitle(String message, int duration = 2500)
=> ShowSubtitle(Text.SplitString(message), duration);

/// <remarks>Use this with an array of <see cref="String"/>s that have 99 or less characters for better performance</remarks>
/// <inheritdoc cref="ShowSubtitle(String, int)"/>
public static void ShowSubtitle(String[] message, int duration = 2500)
{
API.BeginTextCommandPrint("CELL_EMAIL_BCON");

foreach (string s in strings)
for (int i = 0; i < message?.Length; ++i)
{
API.AddTextComponentSubstringPlayerName(s);
API.AddTextComponentSubstringPlayerName(message[i]);
}

API.EndTextCommandPrint(duration, true);
Expand All @@ -281,15 +273,18 @@ public static void ShowSubtitle(string message, int duration = 2500)
/// Displays a help message in the top corner of the screen this frame.
/// </summary>
/// <param name="helpText">The text to display.</param>
public static void DisplayHelpTextThisFrame(string helpText)
{
string[] strings = StringToArray(helpText);
public static void DisplayHelpTextThisFrame(String helpText)
=> DisplayHelpTextThisFrame(Text.SplitString(helpText));

/// <remarks>Use this with an array of <see cref="String"/>s that have 99 or less characters for better performance</remarks>
/// <inheritdoc cref="DisplayHelpTextThisFrame(String)"/>
public static void DisplayHelpTextThisFrame(String[] helpText)
{
API.BeginTextCommandDisplayHelp("CELL_EMAIL_BCON");

foreach (string s in strings)
for (int i = 0; i < helpText?.Length; ++i)
{
API.AddTextComponentSubstringPlayerName(s);
API.AddTextComponentSubstringPlayerName(helpText[i]);
}

API.EndTextCommandDisplayHelp(0, false, false, -1);
Expand All @@ -301,14 +296,16 @@ public static void DisplayHelpTextThisFrame(string helpText)
/// <param name="message">The message in the notification.</param>
/// <param name="blinking">if set to <c>true</c> the notification will blink.</param>
/// <returns>The handle of the <see cref="Notification"/> which can be used to hide it using <see cref="Notification.Hide()"/></returns>
public static Notification ShowNotification(string message, bool blinking = false)
{
string[] strings = StringToArray(message);

public static Notification ShowNotification(String message, bool blinking = false)
=> ShowNotification(Text.SplitString(message), blinking);

/// <remarks>Use this with an array of <see cref="String"/>s that have 99 or less characters for better performance</remarks>
/// <inheritdoc cref="ShowNotification(String, bool)"/>
public static Notification ShowNotification(String[] message, bool blinking = false)
{
API.SetNotificationTextEntry("CELL_EMAIL_BCON");

foreach (string s in strings)
foreach (String s in message)
{
API.AddTextComponentSubstringPlayerName(s);
}
Expand Down
Loading

0 comments on commit 585cd7e

Please sign in to comment.