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 |
| 17 namespace aura_shell { |
| 18 namespace internal { |
| 19 |
| 20 gfx::Rect ImageGrid::TestAPI::GetTransformedLayerBounds( |
| 21 const ui::Layer& layer) { |
| 22 gfx::Rect bounds = layer.bounds(); |
| 23 layer.transform().TransformRect(&bounds); |
| 24 return bounds; |
| 25 } |
| 26 |
| 27 ImageGrid::ImageGrid() |
| 28 : top_image_height_(0), |
| 29 bottom_image_height_(0), |
| 30 left_image_width_(0), |
| 31 right_image_width_(0), |
| 32 top_row_height_(0), |
| 33 bottom_row_height_(0), |
| 34 left_column_width_(0), |
| 35 right_column_width_(0) { |
| 36 } |
| 37 |
| 38 ImageGrid::~ImageGrid() { |
| 39 } |
| 40 |
| 41 void ImageGrid::Init(const gfx::Image* top_left_image, |
| 42 const gfx::Image* top_image, |
| 43 const gfx::Image* top_right_image, |
| 44 const gfx::Image* left_image, |
| 45 const gfx::Image* center_image, |
| 46 const gfx::Image* right_image, |
| 47 const gfx::Image* bottom_left_image, |
| 48 const gfx::Image* bottom_image, |
| 49 const gfx::Image* bottom_right_image) { |
| 50 layer_.reset(new ui::Layer(ui::Layer::LAYER_HAS_NO_TEXTURE)); |
| 51 |
| 52 InitImage(top_left_image, &top_left_layer_, &top_left_painter_); |
| 53 InitImage(top_image, &top_layer_, &top_painter_); |
| 54 InitImage(top_right_image, &top_right_layer_, &top_right_painter_); |
| 55 InitImage(left_image, &left_layer_, &left_painter_); |
| 56 InitImage(center_image, ¢er_layer_, ¢er_painter_); |
| 57 InitImage(right_image, &right_layer_, &right_painter_); |
| 58 InitImage(bottom_left_image, &bottom_left_layer_, &bottom_left_painter_); |
| 59 InitImage(bottom_image, &bottom_layer_, &bottom_painter_); |
| 60 InitImage(bottom_right_image, &bottom_right_layer_, &bottom_right_painter_); |
| 61 |
| 62 top_image_height_ = GetImageSize(top_image).height(); |
| 63 bottom_image_height_ = GetImageSize(bottom_image).height(); |
| 64 left_image_width_ = GetImageSize(left_image).width(); |
| 65 right_image_width_ = GetImageSize(right_image).width(); |
| 66 |
| 67 top_row_height_ = max(GetImageSize(top_left_image).height(), |
| 68 max(GetImageSize(top_image).height(), |
| 69 GetImageSize(top_right_image).height())); |
| 70 bottom_row_height_ = max(GetImageSize(bottom_left_image).height(), |
| 71 max(GetImageSize(bottom_image).height(), |
| 72 GetImageSize(bottom_right_image).height())); |
| 73 left_column_width_ = max(GetImageSize(top_left_image).width(), |
| 74 max(GetImageSize(left_image).width(), |
| 75 GetImageSize(bottom_left_image).width())); |
| 76 right_column_width_ = max(GetImageSize(top_right_image).width(), |
| 77 max(GetImageSize(right_image).width(), |
| 78 GetImageSize(bottom_right_image).width())); |
| 79 } |
| 80 |
| 81 void ImageGrid::SetSize(const gfx::Size& size) { |
| 82 if (size_ == size) |
| 83 return; |
| 84 |
| 85 size_ = size; |
| 86 |
| 87 gfx::Rect updated_bounds = layer_->bounds(); |
| 88 updated_bounds.set_size(size); |
| 89 layer_->SetBounds(updated_bounds); |
| 90 |
| 91 float center_width = size.width() - left_column_width_ - right_column_width_; |
| 92 float center_height = size.height() - top_row_height_ - bottom_row_height_; |
| 93 |
| 94 if (top_layer_.get()) { |
| 95 ui::Transform transform; |
| 96 transform.SetScaleX(center_width / top_layer_->bounds().width()); |
| 97 transform.ConcatTranslate(left_column_width_, 0); |
| 98 top_layer_->SetTransform(transform); |
| 99 } |
| 100 if (bottom_layer_.get()) { |
| 101 ui::Transform transform; |
| 102 transform.SetScaleX(center_width / bottom_layer_->bounds().width()); |
| 103 transform.ConcatTranslate( |
| 104 left_column_width_, size.height() - bottom_layer_->bounds().height()); |
| 105 bottom_layer_->SetTransform(transform); |
| 106 } |
| 107 if (left_layer_.get()) { |
| 108 ui::Transform transform; |
| 109 transform.SetScaleY(center_height / left_layer_->bounds().height()); |
| 110 transform.ConcatTranslate(0, top_row_height_); |
| 111 left_layer_->SetTransform(transform); |
| 112 } |
| 113 if (right_layer_.get()) { |
| 114 ui::Transform transform; |
| 115 transform.SetScaleY(center_height / right_layer_->bounds().height()); |
| 116 transform.ConcatTranslate( |
| 117 size.width() - right_layer_->bounds().width(), top_row_height_); |
| 118 right_layer_->SetTransform(transform); |
| 119 } |
| 120 |
| 121 if (top_left_layer_.get()) { |
| 122 // No transformation needed; it should be at (0, 0) and unscaled. |
| 123 } |
| 124 if (top_right_layer_.get()) { |
| 125 ui::Transform transform; |
| 126 transform.SetTranslateX(size.width() - top_right_layer_->bounds().width()); |
| 127 top_right_layer_->SetTransform(transform); |
| 128 } |
| 129 if (bottom_left_layer_.get()) { |
| 130 ui::Transform transform; |
| 131 transform.SetTranslateY( |
| 132 size.height() - bottom_left_layer_->bounds().height()); |
| 133 bottom_left_layer_->SetTransform(transform); |
| 134 } |
| 135 if (bottom_right_layer_.get()) { |
| 136 ui::Transform transform; |
| 137 transform.SetTranslate( |
| 138 size.width() - bottom_right_layer_->bounds().width(), |
| 139 size.height() - bottom_right_layer_->bounds().height()); |
| 140 bottom_right_layer_->SetTransform(transform); |
| 141 } |
| 142 |
| 143 if (center_layer_.get()) { |
| 144 ui::Transform transform; |
| 145 transform.SetScale(center_width / center_layer_->bounds().width(), |
| 146 center_height / center_layer_->bounds().height()); |
| 147 transform.ConcatTranslate(left_column_width_, top_row_height_); |
| 148 center_layer_->SetTransform(transform); |
| 149 } |
| 150 } |
| 151 |
| 152 void ImageGrid::ImagePainter::OnPaintLayer(gfx::Canvas* canvas) { |
| 153 canvas->DrawBitmapInt(*(image_->ToSkBitmap()), 0, 0); |
| 154 } |
| 155 |
| 156 // static |
| 157 gfx::Size ImageGrid::GetImageSize(const gfx::Image* image) { |
| 158 return image ? |
| 159 gfx::Size(image->ToSkBitmap()->width(), image->ToSkBitmap()->height()) : |
| 160 gfx::Size(); |
| 161 } |
| 162 |
| 163 void ImageGrid::InitImage(const gfx::Image* image, |
| 164 scoped_ptr<ui::Layer>* layer_ptr, |
| 165 scoped_ptr<ImagePainter>* painter_ptr) { |
| 166 if (!image) |
| 167 return; |
| 168 |
| 169 layer_ptr->reset(new ui::Layer(ui::Layer::LAYER_HAS_TEXTURE)); |
| 170 |
| 171 const gfx::Size size = GetImageSize(image); |
| 172 layer_ptr->get()->SetBounds(gfx::Rect(0, 0, size.width(), size.height())); |
| 173 |
| 174 painter_ptr->reset(new ImagePainter(image)); |
| 175 layer_ptr->get()->set_delegate(painter_ptr->get()); |
| 176 layer_ptr->get()->SetFillsBoundsOpaquely(false); |
| 177 layer_ptr->get()->SetVisible(true); |
| 178 layer_->Add(layer_ptr->get()); |
| 179 } |
| 180 |
| 181 } // namespace internal |
| 182 } // namespace aura_shell |
OLD | NEW |