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 @@
#include "base/memory/ptr_util.h"
#include "cc/paint/skia_paint_canvas.h"
#include "chrome/browser/vr/elements/ui_texture.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"
......@@ -18,18 +17,16 @@ namespace vr {
class TextTexture : public UiTexture {
public:
TextTexture(int resource_id,
float font_height,
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(const base::string16& text, float font_height, float text_width)
: text_(text), font_height_(font_height), text_width_(text_width) {}
~TextTexture() override {}
void OnSetMode() override { set_dirty(); }
void SetColor(SkColor color) {
if (color_ == color)
return;
color_ = color;
set_dirty();
}
private:
gfx::Size GetPreferredTextureSize(int width) const override {
......@@ -41,12 +38,12 @@ class TextTexture : public UiTexture {
void Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) override;
gfx::SizeF size_;
int resource_id_;
base::string16 text_;
// These widths are in meters.
float font_height_;
float text_width_;
base::Callback<SkColor(ColorScheme)> color_callback_;
SkColor color_ = SK_ColorBLACK;
DISALLOW_COPY_AND_ASSIGN(TextTexture);
};
......@@ -54,37 +51,37 @@ class TextTexture : public UiTexture {
Text::Text(int maximum_width_pixels,
float font_height_meters,
float text_width_meters,
const base::Callback<SkColor(ColorScheme)>& color_callback,
int resource_id)
const base::string16& text)
: TexturedElement(maximum_width_pixels),
texture_(base::MakeUnique<TextTexture>(resource_id,
texture_(base::MakeUnique<TextTexture>(text,
font_height_meters,
text_width_meters,
color_callback)) {}
text_width_meters)) {}
Text::~Text() {}
UiTexture* Text::GetTexture() const {
return texture_.get();
}
void Text::SetColor(SkColor color) {
texture_->SetColor(color);
}
void TextTexture::Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) {
cc::SkiaPaintCanvas paint_canvas(sk_canvas);
gfx::Canvas gfx_canvas(&paint_canvas, 1.0f);
gfx::Canvas* canvas = &gfx_canvas;
gfx::FontList fonts;
auto text = l10n_util::GetStringUTF16(resource_id_);
float pixels_per_meter = texture_size.width() / text_width_;
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);
std::vector<std::unique_ptr<gfx::RenderText>> lines =
// TODO(vollick): if this subsumes all text, then we should probably move
// this function into this class.
PrepareDrawStringRect(text, fonts, color_callback_.Run(color_scheme()),
&text_bounds, kTextAlignmentCenter,
kWrappingBehaviorWrap);
PrepareDrawStringRect(text_, fonts, color_, &text_bounds,
kTextAlignmentCenter, kWrappingBehaviorWrap);
// Draw the text.
for (auto& render_text : lines)
render_text->Draw(canvas);
......
......@@ -7,13 +7,11 @@
#include <memory>
#include "base/callback.h"
#include "chrome/browser/vr/elements/textured_element.h"
#include "third_party/skia/include/core/SkColor.h"
namespace vr {
struct ColorScheme;
class TextTexture;
class UiTexture;
......@@ -22,10 +20,11 @@ class Text : public TexturedElement {
Text(int maximum_width_pixels,
float font_height_meters,
float text_width_meters,
const base::Callback<SkColor(ColorScheme)>& color_callback,
int resource_id);
const base::string16& text);
~Text() override;
void SetColor(SkColor color);
private:
UiTexture* GetTexture() const override;
......
......@@ -39,6 +39,7 @@
#include "chrome/browser/vr/vr_gl_util.h"
#include "chrome/grit/generated_resources.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/transform_util.h"
namespace vr {
......@@ -58,6 +59,15 @@ void BindColor(UiSceneManager* model, Rect* rect, P p) {
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
using TargetProperty::BOUNDS;
......@@ -300,10 +310,8 @@ void UiSceneManager::CreateSplashScreen() {
// Add "Powered by Chrome" text.
auto text = base::MakeUnique<Text>(
512, kSplashScreenTextFontHeightM, kSplashScreenTextWidthM,
base::Bind([](ColorScheme color_scheme) {
return color_scheme.splash_screen_text_color;
}),
IDS_VR_POWERED_BY_CHROME_MESSAGE);
l10n_util::GetStringUTF16(IDS_VR_POWERED_BY_CHROME_MESSAGE));
BindColor(this, text.get(), &ColorScheme::splash_screen_text_color);
text->set_name(kSplashScreenText);
text->SetVisible(true);
text->set_draw_phase(kPhaseOverlayForeground);
......@@ -327,10 +335,8 @@ void UiSceneManager::CreateSplashScreen() {
void UiSceneManager::CreateUnderDevelopmentNotice() {
auto text = base::MakeUnique<Text>(
512, kUnderDevelopmentNoticeFontHeightM, kUnderDevelopmentNoticeWidthM,
base::Bind([](ColorScheme color_scheme) {
return color_scheme.world_background_text;
}),
IDS_VR_UNDER_DEVELOPMENT_NOTICE);
l10n_util::GetStringUTF16(IDS_VR_UNDER_DEVELOPMENT_NOTICE));
BindColor(this, text.get(), &ColorScheme::world_background_text);
text->set_name(kUnderDevelopmentNotice);
text->set_draw_phase(kPhaseForeground);
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