2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "object/anchor_point.hpp"
24 #include "math/rectf.hpp"
25 #include "util/log.hpp"
27 std::string anchor_point_to_string(AnchorPoint point)
34 case ANCHOR_TOP_RIGHT:
42 case ANCHOR_BOTTOM_LEFT:
46 case ANCHOR_BOTTOM_RIGHT:
49 throw std::runtime_error("Invalid anchor point");
53 AnchorPoint string_to_anchor_point(const std::string& str)
56 return ANCHOR_TOP_LEFT;
59 else if(str == "topright")
60 return ANCHOR_TOP_RIGHT;
61 else if(str == "left")
63 else if(str == "middle")
65 else if(str == "right")
67 else if(str == "bottomleft")
68 return ANCHOR_BOTTOM_LEFT;
69 else if(str == "bottom")
71 else if(str == "bottomright")
72 return ANCHOR_BOTTOM_RIGHT;
74 std::ostringstream msg;
75 msg << "Unknown anchor '" << str << "'";
76 throw std::runtime_error(msg.str());
79 Vector get_anchor_pos(const Rectf& rect, AnchorPoint point)
83 switch(point & ANCHOR_V_MASK) {
85 result.x = rect.get_left();
88 result.x = rect.get_left() + (rect.get_right() - rect.get_left()) / 2.0;
91 result.x = rect.get_right();
95 throw std::runtime_error("Invalid anchor point found");
97 log_warning << "Invalid anchor point found" << std::endl;
98 result.x = rect.get_left();
102 switch(point & ANCHOR_H_MASK) {
104 result.y = rect.get_top();
107 result.y = rect.get_top() + (rect.get_bottom() - rect.get_top()) / 2.0;
110 result.y = rect.get_bottom();
114 throw std::runtime_error("Invalid anchor point found");
116 log_warning << "Invalid anchor point found" << std::endl;
117 result.y = rect.get_top();
124 Vector get_anchor_pos(const Rectf& destrect, float width, float height,
129 switch(point & ANCHOR_V_MASK) {
131 result.x = destrect.get_left();
134 result.x = destrect.get_middle().x - width/2.0;
137 result.x = destrect.get_right() - width;
141 throw std::runtime_error("Invalid anchor point found");
143 log_warning << "Invalid anchor point found" << std::endl;
144 result.x = destrect.get_left();
148 switch(point & ANCHOR_H_MASK) {
150 result.y = destrect.get_top();
153 result.y = destrect.get_middle().y - height/2.0;
156 result.y = destrect.get_bottom() - height;
160 throw std::runtime_error("Invalid anchor point found");
162 log_warning << "Invalid anchor point found" << std::endl;
163 result.y = destrect.get_top();