[NUI] Check spelling
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Window / GLWindowEvent.cs
1 /*
2  * Copyright(c) 2021 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.GlWindowFocusChangedSignal(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.GlWindowKeyEventSignal(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.GlWindowTouchSignal(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.GlWindowResizedSignal(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             if (windoTouchDataEventHandler != null)
232             {
233                 TouchEventArgs e = new TouchEventArgs();
234                 e.Touch = Tizen.NUI.Touch.GetTouchFromPtr(touchData);
235                 windoTouchDataEventHandler(this, e);
236             }
237             return false;
238         }
239
240         // Callback for Stage KeyEventsignal
241         private void OnStageKey(IntPtr data)
242         {
243             if (stageKeyHandler != null)
244             {
245                 KeyEventArgs e = new KeyEventArgs();
246                 e.Key = Tizen.NUI.Key.GetKeyFromPtr(data);
247                 //here we send all data to user event handlers
248                 stageKeyHandler(this, e);
249             }
250         }
251
252         private void OnResized(IntPtr windowSize)
253         {
254             if (windowResizedEventHandler != null)
255             {
256                 ResizedEventArgs e = new ResizedEventArgs();
257                 // var val = new Uint16Pair(windowSize, false);
258                 // e.WindowSize = new Size2D(val.GetWidth(), val.GetHeight());
259                 // val.Dispose();
260
261                 // Workaround : windowSize should be valid pointer from dali, 
262                 // but currently it is fixed and is not Uint16Pair class.
263                 // will be fixed later.
264                 e.WindowSize = this.WindowSize;
265                 windowResizedEventHandler(this, e);
266             }
267         }
268
269         /// <summary>
270         /// The focus changed event argument.
271         /// </summary>
272         [EditorBrowsable(EditorBrowsableState.Never)]
273         public class FocusChangedEventArgs : EventArgs
274         {
275             /// <summary>
276             /// FocusGained flag.
277             /// </summary>
278             [EditorBrowsable(EditorBrowsableState.Never)]
279             public bool FocusGained
280             {
281                 get;
282                 set;
283             }
284         }
285
286         /// <summary>
287         /// The touch event argument.
288         /// </summary>
289         [EditorBrowsable(EditorBrowsableState.Never)]
290         public class TouchEventArgs : EventArgs
291         {
292             private Touch touch;
293
294             /// <summary>
295             /// Touch.
296             /// </summary>
297             [EditorBrowsable(EditorBrowsableState.Never)]
298             public Touch Touch
299             {
300                 get
301                 {
302                     return touch;
303                 }
304                 set
305                 {
306                     touch = value;
307                 }
308             }
309         }
310
311         /// <summary>
312         /// Key event arguments.
313         /// </summary>
314         [EditorBrowsable(EditorBrowsableState.Never)]
315         public class KeyEventArgs : EventArgs
316         {
317             private Key key;
318
319             /// <summary>
320             /// Key.
321             /// </summary>
322             [EditorBrowsable(EditorBrowsableState.Never)]
323             public Key Key
324             {
325                 get
326                 {
327                     return key;
328                 }
329                 set
330                 {
331                     key = value;
332                 }
333             }
334         }
335
336         /// <summary>
337         /// Feeds a key event into the window.
338         /// This resized event arguments.
339         /// </summary>
340         [EditorBrowsable(EditorBrowsableState.Never)]
341         public class ResizedEventArgs : EventArgs
342         {
343             Size2D windowSize;
344
345             /// <summary>
346             /// This window size.
347             /// </summary>
348             [EditorBrowsable(EditorBrowsableState.Never)]
349             public Size2D WindowSize
350             {
351                 get
352                 {
353                     return windowSize;
354                 }
355                 set
356                 {
357                     windowSize = value;
358                 }
359             }
360         }
361
362         /// <summary>
363         /// VisibilityChangedArgs
364         /// </summary>
365         [EditorBrowsable(EditorBrowsableState.Never)]
366         public class VisibilityChangedEventArgs : EventArgs
367         {
368             private bool visibility;
369             /// <summary>
370             /// Visibility
371             /// </summary>
372             [EditorBrowsable(EditorBrowsableState.Never)]
373             public bool Visibility
374             {
375                 get => visibility;
376                 set
377                 {
378                     visibility = value;
379                 }
380             }
381         }
382
383         private void OnVisibilityChanged(IntPtr window, bool visibility)
384         {
385             if (window == global::System.IntPtr.Zero)
386             {
387                 NUILog.Error("[ERR] OnVisibilityChanged() window is null");
388                 return;
389             }
390
391             VisibilityChangedEventArgs e = new VisibilityChangedEventArgs();
392             e.Visibility = visibility;
393             if (visibilityChangedEventHandler != null)
394             {
395                 visibilityChangedEventHandler.Invoke(this, e);
396             }
397         }
398
399         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
400         private delegate void GLVisibilityChangedEventCallbackType(IntPtr window, bool visibility);
401         private GLVisibilityChangedEventCallbackType glVisibilityChangedEventCallback;
402         private event EventHandler<VisibilityChangedEventArgs> visibilityChangedEventHandler;
403         private GLWindowVisibilityChangedEvent glVisibilityChangedEventSignal;
404
405         /// <summary>
406         /// EffectStart
407         /// </summary>
408         [EditorBrowsable(EditorBrowsableState.Never)]
409         public event EventHandler<VisibilityChangedEventArgs> VisibilityChanged
410         {
411             add
412             {
413                 if (visibilityChangedEventHandler == null)
414                 {
415                     glVisibilityChangedEventCallback = OnVisibilityChanged;
416                     glVisibilityChangedEventSignal = new GLWindowVisibilityChangedEvent(this);
417                     glVisibilityChangedEventSignal.Connect(glVisibilityChangedEventCallback);
418                 }
419                 visibilityChangedEventHandler += value;
420             }
421             remove
422             {
423                 visibilityChangedEventHandler -= value;
424                 if (visibilityChangedEventHandler == null)
425                 {
426                     if (glVisibilityChangedEventSignal != null)
427                     {
428                         if (glVisibilityChangedEventSignal.Empty() == false)
429                         {
430                             glVisibilityChangedEventSignal.Disconnect(glVisibilityChangedEventCallback);
431                         }
432                     }
433                 }
434             }
435         }
436
437         /// <summary>
438         /// VisibiltyChangedSignalEmit
439         /// </summary>
440         [EditorBrowsable(EditorBrowsableState.Never)]
441         public void VisibiltyChangedSignalEmit(bool visibility)
442         {
443             if (glVisibilityChangedEventSignal == null)
444             {
445                 glVisibilityChangedEventSignal = new GLWindowVisibilityChangedEvent(this);
446             }
447             glVisibilityChangedEventSignal.Emit(this, visibility);
448         }
449     }
450 }