DALi C# binding - Generic Delegates support for EventHandlers
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / SWIG / events / image-event.i
1 /*
2  * Copyright (c) 2016 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 %define IMAGE_EVENTHANDLER_TYPEMAP_EVENTARG(NameSpace, ClassName)
19 %typemap(csimports) NameSpace::ClassName %{
20 using System;
21 using System.Runtime.InteropServices;
22
23 %}
24 %enddef
25
26 %define IMAGE_EVENTHANDLER_TYPEMAP_HELPER(NameSpace, ClassName)
27 %typemap(cscode) NameSpace::ClassName %{
28
29
30 /**
31   * @brief Event arguments that passed via Uploaded signal
32   *
33   */
34 public class UploadedEventArgs : EventArgs
35 {
36    private Image _image;
37    /**
38      * @brief Image - is the image data that gets uploaded to GL.
39      *
40      */
41    public Image Image
42    {
43       get
44       {
45          return _image;
46       }
47       set
48       {
49          _image = value;
50       }
51    }
52 }
53
54   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
55   private delegate void UploadedEventCallbackDelegate(IntPtr image);
56   private DaliEventHandler<object,UploadedEventArgs> _imageUploadedEventHandler;
57   private UploadedEventCallbackDelegate _imageUploadedEventCallbackDelegate;
58
59   /**
60     * @brief Event for Uploaded signal which can be used to subscribe/unsubscribe the event handler
61     * (in the type of UploadedEventHandler-DaliEventHandler<object,UploadedEventArgs>) 
62     * provided by the user. Uploaded signal is emitted when the image data gets uploaded to GL.
63     */
64   public event DaliEventHandler<object,UploadedEventArgs> Uploaded
65   {
66      add
67      {
68         lock(this)
69         {
70            // Restricted to only one listener
71            if (_imageUploadedEventHandler == null)
72            {
73               _imageUploadedEventHandler += value;
74
75               _imageUploadedEventCallbackDelegate = new UploadedEventCallbackDelegate(OnUploaded);
76               this.UploadedSignal().Connect(_imageUploadedEventCallbackDelegate);
77            }
78         }
79      }
80
81      remove
82      {
83         lock(this)
84         {
85            if (_imageUploadedEventHandler != null)
86            {
87               this.UploadedSignal().Disconnect(_imageUploadedEventCallbackDelegate);
88            }
89
90            _imageUploadedEventHandler -= value;
91         }
92      }
93   }
94
95   // Callback for Image UploadedSignal
96   private void OnUploaded(IntPtr data)
97   {
98      UploadedEventArgs e = new UploadedEventArgs();
99
100      // Populate all members of "e" (UploadedEventArgs) with real data
101      e.Image = Image.GetImageFromPtr(data);
102
103      if (_imageUploadedEventHandler != null)
104      {
105         //here we send all data to user event handlers
106         _imageUploadedEventHandler(this, e);
107      }
108   }
109
110
111 public static ClassName Get ## ClassName ## FromPtr(global::System.IntPtr cPtr) {
112     ClassName ret = new ClassName(cPtr, false);
113    if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
114     return ret;
115   }
116
117 %}
118
119 %enddef
120
121
122 %define DALI_IMAGE_EVENTHANDLER_PARAM( NameSpace, ClassName)
123   IMAGE_EVENTHANDLER_TYPEMAP_EVENTARG( NameSpace, ClassName);
124   IMAGE_EVENTHANDLER_TYPEMAP_HELPER( NameSpace, ClassName);
125 %enddef
126
127 namespace Dali
128 {
129   DALI_IMAGE_EVENTHANDLER_PARAM( Dali, Image);
130 }