[NUI] Change all CallingConvention to `Cdecl`
[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.Cdecl)]
33         private delegate void FocusChangedEventCallbackType(IntPtr window, bool focusGained);
34         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
35         private delegate bool GLWindowTouchDataCallbackType(IntPtr touchData);
36         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
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                     Interop.GLWindow.GlWindowTouchSignalConnect(SwigCPtr, windowTouchDataCallback.ToHandleRef(this));
81                     NDalicPINVOKE.ThrowExceptionIfExists();
82                 }
83                 windoTouchDataEventHandler += value;
84             }
85             remove
86             {
87                 windoTouchDataEventHandler -= value;
88                 if (windoTouchDataEventHandler == null && windowTouchDataCallback != null)
89                 {
90                     Interop.GLWindow.GlWindowTouchSignalDisconnect(SwigCPtr, windowTouchDataCallback.ToHandleRef(this));
91                     NDalicPINVOKE.ThrowExceptionIfExists();
92                     windowTouchDataCallback = null;
93                 }
94             }
95         }
96
97         /// <summary>
98         /// Emits the event when the key event is received.
99         /// </summary>
100         [EditorBrowsable(EditorBrowsableState.Never)]
101         public event EventHandler<KeyEventArgs> KeyEvent
102         {
103             add
104             {
105                 if (stageKeyHandler == null)
106                 {
107                     windowKeyCallbackDelegate = OnStageKey;
108                     KeyEventSignal().Connect(windowKeyCallbackDelegate);
109                 }
110                 stageKeyHandler += value;
111             }
112             remove
113             {
114                 stageKeyHandler -= value;
115                 if (stageKeyHandler == null && KeyEventSignal().Empty() == false)
116                 {
117                     KeyEventSignal().Disconnect(windowKeyCallbackDelegate);
118                 }
119             }
120         }
121
122         /// <summary>
123         /// Emits the event when the window resized.
124         /// </summary>
125         [EditorBrowsable(EditorBrowsableState.Never)]
126         public event EventHandler<ResizedEventArgs> Resized
127         {
128             add
129             {
130                 if (windowResizedEventHandler == null)
131                 {
132                     windowResizedEventCallback = OnResized;
133                     GLWindowResizedSignal().Connect(windowResizedEventCallback);
134                 }
135
136                 windowResizedEventHandler += value;
137             }
138             remove
139             {
140                 windowResizedEventHandler -= value;
141
142                 if (windowResizedEventHandler == null && GLWindowResizedSignal().Empty() == false && windowResizedEventCallback != null)
143                 {
144                     GLWindowResizedSignal().Disconnect(windowResizedEventCallback);
145                 }
146             }
147         }
148
149         private event EventHandler<FocusChangedEventArgs> focusChangedEventHandler;
150         private event EventHandler<TouchEventArgs> windoTouchDataEventHandler;
151         private event EventHandler<KeyEventArgs> stageKeyHandler;
152         private event EventHandler<ResizedEventArgs> windowResizedEventHandler;
153
154         internal WindowFocusSignalType FocusChangedSignal()
155         {
156             WindowFocusSignalType ret = new WindowFocusSignalType(Interop.GLWindow.GlWindowFocusChangedSignal(SwigCPtr), false);
157             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
158             return ret;
159         }
160
161         internal KeyEventSignal KeyEventSignal()
162         {
163             KeyEventSignal ret = new KeyEventSignal(Interop.GLWindow.GlWindowKeyEventSignal(SwigCPtr), false);
164             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
165             return ret;
166         }
167
168         internal GLWindowResizedSignal GLWindowResizedSignal()
169         {
170             GLWindowResizedSignal ret = new GLWindowResizedSignal(Interop.GLWindow.GlWindowResizedSignal(SwigCPtr), false);
171             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
172             return ret;
173         }
174
175         /// <summary>
176         /// Disconnect all native signals
177         /// </summary>
178         internal void DisconnectNativeSignals()
179         {
180             if (focusChangedEventCallback != null)
181             {
182                 FocusChangedSignal().Disconnect(focusChangedEventCallback);
183             }
184
185             if (windowTouchDataCallback != null)
186             {
187                 Interop.GLWindow.GlWindowTouchSignalDisconnect(SwigCPtr, windowTouchDataCallback.ToHandleRef(this));
188                 NDalicPINVOKE.ThrowExceptionIfExists();
189             }
190
191             if (windowKeyCallbackDelegate != null)
192             {
193                 KeyEventSignal().Disconnect(windowKeyCallbackDelegate);
194             }
195
196             if (windowResizedEventCallback != null)
197             {
198                 GLWindowResizedSignal().Disconnect(windowResizedEventCallback);
199             }
200         }
201
202         private void OnWindowFocusedChanged(IntPtr window, bool focusGained)
203         {
204             if (window == IntPtr.Zero)
205             {
206                 NUILog.Error("OnWindowFocusedChanged() Window is null! Do nothing!");
207                 return;
208             }
209
210             FocusChangedEventArgs e = new FocusChangedEventArgs();
211
212             e.FocusGained = focusGained;
213
214             if (focusChangedEventHandler != null)
215             {
216                 focusChangedEventHandler(this, e);
217             }
218         }
219
220         private bool OnWindowTouch(IntPtr touchData)
221         {
222             if (touchData == global::System.IntPtr.Zero)
223             {
224                 NUILog.Error("touchData should not be null!");
225                 return false;
226             }
227
228             if (windoTouchDataEventHandler != null)
229             {
230                 TouchEventArgs e = new TouchEventArgs();
231                 e.Touch = Tizen.NUI.Touch.GetTouchFromPtr(touchData);
232                 windoTouchDataEventHandler(this, e);
233             }
234             return false;
235         }
236
237         // Callback for Stage KeyEventsignal
238         private void OnStageKey(IntPtr data)
239         {
240             if (stageKeyHandler != null)
241             {
242                 KeyEventArgs e = new KeyEventArgs();
243                 e.Key = Tizen.NUI.Key.GetKeyFromPtr(data);
244                 //here we send all data to user event handlers
245                 stageKeyHandler(this, e);
246             }
247         }
248
249         private void OnResized(IntPtr windowSize)
250         {
251             if (windowResizedEventHandler != null)
252             {
253                 ResizedEventArgs e = new ResizedEventArgs();
254                 // var val = new Uint16Pair(windowSize, false);
255                 // e.WindowSize = new Size2D(val.GetWidth(), val.GetHeight());
256                 // val.Dispose();
257
258                 // Workaround : windowSize should be valid pointer from dali, 
259                 // but currently it is fixed and is not Uint16Pair class.
260                 // will be fixed later.
261                 e.WindowSize = this.WindowSize;
262                 windowResizedEventHandler(this, e);
263             }
264         }
265
266         /// <summary>
267         /// The focus changed event argument.
268         /// </summary>
269         [EditorBrowsable(EditorBrowsableState.Never)]
270         public class FocusChangedEventArgs : EventArgs
271         {
272             /// <summary>
273             /// FocusGained flag.
274             /// </summary>
275             [EditorBrowsable(EditorBrowsableState.Never)]
276             public bool FocusGained
277             {
278                 get;
279                 set;
280             }
281         }
282
283         /// <summary>
284         /// The touch event argument.
285         /// </summary>
286         [EditorBrowsable(EditorBrowsableState.Never)]
287         public class TouchEventArgs : EventArgs
288         {
289             private Touch touch;
290
291             /// <summary>
292             /// Touch.
293             /// </summary>
294             [EditorBrowsable(EditorBrowsableState.Never)]
295             public Touch Touch
296             {
297                 get
298                 {
299                     return touch;
300                 }
301                 set
302                 {
303                     touch = value;
304                 }
305             }
306         }
307
308         /// <summary>
309         /// Key event arguments.
310         /// </summary>
311         [EditorBrowsable(EditorBrowsableState.Never)]
312         public class KeyEventArgs : EventArgs
313         {
314             private Key key;
315
316             /// <summary>
317             /// Key.
318             /// </summary>
319             [EditorBrowsable(EditorBrowsableState.Never)]
320             public Key Key
321             {
322                 get
323                 {
324                     return key;
325                 }
326                 set
327                 {
328                     key = value;
329                 }
330             }
331         }
332
333         /// <summary>
334         /// Feeds a key event into the window.
335         /// This resized event arguments.
336         /// </summary>
337         [EditorBrowsable(EditorBrowsableState.Never)]
338         public class ResizedEventArgs : EventArgs
339         {
340             Size2D windowSize;
341
342             /// <summary>
343             /// This window size.
344             /// </summary>
345             [EditorBrowsable(EditorBrowsableState.Never)]
346             public Size2D WindowSize
347             {
348                 get
349                 {
350                     return windowSize;
351                 }
352                 set
353                 {
354                     windowSize = value;
355                 }
356             }
357         }
358
359         /// <summary>
360         /// VisibilityChangedArgs
361         /// </summary>
362         [EditorBrowsable(EditorBrowsableState.Never)]
363         public class VisibilityChangedEventArgs : EventArgs
364         {
365             private bool visibility;
366             /// <summary>
367             /// Visibility
368             /// </summary>
369             [EditorBrowsable(EditorBrowsableState.Never)]
370             public bool Visibility
371             {
372                 get => visibility;
373                 set
374                 {
375                     visibility = value;
376                 }
377             }
378         }
379
380         private void OnVisibilityChanged(IntPtr window, bool visibility)
381         {
382             if (window == global::System.IntPtr.Zero)
383             {
384                 NUILog.Error("[ERR] OnVisibilityChanged() window is null");
385                 return;
386             }
387
388             VisibilityChangedEventArgs e = new VisibilityChangedEventArgs();
389             e.Visibility = visibility;
390             if (visibilityChangedEventHandler != null)
391             {
392                 visibilityChangedEventHandler.Invoke(this, e);
393             }
394         }
395
396         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
397         private delegate void GLVisibilityChangedEventCallbackType(IntPtr window, bool visibility);
398         private GLVisibilityChangedEventCallbackType glVisibilityChangedEventCallback;
399         private event EventHandler<VisibilityChangedEventArgs> visibilityChangedEventHandler;
400         private GLWindowVisibilityChangedEvent glVisibilityChangedEventSignal;
401
402         /// <summary>
403         /// EffectStart
404         /// </summary>
405         [EditorBrowsable(EditorBrowsableState.Never)]
406         public event EventHandler<VisibilityChangedEventArgs> VisibilityChanged
407         {
408             add
409             {
410                 if (visibilityChangedEventHandler == null)
411                 {
412                     glVisibilityChangedEventCallback = OnVisibilityChanged;
413                     glVisibilityChangedEventSignal = new GLWindowVisibilityChangedEvent(this);
414                     glVisibilityChangedEventSignal.Connect(glVisibilityChangedEventCallback);
415                 }
416                 visibilityChangedEventHandler += value;
417             }
418             remove
419             {
420                 visibilityChangedEventHandler -= value;
421                 if (visibilityChangedEventHandler == null)
422                 {
423                     if (glVisibilityChangedEventSignal != null)
424                     {
425                         if (glVisibilityChangedEventSignal.Empty() == false)
426                         {
427                             glVisibilityChangedEventSignal.Disconnect(glVisibilityChangedEventCallback);
428                         }
429                     }
430                 }
431             }
432         }
433
434         /// <summary>
435         /// VisibiltyChangedSignalEmit
436         /// </summary>
437         [EditorBrowsable(EditorBrowsableState.Never)]
438         public void VisibiltyChangedSignalEmit(bool visibility)
439         {
440             if (glVisibilityChangedEventSignal == null)
441             {
442                 glVisibilityChangedEventSignal = new GLWindowVisibilityChangedEvent(this);
443             }
444             glVisibilityChangedEventSignal.Emit(this, visibility);
445         }
446     }
447 }