Development of Text Editor Teditor 1.5


This application is adapted to viewing text files, it can carry out also creation of new files and editing of existing files. It allows the user to enter the name of a created or opened file. The user can scroll the text in a multi-line text component  then there are seen other areas of the text. It is possible to enter the new text, to erase the old text, to save the created text in a file.

 

1. ShapeBoxT - The three-dimensional shape for the multi-line text

 

The three-dimensional shape for the multi-line text is coded in a separate class. This class will be received from ShapeBox and can be used as the basic 3D  shape. Our class will be similar to a parental window in application MDI, it - the container for the interface. Therefore, applications become three-dimensional text editors, having initialized this class to be its parent shape. Class has name ShapeBoxT and its key characteristics are:

  • Class ShapeBoxT allows the application to become text editor. It realizes as the foreground of the text editor. It means, that it carries out the interface.
  •  
  • Class ShapeBoxT represents the parental 3D shape which contains the interface for the text editor. It contains separate 3D box and its general components, which are used to present the text editor and to
  • execute its operations.
     

  • ShapeBoxT supports the text editor through TextHyp member. This class has been developed earlier and used to start the multi-line text.
     

  • ShapeBox has set of the general components, which are used to present the application. It - labels, text fields, and the user buttons which execute various operations.

 
using System;
using System. Collections;
using System. Drawing;
using Microsoft. DirectX;
using D3D=Microsoft. DirectX.Direct3D;

namespace Code9
{
       public class ShapeBoxT
       {
             private D3D.Device device;
             private D3D.Mesh shMesh;
             private D3D.Material shMaterial;
             public string str;
             public Position pos;
             public Position posc;
             public Orientation orient;
             public Size size;
             public Position pos4;
             public Orientation orient4;
             public Size size4;
             public float scaleX;
             public bool InFocus;
             public string type;
             public int Alfa=15;
             public Framer fram;
             public Translator trans;
             public ButtonSph3 bmenu1;
             public MenuHypButton bhypmenu1;
             public TextHyp OTH1;
             public LineTextCyl LTC1;
             public int k=10;
             public Chars CharList;
             public Letters MeshList;
           

This shape contains the following components:

  • Translator trans - the derivative component representing an opportunity of moving 3D shape, realization of the general management 3D shape and demonstrations of  application name.
  • Framer fram - a derivative component which gives an opportunity of change of the size of 3D  shape.
  • MenuHypButton bhypmenu1 - the three-dimensional push-button menu, realizes the basic functions of the text editor.

  • TextHyp OTH1 - a multi-line text field where the user enters the text.

  • LineTextCyl LTC1 - a separate text line in which editing the text is carried out.

 

1.1 Creation of the text editor shape

 

ShapeBoxT represents the application of the text editor, the interface of the editor is included in it. If the copy of this class is initialized, the constructor of a class initialize its members of starting values. In this function, class ShapeBoxT, is created. However, components of the interface, such as the menu and the multi-line text, are created and initialized in method InitializeGpaphics which is developed further. The following definition shows, as the constructor works. 

           public ShapeBoxT (D3D.Device device1, string str1, string tmp, Position pos1, Orientation orient1, Size size1, string type1, AlfabitEn AlfE)
             {
             device = device1;
             str=str1;
             pos=pos1;
             posc=pos;
             orient=orient1;
             size=size1;
             type=type1;
             InFocus=false;
             scaleX=1.0f;
             type = "Box";
             Position pos11;
             Orientation orient11;
             pos11.x=pos.x-size.width/2.0f;
             pos11.y=pos.y+size.height/2.0f;
             pos11.z=pos.z-size.depth/2.0f+size.depth/2.0f;
             Size size11;
             size11.width=size.width;
             int radius=1;
             size11.height=4.0f*radius;
             size11.depth=4.0f*radius;
             float length=size.depth;
             orient11=orient;
        fram=new Framer (device, str + "fram", pos11, orient11, size11, length, radius);
             Position pos2;
             Orientation orient2;
             Size size2;
             pos2.x=pos.x+size.width/2.0f;
             pos2.y=pos.y-size.height/2.0f;
             pos2.z=pos.z;
             orient2=orient;
             radius=1;
             size2.width=size.depth;
             size2.height=4.0f*radius;
             size2.depth=4.0f*radius;
             length=size2.width;
             trans=new Translator (device, str + "trans", "Teditor", pos2, orient2, size2, length, radius);
             Position pos3;
             Orientation orient3;
             Size size3;
             size3=size;
             shMesh=D3D.Mesh. Box (device, size3.width, size3.height, size3.depth);
                   shMesh = shMesh. Clone (D3D.MeshFlags. Managed, D3D.VertexFormats. PositionNormal | D3D.VertexFormats. Diffuse, device);
                   D3D.CustomVertex. PositionNormalColored [] Dest =
                         (D3D.CustomVertex. PositionNormalColored []) shMesh. LockVertexBuffer (typeof (D3D.CustomVertex. PositionNormalColored),      0, shMesh. NumberVertices);
                   for (int x=0; x <(Dest. GetLength (0)); x ++)
                   {
                         Dest [x] .Color = Color. FromArgb (Alfa, 255, 0, 0) .ToArgb ();
                  }
                   shMesh. UnlockVertexBuffer ();
                   pos4=new Position ();
                   orient4=new Orientation ();
                   size4=new Size ();
                   pos4.x=0.0f; pos4.y =-30.0f; pos4.z =-10.0f;
                   orient4.x=0.0f; orient4.y=1.0f; orient4.z=0.0f; orient4.angle=0.75f;
                   size4.width=20.0f; size4.height=40.0f; size4.depth=20.0f;
           
       OTH1 = new TextHyp (device, tmp,    "HypText1", pos4, orient4, size4, k, 1, AlfE);
            }
 

1.2 Drawing the shape

 

Drawing of the shape is carried out by special method Draw ().

Its contents is resulted further:

 

public void Draw (D3D.Device device1, Position pos1, Orientation orient1, Size size1, string type1, float buttlen)

             {...}

                 
1.3 Clearing

 

Clearing of resources is made by method Dispose () and destructor.

 

2 Menus - Performance of functions

 

Till now, for realization of actions three-dimensional buttons as rather independent components were used. They have a little interactions with each other. Thus, if the user clicks by the button - separate function is carried out. For realization of group of the interconnected functions it is necessary to provide communication between components so, that one component  can cooperate with another. For this purpose it is possible to use the three-dimensional push-button menu. Buttons allow us to store pointers on carried out actions and to communicate between them. The push-button menu has been developed earlier as class MenuHypButton. Certainly, this class approaches for this case, but there is a problem: as buttons carry out various functions, you should process clicks of the mouse differently in different applications. For example, button Copy could copy the contents of the buffer while other button Exit could finish the application. Use only MenuHypButton does not allow you to do it. What it is necessary to make for this purpose? Simply, you should add function - we shall tell My_Copy () - in Form1 and to cause it in the necessary event. The following sections describe each of the derivative buttons developed for the menu of the text editor.

 

2.1 The general functions for processing events of the menu

 

Work with the three-dimensional push-button menu is carried out as follows:

  1. The choice of menu button in Translator - activization of the menu is carried out;
  2. Navigation in 3D space for a choice of the button connected with necessary function is carried out:
  3. Click by the button of the mouse on the active button is carried out.

 

For this purpose the following functions are used:

public void MenuItem_GetFocus (string msg1, string msg2)

             {...}
public void MenuItem_LostFocus (string msg1, string msg2)

             {...}

public void MenuItem_Click (string msg1, string msg2)
             {...}                 
 

2.2 MyCopy - The button of copying

 

The first button which will be developed for the 3D menu - the button of copying. After pressing  it handler of event MyCopy should be coded to copy the text fragment

 

2.3 MyCut - The button to cut out

 

The second button which will be developed for the 3D menu - the button to cut. After pressing  it handler of event MyCut should be coded to cut the text fragment

 

2.4 MyPaste - The button to insert

 

When the button to insert click, are carried out the following action. The content of the text buffer is inserted into the current position of the text cursor.

 

2.5 MyEnd - The button stop

 

Users who will press by the button end - will finish work with the menu.

 

2.6 MyExit - The button of closing

 

The final button which will be developed for the 3D menu - the button of closing. It is most simple of its buttons. Everything, that it does, closing of the application, sending the exit message.

                 

3. TextHyp - The multi-line text
 

Proj75NXMultiText.JPG


Fig.1 the Multi-line text

 

Having developed the three-dimensional push-button menu it is possible to proceed to realization of the multi-line text for the text editor. It is carried out in class TextHyp of our application of the text editor. The declaration of a class is given below.

 
using System;
using System. Collections;
using System. Drawing;
using Microsoft. DirectX;
using D3D = Microsoft. DirectX.Direct3D;
using System. Windows. Forms; 

namespace Code9
{
       public class TextHyp: IEnumerable
       {
             private D3D.Device device;
             private D3D.Mesh meTex = null;
             private TexCursor1 curr1;
             public int CursorPos = 0;
             public Position pos;
             public Orientation orient;
             public Size size;
             public float diag;
             public float d2;
             public float d4;
             public string str;
             public string tex;
             public int LengthTex;
             private float angle=0.0f;
             private float x;
             private float y;
             private float z;
             public Chars CharList;
             public Chars BeginBuffer;
             public Chars EndBuffer;
             public LineTextCyl LT1;
             public Letters MeshList;
             public int k;
             public int m;
             public int mk;
             public int ln;
             public int [] llt;
             public int [] mlt;
             public Scroller s;
             public bool InFocus;
             public bool OnPressed;
             public bool OnChange;
             public string StatStr;
             public Size SLetter;
                         public Components NentsTexIns;
           

3.1 TextHyp - The constructor of a class

 

For creation of object of the given class the following constructor is used:

 
           public TextHyp (D3D.Device device1 , string str1, string tex1,
                   Position pos1, Orientation orient1, Size size1, int k1, int m1,
                   AlfabitEn AlfE)
             {...}           

                 
3.2 TextHyp - Drawing

 

For drawing object of the given class the following method is used:

 
public void Draw (D3D.Device device, Position pos, Orientation orient, Size size,
                   int k, float blength, Chars CharList, Letters MeshList)
                 
             {...}                 

     

3.3 TextHyp - clearing

 

At last, multi-line  text should be unloaded, as soon as you have stopped to use it.

 

4. Example of the program - Teditor 1.5

 

Having finished development of auxiliary classes it is possible to include them in the typical application. In other words, applications can become text editors, only after addition of the given classes. Except for that it loads all components which are required - such as labels, buttons, etc. The Declaration of a class is resulted further:

 
using System;
using System. IO;
using System. Drawing;
using System. Collections;
using System. ComponentModel;
using System. Windows. Forms;
using System. Data;
using Microsoft. DirectX;
using D3D=Microsoft. DirectX.Direct3D;
using Microsoft. DirectX.DirectInput;

namespace Code9 // Teditor
{
       public struct Position
       {    public float x;
             public float y;
             public float z;}
       public struct Size
       {
             public float width;
             public float height;
             public float depth;
      }
       public struct Orientation
       {
                   public float x;
             public float y;
             public float z;
             public float angle;
      }
       public class Form1: System. Windows. Forms. Form
       {
*if (! USE_THREADS)
             private bool running = true;
*endif
             pr<
ivate D3D.Device device=null;

             private Device deviceM=null;
             private Device deviceK=null;
             private D3D.Mesh mesh1=null;
             private ShapeBoxT shape1;
             public string type;
             private Shapes shapes1;
             private Components components1;
             public LineTextCyl LTC1;
             public int LengTex;
             public int k=10;
             public int l;
             public int m=0;
             public AlfabitEn AlfE;
             public AlfabitRu AlfR;
             public int ia=0;
             public float buttlen=0.0f;
             public Chars CharList;
             public Letters MeshList;
             public Arrow arr1;
             public float Nz, Nx, Ny, Mz, My, Mx;
             public float sX;
             public float sY;
             public float sZ;
             public float Xold=0.0f;
             public float Yold=0.0f;
             public float Zold=0.0f;
             public float sXold;
             public float sYold;
             public float sZold;
             public bool drag=false;
             public float scale1;
             string msg;
             public Position dpos;
             public Size dsize;
             private float angle=0.0f;
             private float x1=2.0f;
             private float y1=2.0f;
             private float z1=2.0f;
             private float x2=2.0f;
             private float y2=2.0f;
             private float z2=2.0f;
             private Position pos1;
             private Size size1;
             private Orientation orient1;
             private Position pos2;
             private Orientation orient2;
             private Size size2;
             private string type1;
             public MenuHypButton MHB1;
             public MenuLevel ML1;
             public Chars TexBuffer;
             public int beginTexBuffer=0;
             public int lengthTexBuffer=0;
             public bool ins=false;
             string msg1 = "";
             string msg2 = " ";
             private System. Windows. Forms. MainMenu mainMenu1;
             private System. Windows. Forms. MenuItem menuItem1;
             ...

            private System. ComponentModel. Container components = null;

 4.1 Application Teditor - the constructor

 

The constructor of the given class looks so:

 
public Form1 ()
             {
                   InitializeComponent ();
                   TexBuffer=new Chars ();
                   this. SetStyle (ControlStyles. AllPaintingInWmPaint|ControlStyles. Opaque, true);
                   this. KeyDown + = new KeyEventHandler (OnKeyDown);
                   this. MouseUp + = new MouseEventHandler (OnMouseUp);
                   this. MouseDown + = new MouseEventHandler (OnMouseDown);
            }
 

 

4.2. Loading components

 

The interface for our program of the text editor will consist of several components. They contain in the form and include the three-dimensional shape, the three-dimensional menu, the multi-line text. They allow the user to open a file, to create a new file, to carry out editing the text, to save a file. Components are loaded in groups by separate methods - ShapeBoxT (), MenuHypButton (), Arrow (device). These methods causes InitializeGraphics ()  method, which is used to load in the text editor a full set of components and to establish width, height and depth of the form.

 

4.3 Drawing the text editor

 

Drawing of the text editor is made in method OnPaint ().

 
protected override void OnPaint (System. Windows. Forms. PaintEventArgs e)
             {...}
                 

5. Usability of application

 

During using of application Teditor v. 1.0 there is big difficulties for navigation and a choice of virtual objects in three-dimensional space with the help of the  mouse. For overcoming this difficulties has been solved to apply gamepad to management of the pointer  as the device adequate 3D space. Thus the first joystick is used for moving on axes X and Y, and the second joystick for moving on axis Z. Also are used the first and second button of gamepad. If at your system there is gamepad execute Option-Joystick command for its activation and further carry out navigation and a choice with its help. In this case  navigation and a choice does not have essential difficulties as in case of  the mouse. For return to using of the mouse execute Option-Mouse command.

 

The conclusion

 

In this article it is submitted the program which uses results of all this work. See Building Isometric Application article on www.c-sharpcorner.com. The application of the text editor, however, can be advanced. Now, it does not support search and replacement of text fragments. This elements  can be added.


     

     

Up Next
    Ebook Download
    View all
    Learn
    View all