OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/aura_shell/image_grid.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "ui/gfx/canvas.h" | |
10 #include "ui/gfx/image/image.h" | |
11 #include "ui/gfx/transform.h" | |
12 #include "third_party/skia/include/core/SkColor.h" | |
13 #include "third_party/skia/include/core/SkXfermode.h" | |
14 | |
15 using std::max; | |
16 using std::min; | |
17 | |
18 namespace aura_shell { | |
19 namespace internal { | |
20 | |
21 gfx::Rect ImageGrid::TestAPI::GetTransformedLayerBounds( | |
22 const ui::Layer& layer) { | |
23 gfx::Rect bounds = layer.bounds(); | |
24 layer.transform().TransformRect(&bounds); | |
25 return bounds; | |
26 } | |
27 | |
28 ImageGrid::ImageGrid() | |
29 : top_image_height_(0), | |
30 bottom_image_height_(0), | |
31 left_image_width_(0), | |
32 right_image_width_(0), | |
33 top_row_height_(0), | |
34 bottom_row_height_(0), | |
35 left_column_width_(0), | |
36 right_column_width_(0) { | |
37 } | |
38 | |
39 ImageGrid::~ImageGrid() { | |
40 } | |
41 | |
42 void ImageGrid::Init(const gfx::Image* top_left_image, | |
43 const gfx::Image* top_image, | |
44 const gfx::Image* top_right_image, | |
45 const gfx::Image* left_image, | |
46 const gfx::Image* center_image, | |
47 const gfx::Image* right_image, | |
48 const gfx::Image* bottom_left_image, | |
49 const gfx::Image* bottom_image, | |
50 const gfx::Image* bottom_right_image) { | |
51 layer_.reset(new ui::Layer(ui::Layer::LAYER_HAS_NO_TEXTURE)); | |
52 | |
53 InitImage(top_left_image, &top_left_layer_, &top_left_painter_); | |
54 InitImage(top_image, &top_layer_, &top_painter_); | |
55 InitImage(top_right_image, &top_right_layer_, &top_right_painter_); | |
56 InitImage(left_image, &left_layer_, &left_painter_); | |
57 InitImage(center_image, ¢er_layer_, ¢er_painter_); | |
58 InitImage(right_image, &right_layer_, &right_painter_); | |
59 InitImage(bottom_left_image, &bottom_left_layer_, &bottom_left_painter_); | |
60 InitImage(bottom_image, &bottom_layer_, &bottom_painter_); | |
61 InitImage(bottom_right_image, &bottom_right_layer_, &bottom_right_painter_); | |
62 | |
63 top_image_height_ = GetImageSize(top_image).height(); | |
64 bottom_image_height_ = GetImageSize(bottom_image).height(); | |
65 left_image_width_ = GetImageSize(left_image).width(); | |
66 right_image_width_ = GetImageSize(right_image).width(); | |
67 | |
68 top_row_height_ = max(GetImageSize(top_left_image).height(), | |
69 max(GetImageSize(top_image).height(), | |
70 GetImageSize(top_right_image).height())); | |
71 bottom_row_height_ = max(GetImageSize(bottom_left_image).height(), | |
72 max(GetImageSize(bottom_image).height(), | |
73 GetImageSize(bottom_right_image).height())); | |
74 left_column_width_ = max(GetImageSize(top_left_image).width(), | |
75 max(GetImageSize(left_image).width(), | |
76 GetImageSize(bottom_left_image).width())); | |
77 right_column_width_ = max(GetImageSize(top_right_image).width(), | |
78 max(GetImageSize(right_image).width(), | |
79 GetImageSize(bottom_right_image).width())); | |
80 } | |
81 | |
82 void ImageGrid::SetSize(const gfx::Size& size) { | |
83 if (size_ == size) | |
84 return; | |
85 | |
86 size_ = size; | |
87 | |
88 gfx::Rect updated_bounds = layer_->bounds(); | |
89 updated_bounds.set_size(size); | |
90 layer_->SetBounds(updated_bounds); | |
91 | |
92 float center_width = size.width() - left_column_width_ - right_column_width_; | |
93 float center_height = size.height() - top_row_height_ - bottom_row_height_; | |
94 | |
95 if (top_layer_.get()) { | |
96 if (center_width > 0) { | |
97 ui::Transform transform; | |
98 transform.SetScaleX(center_width / top_layer_->bounds().width()); | |
99 transform.ConcatTranslate(left_column_width_, 0); | |
100 top_layer_->SetTransform(transform); | |
101 } | |
102 top_layer_->SetVisible(center_width > 0); | |
103 } | |
104 if (bottom_layer_.get()) { | |
105 if (center_width > 0) { | |
106 ui::Transform transform; | |
107 transform.SetScaleX(center_width / bottom_layer_->bounds().width()); | |
108 transform.ConcatTranslate( | |
109 left_column_width_, size.height() - bottom_layer_->bounds().height()); | |
110 bottom_layer_->SetTransform(transform); | |
111 } | |
112 bottom_layer_->SetVisible(center_width > 0); | |
113 } | |
114 if (left_layer_.get()) { | |
115 if (center_height > 0) { | |
116 ui::Transform transform; | |
117 transform.SetScaleY(center_height / left_layer_->bounds().height()); | |
118 transform.ConcatTranslate(0, top_row_height_); | |
119 left_layer_->SetTransform(transform); | |
120 } | |
121 left_layer_->SetVisible(center_height > 0); | |
122 } | |
123 if (right_layer_.get()) { | |
124 if (center_height > 0) { | |
125 ui::Transform transform; | |
126 transform.SetScaleY(center_height / right_layer_->bounds().height()); | |
127 transform.ConcatTranslate( | |
128 size.width() - right_layer_->bounds().width(), top_row_height_); | |
129 right_layer_->SetTransform(transform); | |
130 } | |
131 right_layer_->SetVisible(center_height > 0); | |
132 } | |
133 | |
134 // Calculate the available amount of space for corner images on all sides of | |
135 // the grid. If the images don't fit, we need to clip them. | |
136 const int left = min(left_column_width_, size_.width() / 2); | |
137 const int right = min(right_column_width_, size_.width() - left); | |
138 const int top = min(top_row_height_, size_.height() / 2); | |
139 const int bottom = min(bottom_row_height_, size_.height() - top); | |
140 | |
141 if (top_left_layer_.get()) { | |
142 // No transformation needed; it should be at (0, 0) and unscaled. | |
143 top_left_painter_->SetClipRect( | |
144 LayerExceedsSize(top_left_layer_.get(), gfx::Size(left, top)) ? | |
145 gfx::Rect(gfx::Rect(0, 0, left, top)) : | |
146 gfx::Rect(), | |
147 top_left_layer_.get()); | |
148 } | |
149 if (top_right_layer_.get()) { | |
150 ui::Transform transform; | |
151 transform.SetTranslateX(size.width() - top_right_layer_->bounds().width()); | |
152 top_right_layer_->SetTransform(transform); | |
153 top_right_painter_->SetClipRect( | |
154 LayerExceedsSize(top_right_layer_.get(), gfx::Size(right, top)) ? | |
155 gfx::Rect(top_right_layer_->bounds().width() - right, 0, | |
156 right, top) : | |
157 gfx::Rect(), | |
158 top_right_layer_.get()); | |
159 } | |
160 if (bottom_left_layer_.get()) { | |
161 ui::Transform transform; | |
162 transform.SetTranslateY( | |
163 size.height() - bottom_left_layer_->bounds().height()); | |
164 bottom_left_layer_->SetTransform(transform); | |
165 bottom_left_painter_->SetClipRect( | |
166 LayerExceedsSize(bottom_left_layer_.get(), gfx::Size(left, bottom)) ? | |
167 gfx::Rect(0, bottom_left_layer_->bounds().height() - bottom, | |
168 left, bottom) : | |
169 gfx::Rect(), | |
170 bottom_left_layer_.get()); | |
171 } | |
172 if (bottom_right_layer_.get()) { | |
173 ui::Transform transform; | |
174 transform.SetTranslate( | |
175 size.width() - bottom_right_layer_->bounds().width(), | |
176 size.height() - bottom_right_layer_->bounds().height()); | |
177 bottom_right_layer_->SetTransform(transform); | |
178 bottom_right_painter_->SetClipRect( | |
179 LayerExceedsSize(bottom_right_layer_.get(), gfx::Size(right, bottom)) ? | |
180 gfx::Rect(bottom_right_layer_->bounds().width() - right, | |
181 bottom_right_layer_->bounds().height() - bottom, | |
182 right, bottom) : | |
183 gfx::Rect(), | |
184 bottom_right_layer_.get()); | |
185 } | |
186 | |
187 if (center_layer_.get()) { | |
188 if (center_width > 0 && center_height > 0) { | |
189 ui::Transform transform; | |
190 transform.SetScale(center_width / center_layer_->bounds().width(), | |
191 center_height / center_layer_->bounds().height()); | |
192 transform.ConcatTranslate(left_column_width_, top_row_height_); | |
193 center_layer_->SetTransform(transform); | |
194 } | |
195 center_layer_->SetVisible(center_width > 0 && center_height > 0); | |
196 } | |
197 } | |
198 | |
199 void ImageGrid::ImagePainter::SetClipRect(const gfx::Rect& clip_rect, | |
200 ui::Layer* layer) { | |
201 if (clip_rect != clip_rect_) { | |
202 clip_rect_ = clip_rect; | |
203 layer->ScheduleDraw(); | |
204 } | |
205 } | |
206 | |
207 void ImageGrid::ImagePainter::OnPaintLayer(gfx::Canvas* canvas) { | |
208 if (!clip_rect_.IsEmpty()) | |
209 canvas->ClipRect(clip_rect_); | |
210 canvas->DrawBitmapInt(*(image_->ToSkBitmap()), 0, 0); | |
211 } | |
212 | |
213 // static | |
214 gfx::Size ImageGrid::GetImageSize(const gfx::Image* image) { | |
215 return image ? | |
216 gfx::Size(image->ToSkBitmap()->width(), image->ToSkBitmap()->height()) : | |
217 gfx::Size(); | |
218 } | |
219 | |
220 // static | |
221 bool ImageGrid::LayerExceedsSize(const ui::Layer* layer, | |
222 const gfx::Size& size) { | |
223 return layer->bounds().width() > size.width() || | |
224 layer->bounds().height() > size.height(); | |
225 } | |
226 | |
227 void ImageGrid::InitImage(const gfx::Image* image, | |
228 scoped_ptr<ui::Layer>* layer_ptr, | |
229 scoped_ptr<ImagePainter>* painter_ptr) { | |
230 if (!image) | |
231 return; | |
232 | |
233 layer_ptr->reset(new ui::Layer(ui::Layer::LAYER_HAS_TEXTURE)); | |
234 | |
235 const gfx::Size size = GetImageSize(image); | |
236 layer_ptr->get()->SetBounds(gfx::Rect(0, 0, size.width(), size.height())); | |
237 | |
238 painter_ptr->reset(new ImagePainter(image)); | |
239 layer_ptr->get()->set_delegate(painter_ptr->get()); | |
240 layer_ptr->get()->SetFillsBoundsOpaquely(false); | |
241 layer_ptr->get()->SetVisible(true); | |
242 layer_->Add(layer_ptr->get()); | |
243 } | |
244 | |
245 } // namespace internal | |
246 } // namespace aura_shell | |
OLD | NEW |