src/object/anchor_point.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "object/anchor_point.hpp"
00018 
00019 #include <config.h>
00020 
00021 #include <stdexcept>
00022 #include <sstream>
00023 
00024 #include "math/rectf.hpp"
00025 #include "util/log.hpp"
00026 
00027 std::string anchor_point_to_string(AnchorPoint point)
00028 {
00029   switch(point) {
00030     case ANCHOR_TOP_LEFT:
00031       return "topleft";
00032     case ANCHOR_TOP:
00033       return "top";
00034     case ANCHOR_TOP_RIGHT:
00035       return "topright";
00036     case ANCHOR_LEFT:
00037       return "left";
00038     case ANCHOR_MIDDLE:
00039       return "middle";
00040     case ANCHOR_RIGHT:
00041       return "right";
00042     case ANCHOR_BOTTOM_LEFT:
00043       return "bottomleft";
00044     case ANCHOR_BOTTOM:
00045       return "bottom";
00046     case ANCHOR_BOTTOM_RIGHT:
00047       return "bottomright";
00048     default:
00049       throw std::runtime_error("Invalid anchor point");
00050   }
00051 }
00052 
00053 AnchorPoint string_to_anchor_point(const std::string& str)
00054 {
00055   if(str == "topleft")
00056     return ANCHOR_TOP_LEFT;
00057   else if(str == "top")
00058     return ANCHOR_TOP;
00059   else if(str == "topright")
00060     return ANCHOR_TOP_RIGHT;
00061   else if(str == "left")
00062     return ANCHOR_LEFT;
00063   else if(str == "middle")
00064     return ANCHOR_MIDDLE;
00065   else if(str == "right")
00066     return ANCHOR_RIGHT;
00067   else if(str == "bottomleft")
00068     return ANCHOR_BOTTOM_LEFT;
00069   else if(str == "bottom")
00070     return ANCHOR_BOTTOM;
00071   else if(str == "bottomright")
00072     return ANCHOR_BOTTOM_RIGHT;
00073 
00074   std::ostringstream msg;
00075   msg << "Unknown anchor '" << str << "'";
00076   throw std::runtime_error(msg.str());
00077 }
00078 
00079 Vector get_anchor_pos(const Rectf& rect, AnchorPoint point)
00080 {
00081   Vector result;
00082 
00083   switch(point & ANCHOR_V_MASK) {
00084     case ANCHOR_LEFT:
00085       result.x = rect.get_left();
00086       break;
00087     case ANCHOR_MIDDLE:
00088       result.x = rect.get_left() + (rect.get_right() - rect.get_left()) / 2.0;
00089       break;
00090     case ANCHOR_RIGHT:
00091       result.x = rect.get_right();
00092       break;
00093     default:
00094       log_warning << "Invalid anchor point found" << std::endl;
00095       result.x = rect.get_left();
00096       break;
00097   }
00098 
00099   switch(point & ANCHOR_H_MASK) {
00100     case ANCHOR_TOP:
00101       result.y = rect.get_top();
00102       break;
00103     case ANCHOR_MIDDLE:
00104       result.y = rect.get_top() + (rect.get_bottom() - rect.get_top()) / 2.0;
00105       break;
00106     case ANCHOR_BOTTOM:
00107       result.y = rect.get_bottom();
00108       break;
00109     default:
00110       log_warning << "Invalid anchor point found" << std::endl;
00111       result.y = rect.get_top();
00112       break;
00113   }
00114 
00115   return result;
00116 }
00117 
00118 Vector get_anchor_pos(const Rectf& destrect, float width, float height,
00119                       AnchorPoint point)
00120 {
00121   Vector result;
00122 
00123   switch(point & ANCHOR_V_MASK) {
00124     case ANCHOR_LEFT:
00125       result.x = destrect.get_left();
00126       break;
00127     case ANCHOR_MIDDLE:
00128       result.x = destrect.get_middle().x - width/2.0;
00129       break;
00130     case ANCHOR_RIGHT:
00131       result.x = destrect.get_right() - width;
00132       break;
00133     default:
00134       log_warning << "Invalid anchor point found" << std::endl;
00135       result.x = destrect.get_left();
00136       break;
00137   }
00138 
00139   switch(point & ANCHOR_H_MASK) {
00140     case ANCHOR_TOP:
00141       result.y = destrect.get_top();
00142       break;
00143     case ANCHOR_MIDDLE:
00144       result.y = destrect.get_middle().y - height/2.0;
00145       break;
00146     case ANCHOR_BOTTOM:
00147       result.y = destrect.get_bottom() - height;
00148       break;
00149     default:
00150       log_warning << "Invalid anchor point found" << std::endl;
00151       result.y = destrect.get_top();
00152       break;
00153   }
00154 
00155   return result;
00156 }
00157 
00158 /* EOF */

Generated on Mon Jun 9 03:38:19 2014 for SuperTux by  doxygen 1.5.1