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