我正在尝试创建一个全屏简约文本编辑器(简约的意思是只有一个闪烁的插入符号,黑色背景,通过箭头控制键盘在文本中移动,退格删除等,仅此而已)。我创建了一个 RenderWindow,我有一个用于文本的全局字符串缓冲区,并且我正在使用 String2D 类在屏幕上可视化它(我正在使用 VC2010 和 SFML .NET 包)。
基本上我所做的就是捕获击键,检查它们是否来自字母、数字或任何其他对书写有用的书面符号,并从字符串缓冲区中添加或减去它们,该缓冲区使用 RenderWindow 的绘图在屏幕上永久可视化。
现在的问题是,如何实现由箭头控制的正确插入符移动。如果有人想使用下/上/左/右箭头在整个屏幕上移动文本。对于一行文本中的左/右移动很容易,可以实现一个计数器来知道我应该在哪个字符后重新插入插入符。尽管如此,最终我将不得不在屏幕结束的地方换行,然后,如果有人想要向下或向上行,我如何正确地“发现”在哪里呈现插入符,以及如何知道它在字符串中的哪个位置,因为有人之后可以添加或删除一部分文本。或者也许我的方法是完全错误的?请指教。
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.
发布评论
评论(1)
将保存索引的
Line
对象的附加列表存储到主字符串缓冲区中。每次向缓冲区添加换行符时,都会更新当前的 Line 对象并为新行添加一个新对象。要移动插入符号,只需找到当前行并根据需要向上或向下移动一行。
另一种方法可能是将每一行存储在单独的字符串中,但这在执行跨行操作时会增加一些复杂性。
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 currentLine
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.