|
1 /* |
|
2 * Copyright (c) 2006-2007 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 CHuiLinearGradientBrush - a Brush that is |
|
15 * able to draw linear color gradients. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 #include "uiacceltk/HuiGradientBrush.h" // Class definition |
|
22 #include "uiacceltk/HuiDrawing.h" |
|
23 #include "uiacceltk/HuiVisual.h" |
|
24 #include "uiacceltk/HuiUtil.h" |
|
25 |
|
26 |
|
27 EXPORT_C CHuiGradientBrush* CHuiGradientBrush::NewL() |
|
28 { |
|
29 CHuiGradientBrush* self = NewLC(); |
|
30 CleanupStack::Pop(); |
|
31 return self; |
|
32 } |
|
33 |
|
34 |
|
35 EXPORT_C CHuiGradientBrush* CHuiGradientBrush::NewLC() |
|
36 { |
|
37 CHuiGradientBrush* self = new (ELeave) CHuiGradientBrush(); |
|
38 CleanupStack::PushL(self); |
|
39 self->ConstructL(); |
|
40 return self; |
|
41 } |
|
42 |
|
43 |
|
44 CHuiGradientBrush::CHuiGradientBrush() |
|
45 : CHuiBrush(), |
|
46 iDirection(EDirectionRight) |
|
47 { |
|
48 } |
|
49 |
|
50 |
|
51 void CHuiGradientBrush::ConstructL() |
|
52 { |
|
53 SColor defaultColor; |
|
54 defaultColor.iPosition = 0; |
|
55 defaultColor.iColor = KRgbWhite; |
|
56 defaultColor.iOpacity = 1.0; |
|
57 User::LeaveIfError(iColors.Append(defaultColor)); |
|
58 } |
|
59 |
|
60 |
|
61 CHuiGradientBrush::~CHuiGradientBrush() |
|
62 { |
|
63 iColors.Reset(); |
|
64 } |
|
65 |
|
66 |
|
67 EXPORT_C void CHuiGradientBrush::SetDirection(TDirection aDirection) |
|
68 { |
|
69 iDirection = aDirection; |
|
70 SetChanged(); |
|
71 } |
|
72 |
|
73 |
|
74 void CHuiGradientBrush::Draw(CHuiGc& aGc, const MHuiBrushGuide& aGuide) const |
|
75 { |
|
76 TRect content = aGuide.BrushRect(); |
|
77 TReal32 opacity = aGuide.BrushOpacity() * iOpacity.Now(); |
|
78 |
|
79 if(opacity <= 0) |
|
80 { |
|
81 return; |
|
82 } |
|
83 |
|
84 TBool useTexture = EFalse; |
|
85 TReal32 texCoords[8]; |
|
86 THuiRealRect texCoordRect; |
|
87 |
|
88 aGc.SetPenAlpha(TInt(opacity * 255)); |
|
89 |
|
90 if(iImage.HasTexture()) |
|
91 { |
|
92 useTexture = ETrue; |
|
93 iImage.GetTexCoords(texCoords); |
|
94 aGc.Enable(CHuiGc::EFeatureTexturing); |
|
95 iImage.Texture().Bind(); |
|
96 } |
|
97 else |
|
98 { |
|
99 aGc.Disable(CHuiGc::EFeatureTexturing); |
|
100 } |
|
101 |
|
102 // Optimization for gradientbrush (MNIA-7G79GD). Draws a simple rect with one color |
|
103 // if there really isn't anything to gradient. |
|
104 // Updated optimization to work with single semi-transparent color |
|
105 if(iColors.Count() == 1 && !iImage.HasTexture() && (iColors[0].iOpacity * opacity) >= 1 ) |
|
106 { |
|
107 aGc.SetPenColor(iColors[0].iColor); |
|
108 aGc.DrawRect(content); |
|
109 return; |
|
110 } |
|
111 |
|
112 aGc.Enable(CHuiGc::EFeatureBlending); |
|
113 |
|
114 TInt segments = iColors.Count() - 1; |
|
115 if(segments <= 0) |
|
116 { |
|
117 segments = 1; |
|
118 } |
|
119 |
|
120 TInt length = content.Width(); |
|
121 TBool isHoriz = ETrue; |
|
122 TBool invert = EFalse; |
|
123 CHuiGc::TGradientType gradientType; |
|
124 |
|
125 switch(iDirection) |
|
126 { |
|
127 default: |
|
128 case EDirectionUp: |
|
129 gradientType = CHuiGc::EGradientLinearUp; |
|
130 isHoriz = EFalse; |
|
131 length = content.Height(); |
|
132 invert = ETrue; |
|
133 break; |
|
134 |
|
135 case EDirectionDown: |
|
136 gradientType = CHuiGc::EGradientLinearDown; |
|
137 isHoriz = EFalse; |
|
138 length = content.Height(); |
|
139 break; |
|
140 |
|
141 case EDirectionRight: |
|
142 gradientType = CHuiGc::EGradientLinearRight; |
|
143 break; |
|
144 |
|
145 case EDirectionLeft: |
|
146 gradientType = CHuiGc::EGradientLinearLeft; |
|
147 invert = ETrue; |
|
148 break; |
|
149 } |
|
150 |
|
151 for(TInt start = 0; start < segments; ++start) |
|
152 { |
|
153 TReal32 startPosition = iColors[start].iPosition; |
|
154 |
|
155 TInt end = start + 1; |
|
156 TReal32 endPosition; |
|
157 if(end >= iColors.Count()) |
|
158 { |
|
159 end = iColors.Count() - 1; |
|
160 endPosition = 1; |
|
161 } |
|
162 else |
|
163 { |
|
164 endPosition = iColors[end].iPosition; |
|
165 } |
|
166 |
|
167 if(iColors[start].iOpacity <= 0 && iColors[end].iOpacity <= 0) |
|
168 { |
|
169 // Won't be visible. |
|
170 continue; |
|
171 } |
|
172 |
|
173 if(invert) |
|
174 { |
|
175 // Inverted direction. |
|
176 startPosition = 1 - startPosition; |
|
177 endPosition = 1 - endPosition; |
|
178 } |
|
179 |
|
180 TInt beginCoord = (isHoriz? content.iTl.iX : content.iTl.iY); |
|
181 TInt startCoord = beginCoord + TInt(startPosition * length); |
|
182 TInt endCoord = beginCoord + TInt(endPosition * length); |
|
183 |
|
184 TRect segmentRect; |
|
185 |
|
186 if(isHoriz) |
|
187 { |
|
188 segmentRect.iTl = TPoint(startCoord, content.iTl.iY); |
|
189 segmentRect.iBr = TPoint(endCoord, content.iBr.iY); |
|
190 } |
|
191 else |
|
192 { |
|
193 segmentRect.iTl = TPoint(content.iTl.iX, startCoord); |
|
194 segmentRect.iBr = TPoint(content.iBr.iX, endCoord); |
|
195 } |
|
196 |
|
197 // Calculate texture coordinates, if necessary. |
|
198 if(useTexture) |
|
199 { |
|
200 if(isHoriz) |
|
201 { |
|
202 texCoordRect.iTl.iX = HuiUtil::Interpolate(startPosition, texCoords[0], texCoords[2]); |
|
203 texCoordRect.iTl.iY = texCoords[3]; |
|
204 texCoordRect.iBr.iX = HuiUtil::Interpolate(endPosition, texCoords[0], texCoords[2]); |
|
205 texCoordRect.iBr.iY = texCoords[5]; |
|
206 } |
|
207 else |
|
208 { |
|
209 texCoordRect.iTl.iX = texCoords[0]; |
|
210 texCoordRect.iTl.iY = HuiUtil::Interpolate(startPosition, texCoords[3], texCoords[5]); |
|
211 texCoordRect.iBr.iX = texCoords[2]; |
|
212 texCoordRect.iBr.iY = HuiUtil::Interpolate(endPosition, texCoords[3], texCoords[5]); |
|
213 } |
|
214 } |
|
215 |
|
216 aGc.DrawGradient(gradientType, segmentRect, |
|
217 iColors[start].iColor, iColors[end].iColor, |
|
218 iColors[start].iOpacity * opacity, |
|
219 iColors[end].iOpacity * opacity, |
|
220 useTexture? &texCoordRect : NULL); |
|
221 } |
|
222 aGc.Disable(CHuiGc::EFeatureBlending); |
|
223 } |
|
224 |
|
225 |
|
226 EXPORT_C void CHuiGradientBrush::SetColor(const TRgb& aColor, TReal32 aOpacity) __SOFTFP |
|
227 { |
|
228 while(iColors.Count() > 1) |
|
229 { |
|
230 iColors.Remove(iColors.Count() - 1); |
|
231 } |
|
232 |
|
233 SColor color; |
|
234 color.iPosition = 0; |
|
235 color.iColor = aColor; |
|
236 color.iOpacity = aOpacity; |
|
237 |
|
238 iColors[0] = color; |
|
239 |
|
240 SetChanged(); |
|
241 } |
|
242 |
|
243 |
|
244 EXPORT_C TRgb CHuiGradientBrush::Color(TInt aIndex) const |
|
245 { |
|
246 // make sure aIndex is not out of bounds |
|
247 if ( aIndex < iColors.Count() ) |
|
248 { |
|
249 return iColors[aIndex].iColor; |
|
250 } |
|
251 else |
|
252 { |
|
253 // default color always exists |
|
254 return iColors[0].iColor; |
|
255 } |
|
256 } |
|
257 |
|
258 |
|
259 EXPORT_C TReal32 CHuiGradientBrush::Opacity(TInt aIndex) const __SOFTFP |
|
260 { |
|
261 // make sure aIndex is not out of bounds |
|
262 // make sure aIndex is not out of bounds |
|
263 if ( aIndex < iColors.Count() ) |
|
264 { |
|
265 return iColors[aIndex].iOpacity; |
|
266 } |
|
267 else |
|
268 { |
|
269 // default color always exists |
|
270 return iColors[0].iOpacity; |
|
271 } |
|
272 } |
|
273 |
|
274 |
|
275 EXPORT_C void CHuiGradientBrush::AppendColorL(TReal32 aPosition, |
|
276 const TRgb& aColor, |
|
277 TReal32 aOpacity) __SOFTFP |
|
278 { |
|
279 SColor color; |
|
280 |
|
281 color.iPosition = aPosition; |
|
282 color.iColor = aColor; |
|
283 color.iOpacity = aOpacity; |
|
284 User::LeaveIfError(iColors.Append(color)); |
|
285 |
|
286 SetChanged(); |
|
287 } |
|
288 |
|
289 |
|
290 EXPORT_C void CHuiGradientBrush::SetImage(const THuiImage& aImage) |
|
291 { |
|
292 iImage = aImage; |
|
293 SetChanged(); |
|
294 } |
|
295 |
|
296 |
|
297 EXPORT_C const THuiImage& CHuiGradientBrush::Image() const |
|
298 { |
|
299 return iImage; |
|
300 } |