forked from LISTEN-moe/windows-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobals.cs
108 lines (88 loc) · 2.93 KB
/
Globals.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace ListenMoeClient
{
static class Globals
{
public static string VERSION = Application.ProductVersion.Substring(0, Application.ProductVersion.LastIndexOf('.')); //Strip build number
public static string USER_AGENT = "LISTEN.moe Desktop Client v" + VERSION + " (https://github.com/anonymousthing/ListenMoeClient)";
public static int SAMPLE_RATE = 48000;
static Random r = new Random();
public static Point Subtract(this Point a, Point b)
{
return new Point(a.X - b.X, a.Y - b.Y);
}
public static float Bound(this float f, float min, float max)
{
return Math.Max(Math.Min(f, max), min);
}
public static byte BoundToByte(this float f)
{
return (byte)(Math.Min(Math.Max(0, f), 255));
}
public static Color Scale(this Color color, float multiplier)
{
return Color.FromArgb(
(color.R * multiplier).BoundToByte(),
(color.G * multiplier).BoundToByte(),
(color.B * multiplier).BoundToByte()
);
}
public static Rectangle Scale(this Rectangle r, float f)
{
return new Rectangle((int)(r.X * f), (int)(r.Y * f), (int)(r.Width * f), (int)(r.Height * f));
}
public static Color RandomColor()
{
return Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
}
public static Rectangle ToRectangle(this RectangleF r)
{
return new Rectangle((int)r.X, (int)r.Y, (int)r.Width, (int)r.Height);
}
public static Point ToPoint(this PointF p)
{
return new Point((int)p.X, (int)p.Y);
}
static Dictionary<Control, Rectangle> originalRect = new Dictionary<Control, Rectangle>();
static Dictionary<Control, Size> originalMinSize = new Dictionary<Control, Size>();
public static void BetterScale(this Control c, float f)
{
c.SuspendLayout();
if (!originalRect.ContainsKey(c))
{
originalRect[c] = new Rectangle(c.Location.X, c.Location.Y, c.Width, c.Height);
originalMinSize[c] = c.MinimumSize;
}
Size origMinSize = originalMinSize[c];
c.MinimumSize = new SizeF(origMinSize.Width * f, origMinSize.Height * f).ToSize();
Rectangle origRect = originalRect[c];
c.Width = (int)(origRect.Width * f);
c.Height = (int)(origRect.Height * f);
if (!(c is Form))
c.Location = new PointF(origRect.X * f, origRect.Y * f).ToPoint();
//Scale children
foreach (Control c2 in c.Controls)
BetterScale(c2, f);
c.ResumeLayout();
}
public static void ResetScale(this Control c)
{
if (originalRect.ContainsKey(c))
{
c.MinimumSize = originalMinSize[c];
Rectangle origRect = originalRect[c];
c.Width = origRect.Width;
c.Height = origRect.Height;
c.Location = origRect.Location;
//Reset children
foreach (Control c2 in c.Controls)
ResetScale(c2);
originalRect.Remove(c);
originalMinSize.Remove(c);
}
}
}
}