The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
progressbar.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "global.hpp"
18 
19 #include "widgets/progressbar.hpp"
20 
21 #include "font.hpp"
22 #include "marked-up_text.hpp"
23 #include "sdl/rect.hpp"
24 #include "video.hpp"
25 
26 namespace gui {
27 
28 progress_bar::progress_bar(CVideo& video) : widget(video), progress_(0)
29 {}
30 
32 {
33  progress_ = progress;
34  set_dirty();
35 }
36 
38 {
39  text_ = text;
40  set_dirty();
41 }
42 
44 {
46  SDL_Rect area = location();
47 
48  if(area.w >= 2 && area.h >= 2) {
49  int fcr = 21, fcg = 53, fcb = 80; // RGB-values for finished piece.
50  int bcr = 0, bcg = 0, bcb = 0; // Border color.
51  int gcr = 255, gcg = 255, gcb = 255; // Groove color.
52  int lightning_thickness = 2;
53  static const SDL_Color selected_text_color = {0xCC,0xCC,0xCC,0};
54 
55  SDL_Rect inner_area = sdl::create_rect(area.x + 1
56  , area.y + 1
57  , area.w - 2
58  , area.h - 2);
59 
60  sdl::fill_rect(surf,&area,SDL_MapRGB(surf->format,bcr,bcg,bcb));
61  sdl::fill_rect(surf,&inner_area,SDL_MapRGB(surf->format,gcr,gcg,gcb));
62 
63  inner_area.w = (inner_area.w*progress_)/100;
64  sdl::fill_rect(surf,&inner_area,SDL_MapRGB(surf->format,fcr,fcg,fcb));
65 
66  SDL_Rect lightning = inner_area;
67  lightning.h = lightning_thickness;
68  //we add 25% of white to the color of the bar to simulate a light effect
69  sdl::fill_rect(surf,&lightning,SDL_MapRGB(surf->format,(fcr*3+255)/4,(fcg*3+255)/4,(fcb*3+255)/4));
70  lightning.y = inner_area.y+inner_area.h-lightning.h;
71  //remove 50% of color to simulate a shadow effect
72  sdl::fill_rect(surf,&lightning,SDL_MapRGB(surf->format,fcr/2,fcg/2,fcb/2));
73 
74  const std::string text = text_.empty() ? std::to_string(progress_) + "%" :
75  text_ + " (" + std::to_string(progress_) + "%)";
77 
78  text_area.x = area.x + area.w/2 - text_area.w/2;
79  text_area.y = area.y + area.h/2 - text_area.h/2;
80 
82  &video(),
83  location(),
86  text,
87  text_area.x,
88  text_area.y
89  );
90 
91  // Draw a white text section for the highlighted area
92  // of the bar
93  SDL_Rect selected_text_location = location();
94  selected_text_location.w = inner_area.w;
95  selected_text_location.h = inner_area.h;
96  {
97  clip_rect_setter clippy(surf, &selected_text_location);
99  &video(),
100  selected_text_location,
102  selected_text_color,
103  text,
104  text_area.x,
105  text_area.y
106  );
107  }
108  }
109 
111 }
112 
113 }
SDL_Rect text_area(const std::string &text, int size, int style)
Calculate the size of a text (in pixels) if it were to be drawn.
void fill_rect(surface &dst, SDL_Rect *dst_rect, const Uint32 color)
Fill a rectangle on a given surface.
Definition: rect.hpp:143
const SDL_Color BLACK_COLOR
Definition: font.cpp:569
Definition: video.hpp:58
void set_text(const std::string &text)
Definition: progressbar.cpp:37
CVideo & video() const
Definition: widget.hpp:83
General purpose widgets.
-file util.hpp
surface & getSurface()
Definition: dummy_video.cpp:22
const int SIZE_NORMAL
Definition: font.hpp:58
void set_dirty(bool dirty=true)
Definition: widget.cpp:217
surf
Definition: filter.cpp:143
SDL_Rect draw_text(surface &dst, const SDL_Rect &area, int size, const SDL_Color &color, const std::string &txt, int x, int y, bool use_tooltips, int style)
Function to draw text on a surface.
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an empty SDL_Rect.
Definition: rect.cpp:28
Contains the SDL_Rect helper code.
std::string text_
Definition: progressbar.hpp:34
progress_bar(CVideo &video)
Definition: progressbar.cpp:28
SDL_Rect const & location() const
Definition: widget.cpp:144
void set_progress_percent(int progress)
Definition: progressbar.cpp:31
void update_rect(const SDL_Rect &)
Definition: dummy_video.cpp:27
GLsizei const GLcharARB ** string
Definition: glew.h:4503