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