Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
VirtualButtonGroup.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.ObjectModel;
5 using System.Text;
6 
7 namespace SiliconStudio.Paradox.Input
8 {
9  /// <summary>
10  /// A combination of <see cref="IVirtualButton"/>, by default evaluated as the operator '&&' to produce a value if all buttons are pressed.
11  /// </summary>
12  public class VirtualButtonGroup : Collection<IVirtualButton>, IVirtualButton
13  {
14  /// <summary>
15  /// Gets or sets a value indicating whether this instance is determining the value as a disjunction ('||' operator between buttons), false by default ('&&' operator).
16  /// </summary>
17  /// <value><c>true</c> if this instance is disjunction; otherwise, <c>false</c>.</value>
18  public bool IsDisjunction { get; set; }
19 
20  protected override void InsertItem(int index, IVirtualButton item)
21  {
22  if (item == null)
23  {
24  throw new ArgumentNullException("item", "Cannot set null instance of VirtualButton");
25  }
26 
27  if (!Contains(item))
28  {
29  base.InsertItem(index, item);
30  }
31  }
32 
33  protected override void SetItem(int index, IVirtualButton item)
34  {
35  if (item == null)
36  {
37  throw new ArgumentNullException("item", "Cannot add null instance of VirtualButton");
38  }
39 
40  if (!Contains(item))
41  {
42  base.SetItem(index, item);
43  }
44  }
45 
46  /// <summary>
47  /// Implements the + operator.
48  /// </summary>
49  /// <param name="combination">The combination.</param>
50  /// <param name="virtualButton">The virtual button.</param>
51  /// <returns>The result of the operator.</returns>
52  public static VirtualButtonGroup operator +(VirtualButtonGroup combination, IVirtualButton virtualButton)
53  {
54  combination.Add(virtualButton);
55  return combination;
56  }
57 
58  public virtual float GetValue(InputManagerBase manager)
59  {
60  float value = 0.0f;
61  foreach (var virtualButton in Items)
62  {
63  float newValue = virtualButton != null ? virtualButton.GetValue(manager) : 0.0f;
64 
65  // In case of a || (disjunction) set, we return the latest non-zero value.
66  if (IsDisjunction)
67  {
68  if (newValue != 0.0f)
69  {
70  value = newValue;
71  }
72  }
73  else
74  {
75  // In case of a && (conjunction) set, we return the last non-zero value unless there is a zero value.
76  if (newValue == 0.0f)
77  {
78  return 0.0f;
79  }
80 
81  value = newValue;
82  }
83  }
84  return value;
85  }
86 
87  public override string ToString()
88  {
89  var text = new StringBuilder();
90  for (int i = 0; i < Items.Count; i++)
91  {
92  var virtualButton = Items[i];
93  if (i > 0)
94  {
95  text.Append(IsDisjunction ? " || " : " && ");
96  }
97  text.AppendFormat("{0}", virtualButton);
98  }
99  return text.ToString();
100  }
101  }
102 }
override void SetItem(int index, IVirtualButton item)
virtual float GetValue(InputManagerBase manager)
Gets the value associated with this virtual button from an input manager.
override void InsertItem(int index, IVirtualButton item)
Interface for input management system, including keyboard, mouse, gamepads and touch.
A combination of IVirtualButton, by default evaluated as the operator '&&' to produce a value if all ...