Refactor layout code, rescale and reformat when changing level

This commit is contained in:
David Boddie
2019-07-18 18:35:04 +02:00
parent f6d4ca0387
commit 6eb63f6a4c
6 changed files with 72 additions and 48 deletions

View File

@ -262,6 +262,8 @@ set_level_from_modifiers (EekKeyboard *self, EekKey *key)
priv->old_level = level;
eek_element_set_level (EEK_ELEMENT(self), level);
eek_layout_update_layout(self);
}
static void
@ -528,6 +530,7 @@ eek_keyboard_init (EekKeyboard *self)
self->priv->outline_array = g_array_new (FALSE, TRUE, sizeof (EekOutline));
self->priv->keycodes = g_hash_table_new (g_direct_hash, g_direct_equal);
eek_element_set_symbol_index (EEK_ELEMENT(self), 0, 0);
self->scale = 1.0;
}
/**

View File

@ -62,6 +62,7 @@ struct _EekKeyboard
struct xkb_keymap *keymap;
int keymap_fd; // keymap formatted as XKB string
size_t keymap_len; // length of the data inside keymap_fd
double scale;
EekboardContextService *manager; // unowned reference
};

View File

@ -128,3 +128,61 @@ eek_layout_place_sections(EekKeyboard *keyboard)
keyboard_bounds.height = total_height;
eek_element_set_bounds(EEK_ELEMENT(keyboard), &keyboard_bounds);
}
static void scale_bounds_callback (EekElement *element,
gpointer user_data);
static void
scale_bounds (EekElement *element,
gdouble scale)
{
EekBounds bounds;
eek_element_get_bounds (element, &bounds);
bounds.x *= scale;
bounds.y *= scale;
bounds.width *= scale;
bounds.height *= scale;
eek_element_set_bounds (element, &bounds);
if (EEK_IS_CONTAINER(element))
eek_container_foreach_child (EEK_CONTAINER(element),
scale_bounds_callback,
&scale);
}
static void
scale_bounds_callback (EekElement *element,
gpointer user_data)
{
scale_bounds (element, *(gdouble *)user_data);
}
void
eek_layout_scale_keyboard(EekKeyboard *keyboard, gdouble scale)
{
gsize n_outlines;
scale_bounds (EEK_ELEMENT(keyboard), scale);
n_outlines = eek_keyboard_get_n_outlines (keyboard);
for (guint i = 0; i < n_outlines; i++) {
EekOutline *outline = eek_keyboard_get_outline (keyboard, i);
gint j;
for (j = 0; j < outline->num_points; j++) {
outline->points[j].x *= scale;
outline->points[j].y *= scale;
}
}
keyboard->scale = scale;
}
void
eek_layout_update_layout(EekKeyboard *keyboard)
{
eek_layout_scale_keyboard(keyboard, 1.0 / keyboard->scale);
eek_layout_place_sections(keyboard);
eek_layout_scale_keyboard(keyboard, 1.0 / keyboard->scale);
}

View File

@ -57,5 +57,9 @@ GType eek_layout_get_type (void) G_GNUC_CONST;
void eek_layout_place_sections(EekKeyboard *keyboard);
void eek_layout_scale_keyboard(EekKeyboard *keyboard, gdouble scale);
void eek_layout_update_layout(EekKeyboard *keyboard);
G_END_DECLS
#endif /* EEK_LAYOUT_H */

View File

@ -943,7 +943,9 @@ eek_xml_layout_real_create_keyboard (EekboardContextService *manager,
return NULL;
}
/* Fit keyboard in the given width and hight. */
eek_layout_place_sections(keyboard);
/* Fit keyboard in the given width and height. */
scale_keyboard (keyboard, initial_width, initial_height);
/* Use pre-defined modifier mask here. */
@ -1277,9 +1279,6 @@ parse_symbols (const gchar *path, EekKeyboard *keyboard, GError **error)
return FALSE;
}
symbols_parse_data_free (data);
eek_layout_place_sections(keyboard);
return TRUE;
}
@ -1358,43 +1357,12 @@ parse_keyboards (const gchar *path, GError **error)
return keyboards;
}
static void scale_bounds_callback (EekElement *element,
gpointer user_data);
static void
scale_bounds (EekElement *element,
gdouble scale)
{
EekBounds bounds;
eek_element_get_bounds (element, &bounds);
bounds.x *= scale;
bounds.y *= scale;
bounds.width *= scale;
bounds.height *= scale;
eek_element_set_bounds (element, &bounds);
if (EEK_IS_CONTAINER(element))
eek_container_foreach_child (EEK_CONTAINER(element),
scale_bounds_callback,
&scale);
}
static void
scale_bounds_callback (EekElement *element,
gpointer user_data)
{
scale_bounds (element, *(gdouble *)user_data);
}
static void scale_keyboard (EekKeyboard *keyboard,
gdouble width,
gdouble height)
{
gdouble scale;
EekBounds bounds;
gsize n_outlines;
guint i;
eek_element_get_bounds (EEK_ELEMENT(keyboard), &bounds);
@ -1403,18 +1371,7 @@ static void scale_keyboard (EekKeyboard *keyboard,
else
scale = height / bounds.height;
scale_bounds (EEK_ELEMENT(keyboard), scale);
n_outlines = eek_keyboard_get_n_outlines (keyboard);
for (i = 0; i < n_outlines; i++) {
EekOutline *outline = eek_keyboard_get_outline (keyboard, i);
gint j;
for (j = 0; j < outline->num_points; j++) {
outline->points[j].x *= scale;
outline->points[j].y *= scale;
}
}
eek_layout_scale_keyboard(keyboard, scale);
}
static gboolean