Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ToggleButton.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.Diagnostics;
5 
6 using SiliconStudio.Core;
7 using SiliconStudio.Paradox.UI.Events;
8 
9 namespace SiliconStudio.Paradox.UI.Controls
10 {
11  /// <summary>
12  /// Represent a UI toggle button. A toggle but can have two or three states depending on the <see cref="IsThreeState"/> property.
13  /// </summary>
14  [DebuggerDisplay("ToggleButton - Name={Name}")]
15  public class ToggleButton : ButtonBase
16  {
17  /// <summary>
18  /// The key to the CheckedImagePropertyKey dependency property.
19  /// </summary>
20  public static readonly PropertyKey<UIImage> CheckedImagePropertyKey = new PropertyKey<UIImage>("CheckedImageModeKey", typeof(ToggleButton), ObjectInvalidationMetadata.New<UIImage>(OnToggleImageInvalidated));
21 
22  /// <summary>
23  /// The key to the IndeterminateImagePropertyKey dependency property.
24  /// </summary>
25  public static readonly PropertyKey<UIImage> IndeterminateImagePropertyKey = new PropertyKey<UIImage>("IndeterminateImageModeKey", typeof(ToggleButton), ObjectInvalidationMetadata.New<UIImage>(OnToggleImageInvalidated));
26 
27  /// <summary>
28  /// The key to the UncheckedImagePropertyKey dependency property.
29  /// </summary>
30  public static readonly PropertyKey<UIImage> UncheckedImagePropertyKey = new PropertyKey<UIImage>("UncheckedImageModeKey", typeof(ToggleButton), ObjectInvalidationMetadata.New<UIImage>(OnToggleImageInvalidated));
31 
32  private static void OnToggleImageInvalidated(object propertyOwner, PropertyKey propertyKey, object propertyOldValue)
33  {
34  var toggle = (ToggleButton)propertyOwner;
35  toggle.OnToggleImageInvalidated();
36  }
37 
38  /// <summary>
39  /// Function triggered when one of the <see cref="CheckedImage"/>, <see cref="IndeterminateImage"/> and <see cref="UncheckedImage"/> images are invalidated.
40  /// This function can be overridden in inherited classes.
41  /// </summary>
42  protected virtual void OnToggleImageInvalidated()
43  {
44  }
45 
46  private bool isThreeState;
47 
48  private ToggleState state;
49 
50  public ToggleButton()
51  {
52  DrawLayerNumber += 1; // (toggle design image)
53  Padding = new Thickness(10, 5, 10, 7);
54  State = ToggleState.UnChecked;
55  }
56 
57  /// <summary>
58  /// Gets or sets the image that the button displays when checked
59  /// </summary>
60  public UIImage CheckedImage
61  {
62  get { return DependencyProperties.Get(CheckedImagePropertyKey); }
63  set { DependencyProperties.Set(CheckedImagePropertyKey, value); }
64  }
65 
66  /// <summary>
67  /// Gets or sets the image that the button displays when indeterminate
68  /// </summary>
69  public UIImage IndeterminateImage
70  {
71  get { return DependencyProperties.Get(IndeterminateImagePropertyKey); }
72  set { DependencyProperties.Set(IndeterminateImagePropertyKey, value); }
73  }
74 
75  /// <summary>
76  /// Gets or sets the image that the button displays when unchecked
77  /// </summary>
78  public UIImage UncheckedImage
79  {
80  get { return DependencyProperties.Get(UncheckedImagePropertyKey); }
81  set { DependencyProperties.Set(UncheckedImagePropertyKey, value); }
82  }
83 
84  /// <summary>
85  /// Gets or sets the state of the <see cref="ToggleButton"/>
86  /// </summary>
87  /// <remarks>Setting the state of the toggle button to <see cref="ToggleState.Indeterminate"/> sets <see cref="IsThreeState"/> to true.</remarks>
88  public ToggleState State
89  {
90  get { return state; }
91  set
92  {
93  if(state == value)
94  return;
95 
96  state = value;
97 
98  switch (value)
99  {
100  case ToggleState.Checked:
101  RaiseEvent(new RoutedEventArgs(CheckedEvent));
102  break;
103  case ToggleState.Indeterminate:
104  IsThreeState = true;
105  RaiseEvent(new RoutedEventArgs(IndeterminateEvent));
106  break;
107  case ToggleState.UnChecked:
108  RaiseEvent(new RoutedEventArgs(UncheckedEvent));
109  break;
110  default:
111  throw new ArgumentOutOfRangeException("value");
112  }
113  }
114  }
115 
116  /// <summary>
117  /// Determines whether the control supports two or three states.
118  /// </summary>
119  /// <remarks>Setting <see cref="IsThreeState"/> to false changes the <see cref="State"/> of the toggle button if currently set to <see cref="ToggleState.Indeterminate"/></remarks>
120  public bool IsThreeState
121  {
122  get { return isThreeState; }
123  set
124  {
125  if(value == false && State == ToggleState.Indeterminate)
126  GoToNextState();
127 
128  isThreeState = value;
129  }
130  }
131 
132  /// <summary>
133  /// Occurs when a <see cref="ToggleButton"/> is checked.
134  /// </summary>
135  /// <remarks>A checked event is bubbling</remarks>
136  public event EventHandler<RoutedEventArgs> Checked
137  {
138  add { AddHandler(CheckedEvent, value); }
139  remove { RemoveHandler(CheckedEvent, value); }
140  }
141 
142  /// <summary>
143  /// Occurs when a <see cref="ToggleButton"/> is Indeterminate.
144  /// </summary>
145  /// <remarks>A Indeterminate event is bubbling</remarks>
146  public event EventHandler<RoutedEventArgs> Indeterminate
147  {
148  add { AddHandler(IndeterminateEvent, value); }
149  remove { RemoveHandler(IndeterminateEvent, value); }
150  }
151 
152  /// <summary>
153  /// Occurs when a <see cref="ToggleButton"/> is Unchecked.
154  /// </summary>
155  /// <remarks>A Unchecked event is bubbling</remarks>
156  public event EventHandler<RoutedEventArgs> Unchecked
157  {
158  add { AddHandler(UncheckedEvent, value); }
159  remove { RemoveHandler(UncheckedEvent, value); }
160  }
161 
162  /// <summary>
163  /// Move the state of the toggle button to the next state. States order is: Unchecked -> Checked [-> Indeterminate] -> Unchecked -> ...
164  /// </summary>
165  protected void GoToNextState()
166  {
167  switch (State)
168  {
169  case ToggleState.Checked:
170  State = IsThreeState ? ToggleState.Indeterminate : ToggleState.UnChecked;
171  break;
172  case ToggleState.Indeterminate:
173  State = ToggleState.UnChecked;
174  break;
175  case ToggleState.UnChecked:
176  State = ToggleState.Checked;
177  break;
178  default:
179  throw new ArgumentOutOfRangeException();
180  }
181  }
182 
183  /// <summary>
184  /// Identifies the <see cref="Checked"/> routed event.
185  /// </summary>
186  public static readonly RoutedEvent<RoutedEventArgs> CheckedEvent = EventManager.RegisterRoutedEvent<RoutedEventArgs>(
187  "Checked",
188  RoutingStrategy.Bubble,
189  typeof(ToggleButton));
190 
191  /// <summary>
192  /// Identifies the <see cref="Indeterminate"/> routed event.
193  /// </summary>
194  public static readonly RoutedEvent<RoutedEventArgs> IndeterminateEvent = EventManager.RegisterRoutedEvent<RoutedEventArgs>(
195  "Indeterminate",
196  RoutingStrategy.Bubble,
197  typeof(ToggleButton));
198 
199  /// <summary>
200  /// Identifies the <see cref="Unchecked"/> routed event.
201  /// </summary>
202  public static readonly RoutedEvent<RoutedEventArgs> UncheckedEvent = EventManager.RegisterRoutedEvent<RoutedEventArgs>(
203  "Unchecked",
204  RoutingStrategy.Bubble,
205  typeof(ToggleButton));
206 
207  protected override void OnClick(RoutedEventArgs args)
208  {
209  base.OnClick(args);
210 
211  GoToNextState();
212  }
213  }
214 }
The state of the toggle button is undetermined
Represent a UI toggle button. A toggle but can have two or three states depending on the IsThreeState...
Definition: ToggleButton.cs:15
Contains state information and event data associated with a routed event.
Represents the base primitive for all the button-like controls
Definition: ButtonBase.cs:15
Describes the thickness of a frame around a cuboid. Six float values describe the Left...
Definition: Thickness.cs:12
The toggle button is checked.
virtual void OnToggleImageInvalidated()
Function triggered when one of the CheckedImage, IndeterminateImage and UncheckedImage images are inv...
Definition: ToggleButton.cs:42
Represents and identifies a routed event and declares its characteristics.
Definition: RoutedEvent.cs:10
Class holding all the data required to define an UI image.
Definition: UIImage.cs:15
override void OnClick(RoutedEventArgs args)
The class handler of the event Click. This method can be overridden in inherited classes to perform a...
A class that represents a tag propety.
Definition: PropertyKey.cs:17
ToggleState
Describe the different possible states of an ToggleButton.
Definition: ToggleState.cs:10
void GoToNextState()
Move the state of the toggle button to the next state. States order is: Unchecked -> Checked [-> Inde...