Click here to Skip to main content
15,887,344 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.2K   14.4K   212  
An article on framework applications to virtual reality
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using Diagram.UI.Interfaces;
using DiagramUI.Labels;

namespace DiagramUI
{
    /// <summary>
    /// Performer of static operations
    /// </summary>
    public static class StaticPerformer
    {
        #region Fields
        /// <summary>
        /// All forms
        /// </summary>
        private static List<Form> forms = new List<Form>();

        #endregion


        /// <summary>
        /// Performs action with form
        /// </summary>
        /// <param name="form">Form for action</param>
        /// <param name="type">Action type</param>
        /// <param name="start">True in start and false in stop</param>
        public static void Action(this Form form, object type, bool start)
        {
            if (form != null)
            {
                if (!form.IsDisposed)
                {
                    if (form is IStartStop)
                    {
                        IStartStop ss = form as IStartStop;
                        ss.Action(type, start);
                    }
                }
            }
        }

        /// <summary>
        /// Shows form
        /// </summary>
        /// <param name="sf">Object which shows form</param>
        public static void Show(this IShowForm sf)
        {
            sf.ShowForm();
            Form form = sf.Form as Form;
            if (form == null)
            {
                return;
            }
            if (forms.Contains(form))
            {
                if (form.IsDisposed)
                {
                    forms.Remove(form);
                }
                return;
            }
            else
            {
                forms.Add(form);
                if (sf is IEnabled)
                {
                    IEnabled en = sf as IEnabled;
                    form.FormClosed += (object senrer, FormClosedEventArgs ev) =>
                   {
                       try
                       {
                           if (forms.Contains(form))
                           {
                               forms.Remove(form);
                           }
                           if (!en.Enabled)
                           {
                               en.Enabled = true;
                           }
                       }
                       catch (Exception ex)
                       {
                           ex.Log();
                       }
                   };
                    if (en.Enabled)
                    {
                        en.Enabled = false;
                    }
                }
            }
        }

        /// <summary>
        /// Post set arrow operation
        /// </summary>
        /// <param name="control">Control</param>
        public static void PostSetArrow(this Control control)
        {
            if (control is CategoryTheory.IPostSetArrow)
            {
                CategoryTheory.IPostSetArrow ps = control as CategoryTheory.IPostSetArrow;
                ps.PostSetArrow();
            }
            foreach (Control c in control.Controls)
            {
                c.PostSetArrow();
            }
        }

        /// <summary>
        /// Checks whether button preesd for arrow motion
        /// </summary>
        /// <param name="args">Mouse arguments</param>
        /// <returns>True for arrow motion and false otherwise</returns>
        static internal bool IsArrowClick(this MouseEventArgs args)
        {
            return (args.Button == MouseButtons.Left) & ((Control.ModifierKeys & Keys.Shift) != Keys.Shift);
        }

        /// <summary>
        /// Gets root label of control
        /// </summary>
        /// <param name="control">The control</param>
        /// <returns>The root label</returns>
        public static IObjectLabel GetRootLabel(this Control control)
        {
            IObjectLabel l = null;
            GetRootLabel(control, ref l);
            return l;
        }


  

        /// <summary>
        /// Gets form of control
        /// </summary>
        /// <param name="control">The control</param>
        /// <returns>The form</returns>
        public static Form GetForm(this Control control)
        {
            if (control is Form)
            {
                return control as Form;
            }
            return control.Parent.GetForm();
        }

        /// <summary>
        /// Gets palette buttons from control
        /// </summary>
        /// <param name="control">The control</param>
        /// <returns>Palette buttons</returns>
        public static Dictionary<Type, Dictionary<string, IPaletteButton>> GetPaletteButtons(this Control control)
        {
            Dictionary<Type, Dictionary<string, IPaletteButton>> dictionary =
                new Dictionary<Type, Dictionary<string, IPaletteButton>>();
            GetButtons(control, dictionary);
            return dictionary;
        }




        private static void GetButtons(Control control, Dictionary<Type, Dictionary<string, IPaletteButton>> dictionary)
        {
            if (control is IPaletteButton)
            {
                IPaletteButton b = control as IPaletteButton;
                Dictionary<string, IPaletteButton> d = null;
                Type t = b.ReflectionType;
                if (dictionary.ContainsKey(t))
                {
                    d = dictionary[t];
                }
                else
                {
                    d = new Dictionary<string, IPaletteButton>();
                    dictionary[t] = d;
                }
                d[b.Kind] = b;
            }
            foreach (Control c in control.Controls)
            {
                GetButtons(c, dictionary);
            }
            if (control is ToolBar)
            {
                ToolBar tb = control as ToolBar;
                foreach (object o in tb.Buttons)
                {
                    if (o is IPaletteButton)
                    {
                        IPaletteButton b = o as IPaletteButton;
                        Dictionary<string, IPaletteButton> d = null;
                        Type t = b.ReflectionType;
                        if (dictionary.ContainsKey(t))
                        {
                            d = dictionary[t];
                        }
                        else
                        {
                            d = new Dictionary<string, IPaletteButton>();
                            dictionary[t] = d;
                        }
                        d[b.Kind] = b;
                    }
                }
            }
        }

        private static void GetRootLabel(Control c, ref IObjectLabel l)
        {
            if (c is IObjectLabel)
            {
                l = c as IObjectLabel;
            }
            Control p = c.Parent;
            if (p == null)
            {
                return;
            }
            GetRootLabel(p, ref l);
        }

    }
}

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