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/image_grid.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "ui/aura/desktop.h" |
| 10 #include "ui/gfx/canvas.h" |
| 11 #include "ui/gfx/image/image.h" |
| 12 #include "ui/gfx/transform.h" |
| 13 #include "third_party/skia/include/core/SkColor.h" |
| 14 #include "third_party/skia/include/core/SkXfermode.h" |
| 15 |
| 16 using std::max; |
| 17 |
| 18 namespace aura { |
| 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(Desktop::GetInstance()->compositor(), |
| 52 ui::Layer::LAYER_HAS_NO_TEXTURE)); |
| 53 |
| 54 InitImage(top_left_image, &top_left_layer_, &top_left_painter_); |
| 55 InitImage(top_image, &top_layer_, &top_painter_); |
| 56 InitImage(top_right_image, &top_right_layer_, &top_right_painter_); |
| 57 InitImage(left_image, &left_layer_, &left_painter_); |
| 58 InitImage(center_image, ¢er_layer_, ¢er_painter_); |
| 59 InitImage(right_image, &right_layer_, &right_painter_); |
| 60 InitImage(bottom_left_image, &bottom_left_layer_, &bottom_left_painter_); |
| 61 InitImage(bottom_image, &bottom_layer_, &bottom_painter_); |
| 62 InitImage(bottom_right_image, &bottom_right_layer_, &bottom_right_painter_); |
| 63 |
| 64 top_image_height_ = GetImageSize(top_image).height(); |
| 65 bottom_image_height_ = GetImageSize(bottom_image).height(); |
| 66 left_image_width_ = GetImageSize(left_image).width(); |
| 67 right_image_width_ = GetImageSize(right_image).width(); |
| 68 |
| 69 top_row_height_ = max(GetImageSize(top_left_image).height(), |
| 70 max(GetImageSize(top_image).height(), |
| 71 GetImageSize(top_right_image).height())); |
| 72 bottom_row_height_ = max(GetImageSize(bottom_left_image).height(), |
| 73 max(GetImageSize(bottom_image).height(), |
| 74 GetImageSize(bottom_right_image).height())); |
| 75 left_column_width_ = max(GetImageSize(top_left_image).width(), |
| 76 max(GetImageSize(left_image).width(), |
| 77 GetImageSize(bottom_left_image).width())); |
| 78 right_column_width_ = max(GetImageSize(top_right_image).width(), |
| 79 max(GetImageSize(right_image).width(), |
| 80 GetImageSize(bottom_right_image).width())); |
| 81 } |
| 82 |
| 83 void ImageGrid::SetSize(const gfx::Size& size) { |
| 84 if (size_ == size) |
| 85 return; |
| 86 |
| 87 size_ = size; |
| 88 |
| 89 gfx::Rect updated_bounds = layer_->bounds(); |
| 90 updated_bounds.set_size(size); |
| 91 layer_->SetBounds(updated_bounds); |
| 92 |
| 93 float center_width = size.width() - left_column_width_ - right_column_width_; |
| 94 float center_height = size.height() - top_row_height_ - bottom_row_height_; |
| 95 |
| 96 if (top_layer_.get()) { |
| 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 if (bottom_layer_.get()) { |
| 103 ui::Transform transform; |
| 104 transform.SetScaleX(center_width / bottom_layer_->bounds().width()); |
| 105 transform.ConcatTranslate( |
| 106 left_column_width_, size.height() - bottom_layer_->bounds().height()); |
| 107 bottom_layer_->SetTransform(transform); |
| 108 } |
| 109 if (left_layer_.get()) { |
| 110 ui::Transform transform; |
| 111 transform.SetScaleY(center_height / left_layer_->bounds().height()); |
| 112 transform.ConcatTranslate(0, top_row_height_); |
| 113 left_layer_->SetTransform(transform); |
| 114 } |
| 115 if (right_layer_.get()) { |
| 116 ui::Transform transform; |
| 117 transform.SetScaleY(center_height / right_layer_->bounds().height()); |
| 118 transform.ConcatTranslate( |
| 119 size.width() - right_layer_->bounds().width(), top_row_height_); |
| 120 right_layer_->SetTransform(transform); |
| 121 } |
| 122 |
| 123 if (top_left_layer_.get()) { |
| 124 // No transformation needed; it should be at (0, 0) and unscaled. |
| 125 } |
| 126 if (top_right_layer_.get()) { |
| 127 ui::Transform transform; |
| 128 transform.SetTranslateX(size.width() - top_right_layer_->bounds().width()); |
| 129 top_right_layer_->SetTransform(transform); |
| 130 } |
| 131 if (bottom_left_layer_.get()) { |
| 132 ui::Transform transform; |
| 133 transform.SetTranslateY( |
| 134 size.height() - bottom_left_layer_->bounds().height()); |
| 135 bottom_left_layer_->SetTransform(transform); |
| 136 } |
| 137 if (bottom_right_layer_.get()) { |
| 138 ui::Transform transform; |
| 139 transform.SetTranslate( |
| 140 size.width() - bottom_right_layer_->bounds().width(), |
| 141 size.height() - bottom_right_layer_->bounds().height()); |
| 142 bottom_right_layer_->SetTransform(transform); |
| 143 } |
| 144 |
| 145 if (center_layer_.get()) { |
| 146 ui::Transform transform; |
| 147 transform.SetScale(center_width / center_layer_->bounds().width(), |
| 148 center_height / center_layer_->bounds().height()); |
| 149 transform.ConcatTranslate(left_column_width_, top_row_height_); |
| 150 center_layer_->SetTransform(transform); |
| 151 } |
| 152 } |
| 153 |
| 154 void ImageGrid::ImagePainter::OnPaintLayer(gfx::Canvas* canvas) OVERRIDE { |
| 155 canvas->DrawBitmapInt(*(image_->ToSkBitmap()), 0, 0); |
| 156 } |
| 157 |
| 158 // static |
| 159 gfx::Size ImageGrid::GetImageSize(const gfx::Image* image) { |
| 160 return image ? |
| 161 gfx::Size(image->ToSkBitmap()->width(), image->ToSkBitmap()->height()) : |
| 162 gfx::Size(); |
| 163 } |
| 164 |
| 165 void ImageGrid::InitImage(const gfx::Image* image, |
| 166 scoped_ptr<ui::Layer>* layer_ptr, |
| 167 scoped_ptr<ImagePainter>* painter_ptr) { |
| 168 if (!image) |
| 169 return; |
| 170 |
| 171 layer_ptr->reset(new ui::Layer(Desktop::GetInstance()->compositor(), |
| 172 ui::Layer::LAYER_HAS_TEXTURE)); |
| 173 |
| 174 const gfx::Size size = GetImageSize(image); |
| 175 layer_ptr->get()->SetBounds(gfx::Rect(0, 0, size.width(), size.height())); |
| 176 |
| 177 painter_ptr->reset(new ImagePainter(image)); |
| 178 layer_ptr->get()->set_delegate(painter_ptr->get()); |
| 179 layer_ptr->get()->SetFillsBoundsOpaquely(false); |
| 180 layer_ptr->get()->SetVisible(true); |
| 181 layer_->Add(layer_ptr->get()); |
| 182 } |
| 183 |
| 184 } // namespace internal |
| 185 } // namespace aura |
OLD | NEW |