Home | History | Annotate | Download | only in geometry
      1 // Copyright (c) 2012 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 #ifndef UI_GFX_GEOMETRY_INSETS_F_H_
      6 #define UI_GFX_GEOMETRY_INSETS_F_H_
      7 
      8 #include <string>
      9 
     10 #include "ui/gfx/gfx_export.h"
     11 
     12 namespace gfx {
     13 
     14 // A floating point version of gfx::Insets.
     15 class GFX_EXPORT InsetsF {
     16  public:
     17   constexpr InsetsF() : top_(0.f), left_(0.f), bottom_(0.f), right_(0.f) {}
     18   constexpr explicit InsetsF(float all)
     19       : top_(all), left_(all), bottom_(all), right_(all) {}
     20   constexpr InsetsF(float vertical, float horizontal)
     21       : top_(vertical),
     22         left_(horizontal),
     23         bottom_(vertical),
     24         right_(horizontal) {}
     25   constexpr InsetsF(float top, float left, float bottom, float right)
     26       : top_(top), left_(left), bottom_(bottom), right_(right) {}
     27 
     28   constexpr float top() const { return top_; }
     29   constexpr float left() const { return left_; }
     30   constexpr float bottom() const { return bottom_; }
     31   constexpr float right() const { return right_; }
     32 
     33   // Returns the total width taken up by the insets, which is the sum of the
     34   // left and right insets.
     35   constexpr float width() const { return left_ + right_; }
     36 
     37   // Returns the total height taken up by the insets, which is the sum of the
     38   // top and bottom insets.
     39   constexpr float height() const { return top_ + bottom_; }
     40 
     41   // Returns true if the insets are empty.
     42   bool IsEmpty() const { return width() == 0.f && height() == 0.f; }
     43 
     44   void Set(float top, float left, float bottom, float right) {
     45     top_ = top;
     46     left_ = left;
     47     bottom_ = bottom;
     48     right_ = right;
     49   }
     50 
     51   bool operator==(const InsetsF& insets) const {
     52     return top_ == insets.top_ && left_ == insets.left_ &&
     53            bottom_ == insets.bottom_ && right_ == insets.right_;
     54   }
     55 
     56   bool operator!=(const InsetsF& insets) const {
     57     return !(*this == insets);
     58   }
     59 
     60   void operator+=(const InsetsF& insets) {
     61     top_ += insets.top_;
     62     left_ += insets.left_;
     63     bottom_ += insets.bottom_;
     64     right_ += insets.right_;
     65   }
     66 
     67   void operator-=(const InsetsF& insets) {
     68     top_ -= insets.top_;
     69     left_ -= insets.left_;
     70     bottom_ -= insets.bottom_;
     71     right_ -= insets.right_;
     72   }
     73 
     74   InsetsF operator-() const {
     75     return InsetsF(-top_, -left_, -bottom_, -right_);
     76   }
     77 
     78   // Returns a string representation of the insets.
     79   std::string ToString() const;
     80 
     81  private:
     82   float top_;
     83   float left_;
     84   float bottom_;
     85   float right_;
     86 };
     87 
     88 inline InsetsF operator+(InsetsF lhs, const InsetsF& rhs) {
     89   lhs += rhs;
     90   return lhs;
     91 }
     92 
     93 inline InsetsF operator-(InsetsF lhs, const InsetsF& rhs) {
     94   lhs -= rhs;
     95   return lhs;
     96 }
     97 
     98 }  // namespace gfx
     99 
    100 #endif  // UI_GFX_GEOMETRY_INSETS_F_H_
    101