Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DroidResources.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.Generic;
5 using Android.Content;
6 using Android.Util;
7 using Android.Views;
8 using Android.Widget;
9 
10 namespace MonoDroid.Dialog
11 {
12  public static class DroidResources
13  {
14  public enum ElementLayout: int
15  {
16  dialog_boolfieldleft,
17  dialog_boolfieldright,
18  dialog_boolfieldsubleft,
19  dialog_boolfieldsubright,
20 
21  dialog_button,
22  dialog_datefield,
23  dialog_fieldsetlabel,
24  dialog_labelfieldbelow,
25  dialog_labelfieldright,
26  dialog_onofffieldright,
27  dialog_panel,
28  dialog_root,
29  dialog_selectlist,
30  dialog_selectlistfield,
31  dialog_textarea,
32 
33  dialog_floatimage,
34 
35  dialog_textfieldbelow,
36  dialog_textfieldright,
37  }
38 
39  public static View LoadFloatElementLayout(Context context, View convertView, ViewGroup parent, int layoutId, out TextView label, out SeekBar slider, out ImageView left, out ImageView right)
40  {
41  View layout = convertView ?? LoadLayout(context, parent, layoutId);
42  if (layout != null)
43  {
44  label = layout.FindViewById<TextView>(context.Resources.GetIdentifier("dialog_LabelField", "id", context.PackageName));
45  slider = layout.FindViewById<SeekBar>(context.Resources.GetIdentifier("dialog_SliderField", "id", context.PackageName));
46  left = layout.FindViewById<ImageView>(context.Resources.GetIdentifier("dialog_ImageLeft", "id", context.PackageName));
47  right = layout.FindViewById<ImageView>(context.Resources.GetIdentifier("dialog_ImageRight", "id", context.PackageName));
48  }
49  else
50  {
51  label = null;
52  slider = null;
53  left = right = null;
54  }
55  return layout;
56  }
57 
58 
59  private static View LoadLayout(Context context, ViewGroup parent, int layoutId)
60  {
61  try
62  {
63  LayoutInflater inflater = LayoutInflater.FromContext(context);
64  if (_resourceMap.ContainsKey((ElementLayout)layoutId))
65  {
66  string layoutName = _resourceMap[(ElementLayout)layoutId];
67  int layoutIndex = context.Resources.GetIdentifier(layoutName, "layout", context.PackageName);
68  return inflater.Inflate(layoutIndex, parent, false);
69  }
70  else
71  {
72  // TODO: figure out what context to use to get this right, currently doesn't inflate application resources
73  return inflater.Inflate(layoutId, parent, false);
74  }
75  }
76  catch (InflateException ex)
77  {
78  Log.Error("MDD", "Inflate failed: " + ex.Cause.Message);
79  }
80  catch (Exception ex)
81  {
82  Log.Error("MDD", "LoadLayout failed: " + ex.Message);
83  }
84  return null;
85  }
86 
87  public static View LoadStringElementLayout(Context context, View convertView, ViewGroup parent, int layoutId, out TextView label, out TextView value)
88  {
89  View layout = convertView ?? LoadLayout(context, parent, layoutId);
90  if (layout != null)
91  {
92  label = layout.FindViewById<TextView>(context.Resources.GetIdentifier("dialog_LabelField", "id", context.PackageName));
93  value = layout.FindViewById<TextView>(context.Resources.GetIdentifier("dialog_ValueField", "id", context.PackageName));
94  if(label == null || value == null)
95  {
96  layout = LoadLayout(context, parent, layoutId);
97  label = layout.FindViewById<TextView>(context.Resources.GetIdentifier("dialog_LabelField", "id", context.PackageName));
98  value = layout.FindViewById<TextView>(context.Resources.GetIdentifier("dialog_ValueField", "id", context.PackageName));
99  }
100  }
101  else
102  {
103  label = null;
104  value = null;
105  }
106  return layout;
107  }
108 
109  public static View LoadButtonLayout(Context context, View convertView, ViewGroup parent, int layoutId, out Button button)
110  {
111  View layout = LoadLayout(context, parent, layoutId);
112  if (layout != null)
113  {
114  button = layout.FindViewById<Button>(context.Resources.GetIdentifier("dialog_Button", "id", context.PackageName));
115  }
116  else
117  {
118  button = null;
119  }
120  return layout;
121  }
122 
123  public static View LoadMultilineElementLayout(Context context, View convertView, ViewGroup parent, int layoutId, out EditText value)
124  {
125  View layout = convertView ?? LoadLayout(context, parent, layoutId);
126  if (layout != null)
127  {
128  value = layout.FindViewById<EditText>(context.Resources.GetIdentifier("dialog_ValueField", "id", context.PackageName));
129  }
130  else
131  {
132  value = null;
133  }
134  return layout;
135  }
136 
137  public static View LoadBooleanElementLayout(Context context, View convertView, ViewGroup parent, int layoutId, out TextView label, out TextView subLabel, out View value)
138  {
139  View layout = convertView ?? LoadLayout(context, parent, layoutId);
140  if (layout != null)
141  {
142  label = layout.FindViewById<TextView>(context.Resources.GetIdentifier("dialog_LabelField", "id", context.PackageName));
143  value = layout.FindViewById<View>(context.Resources.GetIdentifier("dialog_BoolField", "id", context.PackageName));
144  int id = context.Resources.GetIdentifier("dialog_LabelSubtextField", "id", context.PackageName);
145  subLabel = (id >= 0) ? layout.FindViewById<TextView>(id): null;
146  }
147  else
148  {
149  label = null;
150  value = null;
151  subLabel = null;
152  }
153  return layout;
154  }
155 
156  public static View LoadStringEntryLayout(Context context, View convertView, ViewGroup parent, int layoutId, out TextView label, out EditText value)
157  {
158  View layout = LoadLayout(context, parent, layoutId);
159  if (layout != null)
160  {
161  label = layout.FindViewById<TextView>(context.Resources.GetIdentifier("dialog_LabelField", "id", context.PackageName));
162  value = layout.FindViewById<EditText>(context.Resources.GetIdentifier("dialog_ValueField", "id", context.PackageName));
163  }
164  else
165  {
166  label = null;
167  value = null;
168  }
169  return layout;
170  }
171 
172  private static Dictionary<ElementLayout, string> _resourceMap;
173 
174  static DroidResources()
175  {
176  _resourceMap = new Dictionary<ElementLayout, string>()
177  {
178  // Label templates
179  { ElementLayout.dialog_labelfieldbelow, "dialog_labelfieldbelow"},
180  { ElementLayout.dialog_labelfieldright, "dialog_labelfieldright"},
181 
182  // Boolean and Checkbox templates
183  { ElementLayout.dialog_boolfieldleft, "dialog_boolfieldleft"},
184  { ElementLayout.dialog_boolfieldright, "dialog_boolfieldright"},
185  { ElementLayout.dialog_boolfieldsubleft, "dialog_boolfieldsubleft"},
186  { ElementLayout.dialog_boolfieldsubright, "dialog_boolfieldsubright"},
187  { ElementLayout.dialog_onofffieldright, "dialog_onofffieldright"},
188 
189  // Root templates
190  { ElementLayout.dialog_root, "dialog_root"},
191 
192  // Entry templates
193  { ElementLayout.dialog_textfieldbelow, "dialog_textfieldbelow"},
194  { ElementLayout.dialog_textfieldright, "dialog_textfieldright"},
195 
196  // Slider
197  { ElementLayout.dialog_floatimage, "dialog_floatimage"},
198 
199  // Button templates
200  { ElementLayout.dialog_button, "dialog_button"},
201 
202  // Date
203  { ElementLayout.dialog_datefield, "dialog_datefield"},
204 
205  //
206  { ElementLayout.dialog_fieldsetlabel, "dialog_fieldsetlabel"},
207 
208  { ElementLayout.dialog_panel, "dialog_panel"},
209 
210  //
211  { ElementLayout.dialog_selectlist, "dialog_selectlist"},
212  { ElementLayout.dialog_selectlistfield, "dialog_selectlistfield"},
213  { ElementLayout.dialog_textarea, "dialog_textarea"},
214  };
215  }
216  }
217 }
static View LoadMultilineElementLayout(Context context, View convertView, ViewGroup parent, int layoutId, out EditText value)
static View LoadStringEntryLayout(Context context, View convertView, ViewGroup parent, int layoutId, out TextView label, out EditText value)
static View LoadStringElementLayout(Context context, View convertView, ViewGroup parent, int layoutId, out TextView label, out TextView value)
static View LoadButtonLayout(Context context, View convertView, ViewGroup parent, int layoutId, out Button button)
static View LoadFloatElementLayout(Context context, View convertView, ViewGroup parent, int layoutId, out TextView label, out SeekBar slider, out ImageView left, out ImageView right)
static View LoadBooleanElementLayout(Context context, View convertView, ViewGroup parent, int layoutId, out TextView label, out TextView subLabel, out View value)