0

ui: Add methods to clamp Sizes, Points, and Vectors from above or below.

Tests:
PointTest.Clamp
PointTest.ClampF
SizeTest.Clamp
SizeTest.ClampF
Vector2dTest.Clamp
Vector2dTest.ClampF
Vector3dTest.ClampF

R=sky,enne
BUG=147395

Review URL: https://codereview.chromium.org/11361186

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@167014 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
danakj@chromium.org
2012-11-10 00:07:59 +00:00
parent 0175b30c6c
commit fe07b6440c
14 changed files with 252 additions and 60 deletions

@ -12,51 +12,6 @@
namespace cc {
inline gfx::Size ClampSizeFromAbove(gfx::Size s, gfx::Size other) {
return gfx::Size(s.width() < other.width() ? s.width() : other.width(),
s.height() < other.height() ? s.height() : other.height());
}
inline gfx::Vector2dF ClampFromAbove(gfx::Vector2dF s, gfx::Vector2dF max) {
return gfx::Vector2dF(s.x() < max.x() ? s.x() : max.x(),
s.y() < max.y() ? s.y() : max.y());
}
inline gfx::Vector2dF ClampFromBelow(gfx::Vector2dF s, gfx::Vector2dF min) {
return gfx::Vector2dF(s.x() > min.x() ? s.x() : min.x(),
s.y() > min.y() ? s.y() : min.y());
}
inline gfx::Vector2d ClampFromAbove(gfx::Vector2d s, gfx::Vector2d max) {
return gfx::Vector2d(s.x() < max.x() ? s.x() : max.x(),
s.y() < max.y() ? s.y() : max.y());
}
inline gfx::Vector2d ClampFromBelow(gfx::Vector2d s, gfx::Vector2d min) {
return gfx::Vector2d(s.x() > min.x() ? s.x() : min.x(),
s.y() > min.y() ? s.y() : min.y());
}
inline gfx::PointF ClampFromAbove(gfx::PointF s, gfx::PointF max) {
return gfx::PointF(s.x() < max.x() ? s.x() : max.x(),
s.y() < max.y() ? s.y() : max.y());
}
inline gfx::PointF ClampFromBelow(gfx::PointF s, gfx::PointF min) {
return gfx::PointF(s.x() > min.x() ? s.x() : min.x(),
s.y() > min.y() ? s.y() : min.y());
}
inline gfx::Point ClampFromAbove(gfx::Point s, gfx::Point max) {
return gfx::Point(s.x() < max.x() ? s.x() : max.x(),
s.y() < max.y() ? s.y() : max.y());
}
inline gfx::Point ClampFromBelow(gfx::Point s, gfx::Point min) {
return gfx::Point(s.x() > min.x() ? s.x() : min.x(),
s.y() > min.y() ? s.y() : min.y());
}
inline gfx::Point BottomRight(gfx::Rect rect) {
return gfx::Point(rect.right(), rect.bottom());
}

@ -184,8 +184,8 @@ gfx::Vector2dF LayerImpl::scrollBy(const gfx::Vector2dF& scroll)
gfx::Vector2dF maxDelta = m_maxScrollOffset - m_scrollOffset;
// Clamp newDelta so that position + delta stays within scroll bounds.
gfx::Vector2dF newDelta = (m_scrollDelta + scroll);
newDelta = ClampFromBelow(newDelta, minDelta);
newDelta = ClampFromAbove(newDelta, maxDelta);
newDelta.ClampToMin(minDelta);
newDelta.ClampToMax(maxDelta);
gfx::Vector2dF unscrolled = m_scrollDelta + scroll - newDelta;
if (m_scrollDelta == newDelta)

@ -1008,7 +1008,7 @@ void LayerTreeHostImpl::updateMaxScrollOffset()
// The viewport may be larger than the contents in some cases, such as
// having a vertical scrollbar but no horizontal overflow.
maxScroll = ClampFromBelow(maxScroll, gfx::Vector2dF());
maxScroll.ClampToMin(gfx::Vector2dF());
m_rootScrollLayerImpl->setMaxScrollOffset(gfx::ToFlooredVector2d(maxScroll));
}
@ -1296,8 +1296,8 @@ void LayerTreeHostImpl::computePinchZoomDeltas(ScrollAndScaleSet* scrollInfo)
gfx::Vector2dF scrollEnd = scrollBegin + anchorOffset;
scrollEnd.Scale(m_pinchZoomViewport.minPageScaleFactor() / scaleBegin);
scrollEnd -= anchorOffset;
scrollEnd = ClampFromAbove(scrollEnd, BottomRight(gfx::RectF(scaledContentsSize)) - BottomRight(gfx::Rect(m_deviceViewportSize)));
scrollEnd = ClampFromBelow(scrollEnd, gfx::Vector2d());
scrollEnd.ClampToMax(BottomRight(gfx::RectF(scaledContentsSize)) - BottomRight(gfx::Rect(m_deviceViewportSize)));
scrollEnd.ClampToMin(gfx::Vector2d());
scrollEnd.Scale(1 / pageScaleDeltaToSend);
scrollEnd.Scale(m_deviceScaleFactor);

