Follow formatting NUI
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / GLWindowEvent.cs
1 /*
2  * Copyright(c) 2020 Samsung Electronics Co., Ltd.
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
18 using System;
19 using System.ComponentModel;
20 using System.Runtime.InteropServices;
21 using Tizen.NUI.BaseComponents;
22
23 namespace Tizen.NUI
24 {
25     public partial class GLWindow
26     {
27         private FocusChangedEventCallbackType _focusChangedEventCallback;
28         private GLWindowTouchDataCallbackType _windowTouchDataCallback;
29         private EventCallbackDelegateType1 _windowKeyCallbackDelegate;
30         private WindowResizedEventCallbackType _windowResizedEventCallback;
31
32         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
33         private delegate void FocusChangedEventCallbackType(IntPtr window, bool focusGained);
34         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
35         private delegate bool GLWindowTouchDataCallbackType(IntPtr touchData);
36         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
37         private delegate void WindowResizedEventCallbackType(IntPtr windowSize);
38
39         /// <summary>
40         /// FocusChanged event.
41         /// </summary>
42         [EditorBrowsable(EditorBrowsableState.Never)]
43         public event EventHandler<FocusChangedEventArgs> FocusChanged
44         {
45             add
46             {
47                 if (_focusChangedEventCallback == null)
48                 {
49                     _focusChangedEventCallback = OnWindowFocusedChanged;
50                     FocusChangedSignal().Connect(_focusChangedEventCallback);
51                 }
52
53                 _focusChangedEventHandler += value;
54             }
55             remove
56             {
57                 _focusChangedEventHandler -= value;
58
59                 if (_focusChangedEventHandler == null && FocusChangedSignal().Empty() == false && _focusChangedEventCallback != null)
60                 {
61                     FocusChangedSignal().Disconnect(_focusChangedEventCallback);
62                 }
63             }
64         }
65
66         /// <summary>
67         /// Emits the event when the screen is touched and when the touch ends.<br />
68         /// If there are multiple touch points then it is emitted when the first touch occurs and
69         /// when the last finger is lifted too.<br />
70         /// Even though incoming events are interrupted, the event occurs.<br />
71         /// </summary>
72         [EditorBrowsable(EditorBrowsableState.Never)]
73         public event EventHandler<TouchEventArgs> TouchEvent
74         {
75             add
76             {
77                 if (_windoTouchDataEventHandler == null)
78                 {
79                     _windowTouchDataCallback = OnWindowTouch;
80                     this.TouchSignal().Connect(_windowTouchDataCallback);
81                 }
82                 _windoTouchDataEventHandler += value;
83             }
84             remove
85             {
86                 _windoTouchDataEventHandler -= value;
87                 if (_windoTouchDataEventHandler == null && TouchSignal().Empty() == false && _windowTouchDataCallback != null)
88                 {
89                     this.TouchSignal().Disconnect(_windowTouchDataCallback);
90                 }
91             }
92         }
93
94         /// <summary>
95         /// Emits the event when the key event is received.
96         /// </summary>
97         [EditorBrowsable(EditorBrowsableState.Never)]
98         public event EventHandler<KeyEventArgs> KeyEvent
99         {
100             add
101             {
102                 if (_stageKeyHandler == null)
103                 {
104                     _windowKeyCallbackDelegate = OnStageKey;
105                     KeyEventSignal().Connect(_windowKeyCallbackDelegate);
106                 }
107                 _stageKeyHandler += value;
108             }
109             remove
110             {
111                 _stageKeyHandler -= value;
112                 if (_stageKeyHandler == null && KeyEventSignal().Empty() == false)
113                 {
114                     KeyEventSignal().Disconnect(_windowKeyCallbackDelegate);
115                 }
116             }
117         }
118
119         /// <summary>
120         /// Emits the event when the window resized.
121         /// </summary>
122         [EditorBrowsable(EditorBrowsableState.Never)]
123         public event EventHandler<ResizedEventArgs> Resized
124         {
125             add
126             {
127                 if (_windowResizedEventHandler == null)
128                 {
129                     _windowResizedEventCallback = OnResized;
130                     GLWindowResizedSignal().Connect(_windowResizedEventCallback);
131                 }
132
133                 _windowResizedEventHandler += value;
134             }
135             remove
136             {
137                 _windowResizedEventHandler -= value;
138
139                 if (_windowResizedEventHandler == null && GLWindowResizedSignal().Empty() == false && _windowResizedEventCallback != null)
140                 {
141                     GLWindowResizedSignal().Disconnect(_windowResizedEventCallback);
142                 }
143             }
144         }
145
146         private event EventHandler<FocusChangedEventArgs> _focusChangedEventHandler;
147         private event EventHandler<TouchEventArgs> _windoTouchDataEventHandler;
148         private event EventHandler<KeyEventArgs> _stageKeyHandler;
149         private event EventHandler<ResizedEventArgs> _windowResizedEventHandler;
150
151         internal WindowFocusSignalType FocusChangedSignal()
152         {
153             WindowFocusSignalType ret = new WindowFocusSignalType(Interop.GLWindow.GlWindow_FocusChangedSignal(swigCPtr), false);
154             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
155             return ret;
156         }
157
158         internal KeyEventSignal KeyEventSignal()
159         {
160             KeyEventSignal ret = new KeyEventSignal(Interop.GLWindow.GlWindow_KeyEventSignal(swigCPtr), false);
161             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
162             return ret;
163         }
164
165         internal TouchSignal TouchSignal()
166         {
167             TouchSignal ret = new TouchSignal(Interop.GLWindow.GlWindow_TouchSignal(swigCPtr), false);
168             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
169             return ret;
170         }
171
172         internal GLWindowResizedSignal GLWindowResizedSignal()
173         {
174             GLWindowResizedSignal ret = new GLWindowResizedSignal(Interop.GLWindow.GlWindow_ResizedSignal(swigCPtr), false);
175             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
176             return ret;
177         }
178
179         /// <summary>
180         /// Disconnect all native signals
181         /// </summary>
182         internal void DisconnectNativeSignals()
183         {
184             if (_focusChangedEventCallback != null)
185             {
186                 FocusChangedSignal().Disconnect(_focusChangedEventCallback);
187             }
188
189             if (_windowTouchDataCallback != null)
190             {
191                 TouchSignal().Disconnect(_windowTouchDataCallback);
192             }
193
194             if (_windowKeyCallbackDelegate != null)
195             {
196                 KeyEventSignal().Disconnect(_windowKeyCallbackDelegate);
197             }
198
199             if (_windowResizedEventCallback != null)
200             {
201                 GLWindowResizedSignal().Disconnect(_windowResizedEventCallback);
202             }
203         }
204
205         private void OnWindowFocusedChanged(IntPtr window, bool focusGained)
206         {
207             if (window == IntPtr.Zero)
208             {
209                 NUILog.Error("OnWindowFocusedChanged() Window is null! Do nothing!");
210                 return;
211             }
212
213             FocusChangedEventArgs e = new FocusChangedEventArgs();
214
215             e.FocusGained = focusGained;
216
217             if (_focusChangedEventHandler != null)
218             {
219                 _focusChangedEventHandler(this, e);
220             }
221         }
222
223         private bool OnWindowTouch(IntPtr touchData)
224         {
225             if (touchData == global::System.IntPtr.Zero)
226             {
227                 NUILog.Error("touchData should not be null!");
228                 return false;
229             }
230
231             TouchEventArgs e = new TouchEventArgs();
232
233             e.Touch = Tizen.NUI.Touch.GetTouchFromPtr(touchData);
234
235             if (_windoTouchDataEventHandler != null)
236             {
237                 _windoTouchDataEventHandler(this, e);
238             }
239             return false;
240         }
241
242         // Callback for Stage KeyEventsignal
243         private void OnStageKey(IntPtr data)
244         {
245             KeyEventArgs e = new KeyEventArgs();
246             e.Key = Tizen.NUI.Key.GetKeyFromPtr(data);
247
248
249             if (_stageKeyHandler != null)
250             {
251                 //here we send all data to user event handlers
252                 _stageKeyHandler(this, e);
253             }
254         }
255
256         private void OnResized(IntPtr windowSize)
257         {
258             ResizedEventArgs e = new ResizedEventArgs();
259             // var val = new Uint16Pair(windowSize, false);
260             // e.WindowSize = new Size2D(val.GetWidth(), val.GetHeight());
261             // val.Dispose();
262
263             // Workaround : windowSize should be valid pointer from dali, 
264             // but currenlty it is fixed and is not Uint16Pair class.
265             // will be fixed later.
266             e.WindowSize = this.WindowSize;
267
268             if (_windowResizedEventHandler != null)
269             {
270                 _windowResizedEventHandler(this, e);
271             }
272         }
273
274         /// <summary>
275         /// The focus changed event argument.
276         /// </summary>
277         [EditorBrowsable(EditorBrowsableState.Never)]
278         public class FocusChangedEventArgs : EventArgs
279         {
280             /// <summary>
281             /// FocusGained flag.
282             /// </summary>
283             [EditorBrowsable(EditorBrowsableState.Never)]
284             public bool FocusGained
285             {
286                 get;
287                 set;
288             }
289         }
290
291         /// <summary>
292         /// The touch event argument.
293         /// </summary>
294         [EditorBrowsable(EditorBrowsableState.Never)]
295         public class TouchEventArgs : EventArgs
296         {
297             private Touch _touch;
298
299             /// <summary>
300             /// Touch.
301             /// </summary>
302             [EditorBrowsable(EditorBrowsableState.Never)]
303             public Touch Touch
304             {
305                 get
306                 {
307                     return _touch;
308                 }
309                 set
310                 {
311                     _touch = value;
312                 }
313             }
314         }
315
316         /// <summary>
317         /// Key event arguments.
318         /// </summary>
319         [EditorBrowsable(EditorBrowsableState.Never)]
320         public class KeyEventArgs : EventArgs
321         {
322             private Key _key;
323
324             /// <summary>
325             /// Key.
326             /// </summary>
327             [EditorBrowsable(EditorBrowsableState.Never)]
328             public Key Key
329             {
330                 get
331                 {
332                     return _key;
333                 }
334                 set
335                 {
336                     _key = value;
337                 }
338             }
339         }
340
341         /// <summary>
342         /// Feeds a key event into the window.
343         /// This resized event arguments.
344         /// </summary>
345         [EditorBrowsable(EditorBrowsableState.Never)]
346         public class ResizedEventArgs : EventArgs
347         {
348             Size2D _windowSize;
349
350             /// <summary>
351             /// This window size.
352             /// </summary>
353             [EditorBrowsable(EditorBrowsableState.Never)]
354             public Size2D WindowSize
355             {
356                 get
357                 {
358                     return _windowSize;
359                 }
360                 set
361                 {
362                     _windowSize = value;
363                 }
364             }
365         }
366
367         /// <summary>
368         /// VisibilityChangedArgs
369         /// </summary>
370         [EditorBrowsable(EditorBrowsableState.Never)]
371         public class VisibilityChangedArgs : EventArgs
372         {
373             private bool _visibility;
374             /// <summary>
375             /// Visibility
376             /// </summary>
377             [EditorBrowsable(EditorBrowsableState.Never)]
378             public bool Visibility
379             {
380                 get => _visibility;
381                 set
382                 {
383                     _visibility = value;
384                 }
385             }
386         }
387
388         private void OnVisibilityChanged(IntPtr window, bool visibility)
389         {
390             if (window == global::System.IntPtr.Zero)
391             {
392                 NUILog.Error("[ERR] OnVisibilityChanged() window is null");
393                 return;
394             }
395
396             VisibilityChangedArgs e = new VisibilityChangedArgs();
397             e.Visibility = visibility;
398             if (VisibilityChangedEventHandler != null)
399             {
400                 VisibilityChangedEventHandler.Invoke(this, e);
401             }
402         }
403
404         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
405         private delegate void GLVisibilityChangedEventCallbackType(IntPtr window, bool visibility);
406         private GLVisibilityChangedEventCallbackType _GLVisibilityChangedEventCallback;
407         private event EventHandler<VisibilityChangedArgs> VisibilityChangedEventHandler;
408         private GLWindowVisibilityChangedEvent _GLVisibilityChangedEventSignal;
409
410         /// <summary>
411         /// EffectStart
412         /// </summary>
413         [EditorBrowsable(EditorBrowsableState.Never)]
414         public event EventHandler<VisibilityChangedArgs> VisibilityChanged
415         {
416             add
417             {
418                 if (VisibilityChangedEventHandler == null)
419                 {
420                     _GLVisibilityChangedEventCallback = OnVisibilityChanged;
421                     _GLVisibilityChangedEventSignal = new GLWindowVisibilityChangedEvent(this);
422                     _GLVisibilityChangedEventSignal.Connect(_GLVisibilityChangedEventCallback);
423                 }
424                 VisibilityChangedEventHandler += value;
425             }
426             remove
427             {
428                 VisibilityChangedEventHandler -= value;
429                 if (VisibilityChangedEventHandler == null)
430                 {
431                     if (_GLVisibilityChangedEventSignal != null)
432                     {
433                         if (_GLVisibilityChangedEventSignal.Empty() == false)
434                         {
435                             _GLVisibilityChangedEventSignal.Disconnect(_GLVisibilityChangedEventCallback);
436                         }
437                     }
438                 }
439             }
440         }
441
442         /// <summary>
443         /// VisibiltyChangedSignalEmit
444         /// </summary>
445         [EditorBrowsable(EditorBrowsableState.Never)]
446         public void VisibiltyChangedSignalEmit(bool visibility)
447         {
448             if (_GLVisibilityChangedEventSignal == null)
449             {
450                 _GLVisibilityChangedEventSignal = new GLWindowVisibilityChangedEvent(this);
451             }
452             _GLVisibilityChangedEventSignal.Emit(this, visibility);
453         }
454     }
455 }