/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace ElmSharp.Wearable
{
///
/// The RotaryEventManager serves functions for the global Rotary event like Galaxy Gear.
///
/// preview
public static class RotaryEventManager
{
static Dictionary s_rotaryEventHandlers = new Dictionary();
///
/// Rotated will be triggered when the rotatable device like the Galaxy Gear Bezel is rotated.
///
/// preview
public static event RotaryEventHandler Rotated
{
add
{
if (s_rotaryEventHandlers.ContainsKey(value)) return;
Interop.Eext.Eext_Rotary_Handler_Cb cb = (data, infoPtr) =>
{
var info = Interop.Eext.FromIntPtr(infoPtr);
value.Invoke(new RotaryEventArgs
{
IsClockwise = info.Direction == Interop.Eext.Eext_Rotary_Event_Direction.Clockwise,
Timestamp = info.TimeStamp
});
return true;
};
Interop.Eext.eext_rotary_event_handler_add(cb, IntPtr.Zero);
s_rotaryEventHandlers[value] = cb;
}
remove
{
Interop.Eext.Eext_Rotary_Handler_Cb cb;
if (s_rotaryEventHandlers.TryGetValue(value, out cb))
{
Interop.Eext.eext_rotary_event_handler_del(cb);
s_rotaryEventHandlers.Remove(value);
}
}
}
}
///
/// The RotaryEventManager serves extension functions for the Rotary event to EvasObject on a device like Galaxy Gear.
///
/// preview
public static class RotaryEventExtensions
{
static Dictionary s_rotaryObjectEventHandlers = new Dictionary();
static Dictionary s_rotaryObjectEventMap = new Dictionary();
///
/// Adds a handler for the Rotary event on a specific EvasObject.
///
/// Target EvasObject.
/// Event handler for the Rotary event.
/// preview
public static void AddRotaryEventHandler(this EvasObject obj, RotaryEventHandler handler)
{
EnableRotaryEventHandler(obj);
if (s_rotaryObjectEventHandlers.ContainsKey(obj))
{
s_rotaryObjectEventHandlers[obj] += handler;
}
else
{
s_rotaryObjectEventHandlers[obj] = handler;
}
}
///
/// Removes a handler on a specific EvasObject for the Rotary event.
///
/// Target EvasObject.
/// Event handler for Rotary event.
/// preview
public static void RemoveRotaryEventHandler(this EvasObject obj, RotaryEventHandler handler)
{
if (s_rotaryObjectEventHandlers.ContainsKey(obj))
{
s_rotaryObjectEventHandlers[obj] -= handler;
if (s_rotaryObjectEventHandlers[obj] == null)
{
DisableRotaryEventHandler(obj, false);
s_rotaryObjectEventHandlers.Remove(obj);
}
}
}
///
/// Activates this Circle widget that can take the Rotary event.
///
/// Target the Circle widget.
/// preview
public static void Activate(this IRotaryActionWidget widget)
{
Interop.Eext.eext_rotary_object_event_activated_set(widget.CircleHandle, true);
}
///
/// Deactivates this circle widget that is blocked from the Rotary event.
///
/// Target the Circle widget.
/// preview
public static void Deactivate(this IRotaryActionWidget widget)
{
Interop.Eext.eext_rotary_object_event_activated_set(widget.CircleHandle, false);
}
///
/// Activates this object that can take the Rotary event.
///
/// Target object.
/// preview
public static void Activate(this EvasObject obj)
{
Interop.Eext.eext_rotary_object_event_activated_set(obj, true);
}
///
/// Deactivates this object that is blocked from the Rotary event.
///
/// Target object.
/// preview
public static void Deactivate(this EvasObject obj)
{
Interop.Eext.eext_rotary_object_event_activated_set(obj, false);
}
static void EnableRotaryEventHandler(EvasObject obj)
{
if (!s_rotaryObjectEventMap.ContainsKey(obj))
{
Interop.Eext.Eext_Rotary_Event_Cb cb = (d, o, i) =>
{
RotaryEventHandler events;
if (s_rotaryObjectEventHandlers.TryGetValue(obj, out events))
{
var info = Interop.Eext.FromIntPtr(i);
events?.Invoke(new RotaryEventArgs
{
IsClockwise = info.Direction == Interop.Eext.Eext_Rotary_Event_Direction.Clockwise,
Timestamp = info.TimeStamp
});
}
return true;
};
Interop.Eext.eext_rotary_object_event_callback_add(obj, cb, IntPtr.Zero);
s_rotaryObjectEventMap[obj] = cb;
obj.Deleted += (s, e) => DisableRotaryEventHandler(obj, true);
}
}
static void DisableRotaryEventHandler(EvasObject obj, bool removeHandler)
{
Interop.Eext.Eext_Rotary_Event_Cb cb;
if (s_rotaryObjectEventMap.TryGetValue(obj, out cb))
{
Interop.Eext.eext_rotary_object_event_callback_del(obj, cb);
s_rotaryObjectEventMap.Remove(obj);
}
if (removeHandler && s_rotaryObjectEventHandlers.ContainsKey(obj))
{
s_rotaryObjectEventHandlers.Remove(obj);
}
}
}
///
/// Handler for the Rotary event.
///
/// The Rotary event information.
/// preview
public delegate void RotaryEventHandler(RotaryEventArgs args);
///
/// The RotaryEventArgs serves information for the triggered Rotary event.
///
/// preview
public class RotaryEventArgs : EventArgs
{
///
/// IsClockwise is true when the Rotary device rotated in the clockwise direction, or false on counter clockwise.
///
/// preview
public bool IsClockwise { get; set; }
///
/// Timestamp of the Rotary event.
///
/// preview
public uint Timestamp { get; set; }
}
}