Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ButtonBase.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  /// Represents the base primitive for all the button-like controls
13  /// </summary>
14  [DebuggerDisplay("ButtonBase - Name={Name}")]
15  public abstract class ButtonBase : ContentControl
16  {
17  /// <summary>
18  /// The key to the ClickMode dependency property.
19  /// </summary>
20  public readonly static PropertyKey<ClickMode> ClickModePropertyKey = new PropertyKey<ClickMode>("ClickModeKey", typeof(ButtonBase), DefaultValueMetadata.Static(ClickMode.Release));
21 
22  static ButtonBase()
23  {
24  EventManager.RegisterClassHandler(typeof(ButtonBase), ClickEvent, ClickClassHandler);
25  }
26 
27  /// <summary>
28  /// Create an instance of button.
29  /// </summary>
30  protected ButtonBase()
31  {
32  CanBeHitByUser = true;
33  }
34 
35  /// <summary>
36  /// Gets or sets when the Click event occurs.
37  /// </summary>
38  public ClickMode ClickMode
39  {
40  get { return DependencyProperties.Get(ClickModePropertyKey); }
41  set { DependencyProperties.Set(ClickModePropertyKey, value); }
42  }
43 
44  /// <summary>
45  /// Gets a value that indicates whether the button is currently down.
46  /// </summary>
47  public virtual bool IsPressed { get; protected set; }
48 
49  /// <summary>
50  /// Occurs when a <see cref="Button"/> is clicked.
51  /// </summary>
52  /// <remarks>A click event is bubbling</remarks>
53  public event EventHandler<RoutedEventArgs> Click
54  {
55  add { AddHandler(ClickEvent, value); }
56  remove { RemoveHandler(ClickEvent, value); }
57  }
58 
59  /// <summary>
60  /// Identifies the <see cref="Click"/> routed event.
61  /// </summary>
62  public static readonly RoutedEvent<RoutedEventArgs> ClickEvent = EventManager.RegisterRoutedEvent<RoutedEventArgs>(
63  "Click",
64  RoutingStrategy.Bubble,
65  typeof(Button));
66 
67  protected override void OnTouchDown(TouchEventArgs args)
68  {
69  base.OnTouchDown(args);
70 
71  IsPressed = true;
72 
73  if (ClickMode == ClickMode.Press)
74  RaiseEvent(new RoutedEventArgs(ClickEvent));
75  }
76 
77  protected override void OnTouchUp(TouchEventArgs args)
78  {
79  base.OnTouchUp(args);
80 
81  if (IsPressed && ClickMode == ClickMode.Release)
82  RaiseEvent(new RoutedEventArgs(ClickEvent));
83 
84  IsPressed = false;
85  }
86 
87  protected override void OnTouchLeave(TouchEventArgs args)
88  {
89  base.OnTouchLeave(args);
90 
91  IsPressed = false;
92  }
93 
94  /// <summary>
95  /// The class handler of the event <see cref="Click"/>.
96  /// This method can be overridden in inherited classes to perform actions common to all instances of a class.
97  /// </summary>
98  /// <param name="args">The arguments of the event</param>
99  protected virtual void OnClick(RoutedEventArgs args)
100  {
101 
102  }
103 
104  private static void ClickClassHandler(object sender, RoutedEventArgs args)
105  {
106  var buttonBase = (ButtonBase)sender;
107 
108  buttonBase.OnClick(args);
109  }
110  }
111 }
override void OnTouchDown(TouchEventArgs args)
The class handler of the event TouchDown. This method can be overridden in inherited classes to perfo...
Definition: ButtonBase.cs:67
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
override void OnTouchLeave(TouchEventArgs args)
The class handler of the event TouchLeave. This method can be overridden in inherited classes to perf...
Definition: ButtonBase.cs:87
Represents a Windows button control, which reacts to the Click event.
Definition: Button.cs:13
Represents a control with a single piece of content of any type.
ClickMode
Specifies when the Click event should be raised.
Definition: ClickMode.cs:8
ButtonBase()
Create an instance of button.
Definition: ButtonBase.cs:30
Provides data for touch input events.
virtual void OnClick(RoutedEventArgs args)
The class handler of the event Click. This method can be overridden in inherited classes to perform a...
Definition: ButtonBase.cs:99
Represents and identifies a routed event and declares its characteristics.
Definition: RoutedEvent.cs:10
override void OnTouchUp(TouchEventArgs args)
The class handler of the event TouchUp. This method can be overridden in inherited classes to perform...
Definition: ButtonBase.cs:77
A class that represents a tag propety.
Definition: PropertyKey.cs:17