Skip to content

Commit d27edfe

Browse files
committed
Code Rearrangement
Add keyboard shortcut to opening selected patch config Make open selected patch config work Make it if key combination is pressed change back in list box Signed-off-by: Kláben Szabolcs Bence (Tudi20) <[email protected]>
1 parent 9763b87 commit d27edfe

File tree

4 files changed

+152
-143
lines changed

4 files changed

+152
-143
lines changed

Universal THCRAP Launcher/Form1.Designer.cs

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Universal THCRAP Launcher/Form1.cs

Lines changed: 115 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Universal_THCRAP_Launcher
1616
{
1717
public partial class Form1 : Form
1818
{
19+
#region Global variables
1920
private const string ConfigFile = "utl_config.js";
2021
private readonly Image _custom = new Bitmap(Resources.Custom);
2122
private readonly Image _game = new Bitmap(Resources.Game);
@@ -32,16 +33,16 @@ public partial class Form1 : Form
3233
private List<string> _jsFiles = new List<string>();
3334

3435
private int[] _resizeConstants;
35-
36+
37+
private Configuration Configuration1 { get; set; }
38+
private Favourites Favourites1 { get; set; } = new Favourites(new List<string>(), new List<string>());
39+
#endregion
40+
3641
public Form1()
3742
{
3843
InitializeComponent();
3944
}
4045

41-
private Configuration Configuration1 { get; set; }
42-
43-
private Favourites Favourites1 { get; set; } = new Favourites(new List<string>(), new List<string>());
44-
4546
private static void ErrorAndExit(string errorMessage)
4647
{
4748
MessageBox.Show(errorMessage, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
@@ -231,7 +232,47 @@ private void Form1_Load(object sender, EventArgs e)
231232

232233
Debug.WriteLine("Form1 Loaded");
233234
}
235+
236+
private void Form1_Shown(object sender, EventArgs e)
237+
{
238+
ReadConfig();
239+
//Set default selection index
240+
if (listBox1.SelectedIndex == -1 && listBox1.Items.Count > 0)
241+
listBox1.SelectedIndex = 0;
242+
243+
if (listBox2.SelectedIndex == -1 && listBox2.Items.Count > 0)
244+
listBox2.SelectedIndex = 0;
234245

246+
UpdateConfigFile();
247+
}
248+
249+
private void Form1_Resize(object sender, EventArgs e)
250+
{
251+
button1.Size = new Size(Size.Width - _resizeConstants[0], button1.Size.Height);
252+
splitContainer1.Size = new Size(Size.Width - _resizeConstants[1], Size.Height - _resizeConstants[2]);
253+
listBox1.Size = new Size(splitContainer1.Panel1.Width - 1, splitContainer1.Panel1.Height - 1);
254+
listBox2.Size = new Size(splitContainer1.Panel2.Width - 1, splitContainer1.Panel2.Height - 1);
255+
checkBox1.Location = new Point(checkBox1.Location.X, Size.Height - _resizeConstants[3]);
256+
sort_az_button1.Location =
257+
new Point(sort_az_button1.Location.X, splitContainer1.Location.Y - _resizeConstants[4]);
258+
sort_az_button2.Location = new Point(listBox1.Size.Width + _resizeConstants[5],
259+
splitContainer1.Location.Y - _resizeConstants[4]);
260+
star_button1.Location =
261+
new Point(star_button1.Location.X, splitContainer1.Location.Y - _resizeConstants[4]);
262+
star_button2.Location = new Point(sort_az_button2.Location.X + _resizeConstants[6],
263+
splitContainer1.Location.Y - _resizeConstants[4]);
264+
filterByType_button.Location = new Point(
265+
star_button2.Location.X + _resizeConstants[6], splitContainer1.Location.Y - _resizeConstants[4]);
266+
}
267+
268+
private static void AddStars(ListBox listBox, IEnumerable<string> list)
269+
{
270+
foreach (var variable in list)
271+
{
272+
var index = listBox.FindStringExact(variable);
273+
if (index != -1) listBox.Items[index] += " ★";
274+
}
275+
}
235276

236277
private static void SortListBoxItems(ref ListBox lb)
237278
{
@@ -247,6 +288,30 @@ private static void SortListBoxItemsDesc(ref ListBox lb)
247288
lb.Items.AddRange(items.OrderByDescending(i => i).ToArray());
248289
}
249290

291+
/// <summary>
292+
/// Selects the items based on the configuration
293+
/// </summary>
294+
private void ReadConfig()
295+
{
296+
checkBox1.Checked = Configuration1.ExitAfterStartup;
297+
298+
var s = Configuration1.LastConfig;
299+
if (Favourites1.Patches.Contains(s))
300+
s += " ★";
301+
listBox1.SelectedIndex = listBox1.FindStringExact(s);
302+
s = Configuration1.LastGame;
303+
304+
if (Favourites1.Games.Contains(s))
305+
s += " ★";
306+
307+
listBox2.SelectedIndex = listBox2.FindStringExact(s);
308+
309+
if (listBox1.SelectedIndex == -1 && listBox1.Items.Count > 0)
310+
listBox1.SelectedIndex = 0;
311+
if (listBox2.SelectedIndex == -1 && listBox2.Items.Count > 0)
312+
listBox2.SelectedIndex = 0;
313+
}
314+
250315
/// <summary>
251316
/// Updates the configuration and favourites list
252317
/// </summary>
@@ -283,30 +348,6 @@ private void UpdateConfig()
283348
}
284349
}
285350

286-
/// <summary>
287-
/// Selects the items based on the configuration
288-
/// </summary>
289-
private void ReadConfig()
290-
{
291-
checkBox1.Checked = Configuration1.ExitAfterStartup;
292-
293-
var s = Configuration1.LastConfig;
294-
if (Favourites1.Patches.Contains(s))
295-
s += " ★";
296-
listBox1.SelectedIndex = listBox1.FindStringExact(s);
297-
s = Configuration1.LastGame;
298-
299-
if (Favourites1.Games.Contains(s))
300-
s += " ★";
301-
302-
listBox2.SelectedIndex = listBox2.FindStringExact(s);
303-
304-
if (listBox1.SelectedIndex == -1 && listBox1.Items.Count > 0)
305-
listBox1.SelectedIndex = 0;
306-
if (listBox2.SelectedIndex == -1 && listBox2.Items.Count > 0)
307-
listBox2.SelectedIndex = 0;
308-
}
309-
310351
/// <summary>
311352
/// Writes the configuration and favourites to file
312353
/// </summary>
@@ -350,33 +391,13 @@ private void StartThcrap()
350391

351392
private void button1_Click(object sender, EventArgs e) => StartThcrap();
352393

353-
private void Form1_FormClosing(object sender, FormClosingEventArgs e) => UpdateConfigFile();
354-
355394
private void button1_MouseHover(object sender, EventArgs e) =>
356395
button1.BackgroundImage = Resources.Shinmera_Banner_5_mini_size_hover;
357396

358397
private void button1_MouseLeave(object sender, EventArgs e) =>
359398
button1.BackgroundImage = Resources.Shinmera_Banner_5_mini_size;
360399

361-
private void Form1_Resize(object sender, EventArgs e)
362-
{
363-
button1.Size = new Size(Size.Width - _resizeConstants[0], button1.Size.Height);
364-
splitContainer1.Size = new Size(Size.Width - _resizeConstants[1], Size.Height - _resizeConstants[2]);
365-
listBox1.Size = new Size(splitContainer1.Panel1.Width - 1, splitContainer1.Panel1.Height - 1);
366-
listBox2.Size = new Size(splitContainer1.Panel2.Width - 1, splitContainer1.Panel2.Height - 1);
367-
checkBox1.Location = new Point(checkBox1.Location.X, Size.Height - _resizeConstants[3]);
368-
sort_az_button1.Location =
369-
new Point(sort_az_button1.Location.X, splitContainer1.Location.Y - _resizeConstants[4]);
370-
sort_az_button2.Location = new Point(listBox1.Size.Width + _resizeConstants[5],
371-
splitContainer1.Location.Y - _resizeConstants[4]);
372-
star_button1.Location =
373-
new Point(star_button1.Location.X, splitContainer1.Location.Y - _resizeConstants[4]);
374-
star_button2.Location = new Point(sort_az_button2.Location.X + _resizeConstants[6],
375-
splitContainer1.Location.Y - _resizeConstants[4]);
376-
filterByType_button.Location = new Point(
377-
star_button2.Location.X + _resizeConstants[6], splitContainer1.Location.Y - _resizeConstants[4]);
378-
}
379-
400+
#region Sorting/Filtering Button functions
380401
private void sort_az_button1_Click(object sender, EventArgs e)
381402
{
382403
var isDesc = Configuration1.IsDescending;
@@ -415,16 +436,7 @@ private void sort_az_button2_Click(object sender, EventArgs e)
415436

416437
Configuration1.IsDescending = isDesc;
417438
ReadConfig();
418-
}
419-
420-
private static void AddStars(ListBox listBox, IEnumerable<string> list)
421-
{
422-
foreach (var variable in list)
423-
{
424-
var index = listBox.FindStringExact(variable);
425-
if (index != -1) listBox.Items[index] += " ★";
426-
}
427-
}
439+
}
428440

