eb0181c368c31da4bc251f4fd5408751f19c76f8
[platform/core/csapi/tizenfx.git] / src / ElmSharp.Wearable / ElmSharp.Wearable / RotarySelector.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ElmSharp.Wearable
7 {
8     public class RotarySelector : Layout
9     {
10         const string IconPartName = "selector,icon";
11         const string ContentPartName = "selector,content";
12         const string BgPartName = "selector,bg_image";
13
14         const string ItemSelectedEventName = "item,selected";
15         const string ItemClickedEventName = "item,clicked";
16
17         public event EventHandler<RotarySelectorItemEventArgs> Selected;
18         public event EventHandler<RotarySelectorItemEventArgs> Clicked;
19
20         SmartEvent<PointerEventArgs> _selectedEvent;
21         SmartEvent<PointerEventArgs> _clickedEvent;
22         Image _normalBgImage;
23
24         public IList<RotarySelectorItem> Items { get; private set; }
25
26         public RotarySelector(EvasObject parent) : base(parent)
27         {
28             Items = new RotarySelectorList(this);
29
30             _selectedEvent = new SmartEvent<PointerEventArgs>(this, "item,selected", (d, o, info) => new PointerEventArgs { Pointer = info });
31             _clickedEvent = new SmartEvent<PointerEventArgs>(this, "item,clicked", (d, o, info) => new PointerEventArgs { Pointer = info });
32             _selectedEvent.On += (s, e) =>
33             {
34                 RotarySelectorItem selected = Items.FirstOrDefault(i => i.Handle == e.Pointer);
35                 Selected?.Invoke(this, new RotarySelectorItemEventArgs { Item = selected });
36             };
37
38             _clickedEvent.On += (s, e) =>
39             {
40                 RotarySelectorItem selected = Items.FirstOrDefault(i => i.Handle == e.Pointer);
41                 Clicked?.Invoke(this, new RotarySelectorItemEventArgs { Item = selected });
42             };
43         }
44
45         public RotarySelectorItem SelectedItem
46         {
47             get
48             {
49                 IntPtr selectedPtr = Interop.Eext.eext_rotary_selector_selected_item_get(this);
50                 if (selectedPtr == IntPtr.Zero) return null;
51                 RotarySelectorItem item = Items.FirstOrDefault(i => i.Handle == selectedPtr);
52                 return item;
53             }
54
55             set
56             {
57                 if (!Items.Contains(value)) return;
58                 Interop.Eext.eext_rotary_selector_selected_item_set(this, value.Handle);
59             }
60         }
61
62         void setPart(ref Image prop, string partName, State state, Image img)
63         {
64             if (prop == img) return;
65             prop = img;
66             if (this != null)
67             {
68                 Interop.Eext.eext_rotary_selector_part_content_set(this, partName, (int)state, prop);
69             }
70         }
71         void setPart(ref Color prop, string partName, State state, Color color)
72         {
73             if (prop == color) return;
74             if (this != null)
75             {
76                 Interop.Eext.eext_rotary_selector_part_color_set(this, partName, (int)state, color.R, color.G, color.B, color.A);
77             }
78         }
79
80         public Image BackgroundImage { set => setPart(ref _normalBgImage, BgPartName, State.Normal, value); get => _normalBgImage; }
81
82         protected override IntPtr CreateHandle(EvasObject parent)
83         {
84             IntPtr ptr = Interop.Eext.eext_rotary_selector_add(parent);
85             Interop.Eext.eext_rotary_object_event_activated_set(ptr, true);
86             return ptr;
87         }
88
89         internal enum State
90         {
91             Normal,
92             Pressed
93         }
94     }
95 }