diff options
Diffstat (limited to 'srcs/juloo.keyboard2/Theme.java')
| -rw-r--r-- | srcs/juloo.keyboard2/Theme.java | 150 |
1 files changed, 110 insertions, 40 deletions
diff --git a/srcs/juloo.keyboard2/Theme.java b/srcs/juloo.keyboard2/Theme.java index 9eff44f..e896af5 100644 --- a/srcs/juloo.keyboard2/Theme.java +++ b/srcs/juloo.keyboard2/Theme.java @@ -9,9 +9,11 @@ import android.util.AttributeSet; public class Theme { - public final Paint keyBgPaint = new Paint(); - public final Paint keyDownBgPaint = new Paint(); - public final Paint keyBorderPaint = new Paint(); + // Key colors + public final int colorKey; + public final int colorKeyActivated; + + // Label colors public final int lockedColor; public final int activatedColor; public final int labelColor; @@ -19,6 +21,7 @@ public class Theme public final int secondaryLabelColor; public final int greyedLabelColor; + // Key borders public final float keyBorderRadius; public final float keyBorderWidth; public final float keyBorderWidthActivated; @@ -30,19 +33,12 @@ public class Theme public final int colorNavBar; public final boolean isLightNavBar; - private final Paint _keyLabelPaint; - private final Paint _specialKeyLabelPaint; - private final Paint _keySubLabelPaint; - private final Paint _specialKeySubLabelPaint; - private final Paint _indicationPaint; - private final Paint _specialIndicationPaint; - public Theme(Context context, AttributeSet attrs) { + getKeyFont(context); // _key_font will be accessed TypedArray s = context.getTheme().obtainStyledAttributes(attrs, R.styleable.keyboard, 0, 0); - int colorKey = s.getColor(R.styleable.keyboard_colorKey, 0); - keyBgPaint.setColor(colorKey); - keyDownBgPaint.setColor(s.getColor(R.styleable.keyboard_colorKeyActivated, 0)); + colorKey = s.getColor(R.styleable.keyboard_colorKey, 0); + colorKeyActivated = s.getColor(R.styleable.keyboard_colorKeyActivated, 0); // colorKeyboard = s.getColor(R.styleable.keyboard_colorKeyboard, 0); colorNavBar = s.getColor(R.styleable.keyboard_navigationBarColor, 0); isLightNavBar = s.getBoolean(R.styleable.keyboard_windowLightNavigationBar, false); @@ -57,37 +53,11 @@ public class Theme 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); - Typeface specialKeyFont = getKeyFont(context); - _specialKeyLabelPaint = initLabelPaint(Paint.Align.CENTER, specialKeyFont); - _specialKeySubLabelPaint = initLabelPaint(Paint.Align.LEFT, specialKeyFont); - _indicationPaint = initLabelPaint(Paint.Align.CENTER, null); - _specialIndicationPaint = initLabelPaint(Paint.Align.CENTER, specialKeyFont); - } - - public Paint labelPaint(boolean special_font) - { - Paint p = special_font ? _specialKeyLabelPaint : _keyLabelPaint; - return p; - } - - public Paint subLabelPaint(boolean special_font, Paint.Align align) - { - Paint p = special_font ? _specialKeySubLabelPaint : _keySubLabelPaint; - p.setTextAlign(align); - return p; - } - - public Paint indicationPaint(boolean special_font) - { - return special_font ? _specialIndicationPaint : _indicationPaint; } /** Interpolate the 'value' component toward its opposite by 'alpha'. */ @@ -100,7 +70,7 @@ public class Theme return Color.HSVToColor(hsv); } - Paint initLabelPaint(Paint.Align align, Typeface font) + Paint initIndicationPaint(Paint.Align align, Typeface font) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setTextAlign(align); @@ -117,4 +87,104 @@ public class Theme _key_font = Typeface.createFromAsset(context.getAssets(), "special_font.ttf"); return _key_font; } + + public static final class Computed + { + public final float vertical_margin; + public final float horizontal_margin; + public final float margin_top; + public final float margin_left; + public final Paint indication_paint; + + public final Key key; + public final Key key_activated; + + public Computed(Theme theme, Config config, float keyWidth) + { + vertical_margin = config.key_vertical_margin * config.keyHeight; + horizontal_margin = config.key_horizontal_margin * keyWidth; + // Add half of the key margin on the left and on the top as it's also + // added on the right and on the bottom of every keys. + margin_top = config.marginTop + vertical_margin / 2; + margin_left = horizontal_margin / 2; + key = new Key(theme, config, keyWidth, false); + key_activated = new Key(theme, config, keyWidth, true); + indication_paint = init_label_paint(config, null); + indication_paint.setColor(theme.subLabelColor); + } + + public static final class Key + { + public final Paint bg_paint = new Paint(); + public final Paint border_left_paint; + public final Paint border_top_paint; + public final Paint border_right_paint; + public final Paint border_bottom_paint; + public final float border_width; + public final float border_radius; + final Paint _label_paint; + final Paint _special_label_paint; + final Paint _sublabel_paint; + final Paint _special_sublabel_paint; + + public Key(Theme theme, Config config, float keyWidth, boolean activated) + { + bg_paint.setColor(activated ? theme.colorKeyActivated : theme.colorKey); + if (config.borderConfig) + { + border_radius = config.customBorderRadius * keyWidth; + border_width = config.customBorderLineWidth; + } + else + { + border_radius = theme.keyBorderRadius; + border_width = activated ? theme.keyBorderWidthActivated : theme.keyBorderWidth; + } + bg_paint.setAlpha(activated ? config.keyActivatedOpacity : config.keyOpacity); + border_left_paint = init_border_paint(config, border_width, theme.keyBorderColorLeft); + border_top_paint = init_border_paint(config, border_width, theme.keyBorderColorTop); + border_right_paint = init_border_paint(config, border_width, theme.keyBorderColorRight); + border_bottom_paint = init_border_paint(config, border_width, theme.keyBorderColorBottom); + _label_paint = init_label_paint(config, null); + _special_label_paint = init_label_paint(config, _key_font); + _sublabel_paint = init_label_paint(config, null); + _special_sublabel_paint = init_label_paint(config, _key_font); + } + + public Paint label_paint(boolean special_font, float text_size) + { + Paint p = special_font ? _special_label_paint : _label_paint; + p.setTextSize(text_size); + return p; + } + + public Paint sublabel_paint(boolean special_font, float text_size, Paint.Align align) + { + Paint p = special_font ? _special_sublabel_paint : _sublabel_paint; + p.setTextSize(text_size); + p.setTextAlign(align); + return p; + } + } + + static Paint init_border_paint(Config config, float border_width, int color) + { + Paint p = new Paint(); + p.setAlpha(config.keyOpacity); + p.setStyle(Paint.Style.STROKE); + p.setStrokeWidth(border_width); + p.setColor(color); + return p; + } + + static Paint init_label_paint(Config config, Typeface font) + { + Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); + p.setTextAlign(Paint.Align.CENTER); + p.setAlpha(config.labelBrightness); + if (font != null) + p.setTypeface(font); + return p; + } + } } |
