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