-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
progressbar.cpp
113 lines (91 loc) · 3.07 KB
/
progressbar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright (C) 2003 - 2014 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "global.hpp"
#include "widgets/progressbar.hpp"
#include "font.hpp"
#include "marked-up_text.hpp"
#include "sdl/rect.hpp"
#include "video.hpp"
namespace gui {
progress_bar::progress_bar(CVideo& video) : widget(video), progress_(0)
{}
void progress_bar::set_progress_percent(int progress)
{
progress_ = progress;
set_dirty();
}
void progress_bar::set_text(const std::string& text)
{
text_ = text;
set_dirty();
}
void progress_bar::draw_contents()
{
surface surf = video().getSurface();
SDL_Rect area = location();
if(area.w >= 2 && area.h >= 2) {
int fcr = 21, fcg = 53, fcb = 80; // RGB-values for finished piece.
int bcr = 0, bcg = 0, bcb = 0; // Border color.
int gcr = 255, gcg = 255, gcb = 255; // Groove color.
int lightning_thickness = 2;
static const SDL_Color selected_text_color = {0xCC,0xCC,0xCC,0};
SDL_Rect inner_area = sdl::create_rect(area.x + 1
, area.y + 1
, area.w - 2
, area.h - 2);
sdl::fill_rect(surf,&area,SDL_MapRGB(surf->format,bcr,bcg,bcb));
sdl::fill_rect(surf,&inner_area,SDL_MapRGB(surf->format,gcr,gcg,gcb));
inner_area.w = (inner_area.w*progress_)/100;
sdl::fill_rect(surf,&inner_area,SDL_MapRGB(surf->format,fcr,fcg,fcb));
SDL_Rect lightning = inner_area;
lightning.h = lightning_thickness;
//we add 25% of white to the color of the bar to simulate a light effect
sdl::fill_rect(surf,&lightning,SDL_MapRGB(surf->format,(fcr*3+255)/4,(fcg*3+255)/4,(fcb*3+255)/4));
lightning.y = inner_area.y+inner_area.h-lightning.h;
//remove 50% of color to simulate a shadow effect
sdl::fill_rect(surf,&lightning,SDL_MapRGB(surf->format,fcr/2,fcg/2,fcb/2));
const std::string text = text_.empty() ? str_cast(progress_) + "%" :
text_ + " (" + str_cast(progress_) + "%)";
SDL_Rect text_area = font::text_area(text,font::SIZE_NORMAL);
text_area.x = area.x + area.w/2 - text_area.w/2;
text_area.y = area.y + area.h/2 - text_area.h/2;
font::draw_text(
&video(),
location(),
font::SIZE_NORMAL,
font::BLACK_COLOR,
text,
text_area.x,
text_area.y
);
// Draw a white text section for the highlighted area
// of the bar
SDL_Rect selected_text_location = location();
selected_text_location.w = inner_area.w;
selected_text_location.h = inner_area.h;
{
clip_rect_setter clippy(surf, &selected_text_location);
font::draw_text(
&video(),
selected_text_location,
font::SIZE_NORMAL,
selected_text_color,
text,
text_area.x,
text_area.y
);
}
}
update_rect(location());
}
}