Sign in to follow this  

ID3DXFont- textbox cursor won't go after a space

This topic is 4354 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a simple textbox class with a cursor etc. using ID3DXFont and ID3DXSprite, but for some reason, whenever I try to get the cursor positioned after a space, it goes to another character. For example, if I had the phrase "It is a croc" in the textbox, and I tried to get the cursor positioned before the "i" in "is", the cursor would either go after the "i" in "is" or after the "t" in "It". The code to process a click (position is the position of the textbox, buffer is the space between the edge of the textbox and the text): (note that whenever it has the cursor after the "i" in "is" it says cursorPos is 3, whereas it should say 4)
cursorPos=atext->GetLength();
int width=0;
int bwidth=0;
RECT rect;
char* text=atext->GetText();
int i=0;
if(position.x+buffer>ui->GetMousePosition().x)i=atext->GetLength();
for(;i<atext->GetLength();i++)  {
rect.left=rect.right=rect.top=rect.bottom=0;
((*ui->GetFont()))->DrawTextA((*ui->GetSprite()),text,i,&rect,DT_CALCRECT,0);
width=rect.right-rect.left;
((*ui->GetFont()))->DrawTextA((*ui->GetSprite()),text,i+1,&rect,DT_CALCRECT,0);
bwidth=rect.right-rect.left;
if(abs(ui->GetMousePosition().x-(position.x+buffer+width)) < abs(ui->GetMousePosition().x-(position.x+buffer+bwidth)))  {
cursorPos=i;i=atext->GetLength();
}
}



The code to render the cursor:
D3DSURFACE_DESC desc;
cursor->GetLevelDesc(0,&desc);
char* text=atext->GetText();
int width=0;
RECT brect;
if(cursorPos)  {
(*font)->DrawTextA((*sprite),text,cursorPos,&brect,DT_CALCRECT,0);
width=brect.right-brect.left;
}
D3DXVECTOR3 pos=D3DXVECTOR3(position.x+buffer+width,position.y+(((bottomRight.y-position.y)-desc.Height)/2),0);
rect.right=1;
rect.bottom=desc.Height;
(*sprite)->Draw(cursor,&rect,0,&pos,0xFFFFFFFF);



If you need any more information, please tell me. I've been trying to fix this for a couple of days and I just can't figure it out.

Share this post


Link to post
Share on other sites
It is doing this because when the last character in a string is " " (a space), ID3DXFont::DrawText() doesn't render the space, and therefore DT_CALRECT does not calulate the space's width. Is there any way I can dynamically find the width of a space in a ID3DXFont?

Share this post


Link to post
Share on other sites
Possibly, but I never found a quick and easy way. 2 spaces seems to be rendered smaller than 1 space * 2 for non-fixed fonts, so I'm not sure it's possible.

Anyways, I simply checked if the last char was a space in the string and replaced it with another character for the calcrect. It wasn't exactly right, but it was certainly good enough. You could probably append a known width char and subtract that to get something better, but is the work worth it?

*shrug*

Share this post


Link to post
Share on other sites

This topic is 4354 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this