Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "chrome/browser/android/vr_shell/ui_elements/close_button.h" | 5 #include "chrome/browser/android/vr_shell/ui_elements/close_button.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "chrome/browser/android/vr_shell/textures/close_button_texture.h" | 8 #include "chrome/browser/android/vr_shell/textures/close_button_texture.h" |
| 9 | 9 |
| 10 namespace vr_shell { | 10 namespace vr_shell { |
| 11 | 11 |
| 12 CloseButton::CloseButton(base::Callback<void()> click_handler) | 12 CloseButton::CloseButton(base::Callback<void()> click_handler) |
| 13 : TexturedElement(256), | 13 : TexturedElement(256), |
| 14 texture_(base::MakeUnique<CloseButtonTexture>()), | 14 texture_(base::MakeUnique<CloseButtonTexture>()), |
| 15 click_handler_(click_handler) {} | 15 click_handler_(click_handler) {} |
| 16 | 16 |
| 17 CloseButton::~CloseButton() = default; | 17 CloseButton::~CloseButton() = default; |
| 18 | 18 |
| 19 void CloseButton::OnHoverEnter(gfx::PointF position) { | 19 void CloseButton::OnHoverEnter(gfx::PointF position) { |
| 20 hover_ = true; | 20 OnStateUpdated(position); |
| 21 OnStateUpdated(); | |
| 22 } | 21 } |
| 23 | 22 |
| 24 void CloseButton::OnHoverLeave() { | 23 void CloseButton::OnHoverLeave() { |
| 25 hover_ = false; | 24 OnStateUpdated(gfx::PointF(-1000, -1000)); |
|
cjgrant
2017/05/15 16:55:09
Should probably use language numeric limits here,
mthiesse
2017/05/15 21:11:27
I didn't want the code to have to think about infi
cjgrant
2017/05/15 21:51:52
I just meant to use the modern equivalent of INT_M
mthiesse
2017/05/15 22:23:32
Done.
| |
| 26 OnStateUpdated(); | 25 } |
| 26 | |
| 27 void CloseButton::OnMove(gfx::PointF position) { | |
| 28 OnStateUpdated(position); | |
| 27 } | 29 } |
| 28 | 30 |
| 29 void CloseButton::OnButtonDown(gfx::PointF position) { | 31 void CloseButton::OnButtonDown(gfx::PointF position) { |
| 30 down_ = true; | 32 down_ = true; |
| 31 OnStateUpdated(); | 33 OnStateUpdated(position); |
| 32 } | 34 } |
| 33 | 35 |
| 34 void CloseButton::OnButtonUp(gfx::PointF position) { | 36 void CloseButton::OnButtonUp(gfx::PointF position) { |
| 35 down_ = false; | 37 down_ = false; |
| 36 OnStateUpdated(); | 38 OnStateUpdated(position); |
| 37 if (position.x() < 0 || position.x() > 1.0f) | 39 if (HitTest(position)) |
|
cjgrant
2017/05/15 16:55:09
At first glance, I'd have thought the common code
mthiesse
2017/05/15 21:11:26
I think this will make more sense once you see the
| |
| 38 return; | 40 click_handler_.Run(); |
| 39 if (position.y() < 0 || position.y() > 1.0f) | 41 } |
| 40 return; | 42 |
| 41 click_handler_.Run(); | 43 bool CloseButton::HitTest(gfx::PointF point) const { |
| 44 return (point - gfx::PointF(0.5, 0.5)).LengthSquared() < 0.25; | |
| 42 } | 45 } |
| 43 | 46 |
| 44 UiTexture* CloseButton::GetTexture() const { | 47 UiTexture* CloseButton::GetTexture() const { |
| 45 return texture_.get(); | 48 return texture_.get(); |
| 46 } | 49 } |
| 47 | 50 |
| 48 void CloseButton::OnStateUpdated() { | 51 void CloseButton::OnStateUpdated(gfx::PointF position) { |
| 49 int flags = hover_ ? CloseButtonTexture::FLAG_HOVER : 0; | 52 bool hitting = HitTest(position); |
| 50 flags |= (down_ && hover_) ? CloseButtonTexture::FLAG_DOWN : 0; | 53 bool down = hitting ? down_ : false; |
| 54 | |
| 55 int flags = hitting ? CloseButtonTexture::FLAG_HOVER : 0; | |
| 56 flags |= down ? CloseButtonTexture::FLAG_DOWN : 0; | |
| 51 if (!texture_->SetDrawFlags(flags)) | 57 if (!texture_->SetDrawFlags(flags)) |
| 52 return; | 58 return; |
| 53 UpdateTexture(); | 59 UpdateTexture(); |
| 54 } | 60 } |
| 55 | 61 |
| 56 } // namespace vr_shell | 62 } // namespace vr_shell |
| OLD | NEW |