Draw borders and update themes

Themes can define the color of each borders independently. Every borders
must have the same width for now. It's possible to set a different width
when the key is activated, thought this is only used to remove borders.

The 4 themes are updated to take advantage of borders.
This commit is contained in:
Jules Aguillon 2022-11-13 20:53:06 +01:00
parent c1751578ef
commit 64c7c8ce20
3 changed files with 84 additions and 6 deletions

View File

@ -14,8 +14,14 @@
<attr name="secondaryLightOffset" format="float"/>
<!-- Corner labels -->
<attr name="colorSubLabel" format="color"/>
<!-- Dimens -->
<!-- Borders -->
<attr name="keyBorderRadius" format="dimension"/>
<attr name="keyBorderWidth" format="dimension"/>
<attr name="keyBorderWidthActivated" format="dimension"/>
<attr name="keyBorderColorLeft" format="color"/>
<attr name="keyBorderColorTop" format="color"/>
<attr name="keyBorderColorRight" format="color"/>
<attr name="keyBorderColorBottom" format="color"/>
<!-- Emoji panel -->
<attr name="emoji_button_bg" type="color" format="color"/>
<attr name="emoji_color" type="color" format="color"/>
@ -30,6 +36,8 @@
<item name="navigationBarColor">?attr/colorKeyboard</item>
<item name="windowLightNavigationBar">?attr/android:isLightTheme</item>
<item name="keyBorderRadius">5dp</item>
<item name="keyBorderWidth">0dp</item>
<item name="keyBorderWidthActivated">0dp</item>
<item name="emoji_key_bg" type="color">?attr/emoji_button_bg</item>
<item name="emoji_key_text" type="color">?attr/colorLabel</item>
</style>
@ -38,6 +46,9 @@
<item name="colorKeyboard">#1b1b1b</item>
<item name="colorKey">#333333</item>
<item name="colorKeyActivated">#1b1b1b</item>
<item name="keyBorderWidth">1.2dp</item>
<item name="keyBorderWidthActivated">0dp</item>
<item name="keyBorderColorBottom">#404040</item>
<item name="colorLabel">#ffffff</item>
<item name="colorLabelActivated">#3399ff</item>
<item name="colorLabelLocked">#33cc33</item>
@ -51,6 +62,12 @@
<item name="colorKeyboard">#e3e3e3</item>
<item name="colorKey">#cccccc</item>
<item name="colorKeyActivated">#e3e3e3</item>
<item name="keyBorderWidth">0.6dp</item>
<item name="keyBorderWidthActivated">0dp</item>
<item name="keyBorderColorLeft">#cccccc</item>
<item name="keyBorderColorTop">#eeeeee</item>
<item name="keyBorderColorRight">#cccccc</item>
<item name="keyBorderColorBottom">#aaaaaa</item>
<item name="colorLabel">#000000</item>
<item name="colorLabelActivated">#0066cc</item>
<item name="colorLabelLocked">#33cc33</item>
@ -64,6 +81,12 @@
<item name="colorKeyboard">#000000</item>
<item name="colorKey">#000000</item>
<item name="colorKeyActivated">#333333</item>
<item name="keyBorderWidth">1dp</item>
<item name="keyBorderWidthActivated">1dp</item>
<item name="keyBorderColorLeft">#2a2a2a</item>
<item name="keyBorderColorTop">#2a2a2a</item>
<item name="keyBorderColorRight">#2a2a2a</item>
<item name="keyBorderColorBottom">#2a2a2a</item>
<item name="colorLabel">#eeeeee</item>
<item name="colorLabelActivated">#009dff</item>
<item name="colorLabelLocked">#00ff26</item>
@ -77,7 +100,13 @@
<item name="android:isLightTheme">true</item>
<item name="colorKeyboard">#ffffff</item>
<item name="colorKey">#ffffff</item>
<item name="colorKeyActivated">#cccccc</item>
<item name="keyBorderWidth">1dp</item>
<item name="keyBorderWidthActivated">1dp</item>
<item name="keyBorderColorLeft">#f0f0f0</item>
<item name="keyBorderColorTop">#f0f0f0</item>
<item name="keyBorderColorRight">#eeeeee</item>
<item name="keyBorderColorBottom">#eeeeee</item>
<item name="colorKeyActivated">#ffffff</item>
<item name="colorLabel">#000000</item>
<item name="colorLabelActivated">#0066cc</item>
<item name="colorLabelLocked">#33cc33</item>

View File

