Unified Messaging Subsystem
[supertux.git] / src / object / anchor_point.cpp
1 #include <config.h>
2
3 #include <stdexcept>
4 #include <sstream>
5 #include "anchor_point.hpp"
6 #include "math/rect.hpp"
7 #include "msg.hpp"
8
9 std::string anchor_point_to_string(AnchorPoint point)
10 {
11   switch(point) {
12     case ANCHOR_TOP_LEFT:
13       return "topleft";
14     case ANCHOR_TOP:
15       return "top";
16     case ANCHOR_TOP_RIGHT:
17       return "topright";
18     case ANCHOR_LEFT:
19       return "left";
20     case ANCHOR_MIDDLE:
21       return "middle";
22     case ANCHOR_RIGHT:
23       return "right";
24     case ANCHOR_BOTTOM_LEFT:
25       return "bottomleft";
26     case ANCHOR_BOTTOM:
27       return "bottom";
28     case ANCHOR_BOTTOM_RIGHT:
29       return "bottomright";
30     default:
31       throw std::runtime_error("Invalid anchor point");
32   }
33 }
34
35 AnchorPoint string_to_anchor_point(const std::string& str)
36 {
37   if(str == "topleft")
38     return ANCHOR_TOP_LEFT;
39   else if(str == "top")
40     return ANCHOR_TOP;
41   else if(str == "topright")
42     return ANCHOR_TOP_RIGHT;
43   else if(str == "left")
44     return ANCHOR_LEFT;
45   else if(str == "middle")
46     return ANCHOR_MIDDLE;
47   else if(str == "right")
48     return ANCHOR_RIGHT;
49   else if(str == "bottomleft")
50     return ANCHOR_BOTTOM_LEFT;
51   else if(str == "bottom")
52     return ANCHOR_BOTTOM;
53   else if(str == "bottomright")
54     return ANCHOR_BOTTOM_RIGHT;
55     
56   std::ostringstream msg;
57   msg << "Unknown anchor '" << str << "'";
58   throw std::runtime_error(msg.str());
59 }
60
61 Vector get_anchor_pos(const Rect& rect, AnchorPoint point)
62 {
63   Vector result;
64   
65   switch(point & ANCHOR_V_MASK) {
66     case ANCHOR_LEFT:
67       result.x = rect.get_left();
68       break;
69     case ANCHOR_MIDDLE:
70       result.x = rect.get_left() + (rect.get_right() - rect.get_left()) / 2.0;
71       break;
72     case ANCHOR_RIGHT:
73       result.x = rect.get_right();
74       break;
75     default:
76 #ifdef DEBUG
77       throw new std::runtime_error("Invalid anchor point found");
78 #endif
79       msg_warning("Invalid anchor point found");
80       result.x = rect.get_left();
81       break;
82   }
83
84   switch(point & ANCHOR_H_MASK) {
85     case ANCHOR_TOP:
86       result.y = rect.get_top();
87       break;
88     case ANCHOR_MIDDLE:
89       result.y = rect.get_top() + (rect.get_bottom() - rect.get_top()) / 2.0;
90       break;
91     case ANCHOR_BOTTOM:
92       result.y = rect.get_bottom();
93       break;
94     default:
95 #ifdef DEBUG
96       throw new std::runtime_error("Invalid anchor point found");
97 #endif
98       msg_warning("Invalid anchor point found");
99       result.y = rect.get_top();
100       break;
101   }
102   
103   return result;
104 }
105
106 Vector get_anchor_pos(const Rect& destrect, float width, float height,
107                       AnchorPoint point)
108 {
109   Vector result;
110   
111   switch(point & ANCHOR_V_MASK) {
112     case ANCHOR_LEFT:
113       result.x = destrect.get_left();
114       break;
115     case ANCHOR_MIDDLE:
116       result.x = destrect.get_middle().x - width/2.0;
117       break;
118     case ANCHOR_RIGHT:
119       result.x = destrect.get_right() - width;
120       break;
121     default:
122 #ifdef DEBUG
123       throw new std::runtime_error("Invalid anchor point found");
124 #endif
125       msg_warning("Invalid anchor point found");
126       result.x = destrect.get_left();
127       break;
128   }
129
130   switch(point & ANCHOR_H_MASK) {
131     case ANCHOR_TOP:
132       result.y = destrect.get_top();
133       break;
134     case ANCHOR_MIDDLE:
135       result.y = destrect.get_middle().y - height/2.0;
136       break;
137     case ANCHOR_BOTTOM:
138       result.y = destrect.get_bottom() - height;
139       break;
140     default:
141 #ifdef DEBUG
142       throw new std::runtime_error("Invalid anchor point found");
143 #endif
144       msg_warning("Invalid anchor point found");
145       result.y = destrect.get_top();
146       break;
147   }
148   
149   return result; 
150 }
151