src/object/text_object.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/text_object.hpp"
00018 
00019 #include "scripting/squirrel_util.hpp"
00020 #include "supertux/globals.hpp"
00021 #include "supertux/resources.hpp"
00022 #include "video/drawing_context.hpp"
00023 
00024 TextObject::TextObject(std::string name) :
00025   font(),
00026   text(),
00027   fading(0), 
00028   fadetime(0), 
00029   visible(false), 
00030   centered(),
00031   anchor(ANCHOR_MIDDLE),
00032   pos(0, 0)
00033 {
00034   this->name = name;
00035   font = Resources::normal_font;
00036   centered = false;
00037 }
00038 
00039 TextObject::~TextObject()
00040 {
00041 }
00042 
00043 void
00044 TextObject::expose(HSQUIRRELVM vm, SQInteger table_idx)
00045 {
00046   if (name.empty())
00047     return;
00048 
00049   scripting::expose_object(vm, table_idx, dynamic_cast<scripting::Text *>(this), name, false);
00050 }
00051 
00052 void
00053 TextObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
00054 {
00055   if (name.empty())
00056     return;
00057 
00058   scripting::unexpose_object(vm, table_idx, name);
00059 }
00060 
00061 void
00062 TextObject::set_font(const std::string& name)
00063 {
00064   if(name == "normal") {
00065     font = Resources::normal_font;
00066   } else if(name == "big") {
00067     font = Resources::big_font;
00068   } else if(name == "small") {
00069     font = Resources::small_font;
00070   } else {
00071     log_warning << "Unknown font '" << name << "'." << std::endl;
00072     font = Resources::normal_font;
00073   }
00074 }
00075 
00076 void
00077 TextObject::set_text(const std::string& text)
00078 {
00079   this->text = text;
00080 }
00081 
00082 void
00083 TextObject::fade_in(float fadetime)
00084 {
00085   this->fadetime = fadetime;
00086   fading = fadetime;
00087 }
00088 
00089 void
00090 TextObject::fade_out(float fadetime)
00091 {
00092   this->fadetime = fadetime;
00093   fading = -fadetime;
00094 }
00095 
00096 void
00097 TextObject::set_visible(bool visible)
00098 {
00099   this->visible = visible;
00100   fading = 0;
00101 }
00102 
00103 void
00104 TextObject::set_centered(bool centered)
00105 {
00106   this->centered = centered;
00107 }
00108 
00109 void
00110 TextObject::draw(DrawingContext& context)
00111 {
00112   context.push_transform();
00113   context.set_translation(Vector(0, 0));
00114   if(fading > 0) {
00115     context.set_alpha((fadetime-fading) / fadetime);
00116   } else if(fading < 0) {
00117     context.set_alpha(-fading / fadetime);
00118   } else if(!visible) {
00119     context.pop_transform();
00120     return;
00121   }
00122 
00123   float width  = 500;
00124   float height = 70;
00125   Vector spos = pos + get_anchor_pos(Rectf(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
00126                                      width, height, anchor);
00127 
00128   context.draw_filled_rect(spos, Vector(width, height),
00129                            Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-50);
00130   if (centered) {
00131     context.draw_center_text(font, text, spos, LAYER_GUI-40, TextObject::default_color);
00132   } else {
00133     context.draw_text(font, text, spos + Vector(10, 10), ALIGN_LEFT, LAYER_GUI-40, TextObject::default_color);
00134   }
00135 
00136   context.pop_transform();
00137 }
00138 
00139 void
00140 TextObject::update(float elapsed_time)
00141 {
00142   if(fading > 0) {
00143     fading -= elapsed_time;
00144     if(fading <= 0) {
00145       fading = 0;
00146       visible = true;
00147     }
00148   } else if(fading < 0) {
00149     fading += elapsed_time;
00150     if(fading >= 0) {
00151       fading = 0;
00152       visible = false;
00153     }
00154   }
00155 }
00156 
00157 /* EOF */

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