fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / object / anchor_point.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <stdexcept>
23 #include <sstream>
24 #include "anchor_point.hpp"
25 #include "math/rect.hpp"
26 #include "log.hpp"
27
28 std::string anchor_point_to_string(AnchorPoint point)
29 {
30   switch(point) {
31     case ANCHOR_TOP_LEFT:
32       return "topleft";
33     case ANCHOR_TOP:
34       return "top";
35     case ANCHOR_TOP_RIGHT:
36       return "topright";
37     case ANCHOR_LEFT:
38       return "left";
39     case ANCHOR_MIDDLE:
40       return "middle";
41     case ANCHOR_RIGHT:
42       return "right";
43     case ANCHOR_BOTTOM_LEFT:
44       return "bottomleft";
45     case ANCHOR_BOTTOM:
46       return "bottom";
47     case ANCHOR_BOTTOM_RIGHT:
48       return "bottomright";
49     default:
50       throw std::runtime_error("Invalid anchor point");
51   }
52 }
53
54 AnchorPoint string_to_anchor_point(const std::string& str)
55 {
56   if(str == "topleft")
57     return ANCHOR_TOP_LEFT;
58   else if(str == "top")
59     return ANCHOR_TOP;
60   else if(str == "topright")
61     return ANCHOR_TOP_RIGHT;
62   else if(str == "left")
63     return ANCHOR_LEFT;
64   else if(str == "middle")
65     return ANCHOR_MIDDLE;
66   else if(str == "right")
67     return ANCHOR_RIGHT;
68   else if(str == "bottomleft")
69     return ANCHOR_BOTTOM_LEFT;
70   else if(str == "bottom")
71     return ANCHOR_BOTTOM;
72   else if(str == "bottomright")
73     return ANCHOR_BOTTOM_RIGHT;
74
75   std::ostringstream msg;
76   msg << "Unknown anchor '" << str << "'";
77   throw std::runtime_error(msg.str());
78 }
79
80 Vector get_anchor_pos(const Rect& rect, AnchorPoint point)
81 {
82   Vector result;
83
84   switch(point & ANCHOR_V_MASK) {
85     case ANCHOR_LEFT:
86       result.x = rect.get_left();
87       break;
88     case ANCHOR_MIDDLE:
89       result.x = rect.get_left() + (rect.get_right() - rect.get_left()) / 2.0;
90       break;
91     case ANCHOR_RIGHT:
92       result.x = rect.get_right();
93       break;
94     default:
95 #ifdef DEBUG
96       throw std::runtime_error("Invalid anchor point found");
97 #endif
98       log_warning << "Invalid anchor point found" << std::endl;
99       result.x = rect.get_left();
100       break;
101   }
102
103   switch(point & ANCHOR_H_MASK) {
104     case ANCHOR_TOP:
105       result.y = rect.get_top();
106       break;
107     case ANCHOR_MIDDLE:
108       result.y = rect.get_top() + (rect.get_bottom() - rect.get_top()) / 2.0;
109       break;
110     case ANCHOR_BOTTOM:
111       result.y = rect.get_bottom();
112       break;
113     default:
114 #ifdef DEBUG
115       throw std::runtime_error("Invalid anchor point found");
116 #endif
117       log_warning << "Invalid anchor point found" << std::endl;
118       result.y = rect.get_top();
119       break;
120   }
121
122   return result;
123 }
124
125 Vector get_anchor_pos(const Rect& destrect, float width, float height,
126                       AnchorPoint point)
127 {
128   Vector result;
129
130   switch(point & ANCHOR_V_MASK) {
131     case ANCHOR_LEFT:
132       result.x = destrect.get_left();
133       break;
134     case ANCHOR_MIDDLE:
135       result.x = destrect.get_middle().x - width/2.0;
136       break;
137     case ANCHOR_RIGHT:
138       result.x = destrect.get_right() - width;
139       break;
140     default:
141 #ifdef DEBUG
142       throw std::runtime_error("Invalid anchor point found");
143 #endif
144       log_warning << "Invalid anchor point found" << std::endl;
145       result.x = destrect.get_left();
146       break;
147   }
148
149   switch(point & ANCHOR_H_MASK) {
150     case ANCHOR_TOP:
151       result.y = destrect.get_top();
152       break;
153     case ANCHOR_MIDDLE:
154       result.y = destrect.get_middle().y - height/2.0;
155       break;
156     case ANCHOR_BOTTOM:
157       result.y = destrect.get_bottom() - height;
158       break;
159     default:
160 #ifdef DEBUG
161       throw std::runtime_error("Invalid anchor point found");
162 #endif
163       log_warning << "Invalid anchor point found" << std::endl;
164       result.y = destrect.get_top();
165       break;
166   }
167
168   return result;
169 }