Enhance EvasCanvas
authorSeunghyun Choi <sh4682.choi@samsung.com>
Thu, 15 Jun 2017 08:32:14 +0000 (17:32 +0900)
committerSeunghyun Choi <sh4682.choi@samsung.com>
Fri, 23 Jun 2017 01:43:55 +0000 (10:43 +0900)
Change-Id: Id169ecc5d29ac237534b4062e8a9b6bb56c94623

src/ElmSharp/ElmSharp/EvasCanvas.cs [new file with mode: 0644]
src/ElmSharp/ElmSharp/EvasObject.cs

diff --git a/src/ElmSharp/ElmSharp/EvasCanvas.cs b/src/ElmSharp/ElmSharp/EvasCanvas.cs
new file mode 100644 (file)
index 0000000..6f778c6
--- /dev/null
@@ -0,0 +1,173 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+
+namespace ElmSharp
+{
+    /// <summary>
+    /// Low level Evas canvas functions. Sub groups will present more high level ones, though.
+    /// Most of these functions deal with low level Evas actions, like:
+    /// create/destroy raw canvases, not bound to any displaying engine
+    /// tell a canvas i got focused(in a windowing context, for example)
+    /// tell a canvas a region should not be calculated anymore in rendering
+    /// tell a canvas to render its contents, immediately
+    /// 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.
+    /// 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.
+    /// </summary>
+    public class EvasCanvas
+    {
+        IntPtr _handle = IntPtr.Zero;
+        Dictionary<EventData, Interop.Evas.EvasCallback> _eventDatas = new Dictionary<EventData, Interop.Evas.EvasCallback>();
+
+        internal EvasCanvas(IntPtr evasObject)
+        {
+            _handle = CreateHandle(evasObject);
+        }
+
+        /// <summary>
+        /// Gets or sets the image cache.
+        /// This function returns the image cache size of canvas in bytes.
+        /// </summary>
+        public int ImageCacheSize
+        {
+            get
+            {
+                return Interop.Evas.evas_image_cache_get(_handle);
+            }
+            set
+            {
+                Interop.Evas.evas_image_cache_set(_handle, value);
+            }
+        }
+
+        /// <summary>
+        /// Flush the image cache of the canvas.
+        /// </summary>
+        public void FlushImageCache()
+        {
+            Interop.Evas.evas_image_cache_flush(_handle);
+        }
+
+        /// <summary>
+        /// Add a damage rectangle.
+        /// </summary>
+        /// <param name="x">The rectangle's top left corner's horizontal coordinate.</param>
+        /// <param name="y">The rectangle's top left corner's vertical coordinate.</param>
+        /// <param name="width">The rectangle's width.</param>
+        /// <param name="height">The rectangle's height.</param>
+        public void AddDamageRectangle(int x, int y, int width, int height)
+        {
+            Interop.Evas.evas_damage_rectangle_add(_handle, x, y, width, height);
+        }
+
+        /// <summary>
+        /// Add an "obscured region" to an Evas canvas.
+        /// </summary>
+        /// <param name="x">The rectangle's top left corner's horizontal coordinate.</param>
+        /// <param name="y">The rectangle's top left corner's vertical coordinate.</param>
+        /// <param name="width">The rectangle's width.</param>
+        /// <param name="height">The rectangle's height.</param>
+        public void AddObscuredRectangle(int x, int y, int width, int height)
+        {
+            Interop.Evas.evas_obscured_rectangle_add(_handle, x, y, width, height);
+        }
+
+        /// <summary>
+        /// Remove all "obscured regions" from an Evas canvas.
+        /// </summary>
+        public void ClearObscuredRectangle()
+        {
+            Interop.Evas.evas_obscured_clear(_handle);
+        }
+
+        /// <summary>
+        /// Adds or registers a event to a given canvas event.
+        /// </summary>
+        /// <param name="type">The type of event that triggers</param>
+        /// <param name="action">The action to be called when the event is triggered</param>
+        public void AddEventAction(EvasObjectCallbackType type, Action action)
+        {
+            if (action != null)
+            {
+                var eventData = new EventData(type, action);
+
+                if (!_eventDatas.ContainsKey(eventData))
+                {
+                    var evasCallback = new Interop.Evas.EvasCallback((d, o, t) => action());
+                    Interop.Evas.evas_event_callback_add(_handle, (Interop.Evas.ObjectCallbackType)type, evasCallback, IntPtr.Zero);
+                    _eventDatas.Add(eventData, evasCallback);
+                }
+            }
+        }
+
+        /// <summary>
+        /// Deletes a event to a given canvas event.
+        /// </summary>
+        /// <param name="type">The type of event that triggers</param>
+        /// <param name="action">The action to be called when the event is triggered</param>
+        public void DeleteEventAction(EvasObjectCallbackType type, Action action)
+        {
+            if (action != null)
+            {
+                var eventData = new EventData(type, action);
+                Interop.Evas.EvasCallback evasCallback = null;
+                _eventDatas.TryGetValue(eventData, out evasCallback);
+
+                if (evasCallback != null)
+                {
+                    Interop.Evas.evas_event_callback_del(_handle, (Interop.Evas.ObjectCallbackType)type, evasCallback);
+                    _eventDatas.Remove(eventData);
+                }
+            }
+        }
+
+        IntPtr CreateHandle(IntPtr evasObject)
+        {
+            return Interop.Evas.evas_object_evas_get(evasObject);
+        }
+
+        class EventData
+        {
+            public EvasObjectCallbackType Type { get; set; }
+            public Action Action { get; set; }
+
+            public EventData(EvasObjectCallbackType type, Action action)
+            {
+                Type = type;
+                Action = action;
+            }
+
+            public override bool Equals(object obj)
+            {
+                EventData e = obj as EventData;
+                if (e == null)
+                {
+                    return false;
+                }
+                return (Type == e.Type) && (Action == e.Action);
+            }
+
+            public override int GetHashCode()
+            {
+                int hashCode = Type.GetHashCode();
+                hashCode ^= Action.GetHashCode();
+                return hashCode;
+            }
+        }
+    }
+}
\ No newline at end of file
index 9236ba6..598789a 100644 (file)
@@ -49,6 +49,7 @@ namespace ElmSharp
     public abstract class EvasObject
     {
         private IntPtr _realHandle = IntPtr.Zero;
+        private EvasCanvas _evasCanvas;
 
         private event EventHandler _backButtonPressed;
 
@@ -235,6 +236,19 @@ namespace ElmSharp
         public bool IsRealized { get { return Handle != IntPtr.Zero; } }
 
         /// <summary>
+        /// Gets EvasCanvas
+        /// </summary>
+        public EvasCanvas EvasCanvas
+        {
+            get
+            {
+                if (_evasCanvas == null)
+                    _evasCanvas = new EvasCanvas(Handle);
+                return _evasCanvas;
+            }
+        }
+
+        /// <summary>
         /// Gets the current class's Name.
         /// </summary>
         public string ClassName