@ -5,10 +5,11 @@
#include "config.h"
#include "cc/page_scale_animation.h"
#include "cc/geometry.h"
#include "ui/gfx/point_f.h"
#include "ui/gfx/rect_f.h"
#include <algorithm>
#include <math.h>
namespace {
@ -134,13 +135,9 @@ void PageScaleAnimation::inferTargetAnchorFromScrollOffsets()
void PageScaleAnimation::clampTargetScrollOffset()
{
gfx::Vector2dF maxScrollPosition;
maxScrollPosition.set_x(m_rootLayerSize.width() - viewportSizeAtScale(m_targetPageScaleFactor).width());
maxScrollPosition.set_y(m_rootLayerSize.height() - viewportSizeAtScale(m_targetPageScaleFactor).height());
m_targetScrollOffset.set_x(std::max<float>(0, m_targetScrollOffset.x()));
m_targetScrollOffset.set_y(std::max<float>(0, m_targetScrollOffset.y()));
m_targetScrollOffset.set_x(std::min<float>(maxScrollPosition.x(), m_targetScrollOffset.x()));
m_targetScrollOffset.set_y(std::min<float>(maxScrollPosition.y(), m_targetScrollOffset.y()));
gfx::Vector2dF maxScrollOffset = BottomRight(gfx::RectF(m_rootLayerSize)) - BottomRight(gfx::RectF(viewportSizeAtScale(m_targetPageScaleFactor)));
m_targetScrollOffset.ClampToMin(gfx::Vector2dF());
m_targetScrollOffset.ClampToMax(maxScrollOffset);
}
gfx::Vector2dF PageScaleAnimation::scrollOffsetAtTime(double time) const

@ -134,8 +134,8 @@ void TiledLayer::updateTileSizeAndTilingOption()
gfx::Size requestedSize = isTiled ? tileSize : contentBounds();
const int maxSize = layerTreeHost()->rendererCapabilities().maxTextureSize;
gfx::Size clampedSize = ClampSizeFromAbove(requestedSize, gfx::Size(maxSize, maxSize));
setTileSize(clampedSize);
requestedSize.ClampToMax(gfx::Size(maxSize, maxSize));
setTileSize(requestedSize);
}
void TiledLayer::updateBounds()

@ -43,6 +43,16 @@ class UI_EXPORT PointBase {
y_ -= vector.y();
}
void ClampToMax(const Class& max) {
x_ = x_ <= max.x_ ? x_ : max.x_;
y_ = y_ <= max.y_ ? y_ : max.y_;
}
void ClampToMin(const Class& min) {
x_ = x_ >= min.x_ ? x_ : min.x_;
y_ = y_ >= min.y_ ? y_ : min.y_;
}
bool IsOrigin() const {
return x_ == 0 && y_ == 0;
}

