Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

OOP in the Real World - Creating an Equation Editor

Rate me:
Please Sign up or sign in to vote.
4.87/5 (106 votes)
21 Apr 2015MIT11 min read 136.6K   10.5K   220  
Object Oriented Design and Programming process using a real world example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Xml.Linq;
using System.Windows.Input;

namespace Editor
{
    class SignSubSuper : EquationContainer
    {
        RowContainer mainEquation;
        StaticSign sumSign;
        RowContainer superEquation;
        RowContainer subEquation;
        int bottomDif = 6;
        double maxUpperHalf = 0;
        int hGap = 2;

        public SignSubSuper(EquationContainer parent, SignCompositeSymbol symbol, bool useUpright)
            : base(parent)
        {
            ActiveChild = mainEquation = new RowContainer(this);
            subEquation = new RowContainer(this);
            superEquation = new RowContainer(this);
            sumSign = new StaticSign(this, symbol, useUpright);
            subEquation.FontFactor = SmallerFontFactor;
            superEquation.FontFactor = SmallerFontFactor;
            childEquations.AddRange(new EquationBase[] { mainEquation, sumSign, superEquation, subEquation });
        }
        
        protected override void CalculateWidth()
        {
            Width = sumSign.Width + Math.Max(subEquation.Width, superEquation.Width) + mainEquation.Width + hGap;
            sumSign.Left = Left;
            subEquation.Left = sumSign.Right;
            superEquation.Left = sumSign.Right;
            mainEquation.Left = subEquation.Right + hGap;
        }

        protected override void CalculateHeight()
        {
            maxUpperHalf = Math.Max(mainEquation.RefY, Math.Max(sumSign.RefY, subEquation.RefY - (sumSign.RefY - bottomDif)));
            double maxLowerHalf = Math.Max(mainEquation.RefY, sumSign.RefY + subEquation.RefY - bottomDif);
            Height = maxLowerHalf + maxUpperHalf;
            sumSign.MidY = MidY;
            mainEquation.MidY = MidY;
            subEquation.MidY = sumSign.Bottom - bottomDif;
            superEquation.Bottom = sumSign.Top + FontSize / 5;
        }

        public override double Height
        {
            get { return base.Height; }
            set
            {
                base.Height = value;
            }
        }

        public override double RefY
        {
            get
            {
                return maxUpperHalf;                
            }
        }

        public override double Top
        {
            get { return base.Top; }
            set
            {
                base.Top = value;
                sumSign.MidY = MidY;
                mainEquation.MidY = MidY;
                subEquation.MidY = sumSign.Bottom - bottomDif;
                superEquation.Bottom = sumSign.Top + FontSize / 5;
            }
        }
        
        public override bool ConsumeMouseClick(System.Windows.Point mousePoint)
        {
            if (mainEquation.Bounds.Contains(mousePoint))
            {
                ActiveChild = mainEquation;
            }
            else if (superEquation.Bounds.Contains(mousePoint))
            {
                ActiveChild = superEquation;
            }
            else 
            {
                ActiveChild = subEquation;
            }
            return ActiveChild.ConsumeMouseClick(mousePoint);
        }

        public override double Left
        {
            get { return base.Left; }
            set
            {
                base.Left = value;
                sumSign.Left = value;
                subEquation.Left = sumSign.Right;
                superEquation.Left = sumSign.Right;
                mainEquation.Left = subEquation.Right + hGap;
            }
        }

        public override bool ConsumeKey(Key key)
        {
            if (ActiveChild.ConsumeKey(key))
            {
                CalculateSize();
                return true;
            }
            if (key == Key.Down)
            {
                if (ActiveChild == superEquation)
                {
                    ActiveChild = mainEquation;
                    return true;    
                }
                else if (ActiveChild == mainEquation)
                {
                    ActiveChild = subEquation;
                    return true;
                }
            }
            else if (key == Key.Up)
            {
                if (ActiveChild == subEquation)
                {
                    ActiveChild = mainEquation;
                    return true;
                }
                else if (ActiveChild == mainEquation)
                {
                    ActiveChild = superEquation;
                    return true;
                }
            }
            return false;
        }
    }
}

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 MIT License


Written By
Technical Lead https://mathiversity.com
Unknown
I am a full-stack developer. My skills include JavaScript, C#/.Net, MS Azure cloud etc. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.

Comments and Discussions