Google

jueves, diciembre 01, 2005

El código...

Tened en cuenta que todavía no está terminado... Pero aún así posteo el código... :)



Rule.cs:


using System;

 

namespace TreeSharp

{

    /// <summary>

    /// Summary description for Rule.

    /// </summary>

    public class Rule

    {

        char cLeftSide;

        string sRightSide;

        float fProbability;

 

        public Rule()

        {

            cLeftSide = '\0';

            sRightSide = "";

            fProbability = 0.0f;

        }

 

        public Rule(char cLeftSide, string sRightSide, float fProbability)

        {

            this.cLeftSide = cLeftSide;

            this.sRightSide = sRightSide;

            this.fProbability = fProbability;

        }

 

        public char LeftSide

        {

            get

            {

                return this.cLeftSide;

            }

            set

            {

                this.cLeftSide = value;

            }

        }

 

        public string RightSide

        {

            get

            {

                return this.sRightSide;

            }

            set

            {

                this.sRightSide = value;

            }

        }

 

        public float Probability

        {

            get

            {

                return this.fProbability;

            }

            set

            {

                this.fProbability = value;

            }

        }

    }

}



TreeParams.cs:


using System;

using System.ComponentModel;

 

namespace TreeSharp

{

    /// <summary>

    /// Summary description for RenderParameters.

    /// </summary>

    public class TreeParams

    {

 

        private float fXDegrees;

        private float fYDegrees;

        private float fZDegrees;

        private float fSegmentLength;

        private float fSegmentInitialWidth;

        private float fWidthDecrement;

        private float fRotationNoise;

        private float fLengthNoise;

 

        public TreeParams()

        {

            // 30º = PI / 6

            fXDegrees = fYDegrees = fZDegrees = (float)(Math.PI / 6);

            fSegmentLength = 10.0f;

            fSegmentInitialWidth = 5.0f;

            fWidthDecrement = 0.01f;

            fRotationNoise = 0.0f;

            fLengthNoise = 0.0f;

        }

 

        /// <summary>

        /// Degrees to be applied on rotations (X-Axis).

        /// </summary>

        [ Description("Degrees to be applied on rotations (X-Axis)."),

          DefaultValue(Math.PI/6) ]

        public float XDegrees

        {

            get    

            {

                return fXDegrees;

            }

            set 

            {

                fXDegrees = value;

            }

        }

 

        /// <summary>

        /// Degrees to be applied on rotations (Y-Axis).

        /// </summary>

        [ Description("Degrees to be applied on rotations (Y-Axis)."), 

          DefaultValue(Math.PI/6) ]

        public float YDegrees

        {

            get    

            {

                return fYDegrees;

            }

            set 

            {

                fYDegrees = value;

            }

        }

 

        /// <summary>

        /// Degrees to be applied on rotations (Z-Axis).

        /// </summary>

        [ Description("Degrees to be applied on rotations (Z-Axis)."),

          DefaultValue(Math.PI/6) ]

        public float ZDegrees

        {

            get    

            {

                return fZDegrees;

            }

            set 

            {

                fZDegrees = value;

            }

        }

 

        /// <summary>

        /// Segment Length.

        /// </summary>

        [ Description("Segment Length."), 

          DefaultValue(10.0f) ]

        public float SegmentLength

        {

            get    

            {

                return fSegmentLength;

            }

            set 

            {

                fSegmentLength = value;

            }

        }

 

        /// <summary>

        /// Segment width at the tree base.

        /// </summary>

        [ Description("Segment width at the tree base."),

          DefaultValue(5.0f) ]

        public float SegmentInitialWidth

        {

            get    

            {

                return fSegmentInitialWidth;

            }

            set 

            {

                fSegmentInitialWidth = value;

            }

        }

 

        /// <summary>

        /// Width decrement per step.

        /// </summary>

        [ Description("Width decrement per step."),

          DefaultValue(0.01f) ]

        public float WidthDecrement

        {

            get    

            {

                return fWidthDecrement;

            }

            set 

            {

                fWidthDecrement = value;

            }

        }

 

        /// <summary>

        /// Noise factor to be applied to rotations. Zero equals no noise.

        /// Valid values are 0 through 1.

        /// </summary>

        [ Description("Noise factor to be applied to rotations. Zero equals no noise.\nValid values are 0 through 1."), 

          DefaultValue(0.0f) ]

        public float RotationNoise

        {

            get    

            {

                return fRotationNoise;

            }

            set 

            {

                fRotationNoise = value;

            }

        }

 

        /// <summary>

        /// Noise factor to be applied to length calculations. Zero equals no noise.

        /// Valid values are 0 through 1.

        /// </summary>

        [ Description("Noise factor to be applied to length calculations. Zero equals no noise.\nValid values are 0 through 1."), 

          DefaultValue(0.0f) ]

        public float LengthNoise

        {

            get    

            {

                return fLengthNoise;

            }

            set 

            {

                fLengthNoise = value;

            }

        }

    }

}



LSystem.cs:


using System;

using System.Collections;

using TreeSharp;

 

namespace TreeSharp

{

    /// <summary>

    /// Summary description for LSystem.

    /// </summary>

    public class LSystem

