/
Menu.cs
214 lines (189 loc) · 6.14 KB
/
Menu.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
namespace Sourcery
{
/// <summary>Класс описания элемента меню</summary>
class MenuItem
{
#region Поля
/// <summary>Значения</summary>
public List<String> Values;
/// <summary>Контрол кнопки</summary>
public Control ItemControl;
/// <summary>Запись о пункте меню</summary>
private XmlNode _Node;
/// <summary>Наименование пункта меню</summary>
private string _Name;
/// <summary>Действие элемента</summary>
private ActionType _Action;
/// <summary>Область отрисовки кнопки</summary>
private Rectangle _Rectangle;
/// <summary>Расшифровка пункта меню</summary>
private string _Description;
/// <summary>Положение пункта меню</summary>
private int _Position;
/// <summary>Родитель элемента меню</summary>
private MenuItem _Parent;
/// <summary>Тип контрола</summary>
private ControlType _ControlType;
/// <summary>Список дочерних элементов</summary>
public List<MenuItem> Items;
#endregion
#region События
public delegate void ItemChanged(MenuItem item);
public event ItemChanged OnItemChanged;
#endregion
#region Конструкторы
/// <summary>Создаёт новый экземпляр класса <see cref="MenuItem"/>. </summary>
public MenuItem(XmlNode Node,MenuItem Parent,SourceryGame game)
{
_Node = Node;
_Parent = Parent;
_Name = _Node.SelectSingleNode("Name").InnerText;
_Description = _Node.SelectSingleNode("Description").InnerText;
_Position = Convert.ToInt32(_Node.SelectSingleNode("Position").InnerText);
Values = new List<string>();
var val = _Node.SelectSingleNode("Values");
if (val != null)
{
var nodeSelection = val.SelectNodes("Value");
foreach (XmlNode node in nodeSelection)
Values.Add(node.InnerText);
}
var type = _Node.SelectSingleNode("Action").InnerText;
_Action = (ActionType)Enum.Parse(typeof(ActionType), type);
var controltype = _Node.SelectSingleNode("Type").InnerText;
_ControlType = (ControlType)Enum.Parse(typeof(ControlType), controltype);
switch (_ControlType)
{
case ControlType.Button:
{
ItemControl = new Button(game, _Name);
break;
}
case ControlType.CheckBox:
{
ItemControl = new CheckBox(game, _Name);
break;
}
case ControlType.ComboBox:
{
ItemControl = new ComboBox(game, Values);
break;
}
case ControlType.TrackBar:
{
ItemControl = new TrackBar(game,_Name);
break;
}
}
ItemControl.OnValueChanged += OnControlChanged;
var selection = _Node.SelectNodes("Item");
if (selection != null && selection.Count > 0)
{
Items = new List<MenuItem>();
foreach (XmlNode node in selection)
Items.Add(new MenuItem(node,this,game));
}
}
#endregion
#region Свойства
/// <summary>Возвращает или задает тип контрола</summary>
public ControlType Control
{
get { return _ControlType; }
set { _ControlType = value; }
}
/// <summary>Возвращает или задает родителя</summary>
public MenuItem Parent
{
get { return _Parent; }
set { _Parent = value; }
}
/// <summary>Возвращает или задает действие кнопки</summary>
public ActionType Action
{
get { return _Action; }
set { _Action = value; }
}
/// <summary>Возвращает или задает область отрисовки кнопки</summary>
public Rectangle Rect
{
get { return _Rectangle; }
set { _Rectangle = value; }
}
/// <summary>Возвращает или задает имя элемента меню</summary>
public string Name
{
get { return _Name; }
set
{
if (value != _Name)
{
_Node.SelectSingleNode("Name").InnerText = value;
_Name = value;
}
}
}
/// <summary>Возвращает или задает расшифровку элемента меню</summary>
public string Description
{
get { return _Description; }
set
{
if (value != _Description)
{
_Node.SelectSingleNode("Description").InnerText = value;
_Description = value;
}
}
}
/// <summary>Возвращает или задает позицию элемента меню</summary>
public int Position
{
get { return _Position; }
set
{
if (value != _Position)
{
_Node.SelectSingleNode("Position").InnerText = _Position.ToString();
_Position = value;
}
}
}
#endregion
#region Методы
private void OnControlChanged()
{
if (OnItemChanged != null)
OnItemChanged(this);
}
#endregion
}
/// <summary>Класс описания меню</summary>
class Menu
{
#region Поля
/// <summary>Ссылка на документ главного меню</summary>
private XmlDocument _Document;
/// <summary>Элементы меню</summary>
public List<MenuItem> Items;
#endregion
#region Конструкторы
/// <summary> Создаёт новый экземпляр класса <see cref="Menu"/>.</summary>
public Menu(string path, SourceryGame game)
{
_Document = new XmlDocument();
_Document.Load(path);
Items = new List<MenuItem>();
var nodeSelection = _Document.SelectNodes("Menu/Item");
foreach (XmlNode node in nodeSelection)
Items.Add(new MenuItem(node, null, game));
}
#endregion
}
}