Merge "[Multimedia] Added missing comments in AudioIO"
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / EvasCanvas.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18 using System.Collections.Generic;
19
20 namespace ElmSharp
21 {
22     /// <summary>
23     /// Low level Evas canvas functions. Sub groups will present more high level ones, though.
24     /// Most of these functions deal with low level Evas actions, like:
25     /// create/destroy raw canvases, not bound to any displaying engine
26     /// tell a canvas i got focused(in a windowing context, for example)
27     /// tell a canvas a region should not be calculated anymore in rendering
28     /// tell a canvas to render its contents, immediately
29     /// Most users will be using Evas by means of the Ecore_Evas wrapper, which deals with all the above mentioned issues automatically for them.Thus, you'll be looking at this section only if you're building low level stuff.
30     /// The groups within present you functions that deal with the canvas directly, too, and not yet with its objects.They are the functions you need to use at a minimum to get a working canvas.
31     /// </summary>
32     public class EvasCanvas
33     {
34         IntPtr _handle = IntPtr.Zero;
35         Dictionary<EventData, Interop.Evas.EvasCallback> _eventDatas = new Dictionary<EventData, Interop.Evas.EvasCallback>();
36
37         internal EvasCanvas(IntPtr evasObject)
38         {
39             _handle = CreateHandle(evasObject);
40         }
41
42         /// <summary>
43         /// Gets the current known default pointer coordinates.
44         /// This function returns the current known canvas unit coordinates of the mouse pointer.
45         /// </summary>
46         public Point Pointer
47         {
48             get
49             {
50                 int mx, my;
51                 Interop.Evas.evas_pointer_canvas_xy_get(_handle, out mx, out my);
52                 return new Point { X = mx, Y = my };
53             }
54         }
55
56         /// <summary>
57         /// Gets or sets the image cache.
58         /// This function returns the image cache size of canvas in bytes.
59         /// </summary>
60         public int ImageCacheSize
61         {
62             get
63             {
64                 return Interop.Evas.evas_image_cache_get(_handle);
65             }
66             set
67             {
68                 Interop.Evas.evas_image_cache_set(_handle, value);
69             }
70         }
71
72         /// <summary>
73         /// Flush the image cache of the canvas.
74         /// </summary>
75         public void FlushImageCache()
76         {
77             Interop.Evas.evas_image_cache_flush(_handle);
78         }
79
80         /// <summary>
81         /// Add a damage rectangle.
82         /// </summary>
83         /// <param name="x">The rectangle's top left corner's horizontal coordinate.</param>
84         /// <param name="y">The rectangle's top left corner's vertical coordinate.</param>
85         /// <param name="width">The rectangle's width.</param>
86         /// <param name="height">The rectangle's height.</param>
87         public void AddDamageRectangle(int x, int y, int width, int height)
88         {
89             Interop.Evas.evas_damage_rectangle_add(_handle, x, y, width, height);
90         }
91
92         /// <summary>
93         /// Add an "obscured region" to an Evas canvas.
94         /// </summary>
95         /// <param name="x">The rectangle's top left corner's horizontal coordinate.</param>
96         /// <param name="y">The rectangle's top left corner's vertical coordinate.</param>
97         /// <param name="width">The rectangle's width.</param>
98         /// <param name="height">The rectangle's height.</param>
99         public void AddObscuredRectangle(int x, int y, int width, int height)
100         {
101             Interop.Evas.evas_obscured_rectangle_add(_handle, x, y, width, height);
102         }
103
104         /// <summary>
105         /// Remove all "obscured regions" from an Evas canvas.
106         /// </summary>
107         public void ClearObscuredRectangle()
108         {
109             Interop.Evas.evas_obscured_clear(_handle);
110         }
111
112         /// <summary>
113         /// Adds or registers a event to a given canvas event.
114         /// </summary>
115         /// <param name="type">The type of event that triggers</param>
116         /// <param name="action">The action to be called when the event is triggered</param>
117         public void AddEventAction(EvasObjectCallbackType type, Action action)
118         {
119             if (action != null)
120             {
121                 var eventData = new EventData(type, action);
122
123                 if (!_eventDatas.ContainsKey(eventData))
124                 {
125                     var evasCallback = new Interop.Evas.EvasCallback((d, o, t) => action());
126                     Interop.Evas.evas_event_callback_add(_handle, (Interop.Evas.ObjectCallbackType)type, evasCallback, IntPtr.Zero);
127                     _eventDatas.Add(eventData, evasCallback);
128                 }
129             }
130         }
131
132         /// <summary>
133         /// Deletes a event to a given canvas event.
134         /// </summary>
135         /// <param name="type">The type of event that triggers</param>
136         /// <param name="action">The action to be called when the event is triggered</param>
137         public void DeleteEventAction(EvasObjectCallbackType type, Action action)
138         {
139             if (action != null)
140             {
141                 var eventData = new EventData(type, action);
142                 Interop.Evas.EvasCallback evasCallback = null;
143                 _eventDatas.TryGetValue(eventData, out evasCallback);
144
145                 if (evasCallback != null)
146                 {
147                     Interop.Evas.evas_event_callback_del(_handle, (Interop.Evas.ObjectCallbackType)type, evasCallback);
148                     _eventDatas.Remove(eventData);
149                 }
150             }
151         }
152
153         /// <summary>
154         /// Creates an Evas canvas handle.
155         /// </summary>
156         /// <param name="evasObject">EvasObject</param>
157         /// <returns>Handle IntPtr</returns>
158         IntPtr CreateHandle(IntPtr evasObject)
159         {
160             return Interop.Evas.evas_object_evas_get(evasObject);
161         }
162
163         class EventData
164         {
165             public EvasObjectCallbackType Type { get; set; }
166             public Action Action { get; set; }
167
168             public EventData(EvasObjectCallbackType type, Action action)
169             {
170                 Type = type;
171                 Action = action;
172             }
173
174             public override bool Equals(object obj)
175             {
176                 EventData e = obj as EventData;
177                 if (e == null)
178                 {
179                     return false;
180                 }
181                 return (Type == e.Type) && (Action == e.Action);
182             }
183
184             public override int GetHashCode()
185             {
186                 int hashCode = Type.GetHashCode();
187                 hashCode ^= Action.GetHashCode();
188                 return hashCode;
189             }
190         }
191     }
192 }