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

Side by Side Diff: chrome/browser/android/vr_shell/textures/exit_prompt_texture.cc

Issue 2913633002: [vr] Clicking on the security icon should prompt the user to bail out of VR (Closed)
Patch Set: . Created 3 years, 6 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/android/vr_shell/textures/exit_prompt_texture.h"
6
7 #include "cc/paint/skia_paint_canvas.h"
8 #include "chrome/browser/android/vr_shell/color_scheme.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "components/strings/grit/components_strings.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/font_list.h"
14 #include "ui/gfx/geometry/rect.h"
15 #include "ui/gfx/geometry/vector2d.h"
16 #include "ui/gfx/paint_vector_icon.h"
17 #include "ui/gfx/render_text.h"
18 #include "ui/gfx/vector_icon_types.h"
19 #include "ui/vector_icons/vector_icons.h"
20
21 #include "ui/gfx/color_palette.h"
22
23 namespace vr_shell {
24
25 namespace {
26
27 constexpr float kWidth = 0.672;
cjgrant 2017/06/02 04:16:35 Hmm, I'm used to these being static, but the codeb
ymalik 2017/06/02 21:14:31 There's no difference because things in anonymous
28 constexpr float kHeight = 0.2;
29 constexpr float kButtonWidth = 0.162;
30 constexpr float kButtonHeight = 0.066;
31 constexpr float kPromptTextButtonSeperatorHeight = 0.04;
32 constexpr float kButtonsSeperatorWidth = 0.01;
33 constexpr float kButtonRadiusFactor = 0.006;
34 constexpr float kFontSizeFactor = 0.048;
35
36 } // namespace
37
38 ExitPromptTexture::ExitPromptTexture() = default;
39
40 ExitPromptTexture::~ExitPromptTexture() = default;
41
42 const ColorScheme& ExitPromptTexture::color_scheme() const {
43 return ColorScheme::GetColorScheme(mode());
44 }
45
46 void ExitPromptTexture::Draw(SkCanvas* sk_canvas,
47 const gfx::Size& texture_size) {
48 size_.set_width(texture_size.width());
49 size_.set_height(texture_size.height());
50
51 cc::SkiaPaintCanvas paint_canvas(sk_canvas);
52 gfx::Canvas gfx_canvas(&paint_canvas, 1.0f);
53 gfx::Canvas* canvas = &gfx_canvas;
54
55 // Prompt text area
cjgrant 2017/06/02 04:16:35 nit: Periods on comments. :)
ymalik 2017/06/02 21:14:31 Done.
56 auto text = l10n_util::GetStringUTF16(IDS_VR_SHELL_EXIT_PROMPT_DESCRIPTION);
57 gfx::FontList fonts;
58 GetFontList(size_.width() * kFontSizeFactor, text, &fonts);
59 gfx::Rect prompt_text_size(size_.width(), 0);
60 std::vector<std::unique_ptr<gfx::RenderText>> lines = PrepareDrawStringRect(
61 text, fonts, color_scheme().prompt_text, &prompt_text_size,
62 kTextAlignmentCenter, kWrappingBehaviorWrap);
63 for (auto& render_text : lines)
64 render_text->Draw(canvas);
65
66 SkPaint paint;
67 gfx::Rect button_text_size(ToPixels(kButtonWidth), 0);
68 float radius = size_.width() * kButtonRadiusFactor;
69
70 // Secondary button area
71 text = l10n_util::GetStringUTF16(IDS_VR_SHELL_EXIT_PROMPT_EXIT_VR_BUTTON);
72 lines = PrepareDrawStringRect(
73 text, fonts, color_scheme().secondary_button_text, &button_text_size,
74 kTextAlignmentCenter, kWrappingBehaviorWrap);
75 secondary_button_rect_.SetRect(
76 ToPixels(kWidth) / 2 - ToPixels(kButtonsSeperatorWidth) -
cjgrant 2017/06/02 04:16:36 Why 3 ToPixels calls instead of one, on the result
ymalik 2017/06/02 21:14:31 I was being silly :)
77 ToPixels(kButtonWidth),
78 prompt_text_size.height() + ToPixels(kPromptTextButtonSeperatorHeight),
79 ToPixels(kButtonWidth), ToPixels(kButtonHeight));
80 paint.setColor(secondary_hovered_
81 ? color_scheme().button_background_hover
82 : color_scheme().secondary_button_background);
83 canvas->Save();
84 canvas->Translate(
85 gfx::Vector2d(secondary_button_rect_.x(), secondary_button_rect_.y()));
86 sk_canvas->drawRoundRect(
87 SkRect::MakeXYWH(0, 0, ToPixels(kButtonWidth), ToPixels(kButtonHeight)),
88 radius, radius, paint);
89 canvas->Translate(gfx::Vector2d(
90 0, ToPixels(kButtonHeight) / 2 - button_text_size.height() / 2));
91 for (auto& render_text : lines)
92 render_text->Draw(canvas);
93 canvas->Restore();
94
95 // Primary button area
96 text = l10n_util::GetStringUTF16(IDS_OK);
cjgrant 2017/06/02 04:16:36 This block is really similar to the one above. Do
ymalik 2017/06/02 21:14:31 We could, but we would either have to pass a lot o
97 button_text_size.set_size(ToPixels(kButtonWidth), 0);
98 lines = PrepareDrawStringRect(text, fonts, color_scheme().primary_button_text,
99 &button_text_size, kTextAlignmentCenter,
100 kWrappingBehaviorWrap);
101 primary_button_rect_.SetRect(
102 ToPixels(kWidth) / 2 + ToPixels(kButtonsSeperatorWidth),
103 prompt_text_size.height() + ToPixels(kPromptTextButtonSeperatorHeight),
104 ToPixels(kButtonWidth), ToPixels(kButtonHeight));
105 paint.setColor(primary_hovered_ ? color_scheme().button_background_hover
106 : color_scheme().primary_button_background);
107 canvas->Save();
108 canvas->Translate(
109 gfx::Vector2d(primary_button_rect_.x(), primary_button_rect_.y()));
110 sk_canvas->drawRoundRect(
111 SkRect::MakeXYWH(0, 0, ToPixels(kButtonWidth), ToPixels(kButtonHeight)),
112 radius, radius, paint);
113 canvas->Translate(gfx::Vector2d(
114 0, ToPixels(kButtonHeight) / 2 - button_text_size.height() / 2));
115 for (auto& render_text : lines)
116 render_text->Draw(canvas);
117 canvas->Restore();
118 }
119
120 float ExitPromptTexture::ToPixels(float meters) const {
121 return meters * size_.width() / kWidth;
122 }
123
124 gfx::PointF ExitPromptTexture::percentToPixels(
125 const gfx::PointF& percent) const {
126 return gfx::PointF(percent.x() * size_.width(), percent.y() * size_.height());
127 }
128
129 bool ExitPromptTexture::HitsSecondaryButton(const gfx::PointF& position) const {
130 return secondary_button_rect_.Contains(percentToPixels(position));
131 }
132
133 void ExitPromptTexture::SetPrimaryHovered(bool hovered) {
134 if (primary_hovered_ != hovered)
135 set_dirty();
136 primary_hovered_ = hovered;
137 }
138
139 void ExitPromptTexture::SetPrimaryPressed(bool pressed) {
140 if (primary_pressed_ != pressed)
141 set_dirty();
142 primary_pressed_ = pressed;
143 }
144
145 void ExitPromptTexture::SetSecondaryHovered(bool hovered) {
146 if (secondary_hovered_ != hovered)
147 set_dirty();
148 secondary_hovered_ = hovered;
149 }
150
151 void ExitPromptTexture::SetSecondaryPressed(bool pressed) {
152 if (secondary_pressed_ != pressed)
153 set_dirty();
154 secondary_pressed_ = pressed;
155 }
156
157 bool ExitPromptTexture::HitsPrimaryButton(const gfx::PointF& position) const {
cjgrant 2017/06/02 04:16:36 nit: Should probably move this up with the other H
ymalik 2017/06/02 21:14:31 Done.
158 return primary_button_rect_.Contains(percentToPixels(position));
159 }
160
161 gfx::Size ExitPromptTexture::GetPreferredTextureSize(int maximum_width) const {
162 return gfx::Size(maximum_width, maximum_width * kHeight / kWidth);
163 }
164
165 gfx::SizeF ExitPromptTexture::GetDrawnSize() const {
166 return size_;
167 }
168
169 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698