Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RootElement.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using Android.App;
7 using Android.Content;
8 using Android.Views;
9 using Android.Widget;
10 
11 namespace MonoDroid.Dialog
12 {
14  {
15  TextView _caption;
16  TextView _value;
17 
18  int _summarySection, _summaryElement;
19  internal Group _group;
20  public bool UnevenRows;
21  public Func<RootElement, View> _createOnSelected;
22 
23  /// <summary>
24  /// Initializes a RootSection with a caption
25  /// </summary>
26  /// <param name="caption">
27  /// The caption to render.
28  /// </param>
29  public RootElement(string caption)
30  : base(caption, (int) DroidResources.ElementLayout.dialog_root)
31  {
32  _summarySection = -1;
33  Sections = new List<Section>();
34  }
35 
36  /// <summary>
37  /// Initializes a RootSection with a caption and a callback that will
38  /// create the nested UIViewController that is activated when the user
39  /// taps on the element.
40  /// </summary>
41  /// <param name="caption">
42  /// The caption to render.
43  /// </param>
44  public RootElement(string caption, Func<RootElement, View> createOnSelected)
45  : base(caption, (int)DroidResources.ElementLayout.dialog_root)
46  {
47  _summarySection = -1;
48  this._createOnSelected = createOnSelected;
49  Sections = new List<Section>();
50  }
51 
52  /// <summary>
53  /// Initializes a RootElement with a caption with a summary fetched from the specified section and leement
54  /// </summary>
55  /// <param name="caption">
56  /// The caption to render cref="System.String"/>
57  /// </param>
58  /// <param name="section">
59  /// The section that contains the element with the summary.
60  /// </param>
61  /// <param name="element">
62  /// The element index inside the section that contains the summary for this RootSection.
63  /// </param>
64  public RootElement(string caption, int section, int element)
65  : base(caption, (int)DroidResources.ElementLayout.dialog_root)
66  {
67  _summarySection = section;
68  _summaryElement = element;
69  }
70 
71  /// <summary>
72  /// Initializes a RootElement that renders the summary based on the radio settings of the contained elements.
73  /// </summary>
74  /// <param name="caption">
75  /// The caption to ender
76  /// </param>
77  /// <param name="group">
78  /// The group that contains the checkbox or radio information. This is used to display
79  /// the summary information when a RootElement is rendered inside a section.
80  /// </param>
81  public RootElement(string caption, Group group)
82  : base(caption, (int)DroidResources.ElementLayout.dialog_root)
83  {
84  this._group = group;
85  }
86 
87  /// <summary>
88  /// Single save point for a context, elements can get this context via GetContext() for navigation operations
89  /// </summary>
90  public Context Context { get; set; }
91 
92  internal List<Section> Sections = new List<Section>();
93 
94  public int Count
95  {
96  get
97  {
98  return Sections.Count;
99  }
100  }
101 
102  public Section this[int idx]
103  {
104  get
105  {
106  return Sections[idx];
107  }
108  }
109 
110  internal int IndexOf(Section target)
111  {
112  int idx = 0;
113  foreach (Section s in Sections)
114  {
115  if (s == target)
116  return idx;
117  idx++;
118  }
119  return -1;
120  }
121 
122  internal void Prepare()
123  {
124  int current = 0;
125  foreach (Section s in Sections)
126  {
127  foreach (Element e in s.Elements)
128  {
129  var re = e as RadioElement;
130  if (re != null)
131  re.RadioIdx = current++;
132  if (UnevenRows == false && e is IElementSizing)
133  UnevenRows = true;
134  }
135  }
136  }
137 
138  public override string Summary()
139  {
140  return GetSelectedValue();
141  }
142 
143  void SetSectionStartIndex()
144  {
145  int currentIndex = 0;
146  foreach(var section in Sections)
147  {
148  section.StartIndex = currentIndex;
149  currentIndex += section.Count;
150  }
151  }
152  /// <summary>
153  /// Adds a new section to this RootElement
154  /// </summary>
155  /// <param name="section">
156  /// The section to add, if the root is visible, the section is inserted with no animation
157  /// </param>
158  public void Add(Section section)
159  {
160  if (section == null)
161  return;
162 
163  Sections.Add(section);
164  section.Parent = this;
165  SetSectionStartIndex();
166  }
167 
168  //
169  // This makes things LINQ friendly; You can now create RootElements
170  // with an embedded LINQ expression, like this:
171  // new RootElement ("Title") {
172  // from x in names
173  // select new Section (x) { new StringElement ("Sample") }
174  //
175  public void Add(IEnumerable<Section> sections)
176  {
177  foreach (var s in sections)
178  Add(s);
179 
180  SetSectionStartIndex();
181  }
182 
183  /// <summary>
184  /// Inserts a new section into the RootElement
185  /// </summary>
186  /// <param name="idx">
187  /// The index where the section is added <see cref="System.Int32"/>
188  /// </param>
189  /// <param name="newSections">
190  /// A <see cref="Section[]"/> list of sections to insert
191  /// </param>
192  /// <remarks>
193  /// This inserts the specified list of sections (a params argument) into the
194  /// root using the specified animation.
195  /// </remarks>
196  public void Insert(int idx, params Section[] newSections)
197  {
198  if (idx < 0 || idx > Sections.Count)
199  return;
200  if (newSections == null)
201  return;
202 
203  //if (Table != null)
204  // Table.BeginUpdates();
205 
206  int pos = idx;
207  foreach (var s in newSections)
208  {
209  s.Parent = this;
210  Sections.Insert(pos++, s);
211  }
212 
213  SetSectionStartIndex();
214  }
215 
216  /// <summary>
217  /// Removes a section at a specified location
218  /// </summary>
219  public void RemoveAt(int idx)
220  {
221  if (idx < 0 || idx >= Sections.Count)
222  return;
223 
224  Sections.RemoveAt(idx);
225 
226  SetSectionStartIndex();
227  }
228 
229  public void Remove(Section s)
230  {
231  if (s == null)
232  return;
233  int idx = Sections.IndexOf(s);
234  if (idx == -1)
235  return;
236  RemoveAt(idx);
237 
238  SetSectionStartIndex();
239  }
240 
241  public void Clear()
242  {
243  foreach (var s in Sections)
244  s.Dispose();
245  Sections = new List<Section>();
246  }
247 
248  protected override void Dispose(bool disposing)
249  {
250  if (disposing)
251  {
252  Context = null;
253  if (Sections == null)
254  return;
255  Clear();
256  Sections = null;
257  }
258  }
259 
260  /// <summary>
261  /// The currently selected Radio item in the whole Root.
262  /// </summary>
263  public int RadioSelected
264  {
265  get
266  {
267  var radio = _group as RadioGroup;
268  if (radio != null)
269  return radio.Selected;
270  return -1;
271  }
272  set
273  {
274  var radio = _group as RadioGroup;
275  if (radio != null)
276  radio.Selected = value;
277  }
278  }
279 
280  private string GetSelectedValue()
281  {
282  var radio = _group as RadioGroup;
283  if (radio == null)
284  return string.Empty;
285 
286  int selected = radio.Selected;
287  int current = 0;
288  string radioValue = string.Empty;
289  foreach (var s in Sections)
290  {
291  foreach (var e in s.Elements)
292  {
293  if (!(e is RadioElement))
294  continue;
295 
296  if (current == selected)
297  return e.Summary();
298 
299  current++;
300  }
301  }
302 
303  return string.Empty;
304  }
305 
306  public override View GetView(Context context, View convertView, ViewGroup parent)
307  {
308  Context = context;
309 
310  LayoutInflater inflater = LayoutInflater.FromContext(context);
311 
312  View cell = new TextView(context) {TextSize = 16f, Text = Caption};
313  var radio = _group as RadioGroup;
314 
315  if (radio != null)
316  {
317  string radioValue = GetSelectedValue();
318  cell = DroidResources.LoadStringElementLayout(context, convertView, parent, LayoutId, out _caption, out _value);
319  if (cell != null)
320  {
321  _caption.Text = Caption;
322  _value.Text = radioValue;
323 // this.Click = (o, e) => { SelectRadio(); };
324  this.Click += delegate { SelectRadio(); };
325  }
326  }
327  else if (_group != null)
328  {
329  int count = 0;
330  foreach (var s in Sections)
331  {
332  foreach (var e in s.Elements)
333  {
334  var ce = e as CheckboxElement;
335  if (ce != null)
336  {
337  if (ce.Value)
338  count++;
339  continue;
340  }
341  var be = e as BoolElement;
342  if (be != null)
343  {
344  if (be.Value)
345  count++;
346  continue;
347  }
348  }
349  }
350  //cell.DetailTextLabel.Text = count.ToString();
351  }
352  else if (_summarySection != -1 && _summarySection < Sections.Count)
353  {
354  var s = Sections[_summarySection];
355  //if (summaryElement < s.Elements.Count)
356  // cell.DetailTextLabel.Text = s.Elements[summaryElement].Summary();
357  }
358  //cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
359 
360  return cell;
361  }
362 
363 
364  public void SelectRadio()
365  {
366  List<string> items = new List<string>();
367  foreach (var s in Sections)
368  {
369  foreach (var e in s.Elements)
370  {
371  if (e is RadioElement)
372  items.Add(e.Summary());
373  }
374  }
375 
376  var dialog = new AlertDialog.Builder(Context);
377  dialog.SetSingleChoiceItems(items.ToArray(), this.RadioSelected, this);
378  dialog.SetTitle(this.Caption);
379  dialog.SetNegativeButton("Cancel", this);
380  dialog.Create().Show();
381  }
382 
383  void IDialogInterfaceOnClickListener.OnClick(IDialogInterface dialog, int which)
384  {
385  if (which >= 0)
386  {
387  this.RadioSelected = which;
388  string radioValue = GetSelectedValue();
389  _value.Text = radioValue;
390  }
391 
392  dialog.Dismiss();
393  }
394 
395  /// <summary>
396  /// Enumerator that returns all the sections in the RootElement.
397  /// </summary>
398  /// <returns>
399  /// A <see cref="IEnumerator"/>
400  /// </returns>
401  public IEnumerator<Section> GetEnumerator()
402  {
403  return Sections.GetEnumerator();
404  }
405 
406  /// <summary>
407  /// Enumerator that returns all the sections in the RootElement.
408  /// </summary>
409  IEnumerator IEnumerable.GetEnumerator()
410  {
411  return Sections.GetEnumerator();
412  }
413  }
414 }
void Add(Section section)
Adds a new section to this RootElement
Definition: RootElement.cs:158
Func< RootElement, View > _createOnSelected
Definition: RootElement.cs:21
void Add(IEnumerable< Section > sections)
Definition: RootElement.cs:175
_In_ size_t count
Definition: DirectXTexP.h:174
void Insert(int idx, params Section[] newSections)
Inserts a new section into the RootElement
Definition: RootElement.cs:196
RootElement(string caption, Func< RootElement, View > createOnSelected)
Initializes a RootSection with a caption and a callback that will create the nested UIViewController ...
Definition: RootElement.cs:44
function s(a)
RootElement(string caption, int section, int element)
Initializes a RootElement with a caption with a summary fetched from the specified section and leemen...
Definition: RootElement.cs:64
IEnumerator< Section > GetEnumerator()
Enumerator that returns all the sections in the RootElement.
Definition: RootElement.cs:401
override void Dispose(bool disposing)
Definition: RootElement.cs:248
Sections contain individual Element instances that are rendered by MonoDroid.Dialog ...
Definition: Section.cs:28
void RemoveAt(int idx)
Removes a section at a specified location
Definition: RootElement.cs:219
RootElement(string caption)
Initializes a RootSection with a caption
Definition: RootElement.cs:29
RootElement(string caption, Group group)
Initializes a RootElement that renders the summary based on the radio settings of the contained eleme...
Definition: RootElement.cs:81
override View GetView(Context context, View convertView, ViewGroup parent)
Overriden my most derived classes, creates a view that creates a View with the contents for display ...
Definition: RootElement.cs:306
Used by root elements to fetch information when they need to render a summary (Checkbox count or sele...
Definition: Group.cs:9
override string Summary()
Returns a summary of the value represented by this object, suitable for rendering as the result of a ...
Definition: RootElement.cs:138
Captures the information about mutually exclusive elements in a RootElement
Definition: Group.cs:22