Skip to content

Commit 4811875

Browse files
committed
Replace text label properties with async getter/setters
1 parent 908db46 commit 4811875

File tree

3 files changed

+105
-69
lines changed

3 files changed

+105
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Drawing;
3+
using System.Threading.Tasks;
34
using AlternateLife.RageMP.Net.Data;
45
using AlternateLife.RageMP.Net.Enums;
56
using AlternateLife.RageMP.Net.Extensions;
@@ -11,91 +12,85 @@ namespace AlternateLife.RageMP.Net.Elements.Entities
1112
{
1213
internal class TextLabel : Entity, ITextLabel
1314
{
14-
public Color Color
15+
16+
internal TextLabel(IntPtr nativePointer, Plugin plugin) : base(nativePointer, plugin, EntityType.TextLabel)
1517
{
16-
get
17-
{
18-
CheckExistence();
18+
}
1919

20-
return StructConverter.PointerToStruct<ColorRgba>(Rage.TextLabel.TextLabel_GetColor(NativePointer)).FromModColor();
21-
}
22-
set
23-
{
24-
CheckExistence();
20+
public async Task SetColorAsync(Color value)
21+
{
22+
CheckExistence();
2523

26-
Rage.TextLabel.TextLabel_SetColor(NativePointer, value.ToModColor());
27-
}
24+
await _plugin.Schedule(() => Rage.TextLabel.TextLabel_SetColor(NativePointer, value.ToModColor())).ConfigureAwait(false);
2825
}
2926

30-
public string Text
27+
public async Task<Color> GetColorAsync()
3128
{
32-
get
33-
{
34-
CheckExistence();
29+
CheckExistence();
3530

36-
return StringConverter.PointerToString(Rage.TextLabel.TextLabel_GetText(NativePointer));
37-
}
38-
set
31+
return await _plugin.Schedule(() => StructConverter.PointerToStruct<ColorRgba>(Rage.TextLabel.TextLabel_GetColor(NativePointer)).FromModColor()).ConfigureAwait(false);
32+
}
33+
34+
public async Task SetTextAsync(string value)
35+
{
36+
Contract.NotNull(value, nameof(value));
37+
38+
using (var converter = new StringConverter())
3939
{
40-
Contract.NotNull(value, nameof(value));
40+
var text = converter.StringToPointer(value);
4141

42-
using (var converter = new StringConverter())
43-
{
44-
Rage.TextLabel.TextLabel_SetText(NativePointer, converter.StringToPointer(value));
45-
}
42+
await _plugin.Schedule(() => Rage.TextLabel.TextLabel_SetText(NativePointer, text)).ConfigureAwait(false);
4643
}
4744
}
4845

49-
public bool LOS
46+
public async Task<string> GetTextAsync()
5047
{
51-
get
52-
{
53-
CheckExistence();
48+
CheckExistence();
5449

55-
return Rage.TextLabel.TextLabel_GetLOS(NativePointer);
56-
}
57-
set
58-
{
59-
CheckExistence();
50+
return await _plugin.Schedule(() => StringConverter.PointerToString(Rage.TextLabel.TextLabel_GetText(NativePointer))).ConfigureAwait(false);
51+
}
6052

61-
Rage.TextLabel.TextLabel_SetLOS(NativePointer, value);
62-
}
53+
public async Task SetLOSAsync(bool value)
54+
{
55+
CheckExistence();
56+
57+
await _plugin.Schedule(() => Rage.TextLabel.TextLabel_SetLOS(NativePointer, value)).ConfigureAwait(false);
6358
}
6459

65-
public float DrawDistance
60+
public async Task<bool> GetLOSAsync()
6661
{
67-
get
68-
{
69-
CheckExistence();
62+
CheckExistence();
7063

71-
return Rage.TextLabel.TextLabel_GetDrawDistance(NativePointer);
72-
}
73-
set
74-
{
75-
CheckExistence();
64+
return await _plugin.Schedule(() => Rage.TextLabel.TextLabel_GetLOS(NativePointer)).ConfigureAwait(false);
65+
}
7666

77-
Rage.TextLabel.TextLabel_SetDrawDistance(NativePointer, value);
78-
}
67+
public async Task SetDrawDistanceAsync(float value)
68+
{
69+
CheckExistence();
70+
71+
await _plugin.Schedule(() => Rage.TextLabel.TextLabel_SetDrawDistance(NativePointer, value)).ConfigureAwait(false);
7972
}
8073

81-
public uint Font
74+
public async Task<float> GetDrawDistanceAsync()
8275
{
83-
get
84-
{
85-
CheckExistence();
76+
CheckExistence();
8677

87-
return Rage.TextLabel.TextLabel_GetFont(NativePointer);
88-
}
89-
set
90-
{
91-
CheckExistence();
78+
return await _plugin.Schedule(() => Rage.TextLabel.TextLabel_GetDrawDistance(NativePointer)).ConfigureAwait(false);
79+
}
9280

93-
Rage.TextLabel.TextLabel_SetFont(NativePointer, value);
94-
}
81+
public async Task SetFontAsync(uint value)
82+
{
83+
CheckExistence();
84+
85+
await _plugin.Schedule(() => Rage.TextLabel.TextLabel_SetFont(NativePointer, value)).ConfigureAwait(false);
9586
}
9687

97-
internal TextLabel(IntPtr nativePointer, Plugin plugin) : base(nativePointer, plugin, EntityType.TextLabel)
88+
public async Task<uint> GetFontAsync()
9889
{
90+
CheckExistence();
91+
92+
return await _plugin.Schedule(() => Rage.TextLabel.TextLabel_GetFont(NativePointer)).ConfigureAwait(false);
9993
}
94+
10095
}
10196
}
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,82 @@
11
using System;
22
using System.Drawing;
3+
using System.Threading.Tasks;
34
using AlternateLife.RageMP.Net.Exceptions;
45

