2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* progress_bar.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-04-29 00:56:43 +00:00
|
|
|
#include "progress_bar.h"
|
2020-09-03 11:22:16 +00:00
|
|
|
#include "scene/resources/text_line.h"
|
2014-04-29 00:56:43 +00:00
|
|
|
|
|
|
|
Size2 ProgressBar::get_minimum_size() const {
|
2020-03-12 12:37:40 +00:00
|
|
|
Ref<StyleBox> bg = get_theme_stylebox("bg");
|
|
|
|
Ref<StyleBox> fg = get_theme_stylebox("fg");
|
|
|
|
Ref<Font> font = get_theme_font("font");
|
2020-09-03 11:22:16 +00:00
|
|
|
int font_size = get_theme_font_size("font_size");
|
2014-04-29 00:56:43 +00:00
|
|
|
|
2018-03-28 17:52:55 +00:00
|
|
|
Size2 minimum_size = bg->get_minimum_size();
|
|
|
|
minimum_size.height = MAX(minimum_size.height, fg->get_minimum_size().height);
|
|
|
|
minimum_size.width = MAX(minimum_size.width, fg->get_minimum_size().width);
|
2019-02-27 13:56:49 +00:00
|
|
|
if (percent_visible) {
|
2020-09-03 11:22:16 +00:00
|
|
|
String txt = "100%";
|
|
|
|
TextLine tl = TextLine(txt, font, font_size);
|
|
|
|
minimum_size.height = MAX(minimum_size.height, bg->get_minimum_size().height + tl.get_size().y);
|
2019-02-27 13:56:49 +00:00
|
|
|
} else { // this is needed, else the progressbar will collapse
|
|
|
|
minimum_size.width = MAX(minimum_size.width, 1);
|
|
|
|
minimum_size.height = MAX(minimum_size.height, 1);
|
|
|
|
}
|
2018-03-28 17:52:55 +00:00
|
|
|
return minimum_size;
|
2014-04-29 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressBar::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
2020-03-12 12:37:40 +00:00
|
|
|
Ref<StyleBox> bg = get_theme_stylebox("bg");
|
|
|
|
Ref<StyleBox> fg = get_theme_stylebox("fg");
|
|
|
|
Ref<Font> font = get_theme_font("font");
|
2020-09-03 11:22:16 +00:00
|
|
|
int font_size = get_theme_font_size("font_size");
|
2020-03-12 12:37:40 +00:00
|
|
|
Color font_color = get_theme_color("font_color");
|
2014-04-29 00:56:43 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_style_box(bg, Rect2(Point2(), get_size()));
|
2017-01-13 17:08:30 +00:00
|
|
|
float r = get_as_ratio();
|
2014-04-29 00:56:43 +00:00
|
|
|
int mp = fg->get_minimum_size().width;
|
2019-07-31 04:47:11 +00:00
|
|
|
int p = r * (get_size().width - mp);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p > 0) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (is_layout_rtl()) {
|
|
|
|
draw_style_box(fg, Rect2(Point2(p, 0), Size2(fg->get_minimum_size().width, get_size().height)));
|
|
|
|
} else {
|
|
|
|
draw_style_box(fg, Rect2(Point2(0, 0), Size2(p + fg->get_minimum_size().width, get_size().height)));
|
|
|
|
}
|
2014-04-29 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (percent_visible) {
|
2020-09-03 11:22:16 +00:00
|
|
|
String txt = TS->format_number(itos(int(get_as_ratio() * 100))) + TS->percent_sign();
|
|
|
|
TextLine tl = TextLine(txt, font, font_size);
|
2020-12-17 11:22:04 +00:00
|
|
|
tl.draw(get_canvas_item(), (Point2(get_size().width - tl.get_size().x, get_size().height - tl.get_size().y) / 2).round(), font_color);
|
2014-04-29 00:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressBar::set_percent_visible(bool p_visible) {
|
2017-03-05 15:44:50 +00:00
|
|
|
percent_visible = p_visible;
|
2014-04-29 00:56:43 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ProgressBar::is_percent_visible() const {
|
2014-04-29 00:56:43 +00:00
|
|
|
return percent_visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressBar::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_percent_visible", "visible"), &ProgressBar::set_percent_visible);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_percent_visible"), &ProgressBar::is_percent_visible);
|
|
|
|
ADD_GROUP("Percent", "percent_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "percent_visible"), "set_percent_visible", "is_percent_visible");
|
2014-04-29 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ProgressBar::ProgressBar() {
|
|
|
|
set_v_size_flags(0);
|
2019-01-24 11:58:58 +00:00
|
|
|
set_step(0.01);
|
2017-03-05 15:44:50 +00:00
|
|
|
percent_visible = true;
|
2014-04-29 00:56:43 +00:00
|
|
|
}
|