TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / graphics / rect.cpp
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3, as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranties of
10  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11  * PURPOSE.  See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 #include "anbox/graphics/rect.h"
19 #include "anbox/utils.h"
20
21 #include <algorithm>
22 #include <string>
23
24 #include <boost/lexical_cast.hpp>
25
26 namespace anbox {
27 namespace graphics {
28 const Rect Rect::Invalid{-1, -1, -1, -1};
29 const Rect Rect::Empty{0, 0, 0, 0};
30
31 void Rect::merge(const Rect &rhs) {
32   left_ = std::min(left_, rhs.left());
33   top_ = std::min(top_, rhs.top());
34   right_ = std::max(right_, rhs.right());
35   bottom_ = std::max(bottom_, rhs.bottom());
36 }
37
38 void Rect::translate(const std::int32_t &x, const std::int32_t &y) {
39   auto old_width = width();
40   auto old_height = height();
41   left_ = x;
42   top_ = y;
43   right_ = x + old_width;
44   bottom_ = y + old_height;
45 }
46
47 void Rect::resize(const std::int32_t &width, const std::int32_t &height) {
48   right_ = left_ + width;
49   bottom_ = top_ + height;
50 }
51
52 std::ostream &operator<<(std::ostream &out, const Rect &rect) {
53   return out << "{" << rect.left() << "," << rect.top() << "," << rect.right()
54              << "," << rect.bottom() << "} {" << rect.width() << ","
55              << rect.height() << "}";
56 }
57
58 std::istream& operator>>(std::istream& in, anbox::graphics::Rect &rect)
59 try {
60   std::string str;
61   in >> str;
62   auto tokens = anbox::utils::string_split(str, ',');
63
64   switch (tokens.size()) {
65   case 2: {
66     rect = anbox::graphics::Rect(0, 0,
67              boost::lexical_cast<std::int32_t>(tokens[0]),
68              boost::lexical_cast<std::int32_t>(tokens[1]));
69     break;
70   }
71   case 4:
72     rect = anbox::graphics::Rect(
73              boost::lexical_cast<std::int32_t>(tokens[0]),
74              boost::lexical_cast<std::int32_t>(tokens[1]),
75              boost::lexical_cast<std::int32_t>(tokens[2]),
76              boost::lexical_cast<std::int32_t>(tokens[3]));
77     break;
78   default:
79     break;
80   }
81
82   return in;
83 } catch (...) {
84   return in;
85 }
86 }  // namespace graphics
87 }  // namespace anbox