Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
VirtualButton.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 System.Reflection;
6 using SiliconStudio.Core.Extensions;
7 using SiliconStudio.Core.Mathematics;
8 
9 
10 namespace SiliconStudio.Paradox.Input
11 {
12  /// <summary>
13  /// Describes a virtual button (a key from a keyboard, a mouse button, an axis of a joystick...etc.).
14  /// </summary>
15  public abstract partial class VirtualButton : IVirtualButton
16  {
17  private static readonly Dictionary<int, VirtualButton> mapIp = new Dictionary<int, VirtualButton>();
18  private static readonly Dictionary<string, VirtualButton> mapName = new Dictionary<string, VirtualButton>();
19  private static readonly List<VirtualButton> registered = new List<VirtualButton>();
20  private static IReadOnlyCollection<VirtualButton> registeredReadOnly;
21  internal const int TypeIdMask = 0x0FFFFFFF;
22 
23  /// <summary>
24  /// Initializes a new instance of the <see cref="VirtualButton" /> class.
25  /// </summary>
26  /// <param name="name">The name.</param>
27  /// <param name="type">The type.</param>
28  /// <param name="id">The id.</param>
29  /// <param name="isPositiveAndNegative">if set to <c>true</c> [is positive and negative].</param>
30  private VirtualButton(string name, VirtualButtonType type, int id, bool isPositiveAndNegative = false)
31  {
32  Id = (int)type | id;
33  Name = name;
34  Type = type;
35  IsPositiveAndNegative = isPositiveAndNegative;
36  }
37 
38  /// <summary>
39  /// Unique Id for a particular button <see cref="Type"/>.
40  /// </summary>
41  public readonly int Id;
42 
43  /// <summary>
44  /// Name of this button.
45  /// </summary>
46  public readonly string Name;
47 
48  /// <summary>
49  /// Type of this button.
50  /// </summary>
51  public readonly VirtualButtonType Type;
52 
53  /// <summary>
54  /// A boolean indicating whether this button supports positive and negative value.
55  /// </summary>
56  public readonly bool IsPositiveAndNegative;
57 
58  public override string ToString()
59  {
60  return string.Format("{0}", Name);
61  }
62 
63  /// <summary>
64  /// Implements the + operator to combine to <see cref="VirtualButton"/>.
65  /// </summary>
66  /// <param name="left">The left virtual button.</param>
67  /// <param name="right">The right virtual button.</param>
68  /// <returns>A set containting the two virtual buttons.</returns>
69  public static IVirtualButton operator +(IVirtualButton left, VirtualButton right)
70  {
71  if (left == null)
72  {
73  return right;
74  }
75 
76  return right == null ? left : new VirtualButtonGroup { left, right };
77  }
78 
79  /// <summary>
80  /// Performs an implicit conversion from <see cref="VirtualButton" /> to <see cref="VirtualButtonAnd" />.
81  /// </summary>
82  /// <param name="button">The virtual button.</param>
83  /// <returns>The result of the conversion.</returns>
84  public static implicit operator VirtualButtonGroup(VirtualButton button)
85  {
86  return new VirtualButtonGroup() { button };
87  }
88 
89  /// <summary>
90  /// Gets all registered <see cref="VirtualButton"/>.
91  /// </summary>
92  /// <value>The registered virtual buttons.</value>
93  public static IReadOnlyCollection<VirtualButton> Registered
94  {
95  get
96  {
97  EnsureInitialize();
98  return registeredReadOnly;
99  }
100  }
101 
102  /// <summary>
103  /// Finds a virtual button by the specified name.
104  /// </summary>
105  /// <param name="name">The name.</param>
106  /// <returns>An instance of VirtualButton or null if no match.</returns>
107  public static VirtualButton Find(string name)
108  {
109  VirtualButton virtualButton;
110  EnsureInitialize();
111  mapName.TryGetValue(name, out virtualButton);
112  return virtualButton;
113  }
114 
115  /// <summary>
116  /// Finds a virtual button by the specified id.
117  /// </summary>
118  /// <param name="id">The id.</param>
119  /// <returns>An instance of VirtualButton or null if no match.</returns>
120  public static VirtualButton Find(int id)
121  {
122  VirtualButton virtualButton;
123  EnsureInitialize();
124  mapIp.TryGetValue(id, out virtualButton);
125  return virtualButton;
126  }
127 
128  public abstract float GetValue(InputManagerBase manager);
129 
130  private static void EnsureInitialize()
131  {
132  lock (mapIp)
133  {
134  if (mapIp.Count == 0)
135  {
136  RegisterFromType(typeof(Keyboard));
137  RegisterFromType(typeof(GamePad));
138  registeredReadOnly = registered;
139  }
140  }
141  }
142 
143  internal static float ClampValue(float value)
144  {
145  return MathUtil.Clamp(value, -1.0f, 1.0f);
146  }
147 
148  private static void RegisterFromType(Type type)
149  {
150  foreach (var fieldInfo in type.GetTypeInfo().DeclaredFields)
151  {
152  if (fieldInfo.IsStatic && fieldInfo.FieldType == typeof(VirtualButton))
153  {
154  Register((VirtualButton)fieldInfo.GetValue(null));
155  }
156  }
157  }
158 
159  private static void Register(VirtualButton virtualButton)
160  {
161 
162  if (!mapIp.ContainsKey(virtualButton.Id))
163  {
164  mapIp.Add(virtualButton.Id, virtualButton);
165  registered.Add(virtualButton);
166  }
167 
168  if (!mapName.ContainsKey(virtualButton.Name))
169  {
170  mapName.Add(virtualButton.Name, virtualButton);
171  }
172  }
173  }
174 }
readonly bool IsPositiveAndNegative
A boolean indicating whether this button supports positive and negative value.
readonly int Id
Unique Id for a particular button Type.
Describes a virtual button (a key from a keyboard, a mouse button, an axis of a joystick...etc.).
readonly VirtualButtonType Type
Type of this button.
Interface for input management system, including keyboard, mouse, gamepads and touch.
static VirtualButton Find(string name)
Finds a virtual button by the specified name.
readonly string Name
Name of this button.
static VirtualButton Find(int id)
Finds a virtual button by the specified id.
A combination of IVirtualButton, by default evaluated as the operator '&&' to produce a value if all ...
VirtualButtonType
Type of a VirtualButton.