    {

        private Hashtable _htRules;

        private Random _rnd;

 

        public LSystem()

        {

            _htRules = new Hashtable();

            _rnd = new Random();

        }

 

        public void AddRule(Rule newRule)

        {

            Rule[] rules;

            Rule[] oldRules;

 

            if (_htRules.ContainsKey(newRule.LeftSide)) 

            {

                oldRules = (Rule[])_htRules[newRule.LeftSide];

                rules = new Rule[oldRules.Length + 1];

                Array.Copy(oldRules,0,rules,0,oldRules.Length);

                rules[rules.Length-1] = newRule;

                _htRules[newRule.LeftSide] = rules;

            }

            else

            {

                rules = new Rule[1];

                rules[0] = newRule;

                _htRules.Add(newRule.LeftSide, rules);

            }

        }

 

        public void ClearRules()

        {

            _htRules.Clear();

        }

 

        private string sRightSide(char cLeftSide)

        {

            string sResult=cLeftSide.ToString();

            Rule[] rules;

 

            if (_htRules.ContainsKey(cLeftSide))

            {

                rules = (Rule[])_htRules[cLeftSide];

                float fChance;

                float fMin = 0.0f;

                float fMax;

                    

                fChance = (float) _rnd.NextDouble();

                for (int i = 0; i < rules.Length; i++)

                {

                    fMax = fMin + rules[i].Probability;

                    if ((fChance >= fMin) && (fChance < fMax))

                    {

                        sResult = rules[i].RightSide;

                        break;

                    }

                    fMin = fMax;

                }

            }

 

            return sResult;

        }

 

        public string PerformIterations(string sStartString, int iIterations)

        {

            System.Text.StringBuilder sbCurrent = new System.Text.StringBuilder();

            string sCurrentString = sStartString;

 

            for(int i = 0; i < iIterations; i++)

            {

                int iStrLen = sCurrentString.Length;

                for(int iChar = 0; iChar < iStrLen; iChar++)

                {

                    sbCurrent.Append(sRightSide(sCurrentString[iChar]));

                }

                sCurrentString = sbCurrent.ToString();

                sbCurrent.Length = 0;

            }

            return sCurrentString;

        }

    }

}



Renderer.cs:


using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Windows.Forms;

 

using Microsoft.DirectX;

using Microsoft.DirectX.Direct3D;

 

using TreeSharp;

 

namespace TreeSharp

{

    /// <summary>

    /// Summary description for Renderer.

    /// </summary>

    public class Renderer

    {

        /// <summary>

        /// The rendering device

        /// </summary>

        private Device _device = null;

        private Microsoft.DirectX.Direct3D.Font _font = null;

        private Sprite _sprite = null;

        private Control _control = null;

        private System.Drawing.Font _winFont;

        private int _vertexCount;

 

        public Renderer(Control control, System.Drawing.Font font)

        {

            _control = control;

            _winFont = font;

            _vertexCount = -1;

        }

 

        public Device Device

        {

            get

            {

                return _device;

            }

        }

 

        public VertexBuffer Buffer

        {

            set

            {

                _vertexCount = value.SizeInBytes / (CustomVertex.PositionColored.StrideSize);

                _device.SetStreamSource(0, value, 0);

                _device.VertexFormat = CustomVertex.PositionColored.Format;

            }

        }

 

        public bool InitializeGraphics()

        {

            try 

            {

                // Now let's setup the Direct3D stuff

                PresentParameters presentParams = new PresentParameters();

                presentParams.Windowed   = true;

                presentParams.SwapEffect = SwapEffect.Discard;

                

                // Create the device

                _device = new Device(0, DeviceType.Hardware, _control, CreateFlags.SoftwareVertexProcessing, presentParams);

                

                // Setup the event handlers for the device

                _device.DeviceLost     += new EventHandler(this.InvalidateDeviceObjects);

                _device.DeviceReset    += new EventHandler(this.RestoreDeviceObjects);

                _device.Disposing      += new EventHandler(this.DeleteDeviceObjects);

                _device.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);

            

                _font = new Microsoft.DirectX.Direct3D.Font(_device, _winFont);

                _sprite = new Sprite(_device);

    

                return true;

            } 

            catch (DirectXException) 

            {

                return false;

            }

        }

 

        /// <summary>

        /// This method moves the scene

        /// </summary>

        public virtual void FrameMove()

        {

            // TODO : Frame movement

        }

        

        /// <summary>

        /// This method renders the scene

        /// </summary>

        public virtual void Render()

        {

            if (_device != null) 

            {

                _device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);

                _device.BeginScene();

                

                // TODO : Scene rendering

                _sprite.Begin(SpriteFlags.AlphaBlend);

                _font.DrawText(_sprite, "Tree renderer", 0, 0, Color.White.ToArgb());

                _sprite.End();

                

                if (_vertexCount > 0)

                {

                    _device.DrawPrimitives(PrimitiveType.PointList, 0, 1); //_vertexCount);

                }

 

                _device.EndScene();

                _device.Present();

            }

        }

 

        protected virtual void InvalidateDeviceObjects(object sender, EventArgs e)

        {

 

        }

        

        protected virtual void RestoreDeviceObjects(object sender, EventArgs e)

        {

 

        }

        

        protected virtual void DeleteDeviceObjects(object sender, EventArgs e)

        {

 

        }

        

        protected virtual void EnvironmentResizing(object sender, CancelEventArgs e)

        {

 

        }

        

    }

}



