This repository was archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSoundEditor.cs
231 lines (186 loc) · 6.34 KB
/
SoundEditor.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Mechvibes.CSharp
{
public partial class SoundEditor : Form
{
public SoundEditor()
{
InitializeComponent();
string cursorPath = Registry.CurrentUser.OpenSubKey("Control Panel").OpenSubKey("Cursors").GetValue("Hand").ToString();
IntPtr cursorHandle = string.IsNullOrEmpty(cursorPath) ? IntPtr.Zero : LoadCursorFromFile(cursorPath);
Cursor cursorHand = cursorHandle == IntPtr.Zero ? Cursors.Hand : new Cursor(cursorHandle);
picMinimize.Cursor = cursorHand;
picClose.Cursor = cursorHand;
btnResetCurrentPack.Cursor = cursorHand;
btnImportFromManifest.Cursor = cursorHand;
btnExportToManifest.Cursor = cursorHand;
void Unfocus(object sender, EventArgs e) => lblTitle.Focus();
btnResetCurrentPack.Click += new EventHandler(Unfocus);
btnImportFromManifest.Click += new EventHandler(Unfocus);
btnExportToManifest.Click += new EventHandler(Unfocus);
picMinimize.Image = Bitmaps.Instance[32, Color.Gray, Color.White, BitmapType.Minimize];
picClose.Image = Bitmaps.Instance[32, Color.Black, Color.White, BitmapType.Close];
Bitmap iconBitmap = new Bitmap(32, 32);
using (Graphics iconGraphics = Graphics.FromImage(iconBitmap))
iconGraphics.DrawIcon(Icon, new Rectangle(0, 0, 32, 32));
picIcon.Image = iconBitmap;
}
#region Basic Window Functionality
[DllImport("user32.dll")]
private static extern IntPtr LoadCursorFromFile(string lpFilename);
[DllImport("user32.dll")]
private static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private void DragForm(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, 161, 2, 0);
}
}
private void MinimizeWindow(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
private void CloseWindow(object sender, EventArgs e)
{
Close();
}
private void Minimize_MouseEnter(object sender, EventArgs e) => picMinimize.BackColor = SystemColors.Control;
private void Minimize_MouseLeave(object sender, EventArgs e) => picMinimize.BackColor = Color.White;
private void Close_MouseEnter(object sender, EventArgs e) => picClose.BackColor = SystemColors.Control;
private void Close_MouseLeave(object sender, EventArgs e) => picClose.BackColor = Color.White;
#endregion
#region SoundPack Management
private void ResetCurrentPack(object sender, EventArgs e)
{
foreach (TextBox input in Controls.OfType<TextBox>())
input.Text = string.Empty;
}
// from https://stackoverflow.com/questions/6198392/check-whether-a-path-is-valid
private bool IsValidPath(string path, bool allowRelativePaths = false)
{
bool isValid = false;
try
{
string fullPath = Path.GetFullPath(path);
if (allowRelativePaths)
isValid = Path.IsPathRooted(path);
else
{
string root = Path.GetPathRoot(path);
isValid = string.IsNullOrEmpty(root.Trim(new char[] { '\\', '/' })) == false;
}
}
catch { isValid = false; }
return isValid;
}
private void ImportPackFromManifest(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog
{
Title = "Please select a Mechvibes soundpack config to import...",
Filter = "Mechvibes SoundPack Config|*.json",
}) if (ofd.ShowDialog() == DialogResult.OK)
{
SoundPack loadedPack = SoundPackHelper.LoadFromManifest(ofd.FileName);
if (IsValidPath(loadedPack.Keybinds[0].AudioFile))
{
txtPackName.Text = loadedPack.Name;
foreach (Keymap keymap in loadedPack.Keybinds)
foreach (TextBox input in Controls.OfType<TextBox>().Where(textbox => textbox.Name != "txtPackName"))
if (input.Name == keymap.Keybind.ToString())
input.Text = Path.GetFileName(keymap.AudioFile);
}
}
}
private void ExportPackToManifest(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtPackName.Text))
{
using (SaveFileDialog sfd = new SaveFileDialog
{
Title = "Please choose where you want to save your soundpack config...",
Filter = "Mechvibes SoundPack Config|*.json",
}) if (sfd.ShowDialog() == DialogResult.OK)
{
List<Keymap> keybinds = new List<Keymap>();
foreach (TextBox input in Controls.OfType<TextBox>().Where(textbox => textbox.Name != "txtPackName"))
if (!string.IsNullOrEmpty(input.Text))
{
Key keybind = (Key)TypeDescriptor.GetConverter(typeof(Key)).ConvertFromString(input.Name);
string audioFile = input.Text;
keybinds.Add(new Keymap(keybind, audioFile));
}
SoundPackHelper.SaveToManifest(new SoundPack(txtPackName.Text, keybinds), sfd.FileName);
}
}
else MessageBox.Show("Soundpack must at least have a name to be exported.");
}
#endregion
#region Custom Windows Drop Shadow
private bool m_aeroEnabled = false;
[DllImport("dwmapi.dll")]
private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
[DllImport("dwmapi.dll")]
private static extern int DwmSetWindowAttribute(IntPtr hWnd, int attr, ref int attrValue, int attrSize);
[DllImport("dwmapi.dll")]
private static extern int DwmIsCompositionEnabled(ref int pfEnabled);
private bool CheckAeroEnabled()
{
if (Environment.OSVersion.Version.Major >= 6)
{
int enabled = 0;
DwmIsCompositionEnabled(ref enabled);
return enabled == 1;
}
return false;
}
private struct MARGINS
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
m_aeroEnabled = CheckAeroEnabled();
if (!m_aeroEnabled)
cp.ClassStyle |= 0x20000;
return cp;
}
}
protected override void WndProc(ref Message m)
{
if (m_aeroEnabled)
{
int v = 2;
DwmSetWindowAttribute(Handle, 2, ref v, 4);
MARGINS margins = new MARGINS
{
leftWidth = 0,
rightWidth = 0,
topHeight = 1,
bottomHeight = 0,
};
DwmExtendFrameIntoClientArea(Handle, ref margins);
}
base.WndProc(ref m);
}
#endregion
}
}