]> O.S.I.I.S - jp/crow.git/commitdiff
share Picture resource data among widgets
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sat, 3 Feb 2018 05:12:57 +0000 (06:12 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sat, 3 Feb 2018 05:19:51 +0000 (06:19 +0100)
src/BmpPicture.cs
src/Picture.cs
src/SvgPicture.cs

index ea83095c376a1be37347d2ef82dd237e00ffd85a..79302354cf90ca59cd7297081e3b762cc9636c8b 100644 (file)
@@ -30,20 +30,47 @@ using Cairo;
 
 namespace Crow
 {
+
+       /// <summary>
+       /// Derived from FILL for loading and drawing bitmaps in the interface
+       /// </summary>
        public class BmpPicture : Picture
        {
-               byte[] image;
+               byte[] image = null;
 
+               #region CTOR
+               /// <summary>
+               /// Initializes a new instance of BmpPicture.
+               /// </summary>
                public BmpPicture ()
                {}
+               /// <summary>
+               /// Initializes a new instance of BmpPicture by loading the image pointed by the path argument
+               /// </summary>
+               /// <param name="path">image path, may be embedded</param>
                public BmpPicture (string path) : base(path)
                {}
-               protected override void loadFromStream (Stream stream)
+               #endregion
+               /// <summary>
+               /// load the image for rendering from the path given as argument
+               /// </summary>
+               /// <param name="path">image path, may be embedded</param>
+               public override void Load (string path)
                {
-                       using (MemoryStream ms = new MemoryStream ()) {
-                               stream.CopyTo (ms);
-                               loadBitmap (new System.Drawing.Bitmap (ms));    
+                       Path = path;
+                       if (sharedResources.ContainsKey (path)) {
+                               sharedPicture sp = sharedResources [path];
+                               image = (byte[])sp.Data;
+                               Dimensions = sp.Dims;
+                               return;
+                       }
+                       using (Stream stream = Interface.GetStreamFromPath (path)) {
+                               using (MemoryStream ms = new MemoryStream ()) {
+                                       stream.CopyTo (ms);
+                                       loadBitmap (new System.Drawing.Bitmap (ms));    
+                               }
                        }
+                       sharedResources [path] = new sharedPicture (image, Dimensions);
                }
 
                //load image via System.Drawing.Bitmap, cairo load png only
@@ -103,6 +130,13 @@ namespace Crow
                }
                #endregion
 
+               /// <summary>
+               /// paint the image in the rectangle given in arguments according
+               /// to the Scale and keepProportion parameters.
+               /// </summary>
+               /// <param name="gr">drawing Backend context</param>
+               /// <param name="rect">bounds of the target surface to paint</param>
+               /// <param name="subPart">used for svg only</param>
                public override void Paint (Cairo.Context gr, Rectangle rect, string subPart = "")
                {
                        float widthRatio = 1f;
index 0e5212ababd7659aa065a778bdd73a3f24b8f5d1..fe7007c79d43a9d18bde12be95c2f66e32e2357e 100644 (file)
 using System;
 using System.IO;
 using Cairo;
+using System.Collections.Generic;
 
 namespace Crow
 {
+       /// <summary>
+       /// store data and dimensions for resource sharing
+       /// </summary>
+       internal class sharedPicture {
+               //TODO: restructure this whith clever conceptual classes
+               public object Data;
+               public Size Dims;
+               public sharedPicture (object _data, Size _dims){
+                       Data = _data;
+                       Dims = _dims;
+               }
+       }
+       /// <summary>
+       /// virtual class for loading and drawing picture in the interface
+       /// 
+       /// Every loaded resources are stored in a dictonary with their path as key and shared
+       /// among interface elements
+       /// </summary>
        public abstract class Picture : Fill
        {
+               /// <summary>
+               /// share a single store for picture resources among usage in different controls
+               /// </summary>
+               internal static Dictionary<string, sharedPicture> sharedResources = new Dictionary<string, sharedPicture>();
+
+               /// <summary>
+               /// path of the picture
+               /// </summary>
                public string Path;
+               /// <summary>
+               /// unscaled dimensions fetched on loading
+               /// </summary>
                public Size Dimensions;
+               /// <summary>
+               /// if true and image has to be scalled, it will be scaled in both direction
+               /// equaly
+               /// </summary>
                public bool KeepProportions = false;
+               /// <summary>
+               /// allow or not the picture to be scalled on request by the painter
+               /// </summary>
                public bool Scaled = true;
 
+               #region CTOR
+               /// <summary>
+               /// Initializes a new instance of Picture.
+               /// </summary>
                public Picture ()
                {
                }
+               /// <summary>
+               /// Initializes a new instance of Picture by loading the image pointed by the path argument
+               /// </summary>
+               /// <param name="path">image path, may be embedded</param>
                public Picture (string path)
                {
-                       LoadImage (path);
+                       Load (path);
                }
+               #endregion
 
                #region Image Loading
-               public void LoadImage (string path)
-               {
-                       loadFromStream (Interface.GetStreamFromPath (path));
-
-                       Path = path;
-               }
-                       
-               protected abstract void loadFromStream(Stream stream);
+               /// <summary>
+               /// load the image for rendering from the stream given as argument
+               /// </summary>
+               /// <param name="stream">picture stream</param>
+               public abstract void Load(string path);
                #endregion
 
+               /// <summary>
+               /// abstract method to paint the image in the rectangle given in arguments according
+               /// to the Scale and keepProportion parameters.
+               /// </summary>
+               /// <param name="gr">drawing Backend context</param>
+               /// <param name="rect">bounds of the target surface to paint</param>
+               /// <param name="subPart">used for svg only</param>
                public abstract void Paint(Context ctx, Rectangle rect, string subPart = "");
 
-
+               #region Operators
                public static implicit operator Picture(string path)
                {
                        if (string.IsNullOrEmpty (path))
@@ -71,7 +121,7 @@ namespace Crow
                        else 
                                _pic = new BmpPicture ();
 
-                       _pic.LoadImage (path);                  
+                       _pic.Load (path);                       
 
                        return _pic;
                }
@@ -79,6 +129,7 @@ namespace Crow
                {
                        return _pic == null ? null : _pic.Path;
                }
+               #endregion
 
                public static object Parse(string path)
                {
@@ -92,7 +143,7 @@ namespace Crow
                        else 
                                _pic = new BmpPicture ();
 
-                       _pic.LoadImage (path);                  
+                       _pic.Load (path);                       
 
                        return _pic;
                }
index b398d5238b84a134d01023c5dd97e3ce2e81ed15..b402685249ee8b3941f7becd03536d3de5d9f85e 100644 (file)
@@ -30,22 +30,45 @@ using Cairo;
 
 namespace Crow
 {
+       /// <summary>
+       /// Derived from FILL for loading and drawing SVG images in the interface
+       /// </summary>
        public class SvgPicture : Picture
        {
                Rsvg.Handle hSVG;
 
+               #region CTOR
+               /// <summary>
+               /// Initializes a new instance of SvgPicture.
+               /// </summary>
                public SvgPicture ()
                {}
+               /// <summary>
+               /// Initializes a new instance of SvgPicture by loading the SVG file pointed by the path argument
+               /// </summary>
+               /// <param name="path">image path, may be embedded</param>
                public SvgPicture (string path) : base(path)
                {}
-               protected override void loadFromStream (Stream stream)
+               #endregion
+
+               public override void Load (string path)
                {
-                       using (MemoryStream ms = new MemoryStream ()) {
-                               stream.CopyTo (ms);
+                       Path = path;
+                       if (sharedResources.ContainsKey (path)) {
+                               sharedPicture sp = sharedResources [path];
+                               hSVG = (Rsvg.Handle)sp.Data;
+                               Dimensions = sp.Dims;
+                               return;
+                       }
+                       using (Stream stream = Interface.GetStreamFromPath (path)) {
+                               using (MemoryStream ms = new MemoryStream ()) {
+                                       stream.CopyTo (ms);
 
-                               hSVG = new Rsvg.Handle (ms.ToArray ());
-                               Dimensions = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height);
+                                       hSVG = new Rsvg.Handle (ms.ToArray ());
+                                       Dimensions = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height);
+                               }
                        }
+                       sharedResources [path] = new sharedPicture (hSVG, Dimensions);
                }
 
                #region implemented abstract members of Fill
@@ -79,7 +102,14 @@ namespace Crow
                        }       
                }
                #endregion
-                       
+
+               /// <summary>
+               /// paint the image in the rectangle given in arguments according
+               /// to the Scale and keepProportion parameters.
+               /// </summary>
+               /// <param name="gr">drawing Backend context</param>
+               /// <param name="rect">bounds of the target surface to paint</param>
+               /// <param name="subPart">limit rendering to svg part named 'subPart'</param>
                public override void Paint (Cairo.Context gr, Rectangle rect, string subPart = "")
                {
                        float widthRatio = 1f;