Better onscreen keyboard button sizing

No longer restricted to "the same as other UI buttons"
(e.g., 48x48 default) or "1/4th that size" (e.g., 24x24).
Will scale, allowing for larger buttons when appropriate.
h/t Anat Caspi for pointing out the continuing issue.

Closes https://sourceforge.net/p/tuxpaint/feature-requests/212/
This commit is contained in:
Bill Kendrick 2022-01-25 00:52:14 -08:00
parent 3b027b9a4c
commit 3518433808
4 changed files with 121 additions and 109 deletions

View file

@ -42,6 +42,17 @@ http://www.tuxpaint.org/
well as a non-skewed starting angle for octagons (22.5 degrees). well as a non-skewed starting angle for octagons (22.5 degrees).
Bill Kendrick <bill@newbreedsoftware.com> Bill Kendrick <bill@newbreedsoftware.com>
* Improvements to "Text" & "Label" tools:
---------------------------------------
* Onscreen keyboard button sizes no longer restricted to
"the same size as other UI buttons" or "1/4th the size...",
and will now scale down from the full-size buttons to whatever
allows the full layout to fit within 90% of the canvas width
and 50% of the canvas height (picking the smallest size, to keep
square buttons for letters/numbers/symbols).
+ Closes https://sourceforge.net/p/tuxpaint/feature-requests/212/
(h/t Anat Caspi)
* Localization Updates: * Localization Updates:
--------------------- ---------------------
* Albanian translation * Albanian translation

View file

