5 #include "anchor_point.hpp"
6 #include "math/rect.hpp"
8 std::string anchor_point_to_string(AnchorPoint point)
15 case ANCHOR_TOP_RIGHT:
23 case ANCHOR_BOTTOM_LEFT:
27 case ANCHOR_BOTTOM_RIGHT:
30 throw std::runtime_error("Invalid anchor point");
34 AnchorPoint string_to_anchor_point(const std::string& str)
37 return ANCHOR_TOP_LEFT;
40 else if(str == "topright")
41 return ANCHOR_TOP_RIGHT;
42 else if(str == "left")
44 else if(str == "middle")
46 else if(str == "right")
48 else if(str == "bottomleft")
49 return ANCHOR_BOTTOM_LEFT;
50 else if(str == "bottom")
52 else if(str == "bottomright")
53 return ANCHOR_BOTTOM_RIGHT;
55 std::ostringstream msg;
56 msg << "Unknown anchor '" << str << "'";
57 throw std::runtime_error(msg.str());
60 Vector get_anchor_pos(const Rect& rect, AnchorPoint point)
64 switch(point & ANCHOR_V_MASK) {
66 result.x = rect.get_left();
69 result.x = rect.get_left() + (rect.get_right() - rect.get_left()) / 2.0;
72 result.x = rect.get_right();
76 throw new std::runtime_error("Invalid anchor point found");
78 printf("Invalid anchor point found");
79 result.x = rect.get_left();
83 switch(point & ANCHOR_H_MASK) {
85 result.y = rect.get_top();
88 result.y = rect.get_top() + (rect.get_bottom() - rect.get_top()) / 2.0;
91 result.y = rect.get_bottom();
95 throw new std::runtime_error("Invalid anchor point found");
97 printf("Invalid anchor point found");
98 result.y = rect.get_top();
105 Vector get_anchor_pos(const Rect& destrect, float width, float height,
110 switch(point & ANCHOR_V_MASK) {
112 result.x = destrect.get_left();
115 result.x = destrect.get_middle().x - width/2.0;
118 result.x = destrect.get_right() - width;
122 throw new std::runtime_error("Invalid anchor point found");
124 printf("Invalid anchor point found");
125 result.x = destrect.get_left();
129 switch(point & ANCHOR_H_MASK) {
131 result.y = destrect.get_top();
134 result.y = destrect.get_middle().y - height/2.0;
137 result.y = destrect.get_bottom() - height;
141 throw new std::runtime_error("Invalid anchor point found");
143 printf("Invalid anchor point found");
144 result.y = destrect.get_top();