KeyDrawParams.java revision dc34da218a22489d92d1015e9e5dac8d951b89f4
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.keyboard.internal;
18
19import android.graphics.Typeface;
20
21import com.android.inputmethod.latin.ResourceUtils;
22
23public class KeyDrawParams {
24    public Typeface mTypeface;
25
26    public int mLetterSize;
27    public int mLabelSize;
28    public int mLargeLetterSize;
29    public int mLargeLabelSize;
30    public int mHintLetterSize;
31    public int mShiftedLetterHintSize;
32    public int mHintLabelSize;
33    public int mPreviewTextSize;
34
35    public int mTextColor;
36    public int mTextInactivatedColor;
37    public int mTextShadowColor;
38    public int mHintLetterColor;
39    public int mHintLabelColor;
40    public int mShiftedLetterHintInactivatedColor;
41    public int mShiftedLetterHintActivatedColor;
42    public int mPreviewTextColor;
43
44    public int mAnimAlpha;
45
46    private KeyDrawParams(final KeyDrawParams copyFrom) {
47        mTypeface = copyFrom.mTypeface;
48
49        mLetterSize = copyFrom.mLetterSize;
50        mLabelSize = copyFrom.mLabelSize;
51        mLargeLetterSize = copyFrom.mLargeLetterSize;
52        mLargeLabelSize = copyFrom.mLargeLabelSize;
53        mHintLetterSize = copyFrom.mHintLetterSize;
54        mShiftedLetterHintSize = copyFrom.mShiftedLetterHintSize;
55        mHintLabelSize = copyFrom.mHintLabelSize;
56        mPreviewTextSize = copyFrom.mPreviewTextSize;
57
58        mTextColor = copyFrom.mTextColor;
59        mTextInactivatedColor = copyFrom.mTextInactivatedColor;
60        mTextShadowColor = copyFrom.mTextShadowColor;
61        mHintLetterColor = copyFrom.mHintLetterColor;
62        mHintLabelColor = copyFrom.mHintLabelColor;
63        mShiftedLetterHintInactivatedColor = copyFrom.mShiftedLetterHintInactivatedColor;
64        mShiftedLetterHintActivatedColor = copyFrom.mShiftedLetterHintActivatedColor;
65        mPreviewTextColor = copyFrom.mPreviewTextColor;
66
67        mAnimAlpha = copyFrom.mAnimAlpha;
68    }
69
70    public void updateParams(final int keyHeight, final KeyVisualAttributes attr) {
71        if (attr == null) {
72            return;
73        }
74
75        if (attr.mTypeface != null) {
76            mTypeface = attr.mTypeface;
77        }
78
79        mLetterSize = selectTextSizeFromDimensionOrRatio(keyHeight,
80                attr.mLetterSize, attr.mLetterRatio, mLetterSize);
81        mLabelSize = selectTextSizeFromDimensionOrRatio(keyHeight,
82                attr.mLabelSize, attr.mLabelRatio, mLabelSize);
83        mLargeLabelSize = selectTextSize(keyHeight, attr.mLargeLabelRatio, mLargeLabelSize);
84        mLargeLetterSize = selectTextSize(keyHeight, attr.mLargeLetterRatio, mLargeLetterSize);
85        mHintLetterSize = selectTextSize(keyHeight, attr.mHintLetterRatio, mHintLetterSize);
86        mShiftedLetterHintSize = selectTextSize(keyHeight,
87                attr.mShiftedLetterHintRatio, mShiftedLetterHintSize);
88        mHintLabelSize = selectTextSize(keyHeight, attr.mHintLabelRatio, mHintLabelSize);
89        mPreviewTextSize = selectTextSize(keyHeight, attr.mPreviewTextRatio, mPreviewTextSize);
90
91        mTextColor = selectColor(attr.mTextColor, mTextColor);
92        mTextInactivatedColor = selectColor(attr.mTextInactivatedColor, mTextInactivatedColor);
93        mTextShadowColor = selectColor(attr.mTextShadowColor, mTextShadowColor);
94        mHintLetterColor = selectColor(attr.mHintLetterColor, mHintLetterColor);
95        mHintLabelColor = selectColor(attr.mHintLabelColor, mHintLabelColor);
96        mShiftedLetterHintInactivatedColor = selectColor(
97                attr.mShiftedLetterHintInactivatedColor, mShiftedLetterHintInactivatedColor);
98        mShiftedLetterHintActivatedColor = selectColor(
99                attr.mShiftedLetterHintActivatedColor, mShiftedLetterHintActivatedColor);
100        mPreviewTextColor = selectColor(attr.mPreviewTextColor, mPreviewTextColor);
101    }
102
103    public KeyDrawParams mayCloneAndUpdateParams(final int keyHeight,
104            final KeyVisualAttributes attr) {
105        if (attr == null) {
106            return this;
107        }
108        final KeyDrawParams newParams = new KeyDrawParams(this);
109        newParams.updateParams(keyHeight, attr);
110        return newParams;
111    }
112
113    private static final int selectTextSizeFromDimensionOrRatio(final int keyHeight,
114            final int dimens, final float ratio, final int defaultDimens) {
115        if (ResourceUtils.isValidDimensionPixelSize(dimens)) {
116            return dimens;
117        }
118        if (ResourceUtils.isValidFraction(ratio)) {
119            return (int)(keyHeight * ratio);
120        }
121        return defaultDimens;
122    }
123
124    private static final int selectTextSize(final int keyHeight, final float ratio,
125            final int defaultSize) {
126        if (ResourceUtils.isValidFraction(ratio)) {
127            return (int)(keyHeight * ratio);
128        }
129        return defaultSize;
130    }
131
132    private static final int selectColor(final int attrColor, final int defaultColor) {
133        if (attrColor != 0) {
134            return attrColor;
135        }
136        return defaultColor;
137    }
138}
139