Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: chrome/browser/android/vr_shell/ui_scene.cc

Issue 2807293002: VR: Rename ContentRectangle to UiElement (Closed)
Patch Set: Rebase onto Michael's landed GVR types CL. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.h" 15 #include "chrome/browser/android/vr_shell/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 // Parse an integer to an int or enum value. 22 // Parse an integer to an int or enum value.
23 template <typename T> 23 template <typename T>
24 bool ParseInt(const base::DictionaryValue& dict, 24 bool ParseInt(const base::DictionaryValue& dict,
25 const std::string& key, 25 const std::string& key,
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 case easing::EasingType::EASEINOUT: { 135 case easing::EasingType::EASEINOUT: {
136 double pow; 136 double pow;
137 CHECK(dict.GetDouble("pow", &pow)); 137 CHECK(dict.GetDouble("pow", &pow));
138 result = base::MakeUnique<easing::EaseInOut>(pow); 138 result = base::MakeUnique<easing::EaseInOut>(pow);
139 break; 139 break;
140 } 140 }
141 } 141 }
142 return result; 142 return result;
143 } 143 }
144 144
145 void ApplyAnchoring(const ContentRectangle& parent, 145 void ApplyAnchoring(const UiElement& parent,
146 XAnchoring x_anchoring, 146 XAnchoring x_anchoring,
147 YAnchoring y_anchoring, 147 YAnchoring y_anchoring,
148 Transform* transform) { 148 Transform* transform) {
149 // To anchor a child, use the parent's size to find its edge. 149 // To anchor a child, use the parent's size to find its edge.
150 float x_offset; 150 float x_offset;
151 switch (x_anchoring) { 151 switch (x_anchoring) {
152 case XLEFT: 152 case XLEFT:
153 x_offset = -0.5f * parent.size.x(); 153 x_offset = -0.5f * parent.size.x();
154 break; 154 break;
155 case XRIGHT: 155 case XRIGHT:
(...skipping 13 matching lines...) Expand all
169 break; 169 break;
170 case YNONE: 170 case YNONE:
171 y_offset = 0.0f; 171 y_offset = 0.0f;
172 break; 172 break;
173 } 173 }
174 transform->Translate(gfx::Vector3dF(x_offset, y_offset, 0)); 174 transform->Translate(gfx::Vector3dF(x_offset, y_offset, 0));
175 } 175 }
176 176
177 } // namespace 177 } // namespace
178 178
179 void UiScene::AddUiElement(std::unique_ptr<ContentRectangle> element) { 179 void UiScene::AddUiElement(std::unique_ptr<UiElement> element) {
180 CHECK_GE(element->id, 0); 180 CHECK_GE(element->id, 0);
181 CHECK_EQ(GetUiElementById(element->id), nullptr); 181 CHECK_EQ(GetUiElementById(element->id), nullptr);
182 if (element->parent_id >= 0) { 182 if (element->parent_id >= 0) {
183 CHECK_NE(GetUiElementById(element->parent_id), nullptr); 183 CHECK_NE(GetUiElementById(element->parent_id), nullptr);
184 } else { 184 } else {
185 CHECK_EQ(element->x_anchoring, XAnchoring::XNONE); 185 CHECK_EQ(element->x_anchoring, XAnchoring::XNONE);
186 CHECK_EQ(element->y_anchoring, YAnchoring::YNONE); 186 CHECK_EQ(element->y_anchoring, YAnchoring::YNONE);
187 } 187 }
188 ui_elements_.push_back(std::move(element)); 188 ui_elements_.push_back(std::move(element));
189 } 189 }
190 190
191 void UiScene::AddUiElementFromDict(const base::DictionaryValue& dict) { 191 void UiScene::AddUiElementFromDict(const base::DictionaryValue& dict) {
192 int id; 192 int id;
193 CHECK(ParseInt(dict, "id", &id)); 193 CHECK(ParseInt(dict, "id", &id));
194 CHECK_EQ(GetUiElementById(id), nullptr); 194 CHECK_EQ(GetUiElementById(id), nullptr);
195 195
196 auto element = base::MakeUnique<ContentRectangle>(); 196 auto element = base::MakeUnique<UiElement>();
197 element->id = id; 197 element->id = id;
198 198
199 ApplyDictToElement(dict, element.get()); 199 ApplyDictToElement(dict, element.get());
200 ui_elements_.push_back(std::move(element)); 200 ui_elements_.push_back(std::move(element));
201 } 201 }
202 202
203 void UiScene::UpdateUiElementFromDict(const base::DictionaryValue& dict) { 203 void UiScene::UpdateUiElementFromDict(const base::DictionaryValue& dict) {
204 int id; 204 int id;
205 CHECK(ParseInt(dict, "id", &id)); 205 CHECK(ParseInt(dict, "id", &id));
206 ContentRectangle* element = GetUiElementById(id); 206 UiElement* element = GetUiElementById(id);
207 CHECK_NE(element, nullptr); 207 CHECK_NE(element, nullptr);
208 ApplyDictToElement(dict, element); 208 ApplyDictToElement(dict, element);
209 } 209 }
210 210
211 void UiScene::RemoveUiElement(int element_id) { 211 void UiScene::RemoveUiElement(int element_id) {
212 for (auto it = ui_elements_.begin(); it != ui_elements_.end(); ++it) { 212 for (auto it = ui_elements_.begin(); it != ui_elements_.end(); ++it) {
213 if ((*it)->id == element_id) { 213 if ((*it)->id == element_id) {
214 if ((*it)->fill == Fill::CONTENT) { 214 if ((*it)->fill == Fill::CONTENT) {
215 content_element_ = nullptr; 215 content_element_ = nullptr;
216 } 216 }
217 ui_elements_.erase(it); 217 ui_elements_.erase(it);
218 return; 218 return;
219 } 219 }
220 } 220 }
221 } 221 }
222 222
223 void UiScene::AddAnimation(int element_id, 223 void UiScene::AddAnimation(int element_id,
224 std::unique_ptr<Animation> animation) { 224 std::unique_ptr<Animation> animation) {
225 ContentRectangle* element = GetUiElementById(element_id); 225 UiElement* element = GetUiElementById(element_id);
226 CHECK_NE(element, nullptr); 226 CHECK_NE(element, nullptr);
227 for (auto& existing_animation : element->animations) { 227 for (auto& existing_animation : element->animations) {
228 CHECK_NE(existing_animation->id, animation->id); 228 CHECK_NE(existing_animation->id, animation->id);
229 } 229 }
230 element->animations.emplace_back(std::move(animation)); 230 element->animations.emplace_back(std::move(animation));
231 } 231 }
232 232
233 void UiScene::AddAnimationFromDict(const base::DictionaryValue& dict, 233 void UiScene::AddAnimationFromDict(const base::DictionaryValue& dict,
234 const base::TimeTicks& current_time) { 234 const base::TimeTicks& current_time) {
235 int animation_id; 235 int animation_id;
(...skipping 24 matching lines...) Expand all
260 // start from the element's current location. 260 // start from the element's current location.
261 dict.GetDictionary("from", &from_dict); 261 dict.GetDictionary("from", &from_dict);
262 if (from_dict != nullptr) { 262 if (from_dict != nullptr) {
263 ParseEndpointToFloats(property, *from_dict, &from); 263 ParseEndpointToFloats(property, *from_dict, &from);
264 } 264 }
265 265
266 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(start_time_ms); 266 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(start_time_ms);
267 base::TimeTicks start = current_time + delay; 267 base::TimeTicks start = current_time + delay;
268 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(duration_ms); 268 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(duration_ms);
269 269
270 ContentRectangle* element = GetUiElementById(element_id); 270 UiElement* element = GetUiElementById(element_id);
271 CHECK_NE(element, nullptr); 271 CHECK_NE(element, nullptr);
272 element->animations.emplace_back(base::MakeUnique<Animation>( 272 element->animations.emplace_back(base::MakeUnique<Animation>(
273 animation_id, static_cast<Animation::Property>(property), 273 animation_id, static_cast<Animation::Property>(property),
274 std::move(easing), from, to, start, duration)); 274 std::move(easing), from, to, start, duration));
275 } 275 }
276 276
277 void UiScene::RemoveAnimation(int element_id, int animation_id) { 277 void UiScene::RemoveAnimation(int element_id, int animation_id) {
278 ContentRectangle* element = GetUiElementById(element_id); 278 UiElement* element = GetUiElementById(element_id);
279 CHECK_NE(element, nullptr); 279 CHECK_NE(element, nullptr);
280 auto& animations = element->animations; 280 auto& animations = element->animations;
281 for (auto it = animations.begin(); it != animations.end(); ++it) { 281 for (auto it = animations.begin(); it != animations.end(); ++it) {
282 const Animation& existing_animation = **it; 282 const Animation& existing_animation = **it;
283 if (existing_animation.id == animation_id) { 283 if (existing_animation.id == animation_id) {
284 animations.erase(it); 284 animations.erase(it);
285 return; 285 return;
286 } 286 }
287 } 287 }
288 } 288 }
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 for (auto& element : ui_elements_) { 334 for (auto& element : ui_elements_) {
335 // Process all animations before calculating object transforms. 335 // Process all animations before calculating object transforms.
336 element->Animate(time); 336 element->Animate(time);
337 element->dirty = true; 337 element->dirty = true;
338 } 338 }
339 for (auto& element : ui_elements_) { 339 for (auto& element : ui_elements_) {
340 ApplyRecursiveTransforms(element.get()); 340 ApplyRecursiveTransforms(element.get());
341 } 341 }
342 } 342 }
343 343
344 ContentRectangle* UiScene::GetUiElementById(int element_id) { 344 UiElement* UiScene::GetUiElementById(int element_id) {
345 for (auto& element : ui_elements_) { 345 for (auto& element : ui_elements_) {
346 if (element->id == element_id) { 346 if (element->id == element_id) {
347 return element.get(); 347 return element.get();
348 } 348 }
349 } 349 }
350 return nullptr; 350 return nullptr;
351 } 351 }
352 352
353 std::vector<const ContentRectangle*> UiScene::GetWorldElements() const { 353 std::vector<const UiElement*> UiScene::GetWorldElements() const {
354 std::vector<const ContentRectangle*> elements; 354 std::vector<const UiElement*> elements;
355 for (const auto& element : ui_elements_) { 355 for (const auto& element : ui_elements_) {
356 if (element->IsVisible() && !element->lock_to_fov) { 356 if (element->IsVisible() && !element->lock_to_fov) {
357 elements.push_back(element.get()); 357 elements.push_back(element.get());
358 } 358 }
359 } 359 }
360 return elements; 360 return elements;
361 } 361 }
362 362
363 std::vector<const ContentRectangle*> UiScene::GetHeadLockedElements() const { 363 std::vector<const UiElement*> UiScene::GetHeadLockedElements() const {
364 std::vector<const ContentRectangle*> elements; 364 std::vector<const UiElement*> elements;
365 for (const auto& element : ui_elements_) { 365 for (const auto& element : ui_elements_) {
366 if (element->IsVisible() && element->lock_to_fov) { 366 if (element->IsVisible() && element->lock_to_fov) {
367 elements.push_back(element.get()); 367 elements.push_back(element.get());
368 } 368 }
369 } 369 }
370 return elements; 370 return elements;
371 } 371 }
372 372
373 bool UiScene::HasVisibleHeadLockedElements() const { 373 bool UiScene::HasVisibleHeadLockedElements() const {
374 return !GetHeadLockedElements().empty(); 374 return !GetHeadLockedElements().empty();
375 } 375 }
376 376
377 const vr::Colorf& UiScene::GetBackgroundColor() const { 377 const vr::Colorf& UiScene::GetBackgroundColor() const {
378 return background_color_; 378 return background_color_;
379 } 379 }
380 380
381 float UiScene::GetBackgroundDistance() const { 381 float UiScene::GetBackgroundDistance() const {
382 return background_distance_; 382 return background_distance_;
383 } 383 }
384 384
385 bool UiScene::GetWebVrRenderingEnabled() const { 385 bool UiScene::GetWebVrRenderingEnabled() const {
386 return webvr_rendering_enabled_; 386 return webvr_rendering_enabled_;
387 } 387 }
388 388
389 const std::vector<std::unique_ptr<ContentRectangle>>& UiScene::GetUiElements() 389 const std::vector<std::unique_ptr<UiElement>>& UiScene::GetUiElements() const {
390 const {
391 return ui_elements_; 390 return ui_elements_;
392 } 391 }
393 392
394 UiScene::UiScene() = default; 393 UiScene::UiScene() = default;
395 394
396 UiScene::~UiScene() = default; 395 UiScene::~UiScene() = default;
397 396
398 void UiScene::ApplyRecursiveTransforms(ContentRectangle* element) { 397 void UiScene::ApplyRecursiveTransforms(UiElement* element) {
399 if (!element->dirty) 398 if (!element->dirty)
400 return; 399 return;
401 400
402 ContentRectangle* parent = nullptr; 401 UiElement* parent = nullptr;
403 if (element->parent_id >= 0) { 402 if (element->parent_id >= 0) {
404 parent = GetUiElementById(element->parent_id); 403 parent = GetUiElementById(element->parent_id);
405 CHECK(parent != nullptr); 404 CHECK(parent != nullptr);
406 } 405 }
407 406
408 Transform* transform = element->mutable_transform(); 407 Transform* transform = element->mutable_transform();
409 transform->MakeIdentity(); 408 transform->MakeIdentity();
410 transform->Scale(element->size); 409 transform->Scale(element->size);
411 element->computed_opacity = element->opacity; 410 element->computed_opacity = element->opacity;
412 element->computed_lock_to_fov = element->lock_to_fov; 411 element->computed_lock_to_fov = element->lock_to_fov;
(...skipping 15 matching lines...) Expand all
428 element->computed_opacity *= parent->opacity; 427 element->computed_opacity *= parent->opacity;
429 element->computed_lock_to_fov = parent->lock_to_fov; 428 element->computed_lock_to_fov = parent->lock_to_fov;
430 } 429 }
431 430
432 vr::MatrixMul(inheritable->to_world, transform->to_world, 431 vr::MatrixMul(inheritable->to_world, transform->to_world,
433 &transform->to_world); 432 &transform->to_world);
434 element->dirty = false; 433 element->dirty = false;
435 } 434 }
436 435
437 void UiScene::ApplyDictToElement(const base::DictionaryValue& dict, 436 void UiScene::ApplyDictToElement(const base::DictionaryValue& dict,
438 ContentRectangle* element) { 437 UiElement* element) {
439 int parent_id; 438 int parent_id;
440 439
441 if (ParseInt(dict, "parentId", &parent_id)) { 440 if (ParseInt(dict, "parentId", &parent_id)) {
442 CHECK_GE(parent_id, 0); 441 CHECK_GE(parent_id, 0);
443 CHECK_NE(GetUiElementById(parent_id), nullptr); 442 CHECK_NE(GetUiElementById(parent_id), nullptr);
444 element->parent_id = parent_id; 443 element->parent_id = parent_id;
445 } 444 }
446 445
447 dict.GetString("name", &element->name); 446 dict.GetString("name", &element->name);
448 dict.GetBoolean("visible", &element->visible); 447 dict.GetBoolean("visible", &element->visible);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 content_element_ = element; 515 content_element_ = element;
517 break; 516 break;
518 default: 517 default:
519 element->fill = Fill::NONE; 518 element->fill = Fill::NONE;
520 break; 519 break;
521 } 520 }
522 } 521 }
523 } 522 }
524 523
525 } // namespace vr_shell 524 } // namespace vr_shell
OLDNEW
« no previous file with comments | « chrome/browser/android/vr_shell/ui_scene.h ('k') | chrome/browser/android/vr_shell/ui_scene_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698