|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Implementation of the class TFscContactActionMenuTimedValue. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCUDES |
|
20 #include "emailtrace.h" |
|
21 #include "tfsccontactactionmenutimedvalue.h" |
|
22 |
|
23 // CONSTS |
|
24 const TReal KMilliSecondsInSecond = 1000.0; |
|
25 |
|
26 // ======== LOCAL FUNCTIONS ======== |
|
27 |
|
28 // ======== MEMBER FUNCTIONS ======== |
|
29 |
|
30 |
|
31 // --------------------------------------------------------------------------- |
|
32 // TFscContactActionMenuTimedValue::TFscContactActionMenuTimedValue |
|
33 // --------------------------------------------------------------------------- |
|
34 // |
|
35 TFscContactActionMenuTimedValue::TFscContactActionMenuTimedValue( |
|
36 TReal32 aInitialValue ) : iStartValue( aInitialValue ) |
|
37 { |
|
38 FUNC_LOG; |
|
39 } |
|
40 |
|
41 // --------------------------------------------------------------------------- |
|
42 // TFscContactActionMenuTimedValue::TFscContactActionMenuTimedValue |
|
43 // --------------------------------------------------------------------------- |
|
44 // |
|
45 void TFscContactActionMenuTimedValue::Set( |
|
46 TReal32 aTargetValue, TInt aTimeMs ) |
|
47 { |
|
48 FUNC_LOG; |
|
49 if( aTimeMs <= 0 ) |
|
50 { |
|
51 iInternalState &= ~EFlagInterpolating; |
|
52 iInternalState |= EFlagChanged; |
|
53 iStartValue = aTargetValue; |
|
54 iTargetValue = aTargetValue; |
|
55 return; |
|
56 } |
|
57 |
|
58 if( iInternalState & EFlagInterpolating ) |
|
59 { |
|
60 iStartValue = Now(); |
|
61 } |
|
62 else |
|
63 { |
|
64 if( iStartValue == aTargetValue ) |
|
65 { |
|
66 // This is the current value of the timed value, and it won't |
|
67 // change, so we are already at the target. |
|
68 return; |
|
69 } |
|
70 } |
|
71 |
|
72 // If the difference between the current value and the target value |
|
73 // is insignificant, no interpolation is done (it would be subject |
|
74 // to round-off problems). |
|
75 const TReal32 KEpsilon = .0001; |
|
76 TReal32 delta = aTargetValue - iStartValue; |
|
77 if( Abs(delta) < KEpsilon ) |
|
78 { |
|
79 iStartValue = aTargetValue; |
|
80 iTargetValue = aTargetValue; |
|
81 iInternalState &= ~EFlagInterpolating; |
|
82 iInternalState |= EFlagChanged; |
|
83 return; |
|
84 } |
|
85 |
|
86 iStartTime = Time(); |
|
87 |
|
88 // Target time is an offset to the current time. |
|
89 iEndTime = iStartTime + aTimeMs; |
|
90 iTargetValue = aTargetValue; |
|
91 iInternalState |= EFlagChanged | EFlagInterpolating; |
|
92 |
|
93 } |
|
94 |
|
95 // --------------------------------------------------------------------------- |
|
96 // TFscContactActionMenuTimedValue::SetWithSpeed |
|
97 // --------------------------------------------------------------------------- |
|
98 // |
|
99 void TFscContactActionMenuTimedValue::SetWithSpeed( |
|
100 TReal32 aTargetValue, TInt aUnitsPerSecond ) |
|
101 { |
|
102 FUNC_LOG; |
|
103 TReal32 delta = Now() - aTargetValue; |
|
104 if( delta == 0 || aUnitsPerSecond <= 0 ) |
|
105 { |
|
106 // Already there. |
|
107 Set( aTargetValue ); |
|
108 return; |
|
109 } |
|
110 |
|
111 delta = Abs(delta); |
|
112 Set( aTargetValue, ( TInt ) ( ( delta / aUnitsPerSecond ) * KMilliSecondsInSecond ) ); |
|
113 } |
|
114 |
|
115 // --------------------------------------------------------------------------- |
|
116 // TFscContactActionMenuTimedValue::Now |
|
117 // --------------------------------------------------------------------------- |
|
118 // |
|
119 TReal TFscContactActionMenuTimedValue::Now() |
|
120 { |
|
121 FUNC_LOG; |
|
122 |
|
123 if( ( iInternalState & EFlagInterpolating ) && iEndTime > iStartTime ) |
|
124 { |
|
125 iInternalState |= EFlagChanged; |
|
126 |
|
127 TInt duration = iEndTime - iStartTime; // Convert from TInt64 -> TInt |
|
128 TInt elapsed = Time() - iStartTime; // Convert from TInt64 -> TInt |
|
129 |
|
130 if( elapsed > duration ) |
|
131 { |
|
132 // We have reached the destination. |
|
133 iStartValue = iTargetValue; |
|
134 iStartTime = iEndTime; |
|
135 iInternalState &= ~EFlagInterpolating; |
|
136 return iStartValue; |
|
137 } |
|
138 |
|
139 const TInt fixt = FixDiv( elapsed, duration ); |
|
140 TReal32 t = FixToFloat( fixt ); |
|
141 TReal result = ( iStartValue * ( 1 - t ) + iTargetValue * t ); |
|
142 return result; |
|
143 } |
|
144 else |
|
145 { |
|
146 if( iInternalState & EFlagInterpolating ) |
|
147 { |
|
148 iInternalState |= EFlagChanged; |
|
149 iInternalState &= ~EFlagInterpolating; |
|
150 } |
|
151 return iStartValue; |
|
152 } |
|
153 |
|
154 } |
|
155 |
|
156 // --------------------------------------------------------------------------- |
|
157 // TFscContactActionMenuTimedValue::Target |
|
158 // --------------------------------------------------------------------------- |
|
159 // |
|
160 TReal TFscContactActionMenuTimedValue::Target() |
|
161 { |
|
162 FUNC_LOG; |
|
163 return iTargetValue; |
|
164 } |
|
165 |
|
166 // --------------------------------------------------------------------------- |
|
167 // TFscContactActionMenuTimedValue::Interpolating |
|
168 // --------------------------------------------------------------------------- |
|
169 // |
|
170 TBool TFscContactActionMenuTimedValue::Interpolating() |
|
171 { |
|
172 FUNC_LOG; |
|
173 return ( ( iInternalState & EFlagInterpolating ) != 0 ); |
|
174 } |
|
175 |
|
176 // --------------------------------------------------------------------------- |
|
177 // TFscContactActionMenuTimedValue::FixDiv |
|
178 // --------------------------------------------------------------------------- |
|
179 // |
|
180 TInt TFscContactActionMenuTimedValue::FixDiv( TInt aVal1, TInt aVal2 ) |
|
181 { |
|
182 FUNC_LOG; |
|
183 TUint64 temp = aVal1; |
|
184 temp <<= 16; |
|
185 TInt result = 0; |
|
186 if ( aVal2 != 0 ) |
|
187 { |
|
188 result = TInt( temp / aVal2 ); |
|
189 } |
|
190 return result; |
|
191 }; |
|
192 |
|
193 // --------------------------------------------------------------------------- |
|
194 // TFscContactActionMenuTimedValue::FixToFloat |
|
195 // --------------------------------------------------------------------------- |
|
196 // |
|
197 TReal32 TFscContactActionMenuTimedValue::FixToFloat( TInt aVal ) |
|
198 { |
|
199 FUNC_LOG; |
|
200 TReal result = TReal32( ( TReal32( aVal ) ) / 65536.0f ); |
|
201 return result; |
|
202 } |
|
203 |
|
204 // --------------------------------------------------------------------------- |
|
205 // TFscContactActionMenuTimedValue::Time |
|
206 // --------------------------------------------------------------------------- |
|
207 // |
|
208 TInt64 TFscContactActionMenuTimedValue::Time() const |
|
209 { |
|
210 FUNC_LOG; |
|
211 TTime curTime; |
|
212 curTime.UniversalTime(); |
|
213 TTimeIntervalMicroSeconds interval = |
|
214 curTime.MicroSecondsFrom( TTime( 0 ) ); |
|
215 |
|
216 return interval.Int64() / KMilliSecondsInSecond; |
|
217 } |
|
218 |