rect.h 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef ANBOX_GRAPHICS_RECT_H_
#define ANBOX_GRAPHICS_RECT_H_

#include <cstdint>

23 24
#include <ostream>

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
namespace anbox {
namespace graphics {
class Rect {
public:
    static const Rect Invalid;
    static const Rect Empty;

    inline Rect() : Rect(Empty) {}

    inline Rect(const std::int32_t &left,
                const std::int32_t &top,
                const std::int32_t &right,
                const std::int32_t &bottom) :
        left_(left),
        top_(top),
        right_(right),
        bottom_(bottom) {
    }

    inline Rect(const std::int32_t &width, const std::int32_t &height) :
        left_(0),
        top_(0),
        right_(width),
        bottom_(height) {
    }

    inline void clear() {
        left_ = top_ = right_ = bottom_ = 0;
    }

    inline bool valid() const {
        return width() >= 0 && height() >= 0;
    }

    inline std::int32_t width() const {
        return right_ - left_;
    }

    inline std::int32_t height() const {
        return bottom_ - top_;
    }

    inline std::int32_t left() const {
        return left_;
    }

    inline std::int32_t top() const {
        return top_;
    }

    inline std::int32_t right() const {
        return right_;
    }

    inline std::int32_t bottom() const {
        return bottom_;
    }

    inline bool operator == (const Rect &rhs) const {
        return (left_ == rhs.left() &&
                top_ == rhs.top() &&
                right_ == rhs.right() &&
                bottom_ == rhs.bottom());
    }

    inline bool operator != (const Rect &rhs) const {
        return !operator == (rhs);
    }

94 95
    void merge(const Rect &rhs);

96 97 98 99 100 101
private:
    std::int32_t left_;
    std::int32_t top_;
    std::int32_t right_;
    std::int32_t bottom_;
};
102 103

std::ostream& operator<<(std::ostream &out, const Rect &rect);
104 105 106 107
} // namespace graphics
} // namespace anbox

#endif