Text manipulation

Started by
4 comments, last by marshdabeachy 17 years, 2 months ago
I've just written my own input component in DirectX which replicates a lot of the functionality of the respective Windows component using ID3DXFont. The only thing it's missing is text selection... is there an effective way to handle text selection? Also, for the blinking position cursor I've just been using the character "|" and hiding/showing it, which looks fine on the end of the text, but in the middle of the text it offsets everything after it to the right. Is there a way to get the blinking position cursor, or will I have to resort to some kind of workaround? Thanks, Marshall
Advertisement
Don't insert the "|" character into the text itself, but rather render it on top the text (between the spaces). It's easiest to do this with a monospaced font.
NextWar: The Quest for Earth available now for Windows Phone 7.
Even without a monospaced font, it's doable. Assuming you know the width of each character, and assuming the height of the tallest character is 1.0 (these depend on the way that you render your text), you can simply add up all the character's widths up until your cursor position and store that into some variable, x. Then, draw a line from (x, 0.0f) to (x, 1.0f). Ta-da!: a cursor, smack dab in the middle of any two characters, be the font fixed-width or not.

As far as text selection, as far as I've been able to surmise, you'll need the following things:
1) A pair of indices, (firstCharSelected, lastCharSelected), or something of the sort to store what, exactly, is selected.
2) Some way to handle mouse clicks, which will, depending on the projection used, involve either a straight mapping or a(n un)projection.
3) Working the selection into the keydown handling, so that typed characters replace whatever is selected, and delete works, and backspace works, and...

Best of luck!
-jouley
I'm not using a monospaced font, so that makes things a bit more difficult. Is there a way to get individual character widths based on the font face and size I'm using?

As far as text selection goes, I understand what I have to do in principle, but is there a way to actually *select* the text short of drawing a box behind the text and inverting the text color myself?
RE: Your first question: someone more versed in DX will have to fill you in on that one.
Quote:Original post by marshdabeachy
As far as text selection goes, I understand what I have to do in principle, but is there a way to actually *select* the text short of drawing a box behind the text and inverting the text color myself?

You can choose to represent selected text however you want, be it simply changing the text color, background color, font, size, or even the style (underline, italics, etc.). The best way to do this depends on both your personal preference, and the answer to your first question I couldn't answer. [wink] Your proposed method is certainly valid, and shouldn't cause too much strain on your rendering.

Give it a go, and see how it behaves!
-jouley
Alright, I was hoping there was a magic function to handle text selection, but I guess I'll get my hands dirty again. Thanks.

Anyone know about grabbing individual character widths?

This topic is closed to new replies.

Advertisement