Skip to content

Commit

Permalink
showfont: fixed bugs introduced by splitting out the editbox
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 4, 2024
1 parent 62eab79 commit 6e89cae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
16 changes: 8 additions & 8 deletions examples/editbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static bool GetHighlightExtents(EditBox *edit, int *marker1, int *marker2)
if (edit->highlight1 >= 0 && edit->highlight2 >= 0) {
*marker1 = SDL_min(edit->highlight1, edit->highlight2);
*marker2 = SDL_max(edit->highlight1, edit->highlight2) - 1;
if (*marker2 > *marker1) {
if (*marker2 >= *marker1) {
return true;
}
}
Expand Down Expand Up @@ -96,14 +96,14 @@ void EditBox_Draw(EditBox *edit, SDL_Renderer *renderer)
TTF_SubString cursor;
if (edit->cursor_visible && TTF_GetTextSubString(edit->text, edit->cursor, &cursor)) {
SDL_FRect cursorRect;

SDL_RectToFRect(&cursor.rect, &cursorRect);
if (TTF_GetFontDirection(edit->font) == TTF_DIRECTION_RTL) {
cursorRect.x = x + cursor.rect.x + cursor.rect.w;
} else {
cursorRect.x = x + cursor.rect.x;
cursorRect.x += cursor.rect.w;
}
cursorRect.y = y + cursor.rect.y;
cursorRect.x += x;
cursorRect.y += y;
cursorRect.w = 1.0f;
cursorRect.h = (float)cursor.rect.h;

SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
SDL_RenderFillRect(renderer, &cursorRect);
Expand Down Expand Up @@ -357,8 +357,8 @@ static bool HandleMouseMotion(EditBox *edit, float x, float y)

/* Set the highlight position */
TTF_SubString substring;
int textX = (int)SDL_roundf(x - (edit->rect.x + 4.0f));
int textY = (int)SDL_roundf(y - (edit->rect.y + 4.0f));
int textX = (int)SDL_roundf(x - edit->rect.x);
int textY = (int)SDL_roundf(y - edit->rect.y);
if (!TTF_GetTextSubStringForPoint(edit->text, textX, textY, &substring)) {
SDL_Log("Couldn't get cursor location: %s\n", SDL_GetError());
return false;
Expand Down
17 changes: 16 additions & 1 deletion examples/showfont.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ static void SetTextFocus(Scene *scene, bool focused)
static void HandleKeyDown(Scene *scene, SDL_Event *event)
{
int style, outline;
float ptsize;

switch (event->key.key) {
case SDLK_A:
Expand Down Expand Up @@ -211,6 +212,19 @@ static void HandleKeyDown(Scene *scene, SDL_Event *event)
}
TTF_SetFontStyle(scene->font, style);
break;

case SDLK_UP:
/* Increase font size */
ptsize = TTF_GetFontSize(scene->font);
TTF_SetFontSize(scene->font, ptsize + 1.0f);
break;

case SDLK_DOWN:
/* Decrease font size */
ptsize = TTF_GetFontSize(scene->font);
TTF_SetFontSize(scene->font, ptsize - 1.0f);
break;

default:
break;
}
Expand Down Expand Up @@ -392,6 +406,7 @@ int main(int argc, char *argv[])
TTF_SetFontKerning(font, kerning);
TTF_SetFontHinting(font, hinting);
TTF_SetFontWrapAlignment(font, align);
scene.font = font;

if(dump) {
for(i = 48; i < 123; i++) {
Expand Down Expand Up @@ -566,7 +581,7 @@ int main(int argc, char *argv[])
} else if (scene.textFocus) {
EditBox_HandleEvent(scene.edit, &event);
} else {
HandleKeyDown(&scene, &event);
HandleKeyDown HandleKeyDown(&scene, &event);
}
break;

Expand Down

0 comments on commit 6e89cae

Please sign in to comment.