widgets/progressbar.cpp

Go to the documentation of this file.
00001 /* $Id: progressbar.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include "global.hpp"
00019 
00020 #include "widgets/progressbar.hpp"
00021 
00022 #include "font.hpp"
00023 #include "marked-up_text.hpp"
00024 #include "video.hpp"
00025 
00026 namespace gui {
00027 
00028 progress_bar::progress_bar(CVideo& video) : widget(video), progress_(0)
00029 {}
00030 
00031 void progress_bar::set_progress_percent(int progress)
00032 {
00033     progress_ = progress;
00034     set_dirty();
00035 }
00036 
00037 void progress_bar::set_text(const std::string& text)
00038 {
00039     text_ = text;
00040     set_dirty();
00041 }
00042 
00043 void progress_bar::draw_contents()
00044 {
00045     surface surf = video().getSurface();
00046     SDL_Rect area = location();
00047 
00048     if(area.w >= 2 && area.h >= 2) {
00049         int fcr =  21, fcg =  53, fcb =  80;        // RGB-values for finished piece.
00050         int bcr =   0, bcg =   0, bcb =   0;        // Border color.
00051         int gcr = 255, gcg = 255, gcb = 255;        // Groove color.
00052         int lightning_thickness = 2;
00053         static const SDL_Color selected_text_color = {0xCC,0xCC,0xCC,0};
00054 
00055         SDL_Rect inner_area = create_rect(area.x + 1
00056                 , area.y + 1
00057                 , area.w - 2
00058                 , area.h - 2);
00059 
00060         sdl_fill_rect(surf,&area,SDL_MapRGB(surf->format,bcr,bcg,bcb));
00061         sdl_fill_rect(surf,&inner_area,SDL_MapRGB(surf->format,gcr,gcg,gcb));
00062 
00063         inner_area.w = (inner_area.w*progress_)/100;
00064         sdl_fill_rect(surf,&inner_area,SDL_MapRGB(surf->format,fcr,fcg,fcb));
00065 
00066         SDL_Rect lightning = inner_area;
00067         lightning.h = lightning_thickness;
00068         //we add 25% of white to the color of the bar to simulate a light effect
00069         sdl_fill_rect(surf,&lightning,SDL_MapRGB(surf->format,(fcr*3+255)/4,(fcg*3+255)/4,(fcb*3+255)/4));
00070         lightning.y = inner_area.y+inner_area.h-lightning.h;
00071         //remove 50% of color to simulate a shadow effect
00072         sdl_fill_rect(surf,&lightning,SDL_MapRGB(surf->format,fcr/2,fcg/2,fcb/2));
00073 
00074         const std::string text = text_.empty() ? str_cast(progress_) + "%" :
00075                                  text_ + " (" + str_cast(progress_) + "%)";
00076         SDL_Rect text_area = font::text_area(text,font::SIZE_NORMAL);
00077 
00078         text_area.x = area.x + area.w/2 - text_area.w/2;
00079         text_area.y = area.y + area.h/2 - text_area.h/2;
00080 
00081         font::draw_text(
00082             &video(),
00083             location(),
00084             font::SIZE_NORMAL,
00085             font::BLACK_COLOR,
00086             text,
00087             text_area.x,
00088             text_area.y
00089         );
00090 
00091         // Draw a white text section for the highlighted area
00092         // of the bar
00093         SDL_Rect selected_text_location = location();
00094         selected_text_location.w = inner_area.w;
00095         selected_text_location.h = inner_area.h;
00096         {
00097             clip_rect_setter clippy(surf, &selected_text_location);
00098             font::draw_text(
00099                 &video(),
00100                 selected_text_location,
00101                 font::SIZE_NORMAL,
00102                 selected_text_color,
00103                 text,
00104                 text_area.x,
00105                 text_area.y
00106             );
00107         }
00108     }
00109 
00110     update_rect(location());
00111 }
00112 
00113 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:15 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs