Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / Visual Basic

AGE, Another Graphic Engine in .NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (36 votes)
10 May 2007CPOL8 min read 135.8K   5.9K   170  
A library that allows some GDI+ manipulation at runtime in an easy way
/////////////////////////////////////////////////////////////////////////////////
// NeoDataType Another Graphic Engine
// --------------------
// Project Copyright (C)    : Fabio Zanetta, email: support@neodatatype.net
// Portions Copyright (C)   : Microsoft Corporation. All Rights Reserved.
// License                  : docs/license.txt
// ------------------------------------------------------------------------------
// File created by          : Fabio Zanetta, email: support@neodatatype.net
// ------------------------------------------------------------------------------
// Please, if you modify some parts of this file mark them as described in
// docs/modify_guidelines.txt
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Drawing;
using System.ComponentModel;
using NeoDataType.Documents;

namespace NeoDataType.Graphic
{
    [SaveThisClass()]
    public class ScriptedItem : GraphicItem
    {

        string _script = "// item.<property_name>: <value>" + Environment.NewLine +
                         "// stroke: <type>, <parameters>" + Environment.NewLine +
                         "// pen: <color>, <width>" + Environment.NewLine +
                         "// brush: <type>, <parameters>" + Environment.NewLine +
                         "// points: {(0;0) (1;1)}";
        List<PaintStroke> _strokes;
        internal List<StrokeProperty> ItemProps = new List<StrokeProperty>();

        public ScriptedItem()
        {
            _strokes = new List<PaintStroke>();
            Painter = new ScriptedPainter();
            RotationCenterRelativity =  PointRelativity.ToCanvas;
            AntiAlias = true;
        }

        //[Browsable(false)]
        [SaveThisProperty]
        [EditorAttribute(typeof(Editors.ScriptItemEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public string Script
        {
            //get { return string.Join(Environment.NewLine, _scriptlines); }
            //set { ScriptLines = value.Split('\n'); }
            get { return _script; }
            set { _script = value; Parse(_script); }
        }
        
        [Browsable(false)]
        public PaintStroke[] Strokes
        {
            get { return _strokes.ToArray(); }
        }

        public void RefreshParse()
        {
            Parse(Script);
        }


        private static string ClearString(string text)
        {
            // --------------
            // Remove unused text (comments)
            // --------------

            char[] NEW_LINE_CHARS = new char[] { '\n', '\r' };
            int start;
            do
            {
                start = text.IndexOf("//");
                if (start < 0) break;

                int end = text.IndexOfAny(NEW_LINE_CHARS, start);
                if (end < 0)
                    end = text.Length;

                text = text.Remove(start, end - start);

            } while (true);

            return text.Trim();
        }

        public void Parse(string text)
        {

            foreach (PaintStroke stroke in _strokes)
                stroke.Dispose();

            if (text != null)
            {

                text = ClearString(text);

                _strokes.Clear();

                // Parse the item properties
                char[] NEW_LINE_CHARS = new char[] { '\n', '\r' };
                Parser script = new Parser(text);
                string[] values;
                
                ItemProps.Clear();
                while (script.FollowingText.IndexOf("item.") >= 0)
                {
                    script.SkipNext("item.");
                    script.SkipBlanks();
                    values = script.GetStringsUntil(NEW_LINE_CHARS, ":");

                    StrokeProperty sp = new StrokeProperty();
                    sp.Name = values[0];
                    sp.Value = values[1];
                    ItemProps.Add(sp);

                    //System.Reflection.PropertyInfo pi = typeof(ScriptedItem).GetProperty(values[0], System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                    //StrokeProperty.AssignPropertyValue(this, pi, values[1], Painter);
                }
                

                while(text.IndexOf("stroke:") >= 0) 
                {
                
                    int idx;
                    PaintStroke stroke = PaintStroke.Parse(text, out idx);
                    if (stroke != null)
                    {
                        _strokes.Add(stroke);
                        BoundsChanged = true; // force the full redraw
                    }
                    text = text.Remove(0, idx);
                }
            }            
            PropertyChanged();
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions