-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveControllerDialog.xaml.cs
More file actions
197 lines (170 loc) · 7.81 KB
/
RemoveControllerDialog.xaml.cs
File metadata and controls
197 lines (170 loc) · 7.81 KB
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
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ControllerManagementSystem
{
/// <summary>
/// Interaction logic for RemoveControllerWindow.xaml
/// </summary>
public partial class RemoveControllerDialog : UserControl
{
List<Controller> controllerList = new List<Controller>();
Action refreshControllerStatus, closeDialog;
public RemoveControllerDialog(List<Controller> controllerList, Action refreshControllerStatus, Action closeDialog)
{
this.controllerList = controllerList;
this.refreshControllerStatus = refreshControllerStatus;
this.closeDialog = closeDialog;
InitializeComponent();
ControllerTypeBox.Items.Add(Controller.ControllerType.JoyCon);
ControllerTypeBox.Items.Add(Controller.ControllerType.ProController);
ControllerTypeBox.Items.Add(Controller.ControllerType.GameCube);
ControllerTypeBox.Items.Add(Controller.ControllerType.SwitchConsole);
ControllerTypeBox.Items.Add(Controller.ControllerType.XboxWireless);
ControllerTypeBox.Items.Add(Controller.ControllerType.XboxWired);
ControllerTypeBox.Items.Add(Controller.ControllerType.Mouse);
ControllerTypeBox.Items.Add(Controller.ControllerType.Other);
//Set the default selected item for the TypeBox
ControllerTypeBox.SelectedIndex = 0;
}
public List<Controller> GetControllersOfOneType(Controller.ControllerType controllerType)
{
List<Controller> newControllerList = new();
foreach (Controller controller in controllerList)
{
if (controller.controllerType == controllerType)
{
newControllerList.Add(controller);
}
}
return newControllerList;
}
public Controller GetController(Controller.ControllerType controllerType, string name)
{
foreach (Controller controller in controllerList)
{
if (controller.controllerType.Equals(controllerType) && controller.name.Equals(name))
{
return controller;
}
}
return new Controller();
}
public void RefreshControllerStatus()
{
int previousIndex = ControllerNumberBox.SelectedIndex;
ControllerTypeBox_SelectionChanged(new object(), null!);
ControllerNumberBox.SelectedIndex = previousIndex;
}
private void ControllerTypeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ControllerTypeBox.SelectedItem != null)
{
int numOfController = 0;
//Grab the selected controller type and then clear the controller name ComboBox
var controllerTypeItem = (Controller.ControllerType)ControllerTypeBox.SelectedItem;
ControllerNumberBox.Items.Clear();
foreach (Controller controller in GetControllersOfOneType(controllerTypeItem))
{
//Set the hint to name
HintAssist.SetHint(ControllerNumberBox, "Name");
//Create the ComboBox and the StackPanel to go in it
ComboBoxItem newItem = new ComboBoxItem();
StackPanel panel = new StackPanel();
panel.Orientation = Orientation.Horizontal;
//Create the Ellipse and the BulletDecorator it goes inside. This is the "Checked out" StatusLight
Ellipse ellipse = new Ellipse();
ellipse.Fill = controller.isCheckedOut ? Brushes.Red : Brushes.Green;
ellipse.Stroke = Brushes.Black;
ellipse.StrokeThickness = 0;
ellipse.Height = 5;
ellipse.Width = 5;
BulletDecorator statusLight = new BulletDecorator();
statusLight.Bullet = ellipse;
statusLight.VerticalAlignment = VerticalAlignment.Center;
statusLight.Margin = new Thickness(0, 0, 5, 0);
//Create the TextBlock for the controller name
TextBlock textBlock = new TextBlock();
textBlock.Text = controller.name;
//Add the StatusLight and Name to the stack panel
panel.Children.Add(statusLight);
panel.Children.Add(textBlock);
//Add the StackPanel to the ComboBoxItem and then add it to the ComboBox
newItem.Content = panel;
ControllerNumberBox.Items.Add(newItem);
numOfController++;
}
if (numOfController == 0)
{
//Change the hint to say no items found
HintAssist.SetHint(ControllerNumberBox, "No Item Found");
}
ControllerNumberBox.SelectedIndex = 0;
}
}
private void RemoveControllerBtn_Click(object sender, RoutedEventArgs e)
{
if (ControllerNumberBox.SelectedItem != null)
{
//Get the selected controller from the controller list
Controller.ControllerType controllerTypeItem = (Controller.ControllerType)ControllerTypeBox.SelectedItem;
ComboBoxItem nameItem = (ComboBoxItem)ControllerNumberBox.SelectedItem;
StackPanel nameBoxStackPanel = (StackPanel)nameItem.Content;
TextBlock nameTextBox = (TextBlock)nameBoxStackPanel.Children[1];
string controllerName = nameTextBox.Text;
Controller currController = GetController(controllerTypeItem, controllerName);
//Remove the controller and output whether it was successfully removed
bool controllerRemoved = controllerList.Remove(currController);
if (controllerRemoved)
{
ValidityBox.Foreground = Brushes.Green;
ValidityBox.Text = "Controller removed sucessfully";
//Refresh the MainWindow's NumberComboBox
Dispatcher.Invoke(refreshControllerStatus);
//Refresh the NumberComboBox
RefreshControllerStatus();
//Remove the csv file associated with the controller
File.Delete(currController.historyFile);
//Close the dialog
Dispatcher.Invoke(closeDialog);
}
else
{
ValidityBox.Foreground = Brushes.Red;
ValidityBox.Text = "Controller doesn't exist";
}
}
else
{
ValidityBox.Foreground = Brushes.Red;
ValidityBox.Text = "Controller doesn't exist";
}
}
private void CloseBtn_Click(object sender, RoutedEventArgs e)
{
//Close the dialog
Dispatcher.Invoke(closeDialog);
}
public void ClearValidityBox(object sender, RoutedEventArgs e)
{
//Reset the validity box when a new type is selected
ValidityBox.Text = "";
}
private void ControllerNumberBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}