Release 4.0.0-preview1-00285
[platform/core/csapi/tizenfx.git] / src / ElmSharp.Wearable / ElmSharp.Wearable / RotarySelector.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Text;
21
22 namespace ElmSharp.Wearable
23 {
24     /// <summary>
25     /// The Rotary Selector is a widget to display a selector and multiple items surrounding the selector.
26     /// And an item can be selected by rotary event or user item click.
27     /// Inherits <see cref="Layout"/>.
28     /// </summary>
29     public class RotarySelector : Layout
30     {
31         const string IconPartName = "selector,icon";
32         const string ContentPartName = "selector,content";
33         const string BgPartName = "selector,bg_image";
34
35         const string ItemSelectedEventName = "item,selected";
36         const string ItemClickedEventName = "item,clicked";
37
38         /// <summary>
39         /// Selected will be triggered when selected an item.
40         /// </summary>
41         public event EventHandler<RotarySelectorItemEventArgs> Selected;
42
43         /// <summary>
44         /// Clicked will be triggered when selecting again the alredy selected item or selecting a selector.
45         /// </summary>
46         public event EventHandler<RotarySelectorItemEventArgs> Clicked;
47
48         SmartEvent<PointerEventArgs> _selectedEvent;
49         SmartEvent<PointerEventArgs> _clickedEvent;
50         Image _normalBgImage;
51
52         /// <summary>
53         /// Gets the rotary selector item list of a rotary selector object.
54         /// </summary>
55         public IList<RotarySelectorItem> Items { get; private set; }
56
57         /// <summary>
58         /// Creates and initializes a new instance of the Rotary Selector class.
59         /// </summary>
60         /// <param name="parent">The parent of new Rotary Selector instance</param>
61         public RotarySelector(EvasObject parent) : base(parent)
62         {
63             Items = new RotarySelectorList(this);
64
65             _selectedEvent = new SmartEvent<PointerEventArgs>(this, "item,selected", (d, o, info) => new PointerEventArgs { Pointer = info });
66             _clickedEvent = new SmartEvent<PointerEventArgs>(this, "item,clicked", (d, o, info) => new PointerEventArgs { Pointer = info });
67             _selectedEvent.On += (s, e) =>
68             {
69                 RotarySelectorItem selected = Items.FirstOrDefault(i => i.Handle == e.Pointer);
70                 Selected?.Invoke(this, new RotarySelectorItemEventArgs { Item = selected });
71             };
72
73             _clickedEvent.On += (s, e) =>
74             {
75                 RotarySelectorItem selected = Items.FirstOrDefault(i => i.Handle == e.Pointer);
76                 Clicked?.Invoke(this, new RotarySelectorItemEventArgs { Item = selected });
77             };
78         }
79
80         /// <summary>
81         /// Sets or gets the selected item of a rotary selector object.
82         /// </summary>
83         public RotarySelectorItem SelectedItem
84         {
85             get
86             {
87                 IntPtr selectedPtr = Interop.Eext.eext_rotary_selector_selected_item_get(this);
88                 if (selectedPtr == IntPtr.Zero) return null;
89                 RotarySelectorItem item = Items.FirstOrDefault(i => i.Handle == selectedPtr);
90                 return item;
91             }
92
93             set
94             {
95                 if (!Items.Contains(value)) return;
96                 Interop.Eext.eext_rotary_selector_selected_item_set(this, value.Handle);
97             }
98         }
99
100         void setPart(ref Image prop, string partName, State state, Image img)
101         {
102             if (prop == img) return;
103             prop = img;
104             if (this != null)
105             {
106                 Interop.Eext.eext_rotary_selector_part_content_set(this, partName, (int)state, prop);
107             }
108         }
109         void setPart(ref Color prop, string partName, State state, Color color)
110         {
111             if (prop == color) return;
112             if (this != null)
113             {
114                 Interop.Eext.eext_rotary_selector_part_color_set(this, partName, (int)state, color.R, color.G, color.B, color.A);
115             }
116         }
117
118         /// <summary>
119         /// Sets or gets the background image of a rotary selector object.
120         /// </summary>
121         public Image BackgroundImage { set => setPart(ref _normalBgImage, BgPartName, State.Normal, value); get => _normalBgImage; }
122
123         /// <summary>
124         /// Creates a widget handle.
125         /// </summary>
126         /// <param name="parent">Parent EvasObject</param>
127         /// <returns>Handle IntPtr</returns>
128         protected override IntPtr CreateHandle(EvasObject parent)
129         {
130             IntPtr ptr = Interop.Eext.eext_rotary_selector_add(parent);
131             Interop.Eext.eext_rotary_object_event_activated_set(ptr, true);
132             return ptr;
133         }
134
135         internal enum State
136         {
137             Normal,
138             Pressed
139         }
140     }
141 }