ckocagil | a1693d1 | 2015-02-19 03:15:27 +0900 | [diff] [blame] | 1 | // Copyright 2015 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_RANGE_RANGE_F_H_ |
| 6 | #define UI_GFX_RANGE_RANGE_F_H_ |
| 7 | |
| 8 | #include <ostream> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/basictypes.h" |
| 12 | #include "ui/gfx/gfx_export.h" |
xdai | c7f552d | 2015-06-24 05:22:03 +0900 | [diff] [blame] | 13 | #include "ui/gfx/range/range.h" |
ckocagil | a1693d1 | 2015-02-19 03:15:27 +0900 | [diff] [blame] | 14 | |
| 15 | namespace gfx { |
| 16 | |
| 17 | // A float version of Range. RangeF is made of a start and end position; when |
| 18 | // they are the same, the range is empty. Note that |start_| can be greater |
| 19 | // than |end_| to respect the directionality of the range. |
| 20 | class GFX_EXPORT RangeF { |
| 21 | public: |
| 22 | // Creates an empty range {0,0}. |
| 23 | RangeF(); |
| 24 | |
| 25 | // Initializes the range with a start and end. |
| 26 | RangeF(float start, float end); |
| 27 | |
| 28 | // Initializes the range with the same start and end positions. |
| 29 | explicit RangeF(float position); |
| 30 | |
| 31 | // Returns a range that is invalid, which is {float_max,float_max}. |
| 32 | static const RangeF InvalidRange(); |
| 33 | |
| 34 | // Checks if the range is valid through comparison to InvalidRange(). |
| 35 | bool IsValid() const; |
| 36 | |
| 37 | // Getters and setters. |
| 38 | float start() const { return start_; } |
| 39 | void set_start(float start) { start_ = start; } |
| 40 | |
| 41 | float end() const { return end_; } |
| 42 | void set_end(float end) { end_ = end; } |
| 43 | |
| 44 | // Returns the absolute value of the length. |
| 45 | float length() const { |
| 46 | const float length = end() - start(); |
| 47 | return length >= 0 ? length : -length; |
| 48 | } |
| 49 | |
| 50 | bool is_reversed() const { return start() > end(); } |
| 51 | bool is_empty() const { return start() == end(); } |
| 52 | |
| 53 | // Returns the minimum and maximum values. |
| 54 | float GetMin() const; |
| 55 | float GetMax() const; |
| 56 | |
| 57 | bool operator==(const RangeF& other) const; |
| 58 | bool operator!=(const RangeF& other) const; |
| 59 | bool EqualsIgnoringDirection(const RangeF& other) const; |
| 60 | |
| 61 | // Returns true if this range intersects the specified |range|. |
| 62 | bool Intersects(const RangeF& range) const; |
| 63 | |
| 64 | // Returns true if this range contains the specified |range|. |
| 65 | bool Contains(const RangeF& range) const; |
| 66 | |
| 67 | // Computes the intersection of this range with the given |range|. |
| 68 | // If they don't intersect, it returns an InvalidRange(). |
| 69 | // The returned range is always empty or forward (never reversed). |
| 70 | RangeF Intersect(const RangeF& range) const; |
xdai | c7f552d | 2015-06-24 05:22:03 +0900 | [diff] [blame] | 71 | RangeF Intersect(const Range& range) const; |
| 72 | |
| 73 | // Floor/Ceil/Round the start and end values of the given RangeF. |
| 74 | Range Floor() const; |
| 75 | Range Ceil() const; |
| 76 | Range Round() const; |
ckocagil | a1693d1 | 2015-02-19 03:15:27 +0900 | [diff] [blame] | 77 | |
| 78 | std::string ToString() const; |
| 79 | |
| 80 | private: |
| 81 | float start_; |
| 82 | float end_; |
| 83 | }; |
| 84 | |
| 85 | GFX_EXPORT std::ostream& operator<<(std::ostream& os, const RangeF& range); |
| 86 | |
| 87 | } // namespace gfx |
| 88 | |
| 89 | #endif // UI_GFX_RANGE_RANGE_F_H_ |