@ -119,4 +119,56 @@ TEST(PointTest, Scale) {
EXPECT_EQ(PointF(6, -3).ToString(), one.ToString());
}
TEST(PointTest, ClampPoint) {
Point a;
a = Point(3, 5);
EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
a.ClampToMin(Point(2, 4));
EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
a.ClampToMin(Point(3, 5));
EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
a.ClampToMin(Point(4, 2));
EXPECT_EQ(Point(4, 5).ToString(), a.ToString());
a.ClampToMin(Point(8, 10));
EXPECT_EQ(Point(8, 10).ToString(), a.ToString());
a.ClampToMax(Point(9, 11));
EXPECT_EQ(Point(8, 10).ToString(), a.ToString());
a.ClampToMax(Point(8, 10));
EXPECT_EQ(Point(8, 10).ToString(), a.ToString());
a.ClampToMax(Point(11, 9));
EXPECT_EQ(Point(8, 9).ToString(), a.ToString());
a.ClampToMax(Point(7, 11));
EXPECT_EQ(Point(7, 9).ToString(), a.ToString());
a.ClampToMax(Point(3, 5));
EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
}
TEST(PointTest, ClampPointF) {
PointF a;
a = PointF(3.5f, 5.5f);
EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(PointF(2.5f, 4.5f));
EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(PointF(3.5f, 5.5f));
EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(PointF(4.5f, 2.5f));
EXPECT_EQ(PointF(4.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(PointF(8.5f, 10.5f));
EXPECT_EQ(PointF(8.5f, 10.5f).ToString(), a.ToString());
a.ClampToMax(PointF(9.5f, 11.5f));
EXPECT_EQ(PointF(8.5f, 10.5f).ToString(), a.ToString());
a.ClampToMax(PointF(8.5f, 10.5f));
EXPECT_EQ(PointF(8.5f, 10.5f).ToString(), a.ToString());
a.ClampToMax(PointF(11.5f, 9.5f));
EXPECT_EQ(PointF(8.5f, 9.5f).ToString(), a.ToString());
a.ClampToMax(PointF(7.5f, 11.5f));
EXPECT_EQ(PointF(7.5f, 9.5f).ToString(), a.ToString());
a.ClampToMax(PointF(3.5f, 5.5f));
EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
}
} // namespace gfx

@ -35,6 +35,16 @@ class UI_EXPORT SizeBase {
void set_width(Type width) { width_ = width; }
void set_height(Type height) { height_ = height; }
void ClampToMax(const Class& max) {
width_ = width_ <= max.width_ ? width_ : max.width_;
height_ = height_ <= max.height_ ? height_ : max.height_;
}
void ClampToMin(const Class& min) {
width_ = width_ >= min.width_ ? width_ : min.width_;
height_ = height_ >= min.height_ ? height_ : min.height_;
}
bool IsEmpty() const {
return (width_ <= 0) || (height_ <= 0);
}

@ -91,4 +91,56 @@ TEST(SizeTest, ToRoundedSize) {
EXPECT_EQ(Size(-11, -11), ToRoundedSize(SizeF(-10.9999f, -10.9999f)));
}
TEST(SizeTest, ClampSize) {
Size a;
a = Size(3, 5);
EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
a.ClampToMin(Size(2, 4));
EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
a.ClampToMin(Size(3, 5));
EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
a.ClampToMin(Size(4, 2));
EXPECT_EQ(Size(4, 5).ToString(), a.ToString());
a.ClampToMin(Size(8, 10));
EXPECT_EQ(Size(8, 10).ToString(), a.ToString());
a.ClampToMax(Size(9, 11));
EXPECT_EQ(Size(8, 10).ToString(), a.ToString());
a.ClampToMax(Size(8, 10));
EXPECT_EQ(Size(8, 10).ToString(), a.ToString());
a.ClampToMax(Size(11, 9));
EXPECT_EQ(Size(8, 9).ToString(), a.ToString());
a.ClampToMax(Size(7, 11));
EXPECT_EQ(Size(7, 9).ToString(), a.ToString());
a.ClampToMax(Size(3, 5));
EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
}
TEST(SizeTest, ClampSizeF) {
SizeF a;
a = SizeF(3.5f, 5.5f);
EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(SizeF(2.5f, 4.5f));
EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(SizeF(3.5f, 5.5f));
EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(SizeF(4.5f, 2.5f));
EXPECT_EQ(SizeF(4.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(SizeF(8.5f, 10.5f));
EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString());
a.ClampToMax(SizeF(9.5f, 11.5f));
EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString());
a.ClampToMax(SizeF(8.5f, 10.5f));
EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString());
a.ClampToMax(SizeF(11.5f, 9.5f));
EXPECT_EQ(SizeF(8.5f, 9.5f).ToString(), a.ToString());
a.ClampToMax(SizeF(7.5f, 11.5f));
EXPECT_EQ(SizeF(7.5f, 9.5f).ToString(), a.ToString());
a.ClampToMax(SizeF(3.5f, 5.5f));
EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
}
} // namespace gfx