@ -1,7 +1,7 @@
/* /*
onscreen_keyboard.c onscreen_keyboard.c
Copyright (c) 2020 Copyright (c) 2022
http://www.tuxpaint.org/ http://www.tuxpaint.org/
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
@ -22,6 +22,16 @@
#include "onscreen_keyboard.h" #include "onscreen_keyboard.h"
#include "SDL_rotozoom.h"
#include "SDL_gfxBlitFunc.h"
#if !defined(_SDL_rotozoom_h) || !defined(_SDL_gfxBlitFunc_h)
#error "---------------------------------------------------"
#error "If you installed SDL_gfx from a package, be sure"
#error "to get the development package, as well!"
#error "(e.g., 'libsdl-gfx1.2-devel.rpm')"
#error "---------------------------------------------------"
#endif
//#define DEBUG_OSK_COMPOSEMAP //#define DEBUG_OSK_COMPOSEMAP
static SDL_Color def_bgcolor = { 255, 255, 255, 255 }; static SDL_Color def_bgcolor = { 255, 255, 255, 255 };
@ -57,16 +67,11 @@ static void print_composemap(osk_composenode * composemap, char *sp);
#endif #endif
struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas, struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas,
SDL_Surface * LG_button_up, SDL_Surface * LG_button_down, SDL_Surface * BLANK_button_up, SDL_Surface * BLANK_button_down,
SDL_Surface * LG_button_off, SDL_Surface * LG_button_nav, SDL_Surface * BLANK_button_off, SDL_Surface * BLANK_button_nav,
SDL_Surface * LG_button_hold, SDL_Surface * BLANK_button_hold,
SDL_Surface * LG_oskdel, SDL_Surface * LG_osktab, SDL_Surface * LG_oskenter, SDL_Surface * BLANK_oskdel, SDL_Surface * BLANK_osktab, SDL_Surface * BLANK_oskenter,
SDL_Surface * LG_oskcapslock, SDL_Surface * LG_oskshift, SDL_Surface * BLANK_oskcapslock, SDL_Surface * BLANK_oskshift,
SDL_Surface * SM_button_up, SDL_Surface * SM_button_down,
SDL_Surface * SM_button_off, SDL_Surface * SM_button_nav,
SDL_Surface * SM_button_hold,
SDL_Surface * SM_oskdel, SDL_Surface * SM_osktab, SDL_Surface * SM_oskenter,
SDL_Surface * SM_oskcapslock, SDL_Surface * SM_oskshift,
int disable_change) int disable_change)
{ {
SDL_Surface * surface; SDL_Surface * surface;
@ -77,6 +82,7 @@ struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas,
SDL_Surface * oskcapslock, * oskshift; SDL_Surface * oskcapslock, * oskshift;
osk_layout * layout; osk_layout * layout;
on_screen_keyboard * keyboard; on_screen_keyboard * keyboard;
int layout_avail_width, layout_avail_height;
keyboard = malloc(sizeof(on_screen_keyboard)); keyboard = malloc(sizeof(on_screen_keyboard));
@ -100,30 +106,66 @@ struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas,
printf("w %i, h %i\n", layout->width, layout->height); printf("w %i, h %i\n", layout->width, layout->height);
#endif #endif
if (layout->width * LG_button_up->w >= (canvas->w - 48 * 4) * 0.9 || layout_avail_width = (canvas->w * 0.9);
layout->height * LG_button_up->h >= canvas->h * 0.5) { layout_avail_height = (canvas->h * 0.5);
/* Full-size buttons too large, use small buttons */
button_up = SM_button_up; if (layout->width * BLANK_button_up->w >= layout_avail_width || /* Don't allow it to be > 90% of the width of the canvas */
button_down = SM_button_down; layout->height * BLANK_button_up->h >= layout_avail_height /* Don't allow it to be > 50% of the height of the canvas */) {
button_off = SM_button_off; /* Full-size buttons too large, resize to fit */
button_nav = SM_button_nav; float max_w, max_h;
button_hold = SM_button_hold; float scale_w, scale_h;
oskdel = SM_oskdel;
osktab = SM_osktab; #ifdef DEBUG
oskenter = SM_oskenter; printf("%d x %d layout of %d x %d buttons won't fit within %d x %d pixel area...\n",
oskcapslock = SM_oskcapslock; layout->width, layout->height,
oskshift = SM_oskshift; BLANK_button_up->w, BLANK_button_up->h,
layout_avail_width, layout_avail_height);
#endif
max_w = (float) layout_avail_width / (float) layout->width;
max_h = (float) layout_avail_height / (float) layout->height;
#ifdef DEBUG
printf("...want (%d / %d) x (%d x %d) = %.2f x %.2f buttons...\n",
layout_avail_width, layout->width,
layout_avail_height, layout->height,
max_w, max_h);
#endif
if (max_w > max_h)
max_w = max_h;
if (max_h > max_w)
max_h = max_w;
scale_w = (float) max_w / (float) BLANK_button_up->w;
scale_h = (float) max_h / (float) BLANK_button_up->h;
#ifdef DEBUG
printf("...so scaling by w=%.2f & h=%.2f\n",
scale_w, scale_h);
#endif
button_up = zoomSurface(BLANK_button_up, scale_w, scale_h, 1);
button_down = zoomSurface(BLANK_button_down, scale_w, scale_h, 1);
button_off = zoomSurface(BLANK_button_off, scale_w, scale_h, 1);
button_nav = zoomSurface(BLANK_button_nav, scale_w, scale_h, 1);
button_hold = zoomSurface(BLANK_button_hold, scale_w, scale_h, 1);
oskdel = zoomSurface(BLANK_oskdel, scale_w, scale_h, 1);
osktab = zoomSurface(BLANK_osktab, scale_w, scale_h, 1);
oskenter = zoomSurface(BLANK_oskenter, scale_w, scale_h, 1);
oskcapslock = zoomSurface(BLANK_oskcapslock, scale_w, scale_h, 1);
oskshift = zoomSurface(BLANK_oskshift, scale_w, scale_h, 1);
} else { } else {
button_up = LG_button_up; button_up = SDL_DisplayFormatAlpha(BLANK_button_up);
button_down = LG_button_down; button_down = SDL_DisplayFormatAlpha(BLANK_button_down);
button_off = LG_button_off; button_off = SDL_DisplayFormatAlpha(BLANK_button_off);
button_nav = LG_button_nav; button_nav = SDL_DisplayFormatAlpha(BLANK_button_nav);
button_hold = LG_button_hold; button_hold = SDL_DisplayFormatAlpha(BLANK_button_hold);
oskdel = LG_oskdel; oskdel = SDL_DisplayFormatAlpha(BLANK_oskdel);
osktab = LG_osktab; osktab = SDL_DisplayFormatAlpha(BLANK_osktab);
oskenter = LG_oskenter; oskenter = SDL_DisplayFormatAlpha(BLANK_oskenter);
oskcapslock = LG_oskcapslock; oskcapslock = SDL_DisplayFormatAlpha(BLANK_oskcapslock);
oskshift = LG_oskshift; oskshift = SDL_DisplayFormatAlpha(BLANK_oskshift);
} }
surface = SDL_CreateRGBSurface(canvas->flags, surface = SDL_CreateRGBSurface(canvas->flags,
@ -171,26 +213,16 @@ struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas,
keyboard->kmdf.dead3 = NULL; keyboard->kmdf.dead3 = NULL;
keyboard->kmdf.dead4 = NULL; keyboard->kmdf.dead4 = NULL;
keyboard->LG_button_up = LG_button_up; keyboard->BLANK_button_up = BLANK_button_up;
keyboard->LG_button_down = LG_button_down; keyboard->BLANK_button_down = BLANK_button_down;
keyboard->LG_button_off = LG_button_off; keyboard->BLANK_button_off = BLANK_button_off;
keyboard->LG_button_nav = LG_button_nav; keyboard->BLANK_button_nav = BLANK_button_nav;
keyboard->LG_button_hold = LG_button_hold; keyboard->BLANK_button_hold = BLANK_button_hold;
keyboard->LG_oskdel = LG_oskdel; keyboard->BLANK_oskdel = BLANK_oskdel;
keyboard->LG_osktab = LG_osktab; keyboard->BLANK_osktab = BLANK_osktab;
keyboard->LG_oskenter = LG_oskenter; keyboard->BLANK_oskenter = BLANK_oskenter;
keyboard->LG_oskcapslock = LG_oskcapslock; keyboard->BLANK_oskcapslock = BLANK_oskcapslock;
keyboard->LG_oskshift = LG_oskshift; keyboard->BLANK_oskshift = BLANK_oskshift;
keyboard->SM_button_up = SM_button_up;
keyboard->SM_button_down = SM_button_down;
keyboard->SM_button_off = SM_button_off;
keyboard->SM_button_nav = SM_button_nav;
keyboard->SM_button_hold = SM_button_hold;
keyboard->SM_oskdel = SM_oskdel;
keyboard->SM_osktab = SM_osktab;
keyboard->SM_oskenter = SM_oskenter;
keyboard->SM_oskcapslock = SM_oskcapslock;
keyboard->SM_oskshift = SM_oskshift;
SDL_FillRect(surface, NULL, SDL_FillRect(surface, NULL,
SDL_MapRGB(surface->format, keyboard->layout->bgcolor.r, keyboard->layout->bgcolor.g, SDL_MapRGB(surface->format, keyboard->layout->bgcolor.r, keyboard->layout->bgcolor.g,
@ -1725,18 +1757,12 @@ struct osk_keyboard *osk_clicked(on_screen_keyboard * keyboard, int x, int y)
new_keyboard = new_keyboard =
osk_create(name, keyboard->canvas_ptr, osk_create(name, keyboard->canvas_ptr,
keyboard->LG_button_up, keyboard->LG_button_down, keyboard->BLANK_button_up, keyboard->BLANK_button_down,
keyboard->LG_button_off, keyboard->LG_button_nav, keyboard->BLANK_button_off, keyboard->BLANK_button_nav,
keyboard->LG_button_hold, keyboard->BLANK_button_hold,
keyboard->LG_oskdel, keyboard->LG_osktab, keyboard->BLANK_oskdel, keyboard->BLANK_osktab,
keyboard->LG_oskenter, keyboard->LG_oskcapslock, keyboard->BLANK_oskenter, keyboard->BLANK_oskcapslock,
keyboard->LG_oskshift, keyboard->BLANK_oskshift,
keyboard->SM_button_up, keyboard->SM_button_down,
keyboard->SM_button_off, keyboard->SM_button_nav,
keyboard->SM_button_hold,
keyboard->SM_oskdel, keyboard->SM_osktab,
keyboard->SM_oskenter, keyboard->SM_oskcapslock,
keyboard->SM_oskshift,
keyboard->disable_change); keyboard->disable_change);
free(aux_list_ptr); free(aux_list_ptr);

View file

@ -135,39 +135,24 @@ typedef struct osk_keyboard
osk_key *last_key_pressed; /* The last key pressed */ osk_key *last_key_pressed; /* The last key pressed */
SDL_Surface * canvas_ptr; /* Canvas drawing surface, for bpp and sizing needs when cycling through keyboard layouts */ SDL_Surface * canvas_ptr; /* Canvas drawing surface, for bpp and sizing needs when cycling through keyboard layouts */
/* Large and small buttons, to pass back to osk_create() when cycling through keyboard layouts */ /* Large and small buttons, to pass back to osk_create() when cycling through keyboard layouts */
SDL_Surface *LG_button_up; SDL_Surface *BLANK_button_up;
SDL_Surface *LG_button_down; SDL_Surface *BLANK_button_down;
SDL_Surface *LG_button_off; SDL_Surface *BLANK_button_off;
SDL_Surface *LG_button_nav; SDL_Surface *BLANK_button_nav;
SDL_Surface *LG_button_hold; SDL_Surface *BLANK_button_hold;
SDL_Surface *LG_oskdel; SDL_Surface *BLANK_oskdel;
SDL_Surface *LG_osktab; SDL_Surface *BLANK_osktab;
SDL_Surface *LG_oskenter; SDL_Surface *BLANK_oskenter;
SDL_Surface *LG_oskcapslock; SDL_Surface *BLANK_oskcapslock;
SDL_Surface *LG_oskshift; SDL_Surface *BLANK_oskshift;
SDL_Surface *SM_button_up;
SDL_Surface *SM_button_down;
SDL_Surface *SM_button_off;
SDL_Surface *SM_button_nav;
SDL_Surface *SM_button_hold;
SDL_Surface *SM_oskdel;
SDL_Surface *SM_osktab;
SDL_Surface *SM_oskenter;
SDL_Surface *SM_oskcapslock;
SDL_Surface *SM_oskshift;
} on_screen_keyboard; } on_screen_keyboard;
struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas, struct osk_keyboard *osk_create(char * layout_name, SDL_Surface * canvas,
SDL_Surface * LG_button_up, SDL_Surface * LG_button_down, SDL_Surface * BLANK_button_up, SDL_Surface * BLANK_button_down,
SDL_Surface * LG_button_off, SDL_Surface * LG_button_nav, SDL_Surface * BLANK_button_off, SDL_Surface * BLANK_button_nav,
SDL_Surface * LG_button_hold, SDL_Surface * BLANK_button_hold,
SDL_Surface * LG_oskdel, SDL_Surface * LG_osktab, SDL_Surface * LG_oskenter, SDL_Surface * BLANK_oskdel, SDL_Surface * BLANK_osktab, SDL_Surface * BLANK_oskenter,
SDL_Surface * LG_oskcapslock, SDL_Surface * LG_oskshift, SDL_Surface * BLANK_oskcapslock, SDL_Surface * BLANK_oskshift,
SDL_Surface * SM_button_up, SDL_Surface * SM_button_down,
SDL_Surface * SM_button_off, SDL_Surface * SM_button_nav,
SDL_Surface * SM_button_hold,
SDL_Surface * SM_oskdel, SDL_Surface * SM_osktab, SDL_Surface * SM_oskenter,
SDL_Surface * SM_oskcapslock, SDL_Surface * SM_oskshift,
int disable_change); int disable_change);
struct osk_layout *osk_load_layout(char *layout_name); struct osk_layout *osk_load_layout(char *layout_name);

View file

@ -22,7 +22,7 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
(See COPYING.txt) (See COPYING.txt)
June 14, 2002 - January 21, 2022 June 14, 2002 - January 25, 2022
*/ */
#include "platform.h" #include "platform.h"
@ -3252,29 +3252,19 @@ static void mainloop(void)
{ {
if (onscreen_keyboard_layout) if (onscreen_keyboard_layout)
kbd = kbd =
osk_create(onscreen_keyboard_layout, screen, osk_create(onscreen_keyboard_layout, canvas,
img_btn_up, img_btn_down, img_btn_off, img_btn_up, img_btn_down, img_btn_off,
img_btn_nav, img_btn_hold, img_btn_nav, img_btn_hold,
img_oskdel, img_osktab, img_oskenter, img_oskdel, img_osktab, img_oskenter,
img_oskcapslock, img_oskshift, img_oskcapslock, img_oskshift,
img_btnsm_up, img_btnsm_down, img_btnsm_off,
img_btnsm_nav, img_btnsm_hold,
/* FIXME */
img_oskdel, img_osktab, img_oskenter,
img_oskcapslock, img_oskshift,
onscreen_keyboard_disable_change); onscreen_keyboard_disable_change);
else else
kbd = kbd =
osk_create(strdup("default.layout"), screen, osk_create(strdup("default.layout"), canvas,
img_btn_up, img_btn_down, img_btn_off, img_btn_up, img_btn_down, img_btn_off,
img_btn_nav, img_btn_hold, img_btn_nav, img_btn_hold,
img_oskdel, img_osktab, img_oskenter, img_oskdel, img_osktab, img_oskenter,
img_oskcapslock, img_oskshift, img_oskcapslock, img_oskshift,
img_btnsm_up, img_btnsm_down, img_btnsm_off,
img_btnsm_nav, img_btnsm_hold,
/* FIXME */
img_oskdel, img_osktab, img_oskenter,
img_oskcapslock, img_oskshift,
onscreen_keyboard_disable_change); onscreen_keyboard_disable_change);
} }
if (kbd == NULL) if (kbd == NULL)