@ -3,6 +3,7 @@ package juloo.keyboard2;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
@ -265,9 +266,7 @@ public class Keyboard2View extends View
x += k.shift * _keyWidth;
float keyW = _keyWidth * k.width - _config.keyHorizontalInterval;
boolean isKeyDown = _pointers.isKeyDown(k);
_tmpRect.set(x, y, x + keyW, y + keyH);
canvas.drawRoundRect(_tmpRect, _theme.keyBorderRadius, _theme.keyBorderRadius,
isKeyDown ? _theme.keyDownBgPaint : _theme.keyBgPaint);
drawKeyFrame(canvas, x, y, keyW, keyH, isKeyDown);
drawLabel(canvas, k.key0, keyW / 2f + x, y, keyH, isKeyDown);
if (k.edgekeys)
{
@ -299,6 +298,41 @@ public class Keyboard2View extends View
super.onDetachedFromWindow();
}
/** Draw borders and background of the key. */
void drawKeyFrame(Canvas canvas, float x, float y, float keyW, float keyH,
boolean isKeyDown)
{
float r = _theme.keyBorderRadius;
float w = isKeyDown ? _theme.keyBorderWidthActivated : _theme.keyBorderWidth;
float w2 = _theme.keyBorderWidth / 2.f;
_tmpRect.set(x + w2, y + w2, x + keyW - w2, y + keyH - w2);
canvas.drawRoundRect(_tmpRect, r, r,
isKeyDown ? _theme.keyDownBgPaint : _theme.keyBgPaint);
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, y, x + keyW, y + overlap, _theme.keyBorderColorTop);
drawBorder(canvas, x + keyW - overlap, y, x + keyW, y + keyH, _theme.keyBorderColorRight);
drawBorder(canvas, x, y + keyH - overlap, x + keyW, y + keyH, _theme.keyBorderColorBottom);
}
}
/** 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)
{
Paint p = _theme.keyBorderPaint;
float r = _theme.keyBorderRadius;
canvas.save();
canvas.clipRect(clipl, clipt, clipr, clipb);
p.setColor(color);
canvas.drawRoundRect(_tmpRect, r, r, p);
canvas.restore();
}
private int labelColor(KeyValue k, boolean isKeyDown, boolean sublabel)
{
if (isKeyDown)

View File

@ -12,6 +12,7 @@ public class Theme
{
public final Paint keyBgPaint = new Paint();
public final Paint keyDownBgPaint = new Paint();
public final Paint keyBorderPaint = new Paint();
public final int lockedColor;
public final int activatedColor;
public final int labelColor;
@ -19,6 +20,12 @@ public class Theme
public final int secondaryLabelColor;
public final float keyBorderRadius;
public final float keyBorderWidth;
public final float keyBorderWidthActivated;
public final int keyBorderColorLeft;
public final int keyBorderColorTop;
public final int keyBorderColorRight;
public final int keyBorderColorBottom;
public final int colorNavBar;
public final boolean isLightNavBar;
@ -32,7 +39,8 @@ public class Theme
public Theme(Context context, AttributeSet attrs)
{
TypedArray s = context.getTheme().obtainStyledAttributes(attrs, R.styleable.keyboard, 0, 0);
keyBgPaint.setColor(s.getColor(R.styleable.keyboard_colorKey, 0));
int colorKey = s.getColor(R.styleable.keyboard_colorKey, 0);
keyBgPaint.setColor(colorKey);
keyDownBgPaint.setColor(s.getColor(R.styleable.keyboard_colorKeyActivated, 0));
// colorKeyboard = s.getColor(R.styleable.keyboard_colorKeyboard, 0);
colorNavBar = s.getColor(R.styleable.keyboard_navigationBarColor, 0);
@ -44,6 +52,13 @@ public class Theme
float secondaryLightOffset = s.getFloat(R.styleable.keyboard_secondaryLightOffset, 1.f);
secondaryLabelColor = adjustLight(labelColor, secondaryLightOffset);
keyBorderRadius = s.getDimension(R.styleable.keyboard_keyBorderRadius, 0);
keyBorderWidth = s.getDimension(R.styleable.keyboard_keyBorderWidth, 0);
keyBorderWidthActivated = s.getDimension(R.styleable.keyboard_keyBorderWidthActivated, 0);
keyBorderPaint.setStyle(Paint.Style.STROKE);
keyBorderColorLeft = s.getColor(R.styleable.keyboard_keyBorderColorLeft, colorKey);
keyBorderColorTop = s.getColor(R.styleable.keyboard_keyBorderColorTop, colorKey);
keyBorderColorRight = s.getColor(R.styleable.keyboard_keyBorderColorRight, colorKey);
keyBorderColorBottom = s.getColor(R.styleable.keyboard_keyBorderColorBottom, colorKey);
s.recycle();
_keyLabelPaint = initLabelPaint(Paint.Align.CENTER, null);
_keySubLabelPaint = initLabelPaint(Paint.Align.LEFT, null);