Chromium Code Reviews| Index: chrome/browser/android/vr_shell/textures/exit_prompt_texture.cc |
| diff --git a/chrome/browser/android/vr_shell/textures/exit_prompt_texture.cc b/chrome/browser/android/vr_shell/textures/exit_prompt_texture.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c7d369f47e927950d1de125432153ddfe5fd38f6 |
| --- /dev/null |
| +++ b/chrome/browser/android/vr_shell/textures/exit_prompt_texture.cc |
| @@ -0,0 +1,172 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/vr_shell/textures/exit_prompt_texture.h" |
| + |
| +#include "cc/paint/skia_paint_canvas.h" |
| +#include "chrome/browser/android/vr_shell/color_scheme.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "components/strings/grit/components_strings.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/gfx/canvas.h" |
| +#include "ui/gfx/font_list.h" |
| +#include "ui/gfx/geometry/rect.h" |
| +#include "ui/gfx/geometry/vector2d.h" |
| +#include "ui/gfx/render_text.h" |
| + |
| +namespace vr_shell { |
| + |
| +namespace { |
| + |
| +constexpr float kWidth = 0.672; |
| +constexpr float kHeight = 0.2; |
| +constexpr float kButtonWidth = 0.162; |
| +constexpr float kButtonHeight = 0.066; |
| +constexpr float kPromptTextButtonSeperatorHeight = 0.04; |
| +constexpr float kButtonsSeperatorWidth = 0.01; |
| +constexpr float kButtonRadiusFactor = 0.006; |
| +constexpr float kFontSizeFactor = 0.048; |
| + |
| +} // namespace |
| + |
| +ExitPromptTexture::ExitPromptTexture() = default; |
| + |
| +ExitPromptTexture::~ExitPromptTexture() = default; |
| + |
| +const ColorScheme& ExitPromptTexture::color_scheme() const { |
| + return ColorScheme::GetColorScheme(mode()); |
| +} |
| + |
| +void ExitPromptTexture::Draw(SkCanvas* sk_canvas, |
| + const gfx::Size& texture_size) { |
| + size_.set_width(texture_size.width()); |
| + size_.set_height(texture_size.height()); |
| + |
| + cc::SkiaPaintCanvas paint_canvas(sk_canvas); |
| + gfx::Canvas gfx_canvas(&paint_canvas, 1.0f); |
| + gfx::Canvas* canvas = &gfx_canvas; |
| + |
| + // Prompt text area. |
| + auto text = l10n_util::GetStringUTF16(IDS_VR_SHELL_EXIT_PROMPT_DESCRIPTION); |
| + gfx::FontList fonts; |
| + GetFontList(size_.width() * kFontSizeFactor, text, &fonts); |
| + gfx::Rect prompt_text_size(size_.width(), 0); |
| + std::vector<std::unique_ptr<gfx::RenderText>> lines = PrepareDrawStringRect( |
| + text, fonts, color_scheme().prompt_foreground, &prompt_text_size, |
| + kTextAlignmentCenter, kWrappingBehaviorWrap); |
| + for (auto& render_text : lines) |
| + render_text->Draw(canvas); |
| + |
| + SkPaint paint; |
| + gfx::Rect button_text_size(ToPixels(kButtonWidth), 0); |
| + float radius = size_.width() * kButtonRadiusFactor; |
| + |
| + // Secondary button area. |
| + text = l10n_util::GetStringUTF16(IDS_VR_SHELL_EXIT_PROMPT_EXIT_VR_BUTTON); |
| + lines = PrepareDrawStringRect( |
| + text, fonts, color_scheme().prompt_secondary_button_foreground, |
| + &button_text_size, kTextAlignmentCenter, kWrappingBehaviorWrap); |
| + secondary_button_rect_.SetRect( |
| + ToPixels(kWidth / 2 - kButtonsSeperatorWidth - kButtonWidth), |
| + prompt_text_size.height() + ToPixels(kPromptTextButtonSeperatorHeight), |
| + ToPixels(kButtonWidth), ToPixels(kButtonHeight)); |
| + paint.setColor(GetButtonColor(false)); |
| + canvas->Save(); |
| + canvas->Translate( |
| + gfx::Vector2d(secondary_button_rect_.x(), secondary_button_rect_.y())); |
| + sk_canvas->drawRoundRect( |
| + SkRect::MakeXYWH(0, 0, ToPixels(kButtonWidth), ToPixels(kButtonHeight)), |
| + radius, radius, paint); |
| + canvas->Translate(gfx::Vector2d( |
| + 0, ToPixels(kButtonHeight) / 2 - button_text_size.height() / 2)); |
| + for (auto& render_text : lines) |
| + render_text->Draw(canvas); |
| + canvas->Restore(); |
| + |
| + // Primary button area. |
| + text = l10n_util::GetStringUTF16(IDS_OK); |
| + button_text_size.set_size(gfx::Size(ToPixels(kButtonWidth), 0)); |
| + lines = PrepareDrawStringRect( |
| + text, fonts, color_scheme().prompt_primary_button_forground, |
| + &button_text_size, kTextAlignmentCenter, kWrappingBehaviorWrap); |
| + primary_button_rect_.SetRect( |
| + ToPixels(kWidth / 2 + kButtonsSeperatorWidth), |
| + prompt_text_size.height() + ToPixels(kPromptTextButtonSeperatorHeight), |
| + ToPixels(kButtonWidth), ToPixels(kButtonHeight)); |
| + paint.setColor(GetButtonColor(true)); |
| + canvas->Save(); |
| + canvas->Translate( |
| + gfx::Vector2d(primary_button_rect_.x(), primary_button_rect_.y())); |
| + sk_canvas->drawRoundRect( |
| + SkRect::MakeXYWH(0, 0, ToPixels(kButtonWidth), ToPixels(kButtonHeight)), |
| + radius, radius, paint); |
| + canvas->Translate(gfx::Vector2d( |
| + 0, ToPixels(kButtonHeight) / 2 - button_text_size.height() / 2)); |
| + for (auto& render_text : lines) |
| + render_text->Draw(canvas); |
| + canvas->Restore(); |
| +} |
| + |
| +SkColor ExitPromptTexture::GetButtonColor(bool primary) const { |
|
cjgrant
2017/06/05 17:33:53
With all the separate methods for primary and seco
ymalik
2017/06/05 20:02:41
I had it this way because the hover and down color
|
| + SkColor color = primary ? color_scheme().prompt_primary_button_background |
| + : color_scheme().prompt_secondary_button_background; |
| + const bool pressed = primary ? primary_pressed_ : secondary_pressed_; |
| + const bool hovered = primary ? primary_hovered_ : secondary_hovered_; |
| + if (pressed) |
| + color = color_scheme().prompt_button_background_down; |
|
cjgrant
2017/06/05 17:33:53
For me, the default color and overriding based on
ymalik
2017/06/05 20:02:41
I'm pretty indifferent, they both seem reasonable
|
| + else if (hovered) |
| + color = color_scheme().prompt_button_background_hover; |
| + return color; |
| +} |
| + |
| +float ExitPromptTexture::ToPixels(float meters) const { |
| + return meters * size_.width() / kWidth; |
| +} |
| + |
| +gfx::PointF ExitPromptTexture::PercentToPixels( |
| + const gfx::PointF& percent) const { |
| + return gfx::PointF(percent.x() * size_.width(), percent.y() * size_.height()); |
| +} |
| + |
| +bool ExitPromptTexture::HitsPrimaryButton(const gfx::PointF& position) const { |
| + return primary_button_rect_.Contains(PercentToPixels(position)); |
| +} |
| + |
| +bool ExitPromptTexture::HitsSecondaryButton(const gfx::PointF& position) const { |
| + return secondary_button_rect_.Contains(PercentToPixels(position)); |
| +} |
| + |
| +void ExitPromptTexture::SetPrimaryButtonHovered(bool hovered) { |
| + if (primary_hovered_ != hovered) |
| + set_dirty(); |
| + primary_hovered_ = hovered; |
| +} |
| + |
| +void ExitPromptTexture::SetPrimaryButtonPressed(bool pressed) { |
| + if (primary_pressed_ != pressed) |
| + set_dirty(); |
| + primary_pressed_ = pressed; |
| +} |
| + |
| +void ExitPromptTexture::SetSecondaryButtonHovered(bool hovered) { |
| + if (secondary_hovered_ != hovered) |
| + set_dirty(); |
| + secondary_hovered_ = hovered; |
| +} |
| + |
| +void ExitPromptTexture::SetSecondaryButtonPressed(bool pressed) { |
| + if (secondary_pressed_ != pressed) |
| + set_dirty(); |
| + secondary_pressed_ = pressed; |
| +} |
| + |
| +gfx::Size ExitPromptTexture::GetPreferredTextureSize(int maximum_width) const { |
| + return gfx::Size(maximum_width, maximum_width * kHeight / kWidth); |
| +} |
| + |
| +gfx::SizeF ExitPromptTexture::GetDrawnSize() const { |
| + return size_; |
| +} |
| + |
| +} // namespace vr_shell |