From 653c598a1ca1ba74b2c0eb74eb2695d7e042124a Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Thu, 30 Jan 2025 10:54:53 +0100 Subject: Refactor: Compute appearance values before onDraw This moves some computations that used to be done during onDraw into the new Theme.Computed class. This also removes Paint objects from the Theme class, making it data-only. This is a requirement for making some keys render differently. --- srcs/juloo.keyboard2/Keyboard2View.java | 78 +++++++++++++-------------------- 1 file changed, 30 insertions(+), 48 deletions(-) (limited to 'srcs/juloo.keyboard2/Keyboard2View.java') diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index 6f6be2a..90bbd00 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -47,6 +47,7 @@ public class Keyboard2View extends View private float _marginBottom; private Theme _theme; + private Theme.Computed _tc; private static RectF _tmpRect = new RectF(); @@ -303,6 +304,7 @@ public class Keyboard2View extends View _marginRight = Math.max(_config.horizontal_margin, insets_right); _marginBottom = _config.margin_bottom + insets_bottom; _keyWidth = (width - _marginLeft - _marginRight) / _keyboard.keysWidth; + _tc = new Theme.Computed(_theme, _config, _keyWidth); } @Override @@ -340,34 +342,27 @@ public class Keyboard2View extends View { // Set keyboard background opacity getBackground().setAlpha(_config.keyboardOpacity); - // Set keys opacity - _theme.keyBgPaint.setAlpha(_config.keyOpacity); - _theme.keyDownBgPaint.setAlpha(_config.keyActivatedOpacity); - _theme.keyBorderPaint.setAlpha(_config.keyOpacity); - float key_vertical_margin = _config.key_vertical_margin * _config.keyHeight; - float key_horizontal_margin = _config.key_horizontal_margin * _keyWidth; - // Add half of the key margin on the left and on the top as it's then added - // on the right and on the bottom of every keys. - float y = _config.marginTop + key_vertical_margin / 2; + float y = _tc.margin_top; for (KeyboardData.Row row : _keyboard.rows) { y += row.shift * _config.keyHeight; - float x = _marginLeft + key_horizontal_margin / 2; - float keyH = row.height * _config.keyHeight - key_vertical_margin; + float x = _marginLeft + _tc.margin_left; + float keyH = row.height * _config.keyHeight - _tc.vertical_margin; for (KeyboardData.Key k : row.keys) { x += k.shift * _keyWidth; - float keyW = _keyWidth * k.width - key_horizontal_margin; + float keyW = _keyWidth * k.width - _tc.horizontal_margin; boolean isKeyDown = _pointers.isKeyDown(k); - drawKeyFrame(canvas, x, y, keyW, keyH, isKeyDown); + Theme.Computed.Key tc_key = isKeyDown ? _tc.key_activated : _tc.key; + drawKeyFrame(canvas, x, y, keyW, keyH, tc_key); if (k.keys[0] != null) - drawLabel(canvas, k.keys[0], keyW / 2f + x, y, keyH, isKeyDown); + drawLabel(canvas, k.keys[0], keyW / 2f + x, y, keyH, isKeyDown, tc_key); for (int i = 1; i < 9; i++) { if (k.keys[i] != null) - drawSubLabel(canvas, k.keys[i], x, y, keyW, keyH, i, isKeyDown); + drawSubLabel(canvas, k.keys[i], x, y, keyW, keyH, i, isKeyDown, tc_key); } - drawIndication(canvas, k, x, y, keyW, keyH); + drawIndication(canvas, k, x, y, keyW, keyH, _tc); x += _keyWidth * k.width; } y += row.height * _config.keyHeight; @@ -382,42 +377,32 @@ public class Keyboard2View extends View /** Draw borders and background of the key. */ void drawKeyFrame(Canvas canvas, float x, float y, float keyW, float keyH, - boolean isKeyDown) + Theme.Computed.Key tc) { - float r = _theme.keyBorderRadius; - if (_config.borderConfig) - r = _config.customBorderRadius * _keyWidth; - float w = (_config.borderConfig) ? _config.customBorderLineWidth : _theme.keyBorderWidth; + float r = tc.border_radius; + float w = tc.border_width; float padding = w / 2.f; - if (isKeyDown) - w = _theme.keyBorderWidthActivated; _tmpRect.set(x + padding, y + padding, x + keyW - padding, y + keyH - padding); - canvas.drawRoundRect(_tmpRect, r, r, - isKeyDown ? _theme.keyDownBgPaint : _theme.keyBgPaint); + canvas.drawRoundRect(_tmpRect, r, r, tc.bg_paint); if (w > 0.f) { - _theme.keyBorderPaint.setStrokeWidth(w); float overlap = r - r * 0.85f + w; // sin(45°) - drawBorder(canvas, x, y, x + overlap, y + keyH, _theme.keyBorderColorLeft); - drawBorder(canvas, x + keyW - overlap, y, x + keyW, y + keyH, _theme.keyBorderColorRight); - drawBorder(canvas, x, y, x + keyW, y + overlap, _theme.keyBorderColorTop); - drawBorder(canvas, x, y + keyH - overlap, x + keyW, y + keyH, _theme.keyBorderColorBottom); + drawBorder(canvas, x, y, x + overlap, y + keyH, tc.border_left_paint, tc); + drawBorder(canvas, x + keyW - overlap, y, x + keyW, y + keyH, tc.border_right_paint, tc); + drawBorder(canvas, x, y, x + keyW, y + overlap, tc.border_top_paint, tc); + drawBorder(canvas, x, y + keyH - overlap, x + keyW, y + keyH, tc.border_bottom_paint, tc); } } /** Clip to draw a border at a time. This allows to call [drawRoundRect] several time with the same parameters but a different Paint. */ void drawBorder(Canvas canvas, float clipl, float clipt, float clipr, - float clipb, int color) + float clipb, Paint paint, Theme.Computed.Key tc) { - Paint p = _theme.keyBorderPaint; - float r = _theme.keyBorderRadius; - if (_config.borderConfig) - r = _config.customBorderRadius * _keyWidth; + float r = tc.border_radius; canvas.save(); canvas.clipRect(clipl, clipt, clipr, clipb); - p.setColor(color); - canvas.drawRoundRect(_tmpRect, r, r, p); + canvas.drawRoundRect(_tmpRect, r, r, paint); canvas.restore(); } @@ -442,21 +427,21 @@ public class Keyboard2View extends View return sublabel ? _theme.subLabelColor : _theme.labelColor; } - private void drawLabel(Canvas canvas, KeyValue kv, float x, float y, float keyH, boolean isKeyDown) + private void drawLabel(Canvas canvas, KeyValue kv, float x, float y, + float keyH, boolean isKeyDown, Theme.Computed.Key tc) { kv = modifyKey(kv, _mods); if (kv == null) return; float textSize = scaleTextSize(kv, _config.labelTextSize, keyH); - Paint p = _theme.labelPaint(kv.hasFlagsAny(KeyValue.FLAG_KEY_FONT)); + Paint p = tc.label_paint(kv.hasFlagsAny(KeyValue.FLAG_KEY_FONT), textSize); p.setColor(labelColor(kv, isKeyDown, false)); - p.setAlpha(_config.labelBrightness); - p.setTextSize(textSize); canvas.drawText(kv.getString(), x, (keyH - p.ascent() - p.descent()) / 2f + y, p); } private void drawSubLabel(Canvas canvas, KeyValue kv, float x, float y, - float keyW, float keyH, int sub_index, boolean isKeyDown) + float keyW, float keyH, int sub_index, boolean isKeyDown, + Theme.Computed.Key tc) { Paint.Align a = LABEL_POSITION_H[sub_index]; Vertical v = LABEL_POSITION_V[sub_index]; @@ -464,10 +449,8 @@ public class Keyboard2View extends View if (kv == null) return; float textSize = scaleTextSize(kv, _config.sublabelTextSize, keyH); - Paint p = _theme.subLabelPaint(kv.hasFlagsAny(KeyValue.FLAG_KEY_FONT), a); + Paint p = tc.sublabel_paint(kv.hasFlagsAny(KeyValue.FLAG_KEY_FONT), textSize, a); p.setColor(labelColor(kv, isKeyDown, true)); - p.setAlpha(_config.labelBrightness); - p.setTextSize(textSize); float subPadding = _config.keyPadding; if (v == Vertical.CENTER) y += (keyH - p.ascent() - p.descent()) / 2f; @@ -486,12 +469,11 @@ public class Keyboard2View extends View } private void drawIndication(Canvas canvas, KeyboardData.Key k, float x, - float y, float keyW, float keyH) + float y, float keyW, float keyH, Theme.Computed tc) { if (k.indication == null || k.indication.equals("")) return; - Paint p = _theme.indicationPaint(false); - p.setColor(_theme.subLabelColor); + Paint p = tc.indication_paint; p.setTextSize(keyH * _config.sublabelTextSize * _config.characterSize); canvas.drawText(k.indication, 0, k.indication.length(), x + keyW / 2f, (keyH - p.ascent() - p.descent()) * 4/5 + y, p); -- cgit v1.2.3