From 6e89cae675921d488f1eb5c5445ee22d3514756e Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 4 Oct 2024 16:58:35 -0700 Subject: [PATCH] showfont: fixed bugs introduced by splitting out the editbox --- examples/editbox.c | 16 ++++++++-------- examples/showfont.c | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/examples/editbox.c b/examples/editbox.c index 15d3a27d..2d8ec847 100644 --- a/examples/editbox.c +++ b/examples/editbox.c @@ -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; } } @@ -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); @@ -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; diff --git a/examples/showfont.c b/examples/showfont.c index 20e0c837..3470b386 100644 --- a/examples/showfont.c +++ b/examples/showfont.c @@ -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: @@ -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; } @@ -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++) { @@ -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;