OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_scene.h" | 5 #include "chrome/browser/android/vr_shell/ui_scene.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/android/vr_shell/animation.h" | 13 #include "chrome/browser/android/vr_shell/animation.h" |
14 #include "chrome/browser/android/vr_shell/easing.h" | 14 #include "chrome/browser/android/vr_shell/easing.h" |
15 #include "chrome/browser/android/vr_shell/ui_elements/ui_element.h" | 15 #include "chrome/browser/android/vr_shell/ui_elements/ui_element.h" |
16 #include "device/vr/vr_math.h" | 16 #include "device/vr/vr_math.h" |
17 | 17 |
18 namespace vr_shell { | 18 namespace vr_shell { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 void ApplyAnchoring(const UiElement& parent, | 22 void ApplyAnchoring(const UiElement& parent, |
23 XAnchoring x_anchoring, | 23 XAnchoring x_anchoring, |
24 YAnchoring y_anchoring, | 24 YAnchoring y_anchoring, |
25 Transform* transform) { | 25 Transform* transform) { |
26 // To anchor a child, use the parent's size to find its edge. | 26 // To anchor a child, use the parent's size to find its edge. |
27 float x_offset; | 27 float x_offset; |
28 switch (x_anchoring) { | 28 switch (x_anchoring) { |
29 case XLEFT: | 29 case XLEFT: |
30 x_offset = -0.5f * parent.size.x(); | 30 x_offset = -0.5f * parent.size().x(); |
31 break; | 31 break; |
32 case XRIGHT: | 32 case XRIGHT: |
33 x_offset = 0.5f * parent.size.x(); | 33 x_offset = 0.5f * parent.size().x(); |
34 break; | 34 break; |
35 case XNONE: | 35 case XNONE: |
36 x_offset = 0.0f; | 36 x_offset = 0.0f; |
37 break; | 37 break; |
38 } | 38 } |
39 float y_offset; | 39 float y_offset; |
40 switch (y_anchoring) { | 40 switch (y_anchoring) { |
41 case YTOP: | 41 case YTOP: |
42 y_offset = 0.5f * parent.size.y(); | 42 y_offset = 0.5f * parent.size().y(); |
43 break; | 43 break; |
44 case YBOTTOM: | 44 case YBOTTOM: |
45 y_offset = -0.5f * parent.size.y(); | 45 y_offset = -0.5f * parent.size().y(); |
46 break; | 46 break; |
47 case YNONE: | 47 case YNONE: |
48 y_offset = 0.0f; | 48 y_offset = 0.0f; |
49 break; | 49 break; |
50 } | 50 } |
51 transform->Translate(gfx::Vector3dF(x_offset, y_offset, 0)); | 51 transform->Translate(gfx::Vector3dF(x_offset, y_offset, 0)); |
52 } | 52 } |
53 | 53 |
54 } // namespace | 54 } // namespace |
55 | 55 |
56 void UiScene::AddUiElement(std::unique_ptr<UiElement> element) { | 56 void UiScene::AddUiElement(std::unique_ptr<UiElement> element) { |
57 CHECK_GE(element->id, 0); | 57 CHECK_GE(element->id(), 0); |
58 CHECK_EQ(GetUiElementById(element->id), nullptr); | 58 CHECK_EQ(GetUiElementById(element->id()), nullptr); |
59 if (element->parent_id >= 0) { | 59 if (element->parent_id() >= 0) { |
60 CHECK_NE(GetUiElementById(element->parent_id), nullptr); | 60 CHECK_NE(GetUiElementById(element->parent_id()), nullptr); |
61 } else { | 61 } else { |
62 CHECK_EQ(element->x_anchoring, XAnchoring::XNONE); | 62 CHECK_EQ(element->x_anchoring(), XAnchoring::XNONE); |
63 CHECK_EQ(element->y_anchoring, YAnchoring::YNONE); | 63 CHECK_EQ(element->y_anchoring(), YAnchoring::YNONE); |
64 } | 64 } |
65 if (gl_initialized_) | 65 if (gl_initialized_) |
66 element->Initialize(); | 66 element->Initialize(); |
67 ui_elements_.push_back(std::move(element)); | 67 ui_elements_.push_back(std::move(element)); |
68 } | 68 } |
69 | 69 |
70 void UiScene::RemoveUiElement(int element_id) { | 70 void UiScene::RemoveUiElement(int element_id) { |
71 for (auto it = ui_elements_.begin(); it != ui_elements_.end(); ++it) { | 71 for (auto it = ui_elements_.begin(); it != ui_elements_.end(); ++it) { |
72 if ((*it)->id == element_id) { | 72 if ((*it)->id() == element_id) { |
73 if ((*it)->fill == Fill::CONTENT) { | 73 if ((*it)->fill() == Fill::CONTENT) { |
74 content_element_ = nullptr; | 74 content_element_ = nullptr; |
75 } | 75 } |
76 ui_elements_.erase(it); | 76 ui_elements_.erase(it); |
77 return; | 77 return; |
78 } | 78 } |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 void UiScene::AddAnimation(int element_id, | 82 void UiScene::AddAnimation(int element_id, |
83 std::unique_ptr<Animation> animation) { | 83 std::unique_ptr<Animation> animation) { |
84 UiElement* element = GetUiElementById(element_id); | 84 UiElement* element = GetUiElementById(element_id); |
85 CHECK_NE(element, nullptr); | 85 CHECK_NE(element, nullptr); |
86 for (const std::unique_ptr<Animation>& existing : element->animations) { | 86 for (const std::unique_ptr<Animation>& existing : element->animations()) { |
87 CHECK_NE(existing->id, animation->id); | 87 CHECK_NE(existing->id, animation->id); |
88 } | 88 } |
89 element->animations.emplace_back(std::move(animation)); | 89 element->animations().emplace_back(std::move(animation)); |
90 } | 90 } |
91 | 91 |
92 void UiScene::RemoveAnimation(int element_id, int animation_id) { | 92 void UiScene::RemoveAnimation(int element_id, int animation_id) { |
93 UiElement* element = GetUiElementById(element_id); | 93 UiElement* element = GetUiElementById(element_id); |
94 CHECK_NE(element, nullptr); | 94 CHECK_NE(element, nullptr); |
95 auto& animations = element->animations; | 95 auto& animations = element->animations(); |
96 for (auto it = animations.begin(); it != animations.end(); ++it) { | 96 for (auto it = animations.begin(); it != animations.end(); ++it) { |
97 const Animation& existing_animation = **it; | 97 const Animation& existing_animation = **it; |
98 if (existing_animation.id == animation_id) { | 98 if (existing_animation.id == animation_id) { |
99 animations.erase(it); | 99 animations.erase(it); |
100 return; | 100 return; |
101 } | 101 } |
102 } | 102 } |
103 } | 103 } |
104 | 104 |
105 void UiScene::UpdateTransforms(const base::TimeTicks& time) { | 105 void UiScene::UpdateTransforms(const base::TimeTicks& time) { |
106 for (const std::unique_ptr<UiElement>& element : ui_elements_) { | 106 for (const auto& element : ui_elements_) { |
107 // Process all animations before calculating object transforms. | 107 // Process all animations before calculating object transforms. |
108 element->Animate(time); | 108 element->Animate(time); |
109 element->dirty = true; | 109 element->set_dirty(true); |
110 } | 110 } |
111 for (auto& element : ui_elements_) { | 111 for (auto& element : ui_elements_) { |
112 ApplyRecursiveTransforms(element.get()); | 112 ApplyRecursiveTransforms(element.get()); |
113 } | 113 } |
114 } | 114 } |
115 | 115 |
116 UiElement* UiScene::GetUiElementById(int element_id) { | 116 UiElement* UiScene::GetUiElementById(int element_id) { |
117 for (const std::unique_ptr<UiElement>& element : ui_elements_) { | 117 for (const auto& element : ui_elements_) { |
118 if (element->id == element_id) { | 118 if (element->id() == element_id) { |
119 return element.get(); | 119 return element.get(); |
120 } | 120 } |
121 } | 121 } |
122 return nullptr; | 122 return nullptr; |
123 } | 123 } |
124 | 124 |
125 std::vector<const UiElement*> UiScene::GetWorldElements() const { | 125 std::vector<const UiElement*> UiScene::GetWorldElements() const { |
126 std::vector<const UiElement*> elements; | 126 std::vector<const UiElement*> elements; |
127 for (const std::unique_ptr<UiElement>& element : ui_elements_) { | 127 for (const auto& element : ui_elements_) { |
128 if (element->IsVisible() && !element->lock_to_fov) { | 128 if (element->IsVisible() && !element->lock_to_fov()) { |
129 elements.push_back(element.get()); | 129 elements.push_back(element.get()); |
130 } | 130 } |
131 } | 131 } |
132 return elements; | 132 return elements; |
133 } | 133 } |
134 | 134 |
135 std::vector<const UiElement*> UiScene::GetHeadLockedElements() const { | 135 std::vector<const UiElement*> UiScene::GetHeadLockedElements() const { |
136 std::vector<const UiElement*> elements; | 136 std::vector<const UiElement*> elements; |
137 for (const std::unique_ptr<UiElement>& element : ui_elements_) { | 137 for (const auto& element : ui_elements_) { |
138 if (element->IsVisible() && element->lock_to_fov) { | 138 if (element->IsVisible() && element->lock_to_fov()) { |
139 elements.push_back(element.get()); | 139 elements.push_back(element.get()); |
140 } | 140 } |
141 } | 141 } |
142 return elements; | 142 return elements; |
143 } | 143 } |
144 | 144 |
145 bool UiScene::HasVisibleHeadLockedElements() const { | 145 bool UiScene::HasVisibleHeadLockedElements() const { |
146 return !GetHeadLockedElements().empty(); | 146 return !GetHeadLockedElements().empty(); |
147 } | 147 } |
148 | 148 |
(...skipping 23 matching lines...) Expand all Loading... |
172 | 172 |
173 const std::vector<std::unique_ptr<UiElement>>& UiScene::GetUiElements() const { | 173 const std::vector<std::unique_ptr<UiElement>>& UiScene::GetUiElements() const { |
174 return ui_elements_; | 174 return ui_elements_; |
175 } | 175 } |
176 | 176 |
177 UiScene::UiScene() = default; | 177 UiScene::UiScene() = default; |
178 | 178 |
179 UiScene::~UiScene() = default; | 179 UiScene::~UiScene() = default; |
180 | 180 |
181 void UiScene::ApplyRecursiveTransforms(UiElement* element) { | 181 void UiScene::ApplyRecursiveTransforms(UiElement* element) { |
182 if (!element->dirty) | 182 if (!element->dirty()) |
183 return; | 183 return; |
184 | 184 |
185 UiElement* parent = nullptr; | 185 UiElement* parent = nullptr; |
186 if (element->parent_id >= 0) { | 186 if (element->parent_id() >= 0) { |
187 parent = GetUiElementById(element->parent_id); | 187 parent = GetUiElementById(element->parent_id()); |
188 CHECK(parent != nullptr); | 188 CHECK(parent != nullptr); |
189 } | 189 } |
190 | 190 |
191 Transform* transform = element->mutable_transform(); | 191 Transform* transform = element->mutable_transform(); |
192 transform->MakeIdentity(); | 192 transform->MakeIdentity(); |
193 transform->Scale(element->size); | 193 transform->Scale(element->size()); |
194 element->computed_opacity = element->opacity; | 194 element->set_computed_opacity(element->opacity()); |
195 element->computed_lock_to_fov = element->lock_to_fov; | 195 element->set_computed_lock_to_fov(element->lock_to_fov()); |
196 | 196 |
197 // Compute an inheritable transformation that can be applied to this element, | 197 // Compute an inheritable transformation that can be applied to this element, |
198 // and it's children, if applicable. | 198 // and it's children, if applicable. |
199 Transform* inheritable = &element->inheritable_transform; | 199 Transform* inheritable = &element->inheritable_transform(); |
200 inheritable->MakeIdentity(); | 200 inheritable->MakeIdentity(); |
201 inheritable->Scale(element->scale); | 201 inheritable->Scale(element->scale()); |
202 inheritable->Rotate(element->rotation); | 202 inheritable->Rotate(element->rotation()); |
203 inheritable->Translate(element->translation); | 203 inheritable->Translate(element->translation()); |
204 if (parent) { | 204 if (parent) { |
205 ApplyAnchoring(*parent, element->x_anchoring, element->y_anchoring, | 205 ApplyAnchoring(*parent, element->x_anchoring(), element->y_anchoring(), |
206 inheritable); | 206 inheritable); |
207 ApplyRecursiveTransforms(parent); | 207 ApplyRecursiveTransforms(parent); |
208 vr::MatrixMul(parent->inheritable_transform.to_world, inheritable->to_world, | 208 vr::MatrixMul(parent->inheritable_transform().to_world, |
209 &inheritable->to_world); | 209 inheritable->to_world, &inheritable->to_world); |
210 | 210 |
211 element->computed_opacity *= parent->opacity; | 211 element->set_computed_opacity(element->computed_opacity() * |
212 element->computed_lock_to_fov = parent->lock_to_fov; | 212 parent->opacity()); |
| 213 element->set_computed_lock_to_fov(parent->lock_to_fov()); |
213 } | 214 } |
214 | 215 |
215 vr::MatrixMul(inheritable->to_world, transform->to_world, | 216 vr::MatrixMul(inheritable->to_world, transform->to_world, |
216 &transform->to_world); | 217 &transform->to_world); |
217 element->dirty = false; | 218 element->set_dirty(false); |
218 } | 219 } |
219 | 220 |
220 // TODO(mthiesse): Move this to UiSceneManager. | 221 // TODO(mthiesse): Move this to UiSceneManager. |
221 void UiScene::OnGLInitialized() { | 222 void UiScene::OnGLInitialized() { |
222 gl_initialized_ = true; | 223 gl_initialized_ = true; |
223 for (const std::unique_ptr<UiElement>& element : ui_elements_) { | 224 for (auto& element : ui_elements_) { |
224 element->Initialize(); | 225 element->Initialize(); |
225 } | 226 } |
226 } | 227 } |
227 | 228 |
228 } // namespace vr_shell | 229 } // namespace vr_shell |
OLD | NEW |