Commit b049ae01 authored by Ian Vollick's avatar Ian Vollick Committed by Commit Bot

[vr] Simplify vr::Text construction

Previously we'd passed a resource id and a color callback when creating
a Text instance. Now that we've switched to databinding and have
resources for unit tests, this can be simplified to ease the use of
Text in tests.

Bug: None
Change-Id: I610d08a5e18091db03b640f453635f90e5d9f12a
Reviewed-on: https://chromium-review.googlesource.com/701802Reviewed-by: default avatarChristopher Grant <cjgrant@chromium.org>
Commit-Queue: Ian Vollick <vollick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506751}
parent 5a138a29
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "cc/paint/skia_paint_canvas.h" #include "cc/paint/skia_paint_canvas.h"
#include "chrome/browser/vr/elements/ui_texture.h" #include "chrome/browser/vr/elements/ui_texture.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h" #include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
...@@ -18,18 +17,16 @@ namespace vr { ...@@ -18,18 +17,16 @@ namespace vr {
class TextTexture : public UiTexture { class TextTexture : public UiTexture {
public: public:
TextTexture(int resource_id, TextTexture(const base::string16& text, float font_height, float text_width)
float font_height, : text_(text), font_height_(font_height), text_width_(text_width) {}
float text_width,
const base::Callback<SkColor(ColorScheme)>& color_callback)
: resource_id_(resource_id),
font_height_(font_height),
text_width_(text_width),
color_callback_(color_callback) {}
~TextTexture() override {} ~TextTexture() override {}
void OnSetMode() override { set_dirty(); } void SetColor(SkColor color) {
if (color_ == color)
return;
color_ = color;
set_dirty();
}
private: private:
gfx::Size GetPreferredTextureSize(int width) const override { gfx::Size GetPreferredTextureSize(int width) const override {
...@@ -41,12 +38,12 @@ class TextTexture : public UiTexture { ...@@ -41,12 +38,12 @@ class TextTexture : public UiTexture {
void Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) override; void Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) override;
gfx::SizeF size_; gfx::SizeF size_;
int resource_id_; base::string16 text_;
// These widths are in meters. // These widths are in meters.
float font_height_; float font_height_;
float text_width_; float text_width_;
base::Callback<SkColor(ColorScheme)> color_callback_; SkColor color_ = SK_ColorBLACK;
DISALLOW_COPY_AND_ASSIGN(TextTexture); DISALLOW_COPY_AND_ASSIGN(TextTexture);
}; };
...@@ -54,37 +51,37 @@ class TextTexture : public UiTexture { ...@@ -54,37 +51,37 @@ class TextTexture : public UiTexture {
Text::Text(int maximum_width_pixels, Text::Text(int maximum_width_pixels,
float font_height_meters, float font_height_meters,
float text_width_meters, float text_width_meters,
const base::Callback<SkColor(ColorScheme)>& color_callback, const base::string16& text)
int resource_id)
: TexturedElement(maximum_width_pixels), : TexturedElement(maximum_width_pixels),
texture_(base::MakeUnique<TextTexture>(resource_id, texture_(base::MakeUnique<TextTexture>(text,
font_height_meters, font_height_meters,
text_width_meters, text_width_meters)) {}
color_callback)) {}
Text::~Text() {} Text::~Text() {}
UiTexture* Text::GetTexture() const { UiTexture* Text::GetTexture() const {
return texture_.get(); return texture_.get();
} }
void Text::SetColor(SkColor color) {
texture_->SetColor(color);
}
void TextTexture::Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) { void TextTexture::Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) {
cc::SkiaPaintCanvas paint_canvas(sk_canvas); cc::SkiaPaintCanvas paint_canvas(sk_canvas);
gfx::Canvas gfx_canvas(&paint_canvas, 1.0f); gfx::Canvas gfx_canvas(&paint_canvas, 1.0f);
gfx::Canvas* canvas = &gfx_canvas; gfx::Canvas* canvas = &gfx_canvas;
gfx::FontList fonts; gfx::FontList fonts;
auto text = l10n_util::GetStringUTF16(resource_id_);
float pixels_per_meter = texture_size.width() / text_width_; float pixels_per_meter = texture_size.width() / text_width_;
int pixel_font_height = static_cast<int>(font_height_ * pixels_per_meter); int pixel_font_height = static_cast<int>(font_height_ * pixels_per_meter);
GetFontList(pixel_font_height, text, &fonts); GetFontList(pixel_font_height, text_, &fonts);
gfx::Rect text_bounds(texture_size.width(), 0); gfx::Rect text_bounds(texture_size.width(), 0);
std::vector<std::unique_ptr<gfx::RenderText>> lines = std::vector<std::unique_ptr<gfx::RenderText>> lines =
// TODO(vollick): if this subsumes all text, then we should probably move // TODO(vollick): if this subsumes all text, then we should probably move
// this function into this class. // this function into this class.
PrepareDrawStringRect(text, fonts, color_callback_.Run(color_scheme()), PrepareDrawStringRect(text_, fonts, color_, &text_bounds,
&text_bounds, kTextAlignmentCenter, kTextAlignmentCenter, kWrappingBehaviorWrap);
kWrappingBehaviorWrap);
// Draw the text. // Draw the text.
for (auto& render_text : lines) for (auto& render_text : lines)
render_text->Draw(canvas); render_text->Draw(canvas);
......
...@@ -7,13 +7,11 @@ ...@@ -7,13 +7,11 @@
#include <memory> #include <memory>
#include "base/callback.h"
#include "chrome/browser/vr/elements/textured_element.h" #include "chrome/browser/vr/elements/textured_element.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
namespace vr { namespace vr {
struct ColorScheme;
class TextTexture; class TextTexture;
class UiTexture; class UiTexture;
...@@ -22,10 +20,11 @@ class Text : public TexturedElement { ...@@ -22,10 +20,11 @@ class Text : public TexturedElement {
Text(int maximum_width_pixels, Text(int maximum_width_pixels,
float font_height_meters, float font_height_meters,
float text_width_meters, float text_width_meters,
const base::Callback<SkColor(ColorScheme)>& color_callback, const base::string16& text);
int resource_id);
~Text() override; ~Text() override;
void SetColor(SkColor color);
private: private:
UiTexture* GetTexture() const override; UiTexture* GetTexture() const override;
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include "chrome/browser/vr/vr_gl_util.h" #include "chrome/browser/vr/vr_gl_util.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "components/vector_icons/vector_icons.h" #include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/transform_util.h" #include "ui/gfx/transform_util.h"
namespace vr { namespace vr {
...@@ -58,6 +59,15 @@ void BindColor(UiSceneManager* model, Rect* rect, P p) { ...@@ -58,6 +59,15 @@ void BindColor(UiSceneManager* model, Rect* rect, P p) {
base::Unretained(rect)))); base::Unretained(rect))));
} }
template <typename P>
void BindColor(UiSceneManager* model, Text* text, P p) {
text->AddBinding(base::MakeUnique<Binding<SkColor>>(
base::Bind([](UiSceneManager* m, P p) { return (m->color_scheme()).*p; },
base::Unretained(model), p),
base::Bind([](Text* t, const SkColor& c) { t->SetColor(c); },
base::Unretained(text))));
}
} // namespace } // namespace
using TargetProperty::BOUNDS; using TargetProperty::BOUNDS;
...@@ -300,10 +310,8 @@ void UiSceneManager::CreateSplashScreen() { ...@@ -300,10 +310,8 @@ void UiSceneManager::CreateSplashScreen() {
// Add "Powered by Chrome" text. // Add "Powered by Chrome" text.
auto text = base::MakeUnique<Text>( auto text = base::MakeUnique<Text>(
512, kSplashScreenTextFontHeightM, kSplashScreenTextWidthM, 512, kSplashScreenTextFontHeightM, kSplashScreenTextWidthM,
base::Bind([](ColorScheme color_scheme) { l10n_util::GetStringUTF16(IDS_VR_POWERED_BY_CHROME_MESSAGE));
return color_scheme.splash_screen_text_color; BindColor(this, text.get(), &ColorScheme::splash_screen_text_color);
}),
IDS_VR_POWERED_BY_CHROME_MESSAGE);
text->set_name(kSplashScreenText); text->set_name(kSplashScreenText);
text->SetVisible(true); text->SetVisible(true);
text->set_draw_phase(kPhaseOverlayForeground); text->set_draw_phase(kPhaseOverlayForeground);
...@@ -327,10 +335,8 @@ void UiSceneManager::CreateSplashScreen() { ...@@ -327,10 +335,8 @@ void UiSceneManager::CreateSplashScreen() {
void UiSceneManager::CreateUnderDevelopmentNotice() { void UiSceneManager::CreateUnderDevelopmentNotice() {
auto text = base::MakeUnique<Text>( auto text = base::MakeUnique<Text>(
512, kUnderDevelopmentNoticeFontHeightM, kUnderDevelopmentNoticeWidthM, 512, kUnderDevelopmentNoticeFontHeightM, kUnderDevelopmentNoticeWidthM,
base::Bind([](ColorScheme color_scheme) { l10n_util::GetStringUTF16(IDS_VR_UNDER_DEVELOPMENT_NOTICE));
return color_scheme.world_background_text; BindColor(this, text.get(), &ColorScheme::world_background_text);
}),
IDS_VR_UNDER_DEVELOPMENT_NOTICE);
text->set_name(kUnderDevelopmentNotice); text->set_name(kUnderDevelopmentNotice);
text->set_draw_phase(kPhaseForeground); text->set_draw_phase(kPhaseForeground);
text->set_hit_testable(false); text->set_hit_testable(false);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment