2 using System.Collections.Generic;
3 using System.Runtime.InteropServices;
6 namespace ElmSharp.Wearable
9 /// RotaryEventManager serve functions for global Rotary event like Galaxy Gear.
11 public static class RotaryEventManager
13 static Dictionary<RotaryEventHandler, Interop.Eext.Eext_Rotary_Handler_Cb> s_rotaryEventHandlers = new Dictionary<RotaryEventHandler, Interop.Eext.Eext_Rotary_Handler_Cb>();
16 /// Rotated will triggered when rotatable device like Galaxy Gear Bezel is rotated.
18 public static event RotaryEventHandler Rotated
22 if (s_rotaryEventHandlers.ContainsKey(value)) return;
24 Interop.Eext.Eext_Rotary_Handler_Cb cb = (data, infoPtr) =>
26 var info = Interop.Eext.FromIntPtr(infoPtr);
27 value.Invoke(new RotaryEventArgs
29 IsClockwise = info.Direction == Interop.Eext.Eext_Rotary_Event_Direction.Clockwise,
30 Timestamp = info.TimeStamp
34 Interop.Eext.eext_rotary_event_handler_add(cb, IntPtr.Zero);
35 s_rotaryEventHandlers[value] = cb;
40 Interop.Eext.Eext_Rotary_Handler_Cb cb;
41 if (s_rotaryEventHandlers.TryGetValue(value, out cb))
43 Interop.Eext.eext_rotary_event_handler_del(cb);
44 s_rotaryEventHandlers.Remove(value);
52 /// RotaryEventManager serve extension functions for Rotary event to EvasObject on device like Galaxy Gear.
54 public static class EvasObjectExtensions
56 static Dictionary<EvasObject, RotaryEventHandler> s_rotaryObjectEventHandlers = new Dictionary<EvasObject, RotaryEventHandler>();
57 static Dictionary<EvasObject, Interop.Eext.Eext_Rotary_Event_Cb> s_rotaryObjectEventMap = new Dictionary<EvasObject, Interop.Eext.Eext_Rotary_Event_Cb>();
60 /// Add a handler for Rotary event on specific EvasObject.
62 /// <param name="obj">Target EvasObject</param>
63 /// <param name="handler">Event handler for Rotary event</param>
64 public static void AddRotaryEventHandler(this EvasObject obj, RotaryEventHandler handler)
66 EnableRotaryEventHandler(obj);
68 if (s_rotaryObjectEventHandlers.ContainsKey(obj))
70 s_rotaryObjectEventHandlers[obj] += handler;
74 s_rotaryObjectEventHandlers[obj] = handler;
79 /// Remove a handler on specific EvasObject for Rotary event.
81 /// <param name="obj">Target EvasObject</param>
82 /// <param name="handler">Event handler for Rotary event</param>
83 public static void RemoveRotaryEventHandler(this EvasObject obj, RotaryEventHandler handler)
85 if (s_rotaryObjectEventHandlers.ContainsKey(obj))
87 s_rotaryObjectEventHandlers[obj] -= handler;
88 if (s_rotaryObjectEventHandlers[obj] == null)
90 DisableRotaryEventHandler(obj, false);
91 s_rotaryObjectEventHandlers.Remove(obj);
97 /// Activate this object can take Rotary event.
99 /// <param name="obj">Target object</param>
100 public static void Activate(this EvasObject obj)
102 Interop.Eext.eext_rotary_object_event_activated_set(obj, true);
106 /// Deactivate this object is blocked from Rotary event.
108 /// <param name="obj">Target object</param>
109 public static void Deactivate(this EvasObject obj)
111 Interop.Eext.eext_rotary_object_event_activated_set(obj, false);
114 static void EnableRotaryEventHandler(EvasObject obj)
116 if (!s_rotaryObjectEventMap.ContainsKey(obj))
118 Interop.Eext.Eext_Rotary_Event_Cb cb = (d, o, i) =>
120 RotaryEventHandler events;
121 if (s_rotaryObjectEventHandlers.TryGetValue(obj, out events))
123 var info = Interop.Eext.FromIntPtr(i);
124 events?.Invoke(new RotaryEventArgs
126 IsClockwise = info.Direction == Interop.Eext.Eext_Rotary_Event_Direction.Clockwise,
127 Timestamp = info.TimeStamp
132 Interop.Eext.eext_rotary_object_event_callback_add(obj, cb, IntPtr.Zero);
133 s_rotaryObjectEventMap[obj] = cb;
134 obj.Deleted += (s, e) => DisableRotaryEventHandler(obj, true);
138 static void DisableRotaryEventHandler(EvasObject obj, bool removeHandler)
140 Interop.Eext.Eext_Rotary_Event_Cb cb;
141 if (s_rotaryObjectEventMap.TryGetValue(obj, out cb))
143 Interop.Eext.eext_rotary_object_event_callback_del(obj, cb);
144 s_rotaryObjectEventMap.Remove(obj);
146 if (removeHandler && s_rotaryObjectEventHandlers.ContainsKey(obj))
148 s_rotaryObjectEventHandlers.Remove(obj);
154 /// Handler for Rotary event
156 /// <param name="args">Rotary event information</param>
157 public delegate void RotaryEventHandler(RotaryEventArgs args);
160 /// RotaryEventArgs serve information for triggered rotary event.
162 public class RotaryEventArgs : EventArgs
165 /// IsClockwise is true when Rotary device rotated clockwise direction or false on counter clockwise.
167 public bool IsClockwise { get; set; }
170 /// Timestamp of rotary event
172 public uint Timestamp { get; set; }