1using System.Numerics;
2using Fjord.Ui;
3using SDL2;
4using static SDL2.SDL;
5using System.Runtime.CompilerServices;
6using System;
7using System;
8using System.Collections.Generic;
9using System.Linq;
10using System.Runtime.InteropServices;
11using System.Text;
12using System.Threading.Tasks;
13
14namespace Fjord;
15
16public static class Helpers
17{
18 public static float RadiansToDegrees(float radians)
19 {
20 return (float)((180 / Math.PI) * radians);
21 }
22
23 public static float DegreesToRadians(float degrees)
24 {
25 return (float)((System.Math.PI / 180) * degrees);
26 }
27
28 public static float PointDirection(Vector2 origin, Vector2 target)
29 {
30 return RadiansToDegrees((float)Math.Atan2(target.Y - origin.Y, target.X - origin.X));
31 }
32
33 public static float PointDistance(Vector2 pos, Vector2 pos2) {
34 return (float)Math.Pow((Math.Pow(pos2.X - pos.X, 2)) + (Math.Pow(pos2.Y-pos.Y, 2)), 0.5);
35 }
36
37 public static bool PointInside(Vector2 point, SDL_Rect rect)
38 {
39 return (point.X > rect.x && point.X < rect.x + rect.w && point.Y > rect.y && point.Y < rect.y + rect.h);
40 }
41
42 public static bool PointInside(Vector2 point, SDL_FRect rect)
43 {
44 return (point.X > rect.x && point.X < rect.x + rect.w && point.Y > rect.y && point.Y < rect.y + rect.h);
45 }
46
47 public static bool PointInside(Vector2 point, Vector4 rect)
48 {
49 return (point.X > rect.X && point.X < rect.X + rect.Z && point.Y > rect.Y && point.Y < rect.Y + rect.W);
50 }
51
52 public static double LengthDirX(double length, double angle)
53 {
54 angle = 180 - angle + 180;
55 return length * Math.Cos(angle * Math.PI / -180);
56 }
57
58 public static double LengthDirY(double length, double angle)
59 {
60 angle = 180 - angle + 180;
61 return length * Math.Sin(angle * Math.PI / -180);
62 }
63
64 public static Vector2 LengthDir(double length, double angle)
65 {
66 return new Vector2((float)LengthDirX(length, angle), (float)LengthDirY(length, angle));
67 }
68
69 public static float AngleDifference(float dest, float src) {
70 var a = src - dest;
71 a += (a > 180) ? -360 : (a < -180) ? 360 : 0;
72 return a;
73 }
74
75 public static float MeanAngle(float[] angles)
76 {
77 double x = angles.Select(a => Math.Cos(DegreesToRadians(a))).Sum() / angles.Length;
78 double y = angles.Select(a => Math.Sin(RadiansToDegrees(a))).Sum() / angles.Length;
79 return RadiansToDegrees((float)Math.Atan2(y, x));
80 }
81
82 public static SDL_FRect RectToFRect(SDL_Rect rect)
83 {
84 return new SDL_FRect()
85 {
86 x = rect.x,
87 y = rect.y,
88 w = rect.w,
89 h = rect.h
90 };
91 }
92
93 public static SDL_Rect FRectToRect(SDL_FRect rect)
94 {
95 return new SDL_Rect()
96 {
97 x = (int)rect.x,
98 y = (int)rect.y,
99 w = (int)rect.w,
100 h = (int)rect.h
101 };
102 }
103
104 public static int SDL_SetRenderDrawColor(IntPtr renderer, SDL.SDL_Color color)
105 {
106 return SDL.SDL_SetRenderDrawColor(renderer, color.r, color.g ,color.b, color.a);
107 }
108
109 public static float Lerp(float firstFloat, float secondFloat, float by)
110 {
111 return firstFloat + (secondFloat - firstFloat) * by;
112 }
113
114 public static Vector4 Lerp(Vector4 a, Vector4 b, float by)
115 {
116 return new(
117 a.X + (b.X - a.X) * by,
118 a.Y + (b.Y - a.Y) * by,
119 a.Z + (b.Z - a.Z) * by,
120 a.W + (b.W - a.W) * by
121 );
122 }
123
124 public static Vector4 ColorToV4(SDL_Color col) {
125 return new(col.r, col.g, col.b, col.a);
126 }
127
128 public static SDL_Color V4ToColor(Vector4 v) {
129 return new() {
130 r = (byte)v.X,
131 g = (byte)v.Y,
132 b = (byte)v.Z,
133 a = (byte)v.W
134 };
135 }
136
137 public static Vector4 ToV4(this SDL_Color color)
138 {
139 return new(color.r, color.g, color.b, color.a);
140 }
141
142 public static SDL_Color ToCol(this Vector4 v)
143 {
144 return new()
145 {
146 r = (byte)v.X,
147 g = (byte)v.Y,
148 b = (byte)v.Z,
149 a = (byte)v.W
150 };
151 }
152}
153
154static class Extensions
155{
156
157 public static IEnumerable<String> SplitInParts(this String s, Int32 partLength)
158 {
159 if (s == null)
160 throw new ArgumentNullException(nameof(s));
161 if (partLength <= 0)
162 throw new ArgumentException("Part length has to be positive.", nameof(partLength));
163
164 for (var i = 0; i < s.Length; i += partLength)
165 yield return s.Substring(i, Math.Min(partLength, s.Length - i));
166 }
167
168 public static HAlign<T> IntoHAlign<T>(this List<T> s)
169 {
170 HAlign<T> a = new();
171
172 foreach(T i in s)
173 {
174 a.Add(i);
175 }
176
177 return a;
178 }
179
180 public static List<T> IntoList<T>(this HAlign<T> s)
181 {
182 List<T> a = new();
183
184 foreach (T i in s)
185 {
186 a.Add(i);
187 }
188
189 return a;
190 }
191
192 public static string OSPath(this string path, [CallerFilePath] string caller = "")
193 {
194 String newPath = path;
195 if (GetPlatform() == Platform.Windows)
196 newPath = path.Replace("/", "\\");
197 else
198 newPath = path.Replace("\\", "/");
199 return newPath;
200 }
201
202 public enum Platform
203 {
204 Windows,
205 Linux,
206 OSX,
207 Unknown
208 }
209
210 public static Platform GetPlatform()
211 {
212 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
213 return Platform.Windows;
214
215 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
216 return Platform.Linux;
217
218 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
219 return Platform.OSX;
220
221 return Platform.Unknown;
222 }
223}