KeyDrawParams.java revision ba49920e8046ca592d6c914b2e3f7394bf9c281c
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.utils.ResourceUtils;
22
23public final 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 mFunctionalTextColor;
39    public int mHintLetterColor;
40    public int mHintLabelColor;
41    public int mShiftedLetterHintInactivatedColor;
42    public int mShiftedLetterHintActivatedColor;
43    public int mPreviewTextColor;
44
45    public float mHintLabelVerticalAdjustment;
46    public float mLabelOffCenterRatio;
47    public float mHintLabelOffCenterRatio;
48
49    public int mAnimAlpha;
50
51    public KeyDrawParams() {}
52
53    private KeyDrawParams(final KeyDrawParams copyFrom) {
54        mTypeface = copyFrom.mTypeface;
55
56        mLetterSize = copyFrom.mLetterSize;
57        mLabelSize = copyFrom.mLabelSize;
58        mLargeLetterSize = copyFrom.mLargeLetterSize;
59        mLargeLabelSize = copyFrom.mLargeLabelSize;
60        mHintLetterSize = copyFrom.mHintLetterSize;
61        mShiftedLetterHintSize = copyFrom.mShiftedLetterHintSize;
62        mHintLabelSize = copyFrom.mHintLabelSize;
63        mPreviewTextSize = copyFrom.mPreviewTextSize;
64
65        mTextColor = copyFrom.mTextColor;
66        mTextInactivatedColor = copyFrom.mTextInactivatedColor;
67        mTextShadowColor = copyFrom.mTextShadowColor;
68        mFunctionalTextColor = copyFrom.mFunctionalTextColor;
69        mHintLetterColor = copyFrom.mHintLetterColor;
70        mHintLabelColor = copyFrom.mHintLabelColor;
71        mShiftedLetterHintInactivatedColor = copyFrom.mShiftedLetterHintInactivatedColor;
72        mShiftedLetterHintActivatedColor = copyFrom.mShiftedLetterHintActivatedColor;
73        mPreviewTextColor = copyFrom.mPreviewTextColor;
74
75        mHintLabelVerticalAdjustment = copyFrom.mHintLabelVerticalAdjustment;
76        mLabelOffCenterRatio = copyFrom.mLabelOffCenterRatio;
77        mHintLabelOffCenterRatio = copyFrom.mHintLabelOffCenterRatio;
78
79        mAnimAlpha = copyFrom.mAnimAlpha;
80    }
81
82    public void updateParams(final int keyHeight, final KeyVisualAttributes attr) {
83        if (attr == null) {
84            return;
85        }
86
87        if (attr.mTypeface != null) {
88            mTypeface = attr.mTypeface;
89        }
90
91        mLetterSize = selectTextSizeFromDimensionOrRatio(keyHeight,
92                attr.mLetterSize, attr.mLetterRatio, mLetterSize);
93        mLabelSize = selectTextSizeFromDimensionOrRatio(keyHeight,
94                attr.mLabelSize, attr.mLabelRatio, mLabelSize);
95        mLargeLabelSize = selectTextSize(keyHeight, attr.mLargeLabelRatio, mLargeLabelSize);
96        mLargeLetterSize = selectTextSize(keyHeight, attr.mLargeLetterRatio, mLargeLetterSize);
97        mHintLetterSize = selectTextSize(keyHeight, attr.mHintLetterRatio, mHintLetterSize);
98        mShiftedLetterHintSize = selectTextSize(keyHeight,
99                attr.mShiftedLetterHintRatio, mShiftedLetterHintSize);
100        mHintLabelSize = selectTextSize(keyHeight, attr.mHintLabelRatio, mHintLabelSize);
101        mPreviewTextSize = selectTextSize(keyHeight, attr.mPreviewTextRatio, mPreviewTextSize);
102
103        mTextColor = selectColor(attr.mTextColor, mTextColor);
104        mTextInactivatedColor = selectColor(attr.mTextInactivatedColor, mTextInactivatedColor);
105        mTextShadowColor = selectColor(attr.mTextShadowColor, mTextShadowColor);
106        mFunctionalTextColor = selectColor(attr.mFunctionalTextColor, mFunctionalTextColor);
107        mHintLetterColor = selectColor(attr.mHintLetterColor, mHintLetterColor);
108        mHintLabelColor = selectColor(attr.mHintLabelColor, mHintLabelColor);
109        mShiftedLetterHintInactivatedColor = selectColor(
110                attr.mShiftedLetterHintInactivatedColor, mShiftedLetterHintInactivatedColor);
111        mShiftedLetterHintActivatedColor = selectColor(
112                attr.mShiftedLetterHintActivatedColor, mShiftedLetterHintActivatedColor);
113        mPreviewTextColor = selectColor(attr.mPreviewTextColor, mPreviewTextColor);
114
115        mHintLabelVerticalAdjustment = selectFloatIfNonZero(
116                attr.mHintLabelVerticalAdjustment, mHintLabelVerticalAdjustment);
117        mLabelOffCenterRatio = selectFloatIfNonZero(
118                attr.mLabelOffCenterRatio, mLabelOffCenterRatio);
119        mHintLabelOffCenterRatio = selectFloatIfNonZero(
120                attr.mHintLabelOffCenterRatio, mHintLabelOffCenterRatio);
121    }
122
123    public KeyDrawParams mayCloneAndUpdateParams(final int keyHeight,
124            final KeyVisualAttributes attr) {
125        if (attr == null) {
126            return this;
127        }
128        final KeyDrawParams newParams = new KeyDrawParams(this);
129        newParams.updateParams(keyHeight, attr);
130        return newParams;
131    }
132
133    private static int selectTextSizeFromDimensionOrRatio(final int keyHeight,
134            final int dimens, final float ratio, final int defaultDimens) {
135        if (ResourceUtils.isValidDimensionPixelSize(dimens)) {
136            return dimens;
137        }
138        if (ResourceUtils.isValidFraction(ratio)) {
139            return (int)(keyHeight * ratio);
140        }
141        return defaultDimens;
142    }
143
144    private static int selectTextSize(final int keyHeight, final float ratio,
145            final int defaultSize) {
146        if (ResourceUtils.isValidFraction(ratio)) {
147            return (int)(keyHeight * ratio);
148        }
149        return defaultSize;
150    }
151
152    private static int selectColor(final int attrColor, final int defaultColor) {
153        if (attrColor != 0) {
154            return attrColor;
155        }
156        return defaultColor;
157    }
158
159    private static float selectFloatIfNonZero(final float attrFloat, final float defaultFloat) {
160        if (attrFloat != 0) {
161            return attrFloat;
162        }
163        return defaultFloat;
164    }
165}
166