@ -40,6 +40,16 @@ class UI_EXPORT Vector2d {
void operator+=(const Vector2d& other) { Add(other); }
void operator-=(const Vector2d& other) { Subtract(other); }
void ClampToMax(const Vector2d& max) {
x_ = x_ <= max.x_ ? x_ : max.x_;
y_ = y_ <= max.y_ ? y_ : max.y_;
}
void ClampToMin(const Vector2d& min) {
x_ = x_ >= min.x_ ? x_ : min.x_;
y_ = y_ >= min.y_ ? y_ : min.y_;
}
// Gives the square of the diagonal length of the vector. Since this is
// cheaper to compute than Length(), it is useful when you want to compare
// relative lengths of different vectors without needing the actual lengths.

@ -38,6 +38,16 @@ class UI_EXPORT Vector2dF {
void operator+=(const Vector2dF& other) { Add(other); }
void operator-=(const Vector2dF& other) { Subtract(other); }
void ClampToMax(const Vector2dF& max) {
x_ = x_ <= max.x_ ? x_ : max.x_;
y_ = y_ <= max.y_ ? y_ : max.y_;
}
void ClampToMin(const Vector2dF& min) {
x_ = x_ >= min.x_ ? x_ : min.x_;
y_ = y_ >= min.y_ ? y_ : min.y_;
}
// Gives the square of the diagonal length of the vector.
double LengthSquared() const;
// Gives the diagonal length of the vector.

@ -195,4 +195,56 @@ TEST(Vector2dTest, Length) {
}
}
TEST(Vector2dTest, ClampVector2d) {
Vector2d a;
a = Vector2d(3, 5);
EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
a.ClampToMin(Vector2d(2, 4));
EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
a.ClampToMin(Vector2d(3, 5));
EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
a.ClampToMin(Vector2d(4, 2));
EXPECT_EQ(Vector2d(4, 5).ToString(), a.ToString());
a.ClampToMin(Vector2d(8, 10));
EXPECT_EQ(Vector2d(8, 10).ToString(), a.ToString());
a.ClampToMax(Vector2d(9, 11));
EXPECT_EQ(Vector2d(8, 10).ToString(), a.ToString());
a.ClampToMax(Vector2d(8, 10));
EXPECT_EQ(Vector2d(8, 10).ToString(), a.ToString());
a.ClampToMax(Vector2d(11, 9));
EXPECT_EQ(Vector2d(8, 9).ToString(), a.ToString());
a.ClampToMax(Vector2d(7, 11));
EXPECT_EQ(Vector2d(7, 9).ToString(), a.ToString());
a.ClampToMax(Vector2d(3, 5));
EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
}
TEST(Vector2dTest, ClampVector2dF) {
Vector2dF a;
a = Vector2dF(3.5f, 5.5f);
EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(Vector2dF(2.5f, 4.5f));
EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(Vector2dF(3.5f, 5.5f));
EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(Vector2dF(4.5f, 2.5f));
EXPECT_EQ(Vector2dF(4.5f, 5.5f).ToString(), a.ToString());
a.ClampToMin(Vector2dF(8.5f, 10.5f));
EXPECT_EQ(Vector2dF(8.5f, 10.5f).ToString(), a.ToString());
a.ClampToMax(Vector2dF(9.5f, 11.5f));
EXPECT_EQ(Vector2dF(8.5f, 10.5f).ToString(), a.ToString());
a.ClampToMax(Vector2dF(8.5f, 10.5f));
EXPECT_EQ(Vector2dF(8.5f, 10.5f).ToString(), a.ToString());
a.ClampToMax(Vector2dF(11.5f, 9.5f));
EXPECT_EQ(Vector2dF(8.5f, 9.5f).ToString(), a.ToString());
a.ClampToMax(Vector2dF(7.5f, 11.5f));
EXPECT_EQ(Vector2dF(7.5f, 9.5f).ToString(), a.ToString());
a.ClampToMax(Vector2dF(3.5f, 5.5f));
EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
}
} // namespace gfx

