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