Question

I am trying to create a fullscreen minimalistic text editor (by minimalistic I mean having only a blinking caret, black background, keyboard control by arrows to move around the text, backspace delete, etc. and that's it). I have created a RenderWindow, I have a global string buffer for text and I am using String2D class to visualize it on screen(I am developing it in C# using VC2010 and SFML .NET package).

Basicaly what I do is catching keystrokes, checking if they come from letters, digits or any other written symbols useful in writting and add or subtract them from the string buffer which is permanently visualised on-screen using RenderWindow's draw.

Now the problem is, how can I implement the correct caret movement controlled by arrows. If somebody would like to move around the whole screen of text using down/up/left/right arrow. For left/right movement in one line of text it is easy, might implement a counter to know after which character should I reneder caret. Nevertheless, eventualy I am gonna have to break lines where screen ends and what then, how can I correctly "discover" where to render the caret if somebody wants to go line down or up and how to know which position it is in string because somebody could add or delete a portion of text afterwards. Or maybe my approach is completely wrong ? Please advise.

Was it helpful?

Solution

Store an additional list of Line objects that hold indexes into your main string buffer. Every time a newline is added to the buffer update your current Line object and add a new one for the new line.

To move the caret around just find the current line and move up or down a line as necessary.

A different approach might be to store each line in a separate string but this adds some complexity when doing operations which span lines.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top