OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/android/composited_touch_handle_drawable.h" | 5 #include "content/browser/android/composited_touch_handle_drawable.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
10 #include "cc/layers/ui_resource_layer.h" | 10 #include "cc/layers/ui_resource_layer.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 DetachLayer(); | 94 DetachLayer(); |
95 } | 95 } |
96 | 96 |
97 void CompositedTouchHandleDrawable::SetEnabled(bool enabled) { | 97 void CompositedTouchHandleDrawable::SetEnabled(bool enabled) { |
98 layer_->SetIsDrawable(enabled); | 98 layer_->SetIsDrawable(enabled); |
99 // Force a position update in case the disabled layer's properties are stale. | 99 // Force a position update in case the disabled layer's properties are stale. |
100 if (enabled) | 100 if (enabled) |
101 UpdateLayerPosition(); | 101 UpdateLayerPosition(); |
102 } | 102 } |
103 | 103 |
104 void CompositedTouchHandleDrawable::SetOrientation( | 104 void CompositedTouchHandleDrawable::SetLayout( |
105 ui::TouchHandleOrientation orientation) { | 105 const gfx::PointF& position, |
| 106 ui::TouchHandleOrientation orientation, |
| 107 bool mirror_vertical, |
| 108 bool mirror_horizontal, |
| 109 bool mirror_changed) { |
106 DCHECK(layer_->parent()); | 110 DCHECK(layer_->parent()); |
| 111 bool orientation_changed = orientation_ != orientation; |
| 112 focal_position_ = gfx::ScalePoint(position, dpi_scale_); |
| 113 |
107 orientation_ = orientation; | 114 orientation_ = orientation; |
108 | 115 |
109 const SkBitmap& bitmap = g_selection_resources.Get().GetBitmap(orientation); | 116 if (orientation_changed) { |
110 layer_->SetBitmap(bitmap); | 117 const SkBitmap& bitmap = g_selection_resources.Get().GetBitmap(orientation); |
111 layer_->SetBounds(gfx::Size(bitmap.width(), bitmap.height())); | 118 const int bitmap_height = bitmap.height(); |
| 119 const int bitmap_width = bitmap.width(); |
| 120 layer_->SetBitmap(bitmap); |
| 121 layer_->SetBounds(gfx::Size(bitmap_width, bitmap_height)); |
| 122 } |
112 | 123 |
| 124 const int layer_height = layer_->bounds().height(); |
| 125 const int layer_width = layer_->bounds().width(); |
| 126 // Invert about X and Y axis based on the mirror values |
| 127 if (mirror_changed) { |
| 128 gfx::Transform transform; |
| 129 float scale_x = mirror_horizontal ? -1.f : 1.f; |
| 130 float scale_y = mirror_vertical ? -1.f : 1.f; |
| 131 |
| 132 layer_->SetTransformOrigin( |
| 133 gfx::Point3F(layer_width * 0.5f, layer_height * 0.5f, 0)); |
| 134 transform.Scale(scale_x, scale_y); |
| 135 layer_->SetTransform(transform); |
| 136 } |
| 137 |
| 138 // Set the Focal offsets for the composited handle layer |
| 139 // based on the orientation |
| 140 int focal_offset_x = 0; |
| 141 int focal_offset_y = mirror_vertical ? layer_height : 0; |
113 switch (orientation_) { | 142 switch (orientation_) { |
114 case ui::TouchHandleOrientation::LEFT: | 143 case ui::TouchHandleOrientation::LEFT: |
115 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.75f, 0); | 144 focal_offset_x = |
| 145 mirror_horizontal ? layer_width * 0.25f : layer_width * 0.75f; |
116 break; | 146 break; |
117 case ui::TouchHandleOrientation::RIGHT: | 147 case ui::TouchHandleOrientation::RIGHT: |
118 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.25f, 0); | 148 focal_offset_x = |
| 149 mirror_horizontal ? layer_width * 0.75f : layer_width * 0.25f; |
119 break; | 150 break; |
120 case ui::TouchHandleOrientation::CENTER: | 151 case ui::TouchHandleOrientation::CENTER: |
121 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.5f, 0); | 152 focal_offset_x = layer_width * 0.5f; |
122 break; | 153 break; |
123 case ui::TouchHandleOrientation::UNDEFINED: | 154 case ui::TouchHandleOrientation::UNDEFINED: |
124 NOTREACHED() << "Invalid touch handle orientation."; | 155 NOTREACHED() << "Invalid touch handle orientation."; |
125 break; | 156 break; |
126 }; | 157 }; |
127 | 158 |
| 159 focal_offset_from_origin_ = gfx::Vector2dF(focal_offset_x, focal_offset_y); |
128 UpdateLayerPosition(); | 160 UpdateLayerPosition(); |
129 } | 161 } |
130 | 162 |
131 void CompositedTouchHandleDrawable::SetAlpha(float alpha) { | 163 void CompositedTouchHandleDrawable::SetAlpha(float alpha) { |
132 DCHECK(layer_->parent()); | 164 DCHECK(layer_->parent()); |
133 alpha = std::max(0.f, std::min(1.f, alpha)); | 165 alpha = std::max(0.f, std::min(1.f, alpha)); |
134 bool hidden = alpha <= 0; | 166 bool hidden = alpha <= 0; |
135 layer_->SetOpacity(alpha); | 167 layer_->SetOpacity(alpha); |
136 layer_->SetHideLayerAndSubtree(hidden); | 168 layer_->SetHideLayerAndSubtree(hidden); |
137 } | 169 } |
138 | 170 |
139 void CompositedTouchHandleDrawable::SetFocus(const gfx::PointF& position) { | |
140 DCHECK(layer_->parent()); | |
141 focal_position_ = gfx::ScalePoint(position, dpi_scale_); | |
142 UpdateLayerPosition(); | |
143 } | |
144 | |
145 gfx::RectF CompositedTouchHandleDrawable::GetVisibleBounds() const { | 171 gfx::RectF CompositedTouchHandleDrawable::GetVisibleBounds() const { |
146 return gfx::ScaleRect(gfx::RectF(layer_->position().x(), | 172 return gfx::ScaleRect(gfx::RectF(layer_->position().x(), |
147 layer_->position().y(), | 173 layer_->position().y(), |
148 layer_->bounds().width(), | 174 layer_->bounds().width(), |
149 layer_->bounds().height()), | 175 layer_->bounds().height()), |
150 1.f / dpi_scale_); | 176 1.f / dpi_scale_); |
151 } | 177 } |
152 | 178 |
153 void CompositedTouchHandleDrawable::DetachLayer() { | 179 void CompositedTouchHandleDrawable::DetachLayer() { |
154 layer_->RemoveFromParent(); | 180 layer_->RemoveFromParent(); |
155 } | 181 } |
156 | 182 |
157 void CompositedTouchHandleDrawable::UpdateLayerPosition() { | 183 void CompositedTouchHandleDrawable::UpdateLayerPosition() { |
158 layer_->SetPosition(focal_position_ - focal_offset_from_origin_); | 184 layer_->SetPosition(focal_position_ - focal_offset_from_origin_); |
159 } | 185 } |
160 | 186 |
161 // static | 187 // static |
162 bool CompositedTouchHandleDrawable::RegisterHandleViewResources(JNIEnv* env) { | 188 bool CompositedTouchHandleDrawable::RegisterHandleViewResources(JNIEnv* env) { |
163 return RegisterNativesImpl(env); | 189 return RegisterNativesImpl(env); |
164 } | 190 } |
165 | 191 |
166 } // namespace content | 192 } // namespace content |
OLD | NEW |