CommandParser.cs:


using System;

using System.Drawing;

using System.Text.RegularExpressions;

 

using Microsoft.DirectX;

using Microsoft.DirectX.Direct3D;

 

namespace TreeSharp

{

    /// <summary>

    /// Translates the commands into vertex data. Also applies texturing 

    /// information.

    /// </summary>

    public class CommandParser

    {

 

        private TreeParams _treeParams = null;

        private Device _device;

 

        public CommandParser(Device device)

        {

            _device = device;

        }

 

        public TreeParams TreeParameters

        {

            get

            {

                return _treeParams;

            }

            set

            {

                _treeParams = value;

            }

        }

 

        public VertexBuffer Parse(string sCommand)

        {

            Regex fCount, lCount;

            int length = sCommand.Length;

            fCount = new Regex("F");

            lCount = new Regex("L");

            int vertexCount;

 

            vertexCount = (fCount.Matches(sCommand).Count * 16) + (lCount.Matches(sCommand).Count * 3);

 

            VertexBuffer vb = new VertexBuffer(typeof(CustomVertex.PositionColored), vertexCount, _device, Usage.WriteOnly, CustomVertex.PositionColored.Format , Pool.Default);

            GraphicsStream vbData =  vb.Lock(0, 0, 0);

 

            for(int i = 0; i < length; i++)

            {

                CustomVertex.PositionColored vert = new CustomVertex.PositionColored();

                switch(sCommand[i])

                {

                    case 'F':

                        vert.Color = Color.White.ToArgb();

                        vert.X = 1.0f;

                        vert.Y = 1.0f;

                        vert.Z = 1.0f;

                        vbData.Write(vert);

                        break;

                }

            }

            vb.Unlock();

 

            return vb;

        }

 

    }

}



frmTreeParams.cs:


using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

 

namespace TreeSharp

{

    /// <summary>

    /// Summary description for frmTreeParams.

    /// </summary>

    public class frmTreeParams : System.Windows.Forms.Form

    {

        private System.Windows.Forms.PropertyGrid pGridParams;

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.Container components = null;

 

        private TreeParams _treeParams;

 

        public frmTreeParams()

        {

            //

            // Required for Windows Form Designer support

            //

            InitializeComponent();

        }

 

        public TreeParams TreeParameters

        {

            get

            {

                return _treeParams;

            }

            set

            {

                _treeParams = value;

                pGridParams.SelectedObject = _treeParams;

            }

        }

 

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        protected override void Dispose( bool disposing )

        {

            if( disposing )

            {

                if(components != null)

                {

                    components.Dispose();

                }

            }

            base.Dispose( disposing );

        }

 

        #region Windows Form Designer generated code

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.pGridParams = new System.Windows.Forms.PropertyGrid();

            this.SuspendLayout();

            // 

            // pGridParams

            // 

            this.pGridParams.CommandsVisibleIfAvailable = true;

            this.pGridParams.Dock = System.Windows.Forms.DockStyle.Fill;

            this.pGridParams.LargeButtons = false;

            this.pGridParams.LineColor = System.Drawing.SystemColors.ScrollBar;

            this.pGridParams.Location = new System.Drawing.Point(0, 0);

            this.pGridParams.Name = "pGridParams";

            this.pGridParams.PropertySort = System.Windows.Forms.PropertySort.Alphabetical;

            this.pGridParams.Size = new System.Drawing.Size(292, 273);

            this.pGridParams.TabIndex = 0;

            this.pGridParams.Text = "PropertyGrid";

            this.pGridParams.ViewBackColor = System.Drawing.SystemColors.Window;

            this.pGridParams.ViewForeColor = System.Drawing.SystemColors.WindowText;

            // 

            // frmTreeParams

            // 

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

            this.ClientSize = new System.Drawing.Size(292, 273);

            this.ControlBox = false;

            this.Controls.Add(this.pGridParams);

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;

            this.Name = "frmTreeParams";

            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;

            this.Text = "Tree Parameters";

            this.TopMost = true;

            this.ResumeLayout(false);

 

        }

        #endregion

    }

}



frmRender.cs: (contiene el método Main)


using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Windows.Forms;

 

using Microsoft.DirectX;

using Microsoft.DirectX.Direct3D;

 

using TreeSharp;

 

namespace TreeSharp

{

    /// <summary>

    /// This is the main class of my Direct3D application

    /// </summary>

    public class frmRender : Form

    {

 

        #region  Control Initialization 

 

        private System.Windows.Forms.ListView lvwRules;

        private System.Windows.Forms.Button cmdAddRule;

        private System.Windows.Forms.NumericUpDown numProbability;

        private System.Windows.Forms.TextBox txtRightSide;

        private System.Windows.Forms.Splitter spltter;

        private System.Windows.Forms.Panel pnlRender;

        private System.Windows.Forms.Label label1;

        private System.Windows.Forms.Label label2;