@ -44,6 +44,18 @@ class UI_EXPORT Vector3dF {
void operator+=(const Vector3dF& other) { Add(other); }
void operator-=(const Vector3dF& other) { Subtract(other); }
void ClampToMax(const Vector3dF& max) {
x_ = x_ <= max.x_ ? x_ : max.x_;
y_ = y_ <= max.y_ ? y_ : max.y_;
z_ = z_ <= max.z_ ? z_ : max.z_;
}
void ClampToMin(const Vector3dF& min) {
x_ = x_ >= min.x_ ? x_ : min.x_;
y_ = y_ >= min.y_ ? y_ : min.y_;
z_ = z_ >= min.z_ ? z_ : min.z_;
}
// Gives the square of the diagonal length of the vector.
double LengthSquared() const;
// Gives the diagonal length of the vector.

@ -230,4 +230,36 @@ TEST(Vector3dTest, CrossProduct) {
}
TEST(Vector3dFTest, ClampVector3dF) {
Vector3dF a;
a = Vector3dF(3.5f, 5.5f, 7.5f);
EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
a.ClampToMin(Vector3dF(2, 4.5f, 6.5f));
EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
a.ClampToMin(Vector3dF(3.5f, 5.5f, 7.5f));
EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
a.ClampToMin(Vector3dF(4.5f, 2, 6.5f));
EXPECT_EQ(Vector3dF(4.5f, 5.5f, 7.5f).ToString(), a.ToString());
a.ClampToMin(Vector3dF(3.5f, 6.5f, 6.5f));
EXPECT_EQ(Vector3dF(4.5f, 6.5f, 7.5f).ToString(), a.ToString());
a.ClampToMin(Vector3dF(3.5f, 5.5f, 8.5f));
EXPECT_EQ(Vector3dF(4.5f, 6.5f, 8.5f).ToString(), a.ToString());
a.ClampToMin(Vector3dF(8.5f, 10.5f, 12.5f));
EXPECT_EQ(Vector3dF(8.5f, 10.5f, 12.5f).ToString(), a.ToString());
a.ClampToMax(Vector3dF(9.5f, 11.5f, 13.5f));
EXPECT_EQ(Vector3dF(8.5f, 10.5f, 12.5f).ToString(), a.ToString());
a.ClampToMax(Vector3dF(8.5f, 10.5f, 12.5f));
EXPECT_EQ(Vector3dF(8.5f, 10.5f, 12.5f).ToString(), a.ToString());
a.ClampToMax(Vector3dF(7.5f, 11.5f, 13.5f));
EXPECT_EQ(Vector3dF(7.5f, 10.5f, 12.5f).ToString(), a.ToString());
a.ClampToMax(Vector3dF(9.5f, 9.5f, 13.5f));
EXPECT_EQ(Vector3dF(7.5f, 9.5f, 12.5f).ToString(), a.ToString());
a.ClampToMax(Vector3dF(9.5f, 11.5f, 11.5f));
EXPECT_EQ(Vector3dF(7.5f, 9.5f, 11.5f).ToString(), a.ToString());
a.ClampToMax(Vector3dF(3.5f, 5.5f, 7.5f));
EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
}
} // namespace gfx