Click here to Skip to main content
15,891,409 members
Articles / Programming Languages / C#

Universal Framework for Science and Engineering - Part 4: Space elevator

Rate me:
Please Sign up or sign in to vote.
4.56/5 (6 votes)
14 Aug 20066 min read 36.6K   2.2K   37  
An article on framework applications to the space elevator.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace FormulaEditor
{
    public class FieldSymbolDrawable : FieldSymbol, IDrawableSymbol
    {

        #region Fields
        private PureDrawableSymbol pDrawable = new PureDrawableSymbol();


        #endregion

        #region Constructors

        public FieldSymbolDrawable(FieldSymbol sym)
            : base(sym.String)
        {
        }

        #endregion

        #region Overriden Mebers
        public override object Clone()
        {
            return new FieldSymbolDrawable(this);
        }


        public override void SetToFormula(MathFormula formula)
        {
            pDrawable.SetToFormula(formula);
            level = formula.Level;
            sizes = formula.Sizes;
            pDrawable.Font = PureDrawableSymbol.FontsItalic[level];
        }


        #endregion

        #region IDrawableSymbol Members

        void IDrawableSymbol.CalculatePositions()
        {
            SimpleSymbolDrawable.CalculatePositions(this);
        }

        void IDrawableSymbol.CalculateFullRelativeRectangle()
        {
            IDrawableSymbol ds = this;
            ds.CalculateRelativeRectangle();
        }

        int IDrawableSymbol.StandardWidth
        {
            get
            {
                Graphics g = PureDrawableSymbol.Graphics;
                return (int)(g.MeasureString(s + "W", PureDrawableSymbol.FontsBold[0]).Width);
            }
        }

        void IDrawableSymbol.DrawOnComponent(System.Drawing.Graphics g, System.Drawing.Brush brush, System.Drawing.Pen pen)
        {
            Font f = (italic) ? PureDrawableSymbol.FontsItalic[level] : PureDrawableSymbol.FontsBold[level];
            int x = (int)(pDrawable.RectForShow.X + pDrawable.RectForShow.Width / 2 - g.MeasureString(s, f).Width / 2);
            int width = (int)g.MeasureString(s, PureDrawableSymbol.FontsBold[0]).Width;
            g.DrawString(s, f, brush, x, pDrawable.RectForShow.Y + PureDrawableSymbol.TOP_SHIFT);
        }

        void IDrawableSymbol.DrawSelf(System.Drawing.Graphics g)
        {
            int y = pDrawable.Position.Y - pDrawable.Font.Height / 2;
            int width = (int)g.MeasureString(s, pDrawable.Font).Width;
            int x = pDrawable.Position.X + (int)((PureDrawableSymbol.W_SHIFT / 2)) * width;
            g.DrawString(s, pDrawable.Font, PureDrawableSymbol.SymbolBrush, x, y);
        }

        void IDrawableSymbol.CalculateRelativeRectangle()
        {
            Font f = PureDrawableSymbol.FontsBold[level];
            int h = (int)((1 + 2 * PureDrawableSymbol.H_SHIFT) * (f.Height));
            int w = (int)(PureDrawableSymbol.Graphics.MeasureString(s, f).Width);
            pDrawable.RelativeRectangleX = 0;
            pDrawable.RelativeRectangleY = -h / 2;
            pDrawable.RelativeRectangleWidth = w;
            pDrawable.RelativeRectangleHeight = h;
            pDrawable.FullRelativeRectangleX = 0;
            pDrawable.FullRelativeRectangleY = -h / 2;
            pDrawable.FullRelativeRectangleWidth = w;
            pDrawable.FullRelativeRectangleHeight = h;
        }

        void IDrawableSymbol.CalculateChildPositions()
        {
        }

        void IDrawableSymbol.Prepare(FormulaEditorPerformer performer)
        {
            Font f;
            if (italic)
            {
                f = PureDrawableSymbol.FontsItalic[0];
            }
            else
            {
                f = PureDrawableSymbol.FontsBold[0];
            }
            pDrawable.Font = f;
            Control c = performer.EditControl;
            Graphics g = Graphics.FromHwnd(c.Handle);
            int h = (int)((1 + 2 * PureDrawableSymbol.C_HEIGHT) * pDrawable.Font.Height);
            int w = (int)g.MeasureString(s + "iw", pDrawable.Font).Width;
            int w1 = (int)g.MeasureString(s, pDrawable.Font).Width;
            g.Dispose();
            Bitmap im = new Bitmap(w, h);
            Graphics gr = Graphics.FromImage(im);
            Pen pen = new Pen(Color.Black);
            Brush brush = new SolidBrush(Color.White);
            gr.FillRectangle(brush, 1, 1, w - 2, h - 2);
            gr.DrawRectangle(pen, 0, 0, w - 1, h - 1);
            int x = (int)gr.MeasureString("i", pDrawable.Font).Width / 2;
            gr.DrawString(s, pDrawable.Font, PureDrawableSymbol.SymbolBrush, x, PureDrawableSymbol.TOP_SHIFT);
            gr.Dispose();
            im.MakeTransparent(Color.White);
            pDrawable.SetImage(im);
        }

        void IDrawableSymbol.CalculateRectangleForShow()
        {
            Graphics g = PureDrawableSymbol.Graphics;
            pDrawable.RectForShow = new Rectangle(0, 0, (int)g.MeasureString("Wiw" + s, PureDrawableSymbol.FontsBold[0]).Width,
                (int)((1 + 2 * PureDrawableSymbol.C_HEIGHT) * (PureDrawableSymbol.FontsBold[0].Size)));
        }

        PureDrawableSymbol IDrawableSymbol.PureDrawable
        {
            get { return pDrawable; }
        }

        #endregion

        #region IInsertedObject Members

        System.Drawing.Rectangle IInsertedObject.InsertedRect
        {
            get { return pDrawable.InsertedRect; }
        }

        #endregion
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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