forked from Rampastring/Rampastring.XNAUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUICreator.cs
86 lines (72 loc) · 2.93 KB
/
GUICreator.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
using Rampastring.XNAUI.XNAControls;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Rampastring.XNAUI;
/// <summary>
/// The exception that is thrown when the <see cref="GUICreator"/> fails to find a matching
/// constructor for a GUI control type.
/// </summary>
public class ConstructorNotFoundException : Exception
{
public ConstructorNotFoundException(string message) : base(message)
{
}
}
/// <summary>
/// Allows creating controls based on their internal names with the help
/// of reflection.
/// </summary>
public class GUICreator
{
private List<Type> controlTypes = new List<Type>()
{
typeof(XNAControl),
typeof(XNAButton),
typeof(XNACheckBox),
typeof(XNADropDown),
typeof(XNALabel),
typeof(XNALinkLabel),
typeof(XNAListBox),
typeof(XNAMultiColumnListBox),
typeof(XNAPanel),
typeof(XNAProgressBar),
typeof(XNASuggestionTextBox),
typeof(XNATextBox),
typeof(XNATrackbar),
};
/// <summary>
/// Adds a control type to the list of available control types.
/// </summary>
/// <param name="type">The control type to add. Needs to be a class type derived from XNAControl.</param>
public void AddControl(Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (!type.IsSubclassOf(typeof(XNAControl)))
throw new ArgumentException("GUICreator.AddControl: Type needs to be a class type derived from XNAControl.");
if (controlTypes.Contains(type))
throw new InvalidOperationException("GUICreator.AddControl: The type " + type.Name + " is already added to the control type list!");
controlTypes.Add(type);
}
/// <summary>
/// Creates and returns a control.
/// </summary>
/// <param name="windowManager">The WindowManager.</param>
/// <param name="controlTypeName">The name of the control class type to create.</param>
/// <returns>A control of the specified type.</returns>
public XNAControl CreateControl(WindowManager windowManager, string controlTypeName)
{
if (windowManager == null)
throw new ArgumentNullException(nameof(windowManager));
if (controlTypeName == null)
throw new ArgumentNullException(nameof(controlTypeName));
Type type = controlTypes.Find(c => c.Name == controlTypeName);
if (type == null)
throw new ArgumentException("GUICreator.CreateControl: Cannot find control type " + controlTypeName);
ConstructorInfo constructor = type.GetConstructor(new Type[] { typeof(WindowManager) });
if (constructor == null)
throw new ConstructorNotFoundException("GUICreator.CreateControl: Cannot find constructor accepting only WindowManager for control type " + controlTypeName);
return (XNAControl)constructor.Invoke(new object[] { windowManager });
}
}