/
SeeloewenLib.cs
332 lines (294 loc) · 11.7 KB
/
SeeloewenLib.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/////////////////////////////////////////////////////////////////////
// //
// SealLib v1.0.0 //
// Saturday, 8th September 2023 //
// Created by Seeloewen //
// //
// Simple library that contains some code that is used by my apps. //
// You are free to use this library in your apps if you desire. //
// Make sure to acknowledge the license at the bottom of the file. //
// //
// Find the library on GitHub: //
// https://github.com/Seeloewen/SeeloewenLib //
// //
/////////////////////////////////////////////////////////////////////
using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls;
using System;
using System.Windows.Media.Animation;
using System.Windows.Forms.VisualStyles;
using System.Text;
using System.Windows.Markup;
using System.Windows.Media;
using System.IO;
using System.Linq;
namespace SeeloewenLib
{
public class SeeloewenLibTools
{
public string ConvertListToString(List<string> list)
{
//Set the default output string
string outputString = "";
//Add each item in the list as a new line in the string
foreach (string item in list)
{
outputString = string.Format("{0}{1}\n", outputString, item);
}
//Output the string
return outputString;
}
public T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
{
//Takes a child object and returns its visual parent
child = VisualTreeHelper.GetParent(child);
return child as T;
}
public T FindVisualChild<T>(DependencyObject parent, int index) where T : DependencyObject
{
//Get the child corresponding to the index and parent
var child = VisualTreeHelper.GetChild(parent, index);
if (child is T typedChild)
{
//return the child
return typedChild;
}
return null;
}
private string ConvertNumberUnit(double number)
{
//Define unit
string unit = "";
if (number > 1000)
{
//If number is one thousand or more
unit = "k";
number = number / 1000;
if (number > 1000)
{
//If number is one million or more
unit = "m";
number = number / 1000;
if (number > 1000)
{
//If number is one billion or more
unit = "b";
number = number / 1000;
if (number > 1000)
{
//If number is one trillion ore more
unit = "t";
number = number / 1000;
}
}
}
}
//Return the combination of number and unit
return string.Format("{0}{1}", number, unit);
}
}
public class SaveSystem //Currently in alpha, not very reliable. Only for test purposes.
{
public string path; //The path where the settings file is saved
public SaveSystem(string path)
{
//Set the path where the settings file will be saved
this.path = path;
}
public void Save(List<string> saveEntries)
{
//Save the settings to the file
File.WriteAllLines(string.Format("{0}/settings.txt", path), saveEntries);
}
public List<string> Load()
{
//Read the settings from the file and return it as a list
IEnumerable<string> output = File.ReadLines(string.Format("{0}/settings.txt", path));
return output.ToList();
}
}
public class Wizard
{
//Attributes
public List<WizardPage> pages = new List<WizardPage>();
public GroupBox gbWizard;
public Button btnContinue;
public Button btnBack;
public int currentPage = 1;
public int pagesAmount = 0;
public Action codeCancel = null;
public Action codeFinish = null;
//-- Constructor --//
public Wizard(int pagesAmount, int height, int width, Button btnContinue, Button btnBack, Action codeCancel, Action codeFinish, Thickness margin)
{
//Create references
this.btnContinue = btnContinue;
this.btnBack = btnBack;
this.pagesAmount = pagesAmount;
this.codeCancel = codeCancel;
this.codeFinish = codeFinish;
//Create Wizard GroupBox
gbWizard = new GroupBox();
gbWizard.Width = width;
gbWizard.Height = height;
gbWizard.Margin = margin;
//Setup buttons
this.btnContinue.Click += new RoutedEventHandler(btnContinue_Click);
this.btnBack.Click += new RoutedEventHandler(btnBack_Click);
//Create pages based on amount
for (int i = 0; i < pagesAmount; i++)
{
pages.Add(new WizardPage(i + 1, string.Format("Step {0}", i + 1), defaultRequirement, true, true, "Cannot continue to the next page because the requirements are not fulfilled."));
}
//Show first page
ShowPage(1);
}
//-- Custom Methods --//
public void ShowPage(int pageNum)
{
if (pages[pageNum - 1].requirements() == true)
{
//Set button text based on page number
if (pageNum > 1 && pageNum < pagesAmount)
{
btnContinue.Content = "Continue";
btnBack.Content = "Back";
}
else if (pageNum <= 1)
{
btnContinue.Content = "Continue";
btnBack.Content = "Cancel";
}
else if (pageNum >= pagesAmount)
{
btnContinue.Content = "Finish";
btnBack.Content = "Back";
}
//Set button state based on page settings
if (pages[pageNum - 1].canGoBack == true)
{
btnBack.IsEnabled = true;
}
else
{
btnBack.IsEnabled = false;
}
if (pages[pageNum - 1].canContinue == true)
{
btnContinue.IsEnabled = true;
}
else
{
btnContinue.IsEnabled = false;
}
//Only show page if it's in range
if (pageNum > 0 && pageNum <= pagesAmount)
{
//Show page based on the page number and execute code
currentPage = pageNum;
gbWizard.Content = pages[pageNum - 1].grdContent;
gbWizard.Header = pages[pageNum - 1].header;
pages[pageNum - 1].ExecuteCode();
}
}
else
{
//Show error message
MessageBox.Show(pages[pageNum - 1].requirementsNotFulfilledMsg, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public void ShowNextPage()
{
if (currentPage < pagesAmount)
{
//Shows the next page in the wizard
ShowPage(currentPage + 1);
}
else
{
//Invoke code, that gets run when users tries to go to the next page, even though it was the last page
codeFinish?.Invoke();
}
}
public void ShowPreviousPage()
{
if (currentPage > 1)
{
//Show the previous page in the wizard
ShowPage(currentPage - 1);
}
else
{
//Invoke code, that gets run when user tries to go to the previous page even though it was the first page
codeCancel?.Invoke();
}
}
public bool defaultRequirement()
{
//Default requirement used by the pages, always returns true
return true;
}
//-- Event Handlers --//
private void btnContinue_Click(object sender, RoutedEventArgs e)
{
//Show the next page
ShowNextPage();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
//Show the previous page
ShowPreviousPage();
}
}
public class WizardPage
{
//Attributes
public Grid grdContent;
public Action code = null;
public string header;
public int pageNum;
public Func<bool> requirements;
public bool canGoBack;
public bool canContinue;
public string requirementsNotFulfilledMsg;
//-- Constructor --//
public WizardPage(int pageNum, string header, Func<bool> requirements, bool canGoBack, bool canContinue, string requirementsNotFulfilledMsg)
{
//Create references
this.pageNum = pageNum;
this.header = header;
this.requirements = requirements;
this.canGoBack = canGoBack;
this.canContinue = canContinue;
this.requirementsNotFulfilledMsg = requirementsNotFulfilledMsg;
//Create the grid that contains the page content. You can add whatever you want to that grid, it's up to you what the page contains
grdContent = new Grid();
}
//-- Custom Methods --//
public void ExecuteCode()
{
//Run the code that is assigned to this page
code?.Invoke();
}
}
}
////////////////////////////////////////////////////////////////////////////
// //
// SeeloewenLib - A simple yet powerful C# library //
// Copyright(C) 2023 Louis/Seeloewen //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
// //
////////////////////////////////////////////////////////////////////////////