forked from Guad/NativeUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIResText.cs
119 lines (101 loc) · 3.83 KB
/
UIResText.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Drawing;
using GTA;
using GTA.Native;
using Font = GTA.UI.Font;
namespace NativeUI
{
/// <summary>
/// A Text object in the 1080 pixels height base system.
/// </summary>
public class UIResText : GTA.UI.Text
{
public UIResText(string caption, Point position, float scale) : base(caption, position, scale)
{
TextAlignment = Alignment.Left;
}
public UIResText(string caption, Point position, float scale, Color color)
: base(caption, position, scale, color)
{
TextAlignment = Alignment.Left;
}
public UIResText(string caption, Point position, float scale, Color color, Font font, Alignment justify)
: base(caption, position, scale, color, font, false)
{
TextAlignment = justify;
}
public Alignment TextAlignment { get; set; }
public bool DropShadow { get; set; } = false;
public bool Outline { get; set; } = false;
/// <summary>
/// Push a long string into the stack.
/// </summary>
/// <param name="str"></param>
public static void AddLongString(string str)
{
const int strLen = 99;
for (int i = 0; i < str.Length; i += strLen)
{
string substr = str.Substring(i, Math.Min(strLen, str.Length - i));
Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, substr);
}
}
public static float MeasureStringWidth(string str, Font font, float scale)
{
int screenw = Game.ScreenResolution.Width;
int screenh = Game.ScreenResolution.Height;
const float height = 1080f;
float ratio = (float)screenw / screenh;
float width = height * ratio;
return MeasureStringWidthNoConvert(str, font, scale) * width;
}
public static float MeasureStringWidthNoConvert(string str, Font font, float scale)
{
Function.Call((Hash)0x54CE8AC98E120CAB, "STRING");
AddLongString(str);
return Function.Call<float>((Hash)0x85F061DA64ED2F67, (int)font) * scale;
}
public Size WordWrap { get; set; }
public override void Draw(SizeF offset)
{
int screenw = Game.ScreenResolution.Width;
int screenh = Game.ScreenResolution.Height;
const float height = 1080f;
float ratio = (float)screenw / screenh;
var width = height * ratio;
float x = (Position.X) / width;
float y = (Position.Y) / height;
Function.Call(Hash.SET_TEXT_FONT, (int)Font);
Function.Call(Hash.SET_TEXT_SCALE, 1.0f, Scale);
Function.Call(Hash.SET_TEXT_COLOUR, Color.R, Color.G, Color.B, Color.A);
if (DropShadow)
Function.Call(Hash.SET_TEXT_DROP_SHADOW);
if (Outline)
Function.Call(Hash.SET_TEXT_OUTLINE);
switch (TextAlignment)
{
case Alignment.Centered:
Function.Call(Hash.SET_TEXT_CENTRE, true);
break;
case Alignment.Right:
Function.Call(Hash.SET_TEXT_RIGHT_JUSTIFY, true);
Function.Call(Hash.SET_TEXT_WRAP, 0, x);
break;
}
if (WordWrap != new Size(0, 0))
{
float xsize = (Position.X + WordWrap.Width)/width;
Function.Call(Hash.SET_TEXT_WRAP, x, xsize);
}
Function.Call(Hash._SET_TEXT_ENTRY, "jamyfafi");
AddLongString(Caption);
Function.Call(Hash._DRAW_TEXT, x, y);
}
public enum Alignment
{
Left,
Centered,
Right,
}
}
}