summaryrefslogtreecommitdiff
path: root/NUISamples/NUISamples/NUISamples.Tizen/examples/flex-container.cs
blob: f753255c1a5996ed9752ee87825dbf45411d3af5 (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

using System;
using Tizen.NUI;
using Tizen.NUI.UIComponents;
using Tizen.NUI.BaseComponents;

namespace FlexContainerTest
{
    public class SampleMain : NUIApplication
    {
        public readonly static string[] samples = new string[]
        {
            "AnimationSample",
            "BasicElement",
            "TextSample",
            "ImageViewSample",
            "EventSample",
            "FlexContainer",
            "FrameAnimationSample"
        };

        TextLabel[] label;
        FlexContainer container;
        View focusIndicator;
        int numOfSamples;
        PushButton pushButton1, pushButton2;
        private int _cnt;
        private Animation _ani;

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

            Window.Instance.BackgroundColor = new Color(0.1f, 0.8f, 0.1f, 1.0f);

            container = new FlexContainer();
            container.Size2D = new Size2D(Window.Instance.Size.Width, Window.Instance.Size.Height);
            container.PivotPoint = PivotPoint.TopLeft;
            container.Padding = new Vector4(100, 100, 100, 100);

            container.FlexWrap = FlexContainer.WrapType.Wrap;
            container.FlexDirection = FlexContainer.FlexDirectionType.Column;

            Window.Instance.Add(container);

            numOfSamples = samples.GetLength(0);
            Tizen.Log.Debug("NUI", "NUM = " + numOfSamples);
            label = new TextLabel[numOfSamples];
            for (int i = 0; i < numOfSamples; i++)
            {
                label[i] = new TextLabel();
                label[i].Focusable = true;
                label[i].BackgroundColor = Color.Red;
                //label[i].Size = new Size(100, 50, 0);
                label[i].Text = samples[i];
                label[i].FlexMargin = new Vector4(20, 20, 20, 20);
                label[i].PointSize = 10;
                label[i].Name = "label" + i.ToString();
                container.Add(label[i]);
            }

            //label[3].SetKeyInputFocus();  //removed
            FocusManager.Instance.SetCurrentFocusView(label[3]);

            FocusManager.Instance.PreFocusChange += Instance_PreFocusChange;
            //added
            FocusManager.Instance.FocusChanged += (sender, e) =>
            {
                if(e.CurrentView) Tizen.Log.Debug("NUI", "FocusManager FocusChanged signal callback! e.CurrentView.Name=" + e.CurrentView.Name);
                else Tizen.Log.Debug("NUI", "FocusManager FocusChanged signal callback! e.CurrentView is null!");
                if (e.NextView) Tizen.Log.Debug("NUI", "FocusManager FocusChanged signal callback! e.NextView.Name=" + e.NextView.Name);
                else Tizen.Log.Debug("NUI", "FocusManager FocusChanged signal callback! e.NextView is null!");
            };
            //added
            FocusManager.Instance.FocusedViewActivated += (sender, e) =>
            {
                if (e.View) Tizen.Log.Debug("NUI", "FocusManager FocusedViewEnterKeyPressed signal callback! e.View.Name=" + e.View.Name);
                else Tizen.Log.Debug("NUI", "FocusManager FocusChanged signal callback! e.View is null!");
            };

            //added
            Window.Instance.TouchEvent += (sender, e) =>
            {
                Tizen.Log.Debug("NUI", "Window Touch signal callback! To avoid crash, when losing key focus, set here again unless the NextView is null");
                FocusManager.Instance.SetCurrentFocusView(label[3]);
            };

            //added
            pushButton1 = new PushButton();
            pushButton1.MinimumSize = new Size2D(400, 200);
            pushButton1.LabelText = "+PreFocusChange";
            pushButton1.ParentOrigin = ParentOrigin.TopLeft;
            pushButton1.PivotPoint = PivotPoint.TopLeft;
            pushButton1.Position2D = new Position2D(200, 800);
            pushButton1.Clicked += (sender, e) =>
            {
                Tizen.Log.Debug("NUI", "pushbutton1 clicked! add handler!");
                _cnt++;
                FocusManager.Instance.PreFocusChange += Instance_PreFocusChange;
                _ani.Finished += _ani_Finished;
                pushButton1.LabelText = "Add Handler" + _cnt;
                pushButton2.LabelText = "Remove Handler" + _cnt;
                return true;
            };
            Window.Instance.Add(pushButton1);

            pushButton2 = new PushButton();
            pushButton2.MinimumSize = new Size2D(400, 200);
            pushButton2.LabelText = "-PreFocusChange";
            pushButton2.ParentOrigin = ParentOrigin.TopLeft;
            pushButton2.PivotPoint = PivotPoint.TopLeft;
            pushButton2.Position2D = new Position2D(800, 800);
            pushButton2.Clicked += (sender, e) =>
            {
                Tizen.Log.Debug("NUI", "pushbutton2 clicked! remove handler!");
                _cnt--;
                FocusManager.Instance.PreFocusChange -= Instance_PreFocusChange;
                _ani.Finished -= _ani_Finished;
                pushButton1.LabelText = "Add Handler" + _cnt;
                pushButton2.LabelText = "Remove Handler" + _cnt;
                return true;
            };
            Window.Instance.Add(pushButton2);

            //added
            _ani = new Animation(2000);
            _ani.AnimateTo(pushButton1, "Opacity", 0.0f);
            _ani.AnimateTo(pushButton2, "Opacity", 0.0f);
            _ani.EndAction = Animation.EndActions.Discard;
            _ani.Finished += _ani_Finished;

        }

        private void _ani_Finished(object sender, EventArgs e)
        {
            Tizen.Log.Debug("NUI", "_ani finished! _cnt=" + _cnt);
        }

        private View Instance_PreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
        {
            View nextView;
            Tizen.Log.Debug("NUI", "Instance_PreFocusChange = " + e.Direction.ToString());

            //added
            if (e.CurrentView == null) e.CurrentView = label[0];
            if (e.ProposedView == null) e.ProposedView = label[0];

            int index = Array.FindIndex(label, x => x == e.CurrentView);

            Tizen.Log.Debug("NUI", "index = " + index);

            switch (e.Direction)
            {
                case View.FocusDirection.Up:
                    index = (index + numOfSamples - 2) % numOfSamples;  //changed
                    _ani.Play();
                    break;
                case View.FocusDirection.Down:
                    index = (index + 2) % numOfSamples; //changed
                    Tizen.Log.Debug("NUI", "pushbutton1 Visible=" + pushButton1.Visible + "  pushbutton2 Visible=" + pushButton2.Visible); //added
                    break;
                case View.FocusDirection.Left:
                    //added
                    pushButton1.Show();
                    pushButton2.Show();
                    break;
                case View.FocusDirection.Right:
                    //added
                    pushButton1.Hide();
                    pushButton2.Hide();
                    break;
                default:
                    break;
            }

            Tizen.Log.Debug("NUI", "next index = " + index);
            nextView = label[index];

            if (e.CurrentView.HasFocus())
            {
                //currentView?.ClearKeyInputFocus();  //removed
            }
            //nextView?.SetKeyInputFocus();  //removed

            return nextView;
        }

        static void _Main(string[] args)
        {

            SampleMain sample = new SampleMain();
            sample.Run(args);
        }
    }
}