Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
BuildFurnitureDialog.cs
浏览该文件的文档.
1using System.Xml.Linq;
2using Engine;
3
4namespace Game {
5 public class BuildFurnitureDialog : Dialog {
7
9
10 public int m_axis;
11 public static string fName = "BuildFurnitureDialog";
12
13 public Action<bool> m_handler;
14
15 public bool m_isValid;
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
51 public BuildFurnitureDialog(FurnitureDesign design, FurnitureDesign sourceDesign, Action<bool> handler) {
52 XElement node = ContentManager.Get<XElement>("Dialogs/BuildFurnitureDialog");
53 LoadContents(this, node);
54 m_nameLabel = Children.Find<LabelWidget>("BuildFurnitureDialog.Name");
55 m_statusLabel = Children.Find<LabelWidget>("BuildFurnitureDialog.Status");
56 m_designWidget2d = Children.Find<FurnitureDesignWidget>("BuildFurnitureDialog.Design2d");
57 m_designWidget3d = Children.Find<FurnitureDesignWidget>("BuildFurnitureDialog.Design3d");
58 m_nameButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.NameButton");
59 m_axisButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.AxisButton");
60 m_leftButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.LeftButton");
61 m_rightButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.RightButton");
62 m_upButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.UpButton");
63 m_downButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.DownButton");
64 m_mirrorButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.MirrorButton");
65 m_turnRightButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.TurnRightButton");
66 m_increaseResolutionButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.IncreaseResolutionButton");
67 m_decreaseResolutionButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.DecreaseResolutionButton");
68 m_resolutionLabel = Children.Find<LabelWidget>("BuildFurnitureDialog.ResolutionLabel");
69 m_cancelButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.CancelButton");
70 m_buildButton = Children.Find<ButtonWidget>("BuildFurnitureDialog.BuildButton");
71 m_handler = handler;
72 m_design = design;
73 m_sourceDesign = sourceDesign;
74 m_axis = 1;
75 int num = 0;
76 num += m_design.Geometry.SubsetOpaqueByFace.Sum(b => b != null ? b.Indices.Count / 3 : 0);
77 num += m_design.Geometry.SubsetAlphaTestByFace.Sum(b => b != null ? b.Indices.Count / 3 : 0);
79 m_statusLabel.Text = string.Format(
81 num,
84 );
85 m_designWidget2d.Design = m_design;
86 m_designWidget3d.Design = m_design;
87 }
88
89 public override void Update() {
90 m_nameLabel.Text = string.IsNullOrEmpty(m_design.Name) ? m_design.GetDefaultName() : m_design.Name;
91 m_designWidget2d.Mode = (FurnitureDesignWidget.ViewMode)m_axis;
92 m_designWidget3d.Mode = FurnitureDesignWidget.ViewMode.Perspective;
94 m_axisButton.Text = LanguageControl.Get(fName, 4);
95 }
97 m_axisButton.Text = LanguageControl.Get(fName, 5);
98 }
100 m_axisButton.Text = LanguageControl.Get(fName, 6);
101 }
102 m_leftButton.IsEnabled = IsShiftPossible(DirectionAxisToDelta(0, m_axis));
103 m_rightButton.IsEnabled = IsShiftPossible(DirectionAxisToDelta(1, m_axis));
104 m_upButton.IsEnabled = IsShiftPossible(DirectionAxisToDelta(2, m_axis));
105 m_downButton.IsEnabled = IsShiftPossible(DirectionAxisToDelta(3, m_axis));
106 m_decreaseResolutionButton.IsEnabled = IsDecreaseResolutionPossible();
107 m_increaseResolutionButton.IsEnabled = IsIncreaseResolutionPossible();
108 m_resolutionLabel.Text = $"{m_design.Resolution}";
109 m_buildButton.IsEnabled = m_isValid;
110 if (m_nameButton.IsClicked) {
111 List<Tuple<string, Action>> list = new();
112 if (m_sourceDesign != null) {
113 list.Add(
114 new Tuple<string, Action>(
116 delegate {
117 Dismiss(false);
118 DialogsManager.ShowDialog(
119 ParentWidget,
120 new TextBoxDialog(
121 LanguageControl.Get(fName, 10),
122 m_sourceDesign.Name,
123 20,
124 delegate(string s) {
125 try {
126 if (s != null) {
127 m_sourceDesign.Name = s;
128 }
129 }
130 catch (Exception ex3) {
131 DialogsManager.ShowDialog(
132 ParentWidget,
133 new MessageDialog(LanguageControl.Error, ex3.Message, LanguageControl.Ok, null, null)
134 );
135 }
136 }
137 )
138 );
139 }
140 )
141 );
142 list.Add(
143 new Tuple<string, Action>(
145 delegate {
148 new TextBoxDialog(
150 m_design.Name,
151 20,
152 delegate(string s) {
153 try {
154 if (s != null) {
155 m_design.Name = s;
156 }
157 }
158 catch (Exception ex2) {
161 new MessageDialog(LanguageControl.Error, ex2.Message, LanguageControl.Ok, null, null)
162 );
163 }
164 }
165 )
166 );
167 }
168 )
169 );
170 }
171 else {
172 list.Add(
173 new Tuple<string, Action>(
175 delegate {
176 DialogsManager.ShowDialog(
177 ParentWidget,
178 new TextBoxDialog(
179 LanguageControl.Get(fName, 11),
180 m_design.Name,
181 20,
182 delegate(string s) {
183 try {
184 if (s != null) {
185 m_design.Name = s;
186 }
187 }
188 catch (Exception ex) {
189 DialogsManager.ShowDialog(
190 ParentWidget,
191 new MessageDialog(LanguageControl.Error, ex.Message, LanguageControl.Ok, null, null)
192 );
193 }
194 }
195 )
196 );
197 }
198 )
199 );
200 }
201 if (list.Count == 1) {
202 list[0].Item2();
203 }
204 else {
205 DialogsManager.ShowDialog(
207 new ListSelectionDialog(
208 LanguageControl.Get(fName, 11),
209 list,
210 64f,
211 t => ((Tuple<string, Action>)t).Item1,
212 delegate(object t) { ((Tuple<string, Action>)t).Item2(); }
213 )
214 );
215 }
216 }
217 if (m_axisButton.IsClicked) {
218 m_axis = (m_axis + 1) % 3;
219 }
220 if (m_leftButton.IsClicked) {
221 Shift(DirectionAxisToDelta(0, m_axis));
222 }
223 if (m_rightButton.IsClicked) {
224 Shift(DirectionAxisToDelta(1, m_axis));
225 }
226 if (m_upButton.IsClicked) {
227 Shift(DirectionAxisToDelta(2, m_axis));
228 }
229 if (m_downButton.IsClicked) {
230 Shift(DirectionAxisToDelta(3, m_axis));
231 }
232 if (m_mirrorButton.IsClicked) {
233 m_design.Mirror(m_axis);
234 }
235 if (m_turnRightButton.IsClicked) {
236 m_design.Rotate(m_axis, 1);
237 }
238 if (m_decreaseResolutionButton.IsClicked) {
239 DecreaseResolution();
240 }
241 if (m_increaseResolutionButton.IsClicked) {
242 IncreaseResolution();
243 }
244 if (m_buildButton.IsClicked && m_isValid) {
245 Dismiss(true);
246 }
247 if (Input.Back
248 || m_cancelButton.IsClicked) {
249 Dismiss(false);
250 }
251 }
252
253 public bool IsShiftPossible(Point3 delta) {
254 int resolution = m_design.Resolution;
255 Box box = m_design.Box;
256 box.Location += delta;
257 if (box.Left >= 0
258 && box.Top >= 0
259 && box.Near >= 0
260 && box.Right <= resolution
261 && box.Bottom <= resolution) {
262 return box.Far <= resolution;
263 }
264 return false;
265 }
266
267 public void Shift(Point3 delta) {
268 if (IsShiftPossible(delta)) {
269 m_design.Shift(delta);
270 }
271 }
272
274 int resolution = m_design.Resolution;
275 if (resolution > 2) {
276 int num = MathUtils.Max(m_design.Box.Width, m_design.Box.Height, m_design.Box.Depth);
277 return resolution > num;
278 }
279 return false;
280 }
281
282 public void DecreaseResolution() {
284 int resolution = m_design.Resolution;
285 Point3 zero = Point3.Zero;
286 if (m_design.Box.Right >= resolution) {
287 zero.X = -1;
288 }
289 if (m_design.Box.Bottom >= resolution) {
290 zero.Y = -1;
291 }
292 if (m_design.Box.Far >= resolution) {
293 zero.Z = -1;
294 }
295 m_design.Shift(zero);
296 m_design.Resize(resolution - 1);
297 }
298 }
299
300 public bool IsIncreaseResolutionPossible() => m_design.Resolution < FurnitureDesign.maxDesign;
301
302 public void IncreaseResolution() {
304 m_design.Resize(m_design.Resolution + 1);
305 }
306 }
307
308 public static Point3 DirectionAxisToDelta(int direction, int axis) {
309 if (direction == 0) {
310 switch (axis) {
311 case 0: return new Point3(0, 0, 1);
312 case 1: return new Point3(1, 0, 0);
313 case 2: return new Point3(1, 0, 0);
314 }
315 }
316 if (direction == 1) {
317 switch (axis) {
318 case 0: return new Point3(0, 0, -1);
319 case 1: return new Point3(-1, 0, 0);
320 case 2: return new Point3(-1, 0, 0);
321 }
322 }
323 if (direction == 2) {
324 switch (axis) {
325 case 0: return new Point3(0, 1, 0);
326 case 1: return new Point3(0, 0, 1);
327 case 2: return new Point3(0, 1, 0);
328 }
329 }
330 if (direction == 3) {
331 switch (axis) {
332 case 0: return new Point3(0, -1, 0);
333 case 1: return new Point3(0, 0, -1);
334 case 2: return new Point3(0, -1, 0);
335 }
336 }
337 return Point3.Zero;
338 }
339
340 public void Dismiss(bool result) {
342 m_handler(result);
343 }
344 }
345}
static int Max(int x1, int x2)
BuildFurnitureDialog(FurnitureDesign design, FurnitureDesign sourceDesign, Action< bool > handler)
static Point3 DirectionAxisToDelta(int direction, int axis)
readonly WidgetsList Children
static object Get(Type type, string name)
static void HideDialog(Dialog dialog)
static void ShowDialog(ContainerWidget parentWidget, Dialog dialog)
void Rotate(int axis, int steps)
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
ContainerWidget ParentWidget
virtual void LoadContents(object eventsTarget, XElement node)
static readonly Point3 Zero