| OLD | NEW |
| (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/render_text.h" |
| 17 |
| 18 namespace vr_shell { |
| 19 |
| 20 namespace { |
| 21 |
| 22 constexpr float kWidth = 0.672; |
| 23 constexpr float kHeight = 0.2; |
| 24 constexpr float kButtonWidth = 0.162; |
| 25 constexpr float kButtonHeight = 0.066; |
| 26 constexpr float kPromptTextButtonSeperatorHeight = 0.04; |
| 27 constexpr float kButtonsSeperatorWidth = 0.01; |
| 28 constexpr float kButtonRadiusFactor = 0.006; |
| 29 constexpr float kFontSizeFactor = 0.048; |
| 30 |
| 31 } // namespace |
| 32 |
| 33 ExitPromptTexture::ExitPromptTexture() = default; |
| 34 |
| 35 ExitPromptTexture::~ExitPromptTexture() = default; |
| 36 |
| 37 const ColorScheme& ExitPromptTexture::color_scheme() const { |
| 38 return ColorScheme::GetColorScheme(mode()); |
| 39 } |
| 40 |
| 41 void ExitPromptTexture::Draw(SkCanvas* sk_canvas, |
| 42 const gfx::Size& texture_size) { |
| 43 size_.set_width(texture_size.width()); |
| 44 size_.set_height(texture_size.height()); |
| 45 |
| 46 cc::SkiaPaintCanvas paint_canvas(sk_canvas); |
| 47 gfx::Canvas gfx_canvas(&paint_canvas, 1.0f); |
| 48 gfx::Canvas* canvas = &gfx_canvas; |
| 49 |
| 50 // Prompt text area. |
| 51 auto text = l10n_util::GetStringUTF16(IDS_VR_SHELL_EXIT_PROMPT_DESCRIPTION); |
| 52 gfx::FontList fonts; |
| 53 GetFontList(size_.width() * kFontSizeFactor, text, &fonts); |
| 54 gfx::Rect prompt_text_size(size_.width(), 0); |
| 55 std::vector<std::unique_ptr<gfx::RenderText>> lines = PrepareDrawStringRect( |
| 56 text, fonts, color_scheme().prompt_text, &prompt_text_size, |
| 57 kTextAlignmentCenter, kWrappingBehaviorWrap); |
| 58 for (auto& render_text : lines) |
| 59 render_text->Draw(canvas); |
| 60 |
| 61 SkPaint paint; |
| 62 gfx::Rect button_text_size(ToPixels(kButtonWidth), 0); |
| 63 float radius = size_.width() * kButtonRadiusFactor; |
| 64 |
| 65 // Secondary button area. |
| 66 text = l10n_util::GetStringUTF16(IDS_VR_SHELL_EXIT_PROMPT_EXIT_VR_BUTTON); |
| 67 lines = PrepareDrawStringRect( |
| 68 text, fonts, color_scheme().secondary_button_text, &button_text_size, |
| 69 kTextAlignmentCenter, kWrappingBehaviorWrap); |
| 70 secondary_button_rect_.SetRect( |
| 71 ToPixels(kWidth / 2 - kButtonsSeperatorWidth - kButtonWidth), |
| 72 prompt_text_size.height() + ToPixels(kPromptTextButtonSeperatorHeight), |
| 73 ToPixels(kButtonWidth), ToPixels(kButtonHeight)); |
| 74 paint.setColor(GetButtonColor(false)); |
| 75 canvas->Save(); |
| 76 canvas->Translate( |
| 77 gfx::Vector2d(secondary_button_rect_.x(), secondary_button_rect_.y())); |
| 78 sk_canvas->drawRoundRect( |
| 79 SkRect::MakeXYWH(0, 0, ToPixels(kButtonWidth), ToPixels(kButtonHeight)), |
| 80 radius, radius, paint); |
| 81 canvas->Translate(gfx::Vector2d( |
| 82 0, ToPixels(kButtonHeight) / 2 - button_text_size.height() / 2)); |
| 83 for (auto& render_text : lines) |
| 84 render_text->Draw(canvas); |
| 85 canvas->Restore(); |
| 86 |
| 87 // Primary button area. |
| 88 text = l10n_util::GetStringUTF16(IDS_OK); |
| 89 button_text_size.set_size(gfx::Size(ToPixels(kButtonWidth), 0)); |
| 90 lines = PrepareDrawStringRect(text, fonts, color_scheme().primary_button_text, |
| 91 &button_text_size, kTextAlignmentCenter, |
| 92 kWrappingBehaviorWrap); |
| 93 primary_button_rect_.SetRect( |
| 94 ToPixels(kWidth / 2 + kButtonsSeperatorWidth), |
| 95 prompt_text_size.height() + ToPixels(kPromptTextButtonSeperatorHeight), |
| 96 ToPixels(kButtonWidth), ToPixels(kButtonHeight)); |
| 97 paint.setColor(GetButtonColor(true)); |
| 98 canvas->Save(); |
| 99 canvas->Translate( |
| 100 gfx::Vector2d(primary_button_rect_.x(), primary_button_rect_.y())); |
| 101 sk_canvas->drawRoundRect( |
| 102 SkRect::MakeXYWH(0, 0, ToPixels(kButtonWidth), ToPixels(kButtonHeight)), |
| 103 radius, radius, paint); |
| 104 canvas->Translate(gfx::Vector2d( |
| 105 0, ToPixels(kButtonHeight) / 2 - button_text_size.height() / 2)); |
| 106 for (auto& render_text : lines) |
| 107 render_text->Draw(canvas); |
| 108 canvas->Restore(); |
| 109 } |
| 110 |
| 111 SkColor ExitPromptTexture::GetButtonColor(bool primary) const { |
| 112 SkColor color = primary ? color_scheme().primary_button_background |
| 113 : color_scheme().secondary_button_background; |
| 114 const bool pressed = primary ? primary_pressed_ : secondary_pressed_; |
| 115 const bool hovered = primary ? primary_hovered_ : secondary_hovered_; |
| 116 if (pressed) |
| 117 color = color_scheme().button_background_down; |
| 118 else if (hovered) |
| 119 color = color_scheme().button_background_hover; |
| 120 return color; |
| 121 } |
| 122 |
| 123 float ExitPromptTexture::ToPixels(float meters) const { |
| 124 return meters * size_.width() / kWidth; |
| 125 } |
| 126 |
| 127 gfx::PointF ExitPromptTexture::PercentToPixels( |
| 128 const gfx::PointF& percent) const { |
| 129 return gfx::PointF(percent.x() * size_.width(), percent.y() * size_.height()); |
| 130 } |
| 131 |
| 132 bool ExitPromptTexture::HitsPrimaryButton(const gfx::PointF& position) const { |
| 133 return primary_button_rect_.Contains(PercentToPixels(position)); |
| 134 } |
| 135 |
| 136 bool ExitPromptTexture::HitsSecondaryButton(const gfx::PointF& position) const { |
| 137 return secondary_button_rect_.Contains(PercentToPixels(position)); |
| 138 } |
| 139 |
| 140 void ExitPromptTexture::SetPrimaryButtonHovered(bool hovered) { |
| 141 if (primary_hovered_ != hovered) |
| 142 set_dirty(); |
| 143 primary_hovered_ = hovered; |
| 144 } |
| 145 |
| 146 void ExitPromptTexture::SetPrimaryButtonPressed(bool pressed) { |
| 147 if (primary_pressed_ != pressed) |
| 148 set_dirty(); |
| 149 primary_pressed_ = pressed; |
| 150 } |
| 151 |
| 152 void ExitPromptTexture::SetSecondaryButtonHovered(bool hovered) { |
| 153 if (secondary_hovered_ != hovered) |
| 154 set_dirty(); |
| 155 secondary_hovered_ = hovered; |
| 156 } |
| 157 |
| 158 void ExitPromptTexture::SetSecondaryButtonPressed(bool pressed) { |
| 159 if (secondary_pressed_ != pressed) |
| 160 set_dirty(); |
| 161 secondary_pressed_ = pressed; |
| 162 } |
| 163 |
| 164 gfx::Size ExitPromptTexture::GetPreferredTextureSize(int maximum_width) const { |
| 165 return gfx::Size(maximum_width, maximum_width * kHeight / kWidth); |
| 166 } |
| 167 |
| 168 gfx::SizeF ExitPromptTexture::GetDrawnSize() const { |
| 169 return size_; |
| 170 } |
| 171 |
| 172 } // namespace vr_shell |
| OLD | NEW |