        private System.Windows.Forms.Label label3;

        private System.Windows.Forms.ColumnHeader hdrLeftSide;

        private System.Windows.Forms.ColumnHeader hdrRightSide;

        private System.Windows.Forms.ColumnHeader hdrProbability;

        private System.Windows.Forms.Label label4;

        private System.Windows.Forms.Label label5;

        private System.Windows.Forms.TextBox txtStartString;

        private System.Windows.Forms.Button cmdGenerate;

        private System.Windows.Forms.Button cmdPreview;

        private System.Windows.Forms.ColumnHeader hdrCommand;

        private System.Windows.Forms.ColumnHeader hdrDescription;

        private System.Windows.Forms.Panel pnlHelp;

        private System.Windows.Forms.Splitter splHelp;

        private System.Windows.Forms.GroupBox gBoxHelp;

        private System.Windows.Forms.Panel pnlLeft;

        private System.Windows.Forms.Panel pnlRules;

        private System.Windows.Forms.ListView lvwHelp;

        private System.Windows.Forms.Button cmdModify;

        private System.Windows.Forms.Button cmdDelete;

        private System.Windows.Forms.NumericUpDown numIterations;

        private System.Windows.Forms.GroupBox gBoxRules;

        private System.Windows.Forms.TextBox txtLeftSide;

 

        private void InitializeComponent()

        {

            this.lvwRules = new System.Windows.Forms.ListView();

            this.hdrLeftSide = new System.Windows.Forms.ColumnHeader();

            this.hdrRightSide = new System.Windows.Forms.ColumnHeader();

            this.hdrProbability = new System.Windows.Forms.ColumnHeader();

            this.cmdAddRule = new System.Windows.Forms.Button();

            this.numProbability = new System.Windows.Forms.NumericUpDown();

            this.txtRightSide = new System.Windows.Forms.TextBox();

            this.pnlLeft = new System.Windows.Forms.Panel();

            this.pnlRules = new System.Windows.Forms.Panel();

            this.gBoxRules = new System.Windows.Forms.GroupBox();

            this.txtLeftSide = new System.Windows.Forms.TextBox();

            this.cmdModify = new System.Windows.Forms.Button();

            this.cmdDelete = new System.Windows.Forms.Button();

            this.label2 = new System.Windows.Forms.Label();

            this.label3 = new System.Windows.Forms.Label();

            this.label4 = new System.Windows.Forms.Label();

            this.numIterations = new System.Windows.Forms.NumericUpDown();

            this.label5 = new System.Windows.Forms.Label();

            this.txtStartString = new System.Windows.Forms.TextBox();

            this.cmdGenerate = new System.Windows.Forms.Button();

            this.cmdPreview = new System.Windows.Forms.Button();

            this.label1 = new System.Windows.Forms.Label();

            this.splHelp = new System.Windows.Forms.Splitter();

            this.pnlHelp = new System.Windows.Forms.Panel();

            this.gBoxHelp = new System.Windows.Forms.GroupBox();

            this.lvwHelp = new System.Windows.Forms.ListView();

            this.hdrCommand = new System.Windows.Forms.ColumnHeader();

            this.hdrDescription = new System.Windows.Forms.ColumnHeader();

            this.spltter = new System.Windows.Forms.Splitter();

            this.pnlRender = new System.Windows.Forms.Panel();

            ((System.ComponentModel.ISupportInitialize)(this.numProbability)).BeginInit();

            this.pnlLeft.SuspendLayout();

            this.pnlRules.SuspendLayout();

            this.gBoxRules.SuspendLayout();

            ((System.ComponentModel.ISupportInitialize)(this.numIterations)).BeginInit();

            this.pnlHelp.SuspendLayout();

            this.gBoxHelp.SuspendLayout();

            this.SuspendLayout();

            // 

            // lvwRules

            // 

            this.lvwRules.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 

                | System.Windows.Forms.AnchorStyles.Left) 