429441
private void star_button1_Click(object sender, EventArgs e)
430442
{
@@ -483,42 +495,7 @@ private void star_button2_Click(object sender, EventArgs e)
483495
Configuration1.OnlyFavourites = onlyFav;
484496
ReadConfig();
485497
}
486-
487-
private void Form1_Shown(object sender, EventArgs e)
488-
{
489-
ReadConfig();
490-
//Set default selection index
491-
if (listBox1.SelectedIndex == -1 && listBox1.Items.Count > 0)
492-
listBox1.SelectedIndex = 0;
493-
494-
if (listBox2.SelectedIndex == -1 && listBox2.Items.Count > 0)
495-
listBox2.SelectedIndex = 0;
496-
497-
UpdateConfigFile();
498-
}
499-
500-
private void SelectedIndexChanged(object sender, EventArgs e)
501-
{
502-
var lb = (ListBox) sender;
503-
switch (lb.Name)
504-
{
505-
case "listBox1":
506-
if (lb.SelectedIndex != -1)
507-
Configuration1.LastConfig = lb.SelectedItem.ToString().Replace(" ★", "");
508-
break;
509-
case "listBox2":
510-
if (lb.SelectedIndex != -1)
511-
Configuration1.LastGame = lb.SelectedItem.ToString().Replace(" ★", "");
512-
break;
513-
default:
514-
Debug.WriteLine("Invalid ListBox!");
515-
break;
516-
}
517-
}
518-
519-
private void checkBox1_CheckedChanged(object sender, EventArgs e) =>
520-
Configuration1.ExitAfterStartup = checkBox1.Checked;
521-
498+
522499
private void filterByType_button_Click(object sender, EventArgs e)
523500
{
524501
if (filterByType_button.BackgroundImage.Equals(_gameAndCustom))
@@ -554,9 +531,39 @@ private void filterByType_button_Click(object sender, EventArgs e)
554531
if (sender != null) Configuration1.FilterExeType = 0;
555532
}
556533
}
534+
#endregion
535+
536+
private void SelectedIndexChanged(object sender, EventArgs e)
537+
{
538+
if (ModifierKeys != Keys.None) return;
539+
var lb = (ListBox) sender;
540+
switch (lb.Name)
541+
{
542+
case "listBox1":
543+
if (lb.SelectedIndex != -1)
544+
Configuration1.LastConfig = lb.SelectedItem.ToString().Replace(" ★", "");
545+
break;
546+
case "listBox2":
547+
if (lb.SelectedIndex != -1)
548+
Configuration1.LastGame = lb.SelectedItem.ToString().Replace(" ★", "");
549+
break;
550+
default:
551+
Debug.WriteLine("Invalid ListBox!");
552+
break;
553+
}
554+
}
555+
556+
private void checkBox1_CheckedChanged(object sender, EventArgs e) =>
557+
Configuration1.ExitAfterStartup = checkBox1.Checked;
557558

558559
private void Form1_KeyUp(object sender, KeyEventArgs e)
559560
{
561+
if (ModifierKeys != Keys.None)
562+
{
563+
listBox1.SelectedItem = Configuration1.LastConfig;
564+
listBox2.SelectedItem = Configuration1.LastGame;
565+
}
566+
560567
switch (e.KeyCode)
561568
{
562569
case Keys.F2 when sender.GetType().FullName != "System.Windows.Forms.ListBox":
@@ -585,22 +592,24 @@ private void Form1_KeyUp(object sender, KeyEventArgs e)
585592
UpdateConfigFile();
586593
break;
587594
}
595+
588596
}
589597

590598
private static void RestartProgram()
591599
{
592600
Process.Start(Assembly.GetEntryAssembly().Location);
593601
Application.Exit();
594602
}
595-
596-
private void keyboardShortcutsToolStripMenuItem_Click(object sender, EventArgs e) => ShowKeyboardShortcuts();
597-
598-
private void ShowKeyboardShortcuts()
603+
604+
private static void ShowKeyboardShortcuts()
599605
{
600606
MessageBox.Show(Resources.KeyboardShortcuts,
601607
@"Keyboard Shortcuts", MessageBoxButtons.OK, MessageBoxIcon.Information);
602608
}
603-
609+
610+
#region Tool Strip functions
611+
private void keyboardShortcutsToolStripMenuItem_Click(object sender, EventArgs e) => ShowKeyboardShortcuts();
612+
604613
private void restartToolStripMenuItem_Click(object sender, EventArgs e) => RestartProgram();
605614

606615
private void exitToolStripMenuItem_Click(object sender, EventArgs e) => Application.Exit();
@@ -655,10 +664,12 @@ private void createShortcutToolStripMenuItem_Click(object sender, EventArgs e)
655664
shortcut.Save();
656665
}
657666

658-
private void openSelectedPatchConfigurationToolStripMenuItem_Click(object sender, EventArgs e)
659-
{
667+
private void openSelectedPatchConfigurationToolStripMenuItem_Click(object sender, EventArgs e) => Process.Start(Directory.GetCurrentDirectory() + @"/" + listBox1.SelectedItem.ToString().Replace(" ★", ""));
660668

661-
}
669+
#endregion
670+
671+
private void Form1_FormClosing(object sender, FormClosingEventArgs e) => UpdateConfigFile();
672+
662673
}
663674

664675
public class Configuration

0 commit comments

Comments
 (0)