5 #include "anchor_point.hpp"
6 #include "math/rect.hpp"
9 std::string anchor_point_to_string(AnchorPoint point)
16 case ANCHOR_TOP_RIGHT:
24 case ANCHOR_BOTTOM_LEFT:
28 case ANCHOR_BOTTOM_RIGHT:
31 throw std::runtime_error("Invalid anchor point");
35 AnchorPoint string_to_anchor_point(const std::string& str)
38 return ANCHOR_TOP_LEFT;
41 else if(str == "topright")
42 return ANCHOR_TOP_RIGHT;
43 else if(str == "left")
45 else if(str == "middle")
47 else if(str == "right")
49 else if(str == "bottomleft")
50 return ANCHOR_BOTTOM_LEFT;
51 else if(str == "bottom")
53 else if(str == "bottomright")
54 return ANCHOR_BOTTOM_RIGHT;
56 std::ostringstream msg;
57 msg << "Unknown anchor '" << str << "'";
58 throw std::runtime_error(msg.str());
61 Vector get_anchor_pos(const Rect& rect, AnchorPoint point)
65 switch(point & ANCHOR_V_MASK) {
67 result.x = rect.get_left();
70 result.x = rect.get_left() + (rect.get_right() - rect.get_left()) / 2.0;
73 result.x = rect.get_right();
77 throw std::runtime_error("Invalid anchor point found");
79 msg_warning << "Invalid anchor point found" << std::endl;
80 result.x = rect.get_left();
84 switch(point & ANCHOR_H_MASK) {
86 result.y = rect.get_top();
89 result.y = rect.get_top() + (rect.get_bottom() - rect.get_top()) / 2.0;
92 result.y = rect.get_bottom();
96 throw std::runtime_error("Invalid anchor point found");
98 msg_warning << "Invalid anchor point found" << std::endl;
99 result.y = rect.get_top();
106 Vector get_anchor_pos(const Rect& destrect, float width, float height,
111 switch(point & ANCHOR_V_MASK) {
113 result.x = destrect.get_left();
116 result.x = destrect.get_middle().x - width/2.0;
119 result.x = destrect.get_right() - width;
123 throw std::runtime_error("Invalid anchor point found");
125 msg_warning << "Invalid anchor point found" << std::endl;
126 result.x = destrect.get_left();
130 switch(point & ANCHOR_H_MASK) {
132 result.y = destrect.get_top();
135 result.y = destrect.get_middle().y - height/2.0;
138 result.y = destrect.get_bottom() - height;
142 throw std::runtime_error("Invalid anchor point found");
144 msg_warning << "Invalid anchor point found" << std::endl;
145 result.y = destrect.get_top();