summaryrefslogtreecommitdiff
path: root/NUISamples/NUISamples/NUISamples.Tizen/examples/custom-control.cs
blob: df9e60eb9af9b3feb6b4e2e8a7faca6c7df6010a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *
 * 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.Runtime.InteropServices;
using Tizen.NUI.UIComponents;
using Tizen.NUI.BaseComponents;
using Tizen.NUI;

namespace CustomControlTest
{

    // A custom control for star rating (draggable to change the rating)
    class StarRating : CustomView
    {
        private const string resources = "/home/owner/apps_rw/NUISamples.TizenTV/res";
        private FlexContainer _container;
        private ImageView[] _images;
        private Vector3 _gestureDisplacement;
        private int _currentValue;
        private int _myRating;
        private bool _myDragEnabled;

        // Called by DALi Builder if it finds a StarRating control in a JSON file
        static CustomView CreateInstance()
        {
          return new StarRating();
        }

        // static constructor registers the control type (only runs once)
        static StarRating()
        {
          // ViewRegistry registers control type with DALi type registery
          // also uses introspection to find any properties that need to be registered with type registry
          CustomViewRegistry.Instance.Register(CreateInstance, typeof(StarRating) );
        }

        public StarRating() : base(typeof(StarRating).Name, CustomViewBehaviour.ViewBehaviourDefault)
        {
        }

        public override void OnInitialize()
        {
            // Create a container for the star images
            _container = new FlexContainer();

            _container.FlexDirection = FlexContainer.FlexDirectionType.Row;
            _container.WidthResizePolicy = ResizePolicyType.FillToParent;
            _container.HeightResizePolicy = ResizePolicyType.FillToParent;

            this.Add(_container);

            // Create the images
            _images = new ImageView[5];

            for(int i = 0; i < 5; i++)
            {
                _images[i] = new ImageView(resources+"/images/star-dim.png");
                _container.Add( _images[i] );
            }

            // Update the images according to the rating (dimmed star by default)
            _myRating = 0;
            UpdateStartImages(_myRating);

            // Enable pan gesture detection
            EnableGestureDetection(Gesture.GestureType.Pan);
            _myDragEnabled = true; // Allow dragging by default (can be disabled)
        }

        // Pan gesture handling
        public override void OnPan(PanGesture gesture)
        {
            // Only handle pan gesture if dragging is allowed
            if(_myDragEnabled)
            {
                switch (gesture.State)
                {
                    case Gesture.StateType.Started:
                    {
                        _gestureDisplacement = new Vector3(0.0f, 0.0f, 0.0f);
                        _currentValue = 0;
                        break;
                    }
                    case Gesture.StateType.Continuing:
                    {
                        // Calculate the rating according to pan desture displacement
                        _gestureDisplacement.X += gesture.Displacement.X;
                        int delta = (int)Math.Ceiling(_gestureDisplacement.X / 40.0f);
                        _currentValue = _myRating + delta;

                        // Clamp the rating
                        if(_currentValue < 0) _currentValue = 0;
                        if(_currentValue > 5) _currentValue = 5;

                        // Update the images according to the rating
                        UpdateStartImages(_currentValue);
                        break;
                    }
                    default:
                    {
                        _myRating = _currentValue;
                        break;
                    }
                }
            }
        }

        // Update the images according to the rating
        private void UpdateStartImages(int rating)
        {
            for(int i = 0; i < rating; i++)
            {
                _images[i].WidthResizePolicy = ResizePolicyType.UseNaturalSize;
                _images[i].HeightResizePolicy = ResizePolicyType.UseNaturalSize;
                _images[i].SetImage(resources+"/images/star-highlight.png");
            }

            for(int i = rating; i < 5; i++)
            {
                _images[i].WidthResizePolicy = ResizePolicyType.UseNaturalSize;
                _images[i].HeightResizePolicy = ResizePolicyType.UseNaturalSize;
                _images[i].SetImage(resources+"/images/star-dim.png");
            }
        }

        // Rating property of type int:
        public int Rating
        {
            get
            {
                return _myRating;
            }
            set
            {
                _myRating = value;
                UpdateStartImages(_myRating);
            }
        }

        // DragEnabled property of type bool:
        public bool DragEnabled
        {
            get
            {
                return _myDragEnabled;
            }
            set
            {
                _myDragEnabled = value;
            }
        }
    }

    class Example : NUIApplication
    {
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        delegate void CallbackDelegate();
        private const string resources = "/home/owner/apps_rw/NUISamples.TizenTV/res";


        public Example() : base()
        {
        }

        public Example(string stylesheet) : base(stylesheet)
        {
        }

        public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
        {
        }

        protected override void OnCreate()
        {
            base.OnCreate();
            Initialize();
        }

        public void Initialize()
        {
            Window window = Window.Instance;
            window.BackgroundColor = Color.White;

            // Create a container to layout the rows of image and rating vertically
            FlexContainer container = new FlexContainer();

            container.ParentOrigin = ParentOrigin.TopLeft;
            container.PivotPoint = PivotPoint.TopLeft;
            container.FlexDirection = (int)FlexContainer.FlexDirectionType.Column;
            container.WidthResizePolicy = ResizePolicyType.FillToParent;
            container.HeightResizePolicy = ResizePolicyType.FillToParent;

            window.Add(container);

            Random random = new Random();

            for(int i = 0; i < 6; i++) // 6 rows in total
            {
                // Create a container to layout the image and rating (in each row) horizontally
                FlexContainer imageRow = new FlexContainer();
                imageRow.ParentOrigin = ParentOrigin.TopLeft;
                imageRow.PivotPoint = PivotPoint.TopLeft;
                imageRow.FlexDirection = FlexContainer.FlexDirectionType.Row;
                imageRow.Flex = 1.0f;
                container.Add(imageRow);

                // Add the image view to the row
                ImageView image = new ImageView(resources+"/images/gallery-" + i + ".jpg");
                image.Size2D = new Size2D(120, 120);
                image.WidthResizePolicy = ResizePolicyType.Fixed;
                image.HeightResizePolicy = ResizePolicyType.Fixed;
                image.AlignSelf = (int)FlexContainer.Alignment.AlignCenter;
                image.Flex = 0.3f;
                image.FlexMargin = new Vector4(10.0f, 0.0f, 0.0f, 0.0f);
                imageRow.Add(image);

                // Create a rating control
                StarRating view = new StarRating();

                // Add the rating control to the row
                view.ParentOrigin = ParentOrigin.Center;
                view.PivotPoint = PivotPoint.Center;
                view.Size2D = new Size2D(200, 40);
                view.Flex = 0.7f;
                view.AlignSelf = (int)FlexContainer.Alignment.AlignCenter;
                view.FlexMargin = new Vector4(30.0f, 0.0f, 0.0f, 0.0f);
                imageRow.Add(view);

                // Set the initial rating randomly between 1 and 5
                view.Rating = random.Next(1, 6);
            }
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void _Main(string[] args)
        {
            //System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (typeof(MyCSharpExample.StarRating).TypeHandle);

            Example example = new Example();
            example.Run(args);
        }
    }
}