Click here to Skip to main content
15,894,460 members
Articles / Multimedia / OpenGL

Universal Framework for Science and Engineering - Part 7: Virtual Reality at Once

Rate me:
Please Sign up or sign in to vote.
4.96/5 (105 votes)
19 Nov 2010CPOL25 min read 187.5K   14.4K   212  
An article on framework applications to virtual reality
using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Configuration.Assemblies;
using System.Collections;
using System.Drawing;
using System.ComponentModel;
using System.Xml;


using CategoryTheory;
using DiagramUI;
using FormulaEditor;
using FormulaEditorUI;
using FormulaEditorUI.Forms;
using FormulaEditor.Drawing;

namespace DataPerformerUI
{
    /// <summary>
    /// Panel which draws formula
    /// </summary>
	public class PanelFormula : Panel
	{
		private char var;
		private Control control;
		private Button button;
		private string formulaString;
		private MathFormulaDrawable formula;
		private Rectangle formulaRect;
		private Image bkgnd;
		private Pen linePen = new Pen(Color.Black);
		private Brush bkBrush = new SolidBrush(Color.Gray);
		private Brush rBrush = new SolidBrush(Color.FromArgb(216, 203, 187));
		private Brush blackBrush = new SolidBrush(Color.Black);
		private Font font = new Font("Times", MathSymbolFactory.Sizes[0], FontStyle.Bold | FontStyle.Italic);
		private Point pointFormula;
		private string variables;
		//		private string cap;
		private string[,] sub;
        private string[] symbols;
        private static string[] staticSymbols;
        private int left;
        private string cap;
        private bool deriv;
        private int center;
        private int shift;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="cap">Caption</param>
        /// <param name="control">Control</param>
        /// <param name="width">Width</param>
        /// <param name="height">Height</param>
        /// <param name="variables">Variable</param>
        /// <param name="deriv">The "dertivation" sign</param>
        /// <param name="sub">Subscripted symbols</param>
        /// <param name="symbols">Symbols</param>
		public PanelFormula(string cap, Control control, int width, int height, string variables, 
			bool deriv, string[,] sub, string[] symbols)
		{
            if (symbols == null)
            {
                this.symbols = staticSymbols;
            }
            else
            {
                if (symbols[0] != null)
                {
                    this.symbols = symbols;
                }
            }
			this.var = cap[0];
			this.sub = sub;
            this.cap = cap;
            this.deriv = deriv;
			this.control = control;
			this.variables = variables;
			Width = width;
			Height = height;
            center = height / 2;
            shift = -10;
            button = new Button();
            button.Width = 150;
            button.Text = ResourceService.Resources.GetControlResource("Edit", DataPerformerUI.Utils.ControlUtilites.Resources);
            button.Left = 20;
            button.Top = center - button.Height / 2;
            Controls.Add(button);
            button.Click += new EventHandler(onClick);

            setBkgnd();
			Paint += new PaintEventHandler(onPaint);
 			formulaRect = new Rectangle(left, 5, width - left - 5, height - 10);
			center = formulaRect.Top + formulaRect.Height / 2;
			pointFormula = new Point(left + 30, center);
		}

        private void setBkgnd()
        {
            bkgnd = new Bitmap(Width, Height);
            Graphics g = Graphics.FromImage(bkgnd);
            left = button.Left + button.Width + 30;
            g.FillRectangle(bkBrush, 0, 0, Width, Height);
            g.DrawRectangle(linePen, 0, 0, Width - 1, Height - 1);
            g.DrawString(cap + " =", font, blackBrush, left, center + shift);
            if (deriv)
            {
                g.DrawString(".", font, blackBrush, left, center + shift - font.Height - 3);
            }
            int w = (int)g.MeasureString(cap, font).Width;
            left += 60 + w;
            formulaRect = new Rectangle(left, 5, Width - left - 5, Height - 10);
            Formula = formulaString;

        }


        /// <summary>
        /// Edited variable
        /// </summary>
		public char Variable
		{
			get
			{
				return var;
			}
		}

        /// <summary>
        /// Edited formula
        /// </summary>
		public string Formula
		{
			get
			{
				return formulaString;
			}
			set
			{
				if (value == null)
				{
					return;
				}
				formulaString = value;
				formula = new MathFormulaDrawable(MathFormula.FromString(MathSymbolFactory.Sizes, value), DrawableConverter.Object);
				formula.Position = pointFormula;
				formula.CalculateFullRelativeRectangle();
				formula.CalculatePositions();
				Graphics g = Graphics.FromImage(bkgnd);
				g.FillRectangle(rBrush, formulaRect.Left, formulaRect.Top, formulaRect.Width, formulaRect.Height);
				g.DrawRectangle(linePen, formulaRect.Left, formulaRect.Top, formulaRect.Width, formulaRect.Height);
				formula.Draw(g);
			}
		}

        /// <summary>
        /// Symbols
        /// </summary>
        public static string[] Symbols
        {
            set
            {
                staticSymbols = value;
            }
        }

        private static void control_Resize(object sender, EventArgs args)
        {
            Control cont = sender as Control;
            foreach (Control c in cont.Controls)
            {
                c.Width = cont.Width - 40;
                if (c is PanelFormula)
                {
                    PanelFormula p = c as PanelFormula;
                    p.setBkgnd();
                }
                c.Refresh();
            }
        }

        /// <summary>
        /// Sets resize operation for control
        /// </summary>
        /// <param name="control">The control</param>
        public static void SetResize(Control control)
        {
            control.Resize += new EventHandler(control_Resize);
        }



		private void onPaint(object sender, PaintEventArgs e)
		{
			e.Graphics.DrawImage(bkgnd, 0, 0);
		}

		private void onClick(object sender, System.EventArgs e)
		{
			FormulaEditorForm f = new FormulaEditorForm(variables, sub, symbols);
			if (formulaString != null)
			{
				f.Formula = formulaString;
			}
			Form form = null;
			Control c = this;
			while (true)
			{
				if (c is Form)
				{
					form = c as Form;
					break;
				}
				c = c.Parent;
			}
			f.ShowDialog(form);
			if (!f.Accepted)
			{
				return;
			}
			Formula = f.Formula;
			Refresh();
		}

	}

}

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
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions