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 theapplication to become text editor. It realizes as the foreground of the texteditor. It means, that it carries out the interface.
  • Class ShapeBoxT represents theparental 3D shape which contains the interface for the text editor. It containsseparate 3D box and its general components, which are used to present the texteditor and to execute its operations.
  • ShapeBoxT supports the texteditor through TextHyp member. This class has been developed earlier and used tostart the multi-line text.
  • ShapeBox has set of the generalcomponents, which are used to present the application. It - labels, text fields,and the user buttons which execute various operations.
  1. using System;  
  2. using System. Collections;  
  3. using System. Drawing;  
  4. using Microsoft. DirectX;  
  5. using D3D=Microsoft. DirectX.Direct3D;  
  6.   
  7. namespace Code9  
  8. {  
  9.        public class ShapeBoxT  
  10.        {  
  11.              private D3D.Device device;  
  12.              private D3D.Mesh shMesh;  
  13.              private D3D.Material shMaterial;  
  14.              public string str;  
  15.              public Position pos;  
  16.              public Position posc;  
  17.              public Orientation orient;  
  18.              public Size size;  
  19.              public Position pos4;  
  20.              public Orientation orient4;  
  21.              public Size size4;  
  22.              public float scaleX;  
  23.              public bool InFocus;  
  24.              public string type;  
  25.              public int Alfa=15;  
  26.              public Framer fram;  
  27.              public Translator trans;  
  28.              public ButtonSph3 bmenu1;  
  29.              public MenuHypButton bhypmenu1;  
  30.              public TextHyp OTH1;  
  31.              public LineTextCyl LTC1;  
  32.              public int k=10;  
  33.              public Chars CharList;  
  34.              public Letters MeshList; 

This shape contains the following components:

  • Translator trans - the derivativecomponent representing an opportunity of moving 3D shape, realization of thegeneral management 3D shape and demonstrations of  application name.
  • Framer fram - a derivativecomponent which gives an opportunity of change of the size of 3D  shape.
  • MenuHypButton bhypmenu1 - thethree-dimensional push-button menu, realizes the basic functions of the texteditor.
  • TextHyp OTH1 - a multi-line textfield where the user enters the text.
  • LineTextCyl LTC1 - a separatetext 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.

  1. public ShapeBoxT (D3D.Device device1, string str1, string tmp, Position pos1, Orientation orient1, Size size1, string type1, AlfabitEn AlfE)  
  2.              {  
  3.              device = device1;  
  4.              str=str1;  
  5.              pos=pos1;  
  6.              posc=pos;  
  7.              orient=orient1;  
  8.              size=size1;  
  9.              type=type1;  
  10.              InFocus=false;  
  11.              scaleX=1.0f;  
  12.              type = "Box";  
  13.              Position pos11;  
  14.              Orientation orient11;  
  15.              pos11.x=pos.x-size.width/2.0f;  
  16.              pos11.y=pos.y+size.height/2.0f;  
  17.              pos11.z=pos.z-size.depth/2.0f+size.depth/2.0f;  
  18.              Size size11;  
  19.              size11.width=size.width;  
  20.              int radius=1;  
  21.              size11.height=4.0f*radius;  
  22.              size11.depth=4.0f*radius;  
  23.              float length=size.depth;  
  24.              orient11=orient;  
  25.         fram=new Framer (device, str + "fram", pos11, orient11, size11, length, radius);  
  26.              Position pos2;  
  27.              Orientation orient2;  
  28.              Size size2;  
  29.              pos2.x=pos.x+size.width/2.0f;  
  30.              pos2.y=pos.y-size.height/2.0f;  
  31.              pos2.z=pos.z;  
  32.              orient2=orient;  
  33.              radius=1;  
  34.              size2.width=size.depth;  
  35.              size2.height=4.0f*radius;  
  36.              size2.depth=4.0f*radius;  
  37.              length=size2.width;  
  38.              trans=new Translator (device, str + "trans""Teditor", pos2, orient2, size2, length, radius);  
  39.              Position pos3;  
  40.              Orientation orient3;  
  41.              Size size3;  
  42.              size3=size;  
  43.              shMesh=D3D.Mesh. Box (device, size3.width, size3.height, size3.depth);  
  44.                    shMesh = shMesh. Clone (D3D.MeshFlags. Managed, D3D.VertexFormats. PositionNormal | D3D.VertexFormats. Diffuse, device);  
  45.                    D3D.CustomVertex. PositionNormalColored [] Dest =  
  46.                          (D3D.CustomVertex. PositionNormalColored []) shMesh. LockVertexBuffer (typeof (D3D.CustomVertex. PositionNormalColored),      0, shMesh. NumberVertices);  
  47.                    for (int x=0; x <(Dest. GetLength (0)); x ++)  
  48.                    {  
  49.                          Dest [x] .Color = Color. FromArgb (Alfa, 255, 0, 0) .ToArgb ();  
  50.                   }  
  51.                    shMesh. UnlockVertexBuffer ();  
  52.                    pos4=new Position ();  
  53.                    orient4=new Orientation ();  
  54.                    size4=new Size ();  
  55.                    pos4.x=0.0f; pos4.y =-30.0f; pos4.z =-10.0f;  
  56.                    orient4.x=0.0f; orient4.y=1.0f; orient4.z=0.0f; orient4.angle=0.75f;  
  57.                    size4.width=20.0f; size4.height=40.0f; size4.depth=20.0f;  
  58.              
  59.        OTH1 = new TextHyp (device, tmp,    "HypText1", pos4, orient4, size4, k, 1, AlfE);  
  60.             } 
1.2 Drawing the shape

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

Its contents is resulted further:

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

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 inTranslator - activization of the menu is carried out;
  2. Navigation in 3D space for achoice of the button connected with necessary function is carried out:
  3. Click by the button of the mouseon the active button is carried out.

For this purpose the following functions are used:

  1. public void MenuItem_GetFocus (string msg1, string msg2)   
  2.              {...}  
  3. public void MenuItem_LostFocus (string msg1, string msg2)  
  4.              {...}  
  5.   
  6. public void MenuItem_Click (string msg1, string msg2)  
  7.              {...}    
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.

  1. using System;  
  2. using System. Collections;  
  3. using System. Drawing;  
  4. using Microsoft. DirectX;  
  5. using D3D = Microsoft. DirectX.Direct3D;  
  6. using System. Windows. Forms;   
  7.   
  8. namespace Code9  
  9. {  
  10.        public class TextHyp: IEnumerable  
  11.        {  
  12.              private D3D.Device device;  
  13.              private D3D.Mesh meTex = null;  
  14.              private TexCursor1 curr1;  
  15.              public int CursorPos = 0;  
  16.              public Position pos;  
  17.              public Orientation orient;  
  18.              public Size size;  
  19.              public float diag;  
  20.              public float d2;  
  21.              public float d4;  
  22.              public string str;  
  23.              public string tex;  
  24.              public int LengthTex;  
  25.              private float angle=0.0f;  
  26.              private float x;  
  27.              private float y;  
  28.              private float z;  
  29.              public Chars CharList;  
  30.              public Chars BeginBuffer;  
  31.              public Chars EndBuffer;  
  32.              public LineTextCyl LT1;  
  33.              public Letters MeshList;  
  34.              public int k;  
  35.              public int m;  
  36.              public int mk;  
  37.              public int ln;  
  38.              public int [] llt;  
  39.              public int [] mlt;  
  40.              public Scroller s;  
  41.              public bool InFocus;  
  42.              public bool OnPressed;  
  43.              public bool OnChange;  
  44.              public string StatStr;  
  45.              public Size SLetter;  
  46.              public Components NentsTexIns; 
3.1 TextHyp - The constructor of a class

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

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

3.2 TextHyp - Drawing

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

  1. public void Draw (D3D.Device device, Position pos, Orientation orient, Size size, int k, float blength, Chars CharList, Letters MeshList)     
  2.              {...}
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

  1. using System;  
  2. using System. IO;  
  3. using System. Drawing;  
  4. using System. Collections;  
  5. using System. ComponentModel;  
  6. using System. Windows. Forms;  
  7. using System. Data;  
  8. using Microsoft. DirectX;  
  9. using D3D=Microsoft. DirectX.Direct3D;  
  10. using Microsoft. DirectX.DirectInput;  
  11.   
  12. namespace Code9 // Teditor  
  13. {  
  14.        public struct Position  
  15.        {    public float x;  
  16.              public float y;  
  17.              public float z;}  
  18.        public struct Size  
  19.        {  
  20.              public float width;  
  21.              public float height;  
  22.              public float depth;  
  23.       }  
  24.        public struct Orientation  
  25.        {  
  26.                    public float x;  
  27.              public float y;  
  28.              public float z;  
  29.              public float angle;  
  30.       }  
  31.        public class Form1: System. Windows. Forms. Form  
  32.        {  
  33. *if (! USE_THREADS)  
  34.              private bool running = true;  
  35. *endif  
  36.              pr<  
  37. ivate D3D.Device device=null;  
  38.   
  39.              private Device deviceM=null;  
  40.              private Device deviceK=null;  
  41.              private D3D.Mesh mesh1=null;  
  42.              private ShapeBoxT shape1;  
  43.              public string type;  
  44.              private Shapes shapes1;  
  45.              private Components components1;  
  46.              public LineTextCyl LTC1;  
  47.              public int LengTex;  
  48.              public int k=10;  
  49.              public int l;  
  50.              public int m=0;  
  51.              public AlfabitEn AlfE;  
  52.              public AlfabitRu AlfR;  
  53.              public int ia=0;  
  54.              public float buttlen=0.0f;  
  55.              public Chars CharList;  
  56.              public Letters MeshList;  
  57.              public Arrow arr1;  
  58.              public float Nz, Nx, Ny, Mz, My, Mx;  
  59.              public float sX;  
  60.              public float sY;  
  61.              public float sZ;  
  62.              public float Xold=0.0f;  
  63.              public float Yold=0.0f;  
  64.              public float Zold=0.0f;  
  65.              public float sXold;  
  66.              public float sYold;  
  67.              public float sZold;  
  68.              public bool drag=false;  
  69.              public float scale1;  
  70.              string msg;  
  71.              public Position dpos;  
  72.              public Size dsize;  
  73.              private float angle=0.0f;  
  74.              private float x1=2.0f;  
  75.              private float y1=2.0f;  
  76.              private float z1=2.0f;  
  77.              private float x2=2.0f;  
  78.              private float y2=2.0f;  
  79.              private float z2=2.0f;  
  80.              private Position pos1;  
  81.              private Size size1;  
  82.              private Orientation orient1;  
  83.              private Position pos2;  
  84.              private Orientation orient2;  
  85.              private Size size2;  
  86.              private string type1;  
  87.              public MenuHypButton MHB1;  
  88.              public MenuLevel ML1;  
  89.              public Chars TexBuffer;  
  90.              public int beginTexBuffer=0;  
  91.              public int lengthTexBuffer=0;  
  92.              public bool ins=false;  
  93.              string msg1 = "";  
  94.              string msg2 = " ";  
  95.              private System. Windows. Forms. MainMenu mainMenu1;  
  96.              private System. Windows. Forms. MenuItem menuItem1;  
  97.              ...  
  98.   
  99.             private System. ComponentModel. Container components = null
4.1 Application Teditor - the constructor

The constructor of the given class looks so: 

  1. public Form1()  
  2. {  
  3.     InitializeComponent();  
  4.     TexBuffer = new Chars();  
  5.     this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);  
  6.     this.KeyDown + = new KeyEventHandler(OnKeyDown);  
  7.     this.MouseUp + = new MouseEventHandler(OnMouseUp);  
  8.     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 ().
  1. protected override void OnPaint (System. Windows. Forms. PaintEventArgs e)  
  2.              {...} 

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 C# Corner. 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.


Similar Articles