56
namespace AlternateLife.RageMP.Net.Interfaces
67
{
78
public interface ITextLabel : IEntity
89
{
910
/// <summary>
10-
/// Get or set the color of the text label.
11+
/// Set the color of the text label.
1112
/// </summary>
13+
/// <param name="color">New color of the text label</param>
1214
/// <exception cref="EntityDeletedException">This entity was deleted before</exception>
13-
Color Color { get; set; }
15+
Task SetColorAsync(Color color);
1416

1517
/// <summary>
16-
/// Get or set the text of the text label.
18+
/// Get the color of the text label.
1719
/// </summary>
20+
/// <returns>Color of the text label</returns>
1821
/// <exception cref="EntityDeletedException">This entity was deleted before</exception>
19-
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception>
20-
string Text { get; set; }
22+
Task<Color> GetColorAsync();
2123

2224
/// <summary>
23-
/// Get or set the line of sight state of the text label.
25+
/// Set the text of the text label.
2426
/// </summary>
27+
/// <param name="text">New text of the text label</param>
2528
/// <exception cref="EntityDeletedException">This entity was deleted before</exception>
26-
bool LOS { get; set; }
29+
/// <exception cref="ArgumentNullException"><paramref name="text"/> is null</exception>
30+
Task SetTextAsync(string text);
2731

2832
/// <summary>
29-
/// Get or set the draw distance of the text label.
33+
/// Get the text of the text label.
3034
/// </summary>
35+
/// <returns>Text of the text label</returns>
3136
/// <exception cref="EntityDeletedException">This entity was deleted before</exception>
32-
float DrawDistance { get; set; }
37+
Task<string> GetTextAsync();
3338

3439
/// <summary>
35-
/// Get or set the font of the text label.
40+
/// Set the line of sight state of the text label.
3641
/// </summary>
42+
/// <param name="los">New line of sight state of the text label</param>
3743
/// <exception cref="EntityDeletedException">This entity was deleted before</exception>
38-
uint Font { get; set; }
44+
Task SetLOSAsync(bool los);
45+
46+
/// <summary>
47+
/// Get the line of sight state of the text label.
48+
/// </summary>
49+
/// <returns>Line of sight state of the text label</returns>
50+
/// <exception cref="EntityDeletedException">This entity was deleted before</exception>
51+
Task<bool> GetLOSAsync();
52+
53+
/// <summary>
54+
/// Set the draw distance of the text label.
55+
/// </summary>
56+
/// <param name="drawDistance">New draw distance of the text label</param>
57+
/// <exception cref="EntityDeletedException">This entity was deleted before</exception>
58+
Task SetDrawDistanceAsync(float drawDistance);
59+
60+
/// <summary>
61+
/// Get the draw distance of the text label.
62+
/// </summary>
63+
/// <returns>New draw distance of the text label</returns>
64+
/// <exception cref="EntityDeletedException">This entity was deleted before</exception>
65+
Task<float> GetDrawDistanceAsync();
66+
67+
/// <summary>
68+
/// Set the font of the text label.
69+
/// </summary>
70+
/// <param name="font">New font of the text label</param>
71+
/// <exception cref="EntityDeletedException">This entity was deleted before</exception>
72+
Task SetFontAsync(uint font);
73+
74+
/// <summary>
75+
/// Get the font of the text label.
76+
/// </summary>
77+
/// <returns>Font of the text label</returns>
78+
/// <exception cref="EntityDeletedException">This entity was deleted before</exception>
79+
Task<uint> GetFontAsync();
3980

4081
}
4182
}

src/AlternateLife.RageMP.Net/Native/Rage.TextLabel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal static class TextLabel
1818
internal static extern IntPtr TextLabel_GetText(IntPtr textLabel);
1919

2020
[DllImport(_dllName, CallingConvention = _callingConvention)]
21-
internal static extern void TextLabel_SetText(IntPtr textLabel, IntPtr color);
21+
internal static extern void TextLabel_SetText(IntPtr textLabel, IntPtr text);
2222

2323
[DllImport(_dllName, CallingConvention = _callingConvention)]
2424
internal static extern bool TextLabel_GetLOS(IntPtr textLabel);

0 commit comments

Comments
 (0)