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