                | System.Windows.Forms.AnchorStyles.Right)));

            this.lvwRules.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {

                                                                                       this.hdrLeftSide,

                                                                                       this.hdrRightSide,

                                                                                       this.hdrProbability});

            this.lvwRules.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

            this.lvwRules.FullRowSelect = true;

            this.lvwRules.GridLines = true;

            this.lvwRules.HideSelection = false;

            this.lvwRules.Location = new System.Drawing.Point(8, 96);

            this.lvwRules.MultiSelect = false;

            this.lvwRules.Name = "lvwRules";

            this.lvwRules.Size = new System.Drawing.Size(256, 82);

            this.lvwRules.TabIndex = 9;

            this.lvwRules.View = System.Windows.Forms.View.Details;

            this.lvwRules.SelectedIndexChanged += new System.EventHandler(this.lvwRules_SelectedIndexChanged);

            // 

            // hdrLeftSide

            // 

            this.hdrLeftSide.Text = "Left";

            this.hdrLeftSide.Width = 69;

            // 

            // hdrRightSide

            // 

            this.hdrRightSide.Text = "Right";

            this.hdrRightSide.Width = 115;

            // 

            // hdrProbability

            // 

            this.hdrProbability.Text = "Prob";

            this.hdrProbability.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;

            this.hdrProbability.Width = 52;

            // 

            // cmdAddRule

            // 

            this.cmdAddRule.Location = new System.Drawing.Point(8, 64);

            this.cmdAddRule.Name = "cmdAddRule";

            this.cmdAddRule.TabIndex = 6;

            this.cmdAddRule.Text = "&Add";

            this.cmdAddRule.Click += new System.EventHandler(this.cmdAddRule_Click);

            // 

            // numProbability

            // 

            this.numProbability.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));

            this.numProbability.DecimalPlaces = 2;

            this.numProbability.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

            this.numProbability.Increment = new System.Decimal(new int[] {

                                                                             5,

                                                                             0,

                                                                             0,

                                                                             131072});

            this.numProbability.Location = new System.Drawing.Point(208, 32);

            this.numProbability.Maximum = new System.Decimal(new int[] {

                                                                           1,

                                                                           0,

                                                                           0,

                                                                           0});

            this.numProbability.Name = "numProbability";

            this.numProbability.Size = new System.Drawing.Size(56, 22);

            this.numProbability.TabIndex = 5;

            this.numProbability.Value = new System.Decimal(new int[] {

                                                                         10,

                                                                         0,

                                                                         0,

                                                                         65536});

            this.numProbability.Leave += new System.EventHandler(this.RuleControls_Leave);

            // 

            // txtRightSide

            // 

            this.txtRightSide.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 

                | System.Windows.Forms.AnchorStyles.Right)));

            this.txtRightSide.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;

            this.txtRightSide.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

            this.txtRightSide.Location = new System.Drawing.Point(64, 32);

            this.txtRightSide.Name = "txtRightSide";

            this.txtRightSide.Size = new System.Drawing.Size(136, 22);

            this.txtRightSide.TabIndex = 3;

            this.txtRightSide.Text = "";

            this.txtRightSide.TextChanged += new System.EventHandler(this.RuleControls_Leave);

            this.txtRightSide.Leave += new System.EventHandler(this.RuleControls_Leave);

            // 

            // pnlLeft

            // 

            this.pnlLeft.Controls.Add(this.pnlRules);

            this.pnlLeft.Controls.Add(this.splHelp);

            this.pnlLeft.Controls.Add(this.pnlHelp);

            this.pnlLeft.Dock = System.Windows.Forms.DockStyle.Left;

            this.pnlLeft.Location = new System.Drawing.Point(0, 0);

            this.pnlLeft.Name = "pnlLeft";

            this.pnlLeft.Size = new System.Drawing.Size(272, 480);

            this.pnlLeft.TabIndex = 10;

            // 

            // pnlRules

            // 

            this.pnlRules.Controls.Add(this.gBoxRules);

            this.pnlRules.Dock = System.Windows.Forms.DockStyle.Fill;

            this.pnlRules.Location = new System.Drawing.Point(0, 0);

            this.pnlRules.Name = "pnlRules";

            this.pnlRules.Size = new System.Drawing.Size(272, 300);

            this.pnlRules.TabIndex = 23;

            // 

            // gBoxRules

            // 

            this.gBoxRules.Controls.Add(this.txtLeftSide);

            this.gBoxRules.Controls.Add(this.cmdAddRule);

            this.gBoxRules.Controls.Add(this.cmdModify);

            this.gBoxRules.Controls.Add(this.cmdDelete);

            this.gBoxRules.Controls.Add(this.numProbability);

            this.gBoxRules.Controls.Add(this.txtRightSide);

            this.gBoxRules.Controls.Add(this.label2);

            this.gBoxRules.Controls.Add(this.label3);

            this.gBoxRules.Controls.Add(this.label4);

            this.gBoxRules.Controls.Add(this.numIterations);

            this.gBoxRules.Controls.Add(this.label5);

            this.gBoxRules.Controls.Add(this.txtStartString);

            this.gBoxRules.Controls.Add(this.lvwRules);

            this.gBoxRules.Controls.Add(this.cmdGenerate);

            this.gBoxRules.Controls.Add(this.cmdPreview);

            this.gBoxRules.Controls.Add(this.label1);

            this.gBoxRules.Dock = System.Windows.Forms.DockStyle.Fill;

            this.gBoxRules.Location = new System.Drawing.Point(0, 0);

            this.gBoxRules.Name = "gBoxRules";

            this.gBoxRules.Size = new System.Drawing.Size(272, 300);

            this.gBoxRules.TabIndex = 0;

            this.gBoxRules.TabStop = false;

            this.gBoxRules.Text = "Rule Editor";

            // 

            // txtLeftSide

            // 

            this.txtLeftSide.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;

            this.txtLeftSide.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

            this.txtLeftSide.Location = new System.Drawing.Point(8, 32);

            this.txtLeftSide.MaxLength = 1;

            this.txtLeftSide.Name = "txtLeftSide";

            this.txtLeftSide.Size = new System.Drawing.Size(48, 22);

            this.txtLeftSide.TabIndex = 1;

            this.txtLeftSide.Text = "";

            // 

            // cmdModify

            // 

            this.cmdModify.Location = new System.Drawing.Point(88, 64);

            this.cmdModify.Name = "cmdModify";

            this.cmdModify.TabIndex = 7;

            this.cmdModify.Text = "&Modify";

            this.cmdModify.Click += new System.EventHandler(this.cmdModify_Click);

            // 

            // cmdDelete

            // 

            this.cmdDelete.Location = new System.Drawing.Point(168, 64);

            this.cmdDelete.Name = "cmdDelete";

            this.cmdDelete.TabIndex = 8;

            this.cmdDelete.Text = "&Delete";

            this.cmdDelete.Click += new System.EventHandler(this.cmdDelete_Click);

            // 

            // label2

            // 

            this.label2.Location = new System.Drawing.Point(64, 16);

            this.label2.Name = "label2";

            this.label2.Size = new System.Drawing.Size(100, 16);

            this.label2.TabIndex = 2;

            this.label2.Text = "Right Side:";

            // 

            // label3

            // 

            this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));

            this.label3.Location = new System.Drawing.Point(208, 16);

            this.label3.Name = "label3";

            this.label3.Size = new System.Drawing.Size(40, 16);

            this.label3.TabIndex = 4;

            this.label3.Text = "Prob.";

            // 

            // label4

            // 

            this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

            this.label4.Location = new System.Drawing.Point(8, 184);

            this.label4.Name = "label4";

            this.label4.Size = new System.Drawing.Size(80, 16);

            this.label4.TabIndex = 10;

            this.label4.Text = "Iterations:";

            // 

            // numIterations

            // 

            this.numIterations.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

            this.numIterations.Location = new System.Drawing.Point(8, 200);

            this.numIterations.Name = "numIterations";

            this.numIterations.Size = new System.Drawing.Size(80, 20);

            this.numIterations.TabIndex = 11;

            this.numIterations.Value = new System.Decimal(new int[] {

                                                                        5,

                                                                        0,

                                                                        0,

                                                                        0});

            // 

            // label5

            // 

            this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

            this.label5.Location = new System.Drawing.Point(8, 224);

            this.label5.Name = "label5";

            this.label5.Size = new System.Drawing.Size(100, 16);

            this.label5.TabIndex = 12;

            this.label5.Text = "Start String:";

            // 

            // txtStartString

            // 

            this.txtStartString.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 

                | System.Windows.Forms.AnchorStyles.Right)));

            this.txtStartString.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;

            this.txtStartString.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

            this.txtStartString.Location = new System.Drawing.Point(8, 240);

            this.txtStartString.Name = "txtStartString";

            this.txtStartString.Size = new System.Drawing.Size(168, 22);

            this.txtStartString.TabIndex = 13;

            this.txtStartString.Text = "F";

            // 

            // cmdGenerate

            // 

            this.cmdGenerate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

            this.cmdGenerate.Location = new System.Drawing.Point(8, 272);

            this.cmdGenerate.Name = "cmdGenerate";

            this.cmdGenerate.Size = new System.Drawing.Size(80, 23);

            this.cmdGenerate.TabIndex = 15;

            this.cmdGenerate.Text = "&Render Tree";

            this.cmdGenerate.Click += new System.EventHandler(this.cmdGenerate_Click);

            // 

            // cmdPreview

            // 

            this.cmdPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));

            this.cmdPreview.Location = new System.Drawing.Point(184, 240);

            this.cmdPreview.Name = "cmdPreview";

            this.cmdPreview.Size = new System.Drawing.Size(80, 23);

            this.cmdPreview.TabIndex = 14;

            this.cmdPreview.Text = "Preview";

            this.cmdPreview.Click += new System.EventHandler(this.cmdPreview_Click);

            // 

            // label1

            // 

            this.label1.BackColor = System.Drawing.Color.Transparent;

            this.label1.Location = new System.Drawing.Point(8, 16);

            this.label1.Name = "label1";

            this.label1.Size = new System.Drawing.Size(56, 16);

            this.label1.TabIndex = 0;

            this.label1.Text = "Left Side:";

            // 

            // splHelp

            // 

            this.splHelp.Dock = System.Windows.Forms.DockStyle.Bottom;

            this.splHelp.Location = new System.Drawing.Point(0, 300);

            this.splHelp.Name = "splHelp";

            this.splHelp.Size = new System.Drawing.Size(272, 4);

            this.splHelp.TabIndex = 22;

            this.splHelp.TabStop = false;

            // 

            // pnlHelp

            // 

            this.pnlHelp.Controls.Add(this.gBoxHelp);

            this.pnlHelp.Dock = System.Windows.Forms.DockStyle.Bottom;

            this.pnlHelp.Location = new System.Drawing.Point(0, 304);

            this.pnlHelp.Name = "pnlHelp";

            this.pnlHelp.Size = new System.Drawing.Size(272, 176);

            this.pnlHelp.TabIndex = 21;

            // 

            // gBoxHelp

            // 

            this.gBoxHelp.Controls.Add(this.lvwHelp);

            this.gBoxHelp.Dock = System.Windows.Forms.DockStyle.Fill;

            this.gBoxHelp.Location = new System.Drawing.Point(0, 0);

            this.gBoxHelp.Name = "gBoxHelp";

            this.gBoxHelp.Size = new System.Drawing.Size(272, 176);

            this.gBoxHelp.TabIndex = 0;

            this.gBoxHelp.TabStop = false;

            this.gBoxHelp.Text = "Help";

            // 

            // lvwHelp

            // 

            this.lvwHelp.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {

                                                                                      this.hdrCommand,

                                                                                      this.hdrDescription});

            this.lvwHelp.Dock = System.Windows.Forms.DockStyle.Fill;

            this.lvwHelp.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;

            this.lvwHelp.Location = new System.Drawing.Point(3, 16);

            this.lvwHelp.MultiSelect = false;

            this.lvwHelp.Name = "lvwHelp";

            this.lvwHelp.Size = new System.Drawing.Size(266, 157);

            this.lvwHelp.TabIndex = 0;

            this.lvwHelp.View = System.Windows.Forms.View.Details;

            // 

            // hdrCommand

            // 

            this.hdrCommand.Text = "Command";

            this.hdrCommand.Width = 40;

            // 

            // hdrDescription

            // 

            this.hdrDescription.Text = "Description";

            this.hdrDescription.Width = 201;

            // 

            // spltter

            // 

            this.spltter.Location = new System.Drawing.Point(272, 0);

            this.spltter.Name = "spltter";

            this.spltter.Size = new System.Drawing.Size(4, 480);

            this.spltter.TabIndex = 11;

            this.spltter.TabStop = false;

            // 

            // pnlRender

            // 

            this.pnlRender.Dock = System.Windows.Forms.DockStyle.Fill;

            this.pnlRender.Location = new System.Drawing.Point(276, 0);

            this.pnlRender.Name = "pnlRender";

            this.pnlRender.Size = new System.Drawing.Size(364, 480);

            this.pnlRender.TabIndex = 0;

            // 

            // frmRender

            // 

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

            this.ClientSize = new System.Drawing.Size(640, 480);

            this.Controls.Add(this.pnlRender);

            this.Controls.Add(this.spltter);

            this.Controls.Add(this.pnlLeft);

            this.MinimumSize = new System.Drawing.Size(648, 507);

            this.Name = "frmRender";

            this.Text = "L-System Generator";

            this.Closing += new System.ComponentModel.CancelEventHandler(this.frmRender_Closing);

            ((System.ComponentModel.ISupportInitialize)(this.numProbability)).EndInit();

            this.pnlLeft.ResumeLayout(false);

            this.pnlRules.ResumeLayout(false);

            this.gBoxRules.ResumeLayout(false);

            ((System.ComponentModel.ISupportInitialize)(this.numIterations)).EndInit();

            this.pnlHelp.ResumeLayout(false);

            this.gBoxHelp.ResumeLayout(false);

            this.ResumeLayout(false);

 

        }

        

        #endregion

 

        private Renderer _renderer;

        private LSystem _lSystem;

        private TreeParams _treeParams;

        private frmTreeParams _frmParams;

        private CommandParser _parser;

        private bool _rendererIsOK;

        

        public frmRender()

        {

            this.InitializeComponent();

            this.FillHelpListView();

            

            _lSystem = new LSystem();

            _treeParams = new TreeParams();

 

            _renderer = new Renderer(this.pnlRender, new System.Drawing.Font(this.Font, FontStyle.Regular));

            _rendererIsOK = _renderer.InitializeGraphics();

 

            if(!_rendererIsOK)

            {

                MessageBox.Show("Couldn't initialize graphics.\nNo trees will be rendered.","Error",MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            else

            {

                _parser = new CommandParser(_renderer.Device);

                _parser.TreeParameters = _treeParams;

            }

 

            this.InitParametersForm();

            this.EnableControls();

            this.Focus();

        }

        

        private void FillHelpListView()

        {

            lvwHelp.Items.Add(new ListViewItem(new string[] {"F","Moves Forward"}, -1));

            lvwHelp.Items.Add(new ListViewItem(new string[] {"+","Rotates clockwise in X axis"}, -1));

            lvwHelp.Items.Add(new ListViewItem(new string[] {"-","Rotates counter-clockwise in X axis"}, -1));

            lvwHelp.Items.Add(new ListViewItem(new string[] {"*","Rotates clockwise in Y axis"}, -1));

            lvwHelp.Items.Add(new ListViewItem(new string[] {"/","Rotates counter-clockwise in Y axis"}, -1));

            lvwHelp.Items.Add(new ListViewItem(new string[] {"|","Rotates clockwise in Z axis"}, -1));

            lvwHelp.Items.Add(new ListViewItem(new string[] {"#","Rotates counter-clockwise in Z axis"}, -1));

            lvwHelp.Items.Add(new ListViewItem(new string[] {"L","Draws a leaf"}, -1));

            lvwHelp.Items.Add(new ListViewItem(new string[] {"[","Creates a new branch"}, -1));

            lvwHelp.Items.Add(new ListViewItem(new string[] {"]","Ends the current branch"}, -1));

        }

 

        private void InitParametersForm()

        {

            Point location;

 

            _frmParams = new frmTreeParams();

 

            location = this.Location;

            location.X += this.Width;

            if ((location.X + _frmParams.Width) > Screen.PrimaryScreen.Bounds.Width) 

            {

                location.X = Screen.PrimaryScreen.Bounds.Width - _frmParams.Width;

            }

            if ((location.Y + _frmParams.Height) > Screen.PrimaryScreen.Bounds.Height) 

            {

                location.X = Screen.PrimaryScreen.Bounds.Height - _frmParams.Height;

            }

            if (location.Y < this.PointToScreen(Point.Empty).Y)

            {

                location.Y = this.PointToScreen(Point.Empty).Y ;

            }

 

            _frmParams.Location = location;

            _frmParams.TreeParameters = _treeParams;

            _frmParams.Show();

        }

 

        /// <summary>

        /// Our mainloop

        /// </summary>

        public void Run()

        {

            if(_rendererIsOK)

            {

                // While the form is still valid, render and process messages

                while (Created) 

                {

                    _renderer.FrameMove();    

                    _renderer.Render();

                    Application.DoEvents();

                }

            }

        }

        

        protected override void OnPaint(PaintEventArgs e)

        {

            if(_rendererIsOK)

            {

                _renderer.Render();

            }

        }

 

        protected override void OnPaintBackground(PaintEventArgs pevent)

        {

            // Don't do anything to avoid the background being painted.

        }

 

        private void EnableControls()

        {

            cmdAddRule.Enabled = ((txtLeftSide.Text != "") && (txtRightSide.Text != ""));

            cmdModify.Enabled = (lvwRules.SelectedItems.Count > 0);

            cmdDelete.Enabled = (lvwRules.SelectedItems.Count > 0);

            cmdPreview.Enabled = (txtStartString.Text != "");

            cmdGenerate.Enabled = ((txtStartString.Text != "") && (lvwRules.Items.Count > 0));

        }

 

        private void cboLeftSide_SelectedIndexChanged(object sender, System.EventArgs e)

        {

            EnableControls();

        }

 

        private void buildRulesTable()

        {

            _lSystem.ClearRules();

 

            foreach(ListViewItem lvi in lvwRules.Items)

            {

                char cLeftSide = lvi.SubItems[0].Text[0];

                string sLeftSide = lvi.SubItems[1].Text;

                float fProb = float.Parse(lvi.SubItems[2].Text);

 

                Rule newRule = new Rule(cLeftSide,sLeftSide,fProb);

                _lSystem.AddRule(newRule);

            }

        }

 

        private void cmdAddRule_Click(object sender, System.EventArgs e)

        {

            lvwRules.Items.Add(new ListViewItem(new string[] {txtLeftSide.Text, txtRightSide.Text, numProbability.Value.ToString("0.00")}, -1));

            buildRulesTable();

            EnableControls();

            txtLeftSide.Focus();

        }

 

        private void cmdModify_Click(object sender, System.EventArgs e)

        {

            ListViewItem lvwItem;

 

            lvwItem = lvwRules.SelectedItems[0];

            

            lvwItem.SubItems[0].Text = txtLeftSide.Text;

            lvwItem.SubItems[1].Text = txtRightSide.Text;

            lvwItem.SubItems[2].Text = numProbability.Value.ToString("0.00");

            buildRulesTable();

            EnableControls();

        }

 

        private void cmdDelete_Click(object sender, System.EventArgs e)

        {

            lvwRules.SelectedItems[0].Remove();

            buildRulesTable();

            EnableControls();

        }

 

        private void lvwRules_SelectedIndexChanged(object sender, System.EventArgs e)

        {

            if (lvwRules.SelectedItems.Count > 0) 

            {

                ListViewItem lvwItem = lvwRules.SelectedItems[0];

                txtLeftSide.Text = lvwItem.SubItems[0].Text;

                txtRightSide.Text = lvwItem.SubItems[1].Text;

                numProbability.Value = decimal.Parse(lvwItem.SubItems[2].Text);

            }

            EnableControls();

        }

 

        private void cmdPreview_Click(object sender, System.EventArgs e)

        {

        

        }

 

        private void cmdGenerate_Click(object sender, System.EventArgs e)

        {

            string sCommand = _lSystem.PerformIterations(txtStartString.Text, (int)numIterations.Value);

            Console.WriteLine("({0}) - {1}", sCommand.Length, sCommand);

            VertexBuffer vb = _parser.Parse(sCommand);

            _renderer.Buffer = vb;

        }

 

        private void RuleControls_Leave(object sender, System.EventArgs e)

        {

            EnableControls();

        }

 

        private void frmRender_Closing(object sender, CancelEventArgs e)

        {

            if ( ! ( null == _frmParams  ||  _frmParams.IsDisposed ) )

            {

                _frmParams.Close();

            }

        }

 

        #region Main 

 

        /// <summary>

        /// The main entry point for the application

        /// </summary>

        static void Main()

        {

            using (frmRender frmRender = new frmRender()) 

            {

                frmRender.Show();

                frmRender.Run();

            }

        }

 

        #endregion

 

    }

}



Y ya está... De momento eso es todo... ;)

Cuando tenga listo el pintado del árbol, postearé el código completo (si puedo, en un zip, en vez de poner el código...)

Comments: Publicar un comentario



<< Home

This page is powered by Blogger. Isn't yours?