2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* progress_dialog.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2019-01-03 11:41:35 +01:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01: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-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "progress_dialog.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "editor_scale.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "main/main.h"
|
|
|
|
#include "message_queue.h"
|
2015-12-21 13:05:49 +01:00
|
|
|
#include "os/os.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BackgroundProgress::_add_task(const String &p_task, const String &p_label, int p_steps) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
ERR_FAIL_COND(tasks.has(p_task));
|
|
|
|
Task t;
|
2017-03-05 16:44:50 +01:00
|
|
|
t.hb = memnew(HBoxContainer);
|
|
|
|
Label *l = memnew(Label);
|
|
|
|
l->set_text(p_label + " ");
|
2014-02-10 02:10:30 +01:00
|
|
|
t.hb->add_child(l);
|
2017-03-05 16:44:50 +01:00
|
|
|
t.progress = memnew(ProgressBar);
|
2014-02-10 02:10:30 +01:00
|
|
|
t.progress->set_max(p_steps);
|
2017-01-04 05:16:14 +01:00
|
|
|
t.progress->set_value(p_steps);
|
2017-03-05 16:44:50 +01:00
|
|
|
Control *ec = memnew(Control);
|
2014-02-10 02:10:30 +01:00
|
|
|
ec->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
ec->set_v_size_flags(SIZE_EXPAND_FILL);
|
2017-09-22 00:12:33 +02:00
|
|
|
t.progress->set_anchors_and_margins_preset(Control::PRESET_WIDE);
|
2014-02-10 02:10:30 +01:00
|
|
|
ec->add_child(t.progress);
|
2017-03-05 16:44:50 +01:00
|
|
|
ec->set_custom_minimum_size(Size2(80, 5) * EDSCALE);
|
2014-02-10 02:10:30 +01:00
|
|
|
t.hb->add_child(ec);
|
|
|
|
|
|
|
|
add_child(t.hb);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
tasks[p_task] = t;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundProgress::_update() {
|
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<String, int>::Element *E = updates.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (tasks.has(E->key())) {
|
2017-03-05 16:44:50 +01:00
|
|
|
_task_step(E->key(), E->get());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updates.clear();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BackgroundProgress::_task_step(const String &p_task, int p_step) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!tasks.has(p_task));
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Task &t = tasks[p_task];
|
|
|
|
if (p_step < 0)
|
|
|
|
t.progress->set_value(t.progress->get_value() + 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-01-04 05:16:14 +01:00
|
|
|
t.progress->set_value(p_step);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
void BackgroundProgress::_end_task(const String &p_task) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!tasks.has(p_task));
|
2017-03-05 16:44:50 +01:00
|
|
|
Task &t = tasks[p_task];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
memdelete(t.hb);
|
|
|
|
tasks.erase(p_task);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BackgroundProgress::_bind_methods() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method("_add_task", &BackgroundProgress::_add_task);
|
|
|
|
ClassDB::bind_method("_task_step", &BackgroundProgress::_task_step);
|
|
|
|
ClassDB::bind_method("_end_task", &BackgroundProgress::_end_task);
|
|
|
|
ClassDB::bind_method("_update", &BackgroundProgress::_update);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BackgroundProgress::add_task(const String &p_task, const String &p_label, int p_steps) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
MessageQueue::get_singleton()->push_call(this, "_add_task", p_task, p_label, p_steps);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
void BackgroundProgress::task_step(const String &p_task, int p_step) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//this code is weird, but it prevents deadlock.
|
|
|
|
bool no_updates;
|
|
|
|
{
|
|
|
|
_THREAD_SAFE_METHOD_
|
2017-03-05 16:44:50 +01:00
|
|
|
no_updates = updates.empty();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (no_updates)
|
2017-03-05 16:44:50 +01:00
|
|
|
MessageQueue::get_singleton()->push_call(this, "_update");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
_THREAD_SAFE_METHOD_
|
2017-03-05 16:44:50 +01:00
|
|
|
updates[p_task] = p_step;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void BackgroundProgress::end_task(const String &p_task) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
MessageQueue::get_singleton()->push_call(this, "_end_task", p_task);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ProgressDialog *ProgressDialog::singleton = NULL;
|
2016-03-12 14:44:12 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void ProgressDialog::_notification(int p_what) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
|
|
|
|
draw_style_box(style, Rect2(Point2(), get_size()));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressDialog::_popup() {
|
|
|
|
|
|
|
|
Size2 ms = main->get_combined_minimum_size();
|
2017-03-05 16:44:50 +01:00
|
|
|
ms.width = MAX(500 * EDSCALE, ms.width);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
|
|
|
|
ms += style->get_minimum_size();
|
2017-08-17 10:59:21 +02:00
|
|
|
main->set_margin(MARGIN_LEFT, style->get_margin(MARGIN_LEFT));
|
|
|
|
main->set_margin(MARGIN_RIGHT, -style->get_margin(MARGIN_RIGHT));
|
|
|
|
main->set_margin(MARGIN_TOP, style->get_margin(MARGIN_TOP));
|
|
|
|
main->set_margin(MARGIN_BOTTOM, -style->get_margin(MARGIN_BOTTOM));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
popup_centered(ms);
|
|
|
|
}
|
|
|
|
|
2017-12-14 12:59:46 +01:00
|
|
|
void ProgressDialog::add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(tasks.has(p_task));
|
|
|
|
Task t;
|
2017-03-05 16:44:50 +01:00
|
|
|
t.vb = memnew(VBoxContainer);
|
|
|
|
VBoxContainer *vb2 = memnew(VBoxContainer);
|
|
|
|
t.vb->add_margin_child(p_label, vb2);
|
|
|
|
t.progress = memnew(ProgressBar);
|
2014-02-10 02:10:30 +01:00
|
|
|
t.progress->set_max(p_steps);
|
2017-01-04 05:16:14 +01:00
|
|
|
t.progress->set_value(p_steps);
|
2014-02-10 02:10:30 +01:00
|
|
|
vb2->add_child(t.progress);
|
2017-03-05 16:44:50 +01:00
|
|
|
t.state = memnew(Label);
|
2014-11-06 01:20:42 +01:00
|
|
|
t.state->set_clip_text(true);
|
2014-02-10 02:10:30 +01:00
|
|
|
vb2->add_child(t.state);
|
|
|
|
main->add_child(t.vb);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
tasks[p_task] = t;
|
2017-12-14 12:59:46 +01:00
|
|
|
if (p_can_cancel) {
|
|
|
|
cancel_hb->show();
|
|
|
|
} else {
|
|
|
|
cancel_hb->hide();
|
|
|
|
}
|
|
|
|
cancel_hb->raise();
|
|
|
|
cancelled = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
_popup();
|
2017-12-19 13:55:01 +01:00
|
|
|
if (p_can_cancel) {
|
|
|
|
cancel->grab_focus();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 12:59:46 +01:00
|
|
|
bool ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-12-14 12:59:46 +01:00
|
|
|
ERR_FAIL_COND_V(!tasks.has(p_task), cancelled);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-12-21 13:05:49 +01:00
|
|
|
if (!p_force_redraw) {
|
|
|
|
uint64_t tus = OS::get_singleton()->get_ticks_usec();
|
2018-01-12 04:08:32 +01:00
|
|
|
if (tus - last_progress_tick < 200000) //200ms
|
2017-12-14 12:59:46 +01:00
|
|
|
return cancelled;
|
2015-12-21 13:05:49 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Task &t = tasks[p_task];
|
|
|
|
if (p_step < 0)
|
|
|
|
t.progress->set_value(t.progress->get_value() + 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-01-04 05:16:14 +01:00
|
|
|
t.progress->set_value(p_step);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
t.state->set_text(p_state);
|
2017-03-05 16:44:50 +01:00
|
|
|
last_progress_tick = OS::get_singleton()->get_ticks_usec();
|
2017-12-14 12:59:46 +01:00
|
|
|
if (cancel_hb->is_visible()) {
|
|
|
|
OS::get_singleton()->force_process_input();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
Main::iteration(); // this will not work on a lot of platforms, so it's only meant for the editor
|
2017-12-14 12:59:46 +01:00
|
|
|
return cancelled;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ProgressDialog::end_task(const String &p_task) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!tasks.has(p_task));
|
2017-03-05 16:44:50 +01:00
|
|
|
Task &t = tasks[p_task];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
memdelete(t.vb);
|
|
|
|
tasks.erase(p_task);
|
|
|
|
|
|
|
|
if (tasks.empty())
|
|
|
|
hide();
|
|
|
|
else
|
|
|
|
_popup();
|
|
|
|
}
|
|
|
|
|
2017-12-14 12:59:46 +01:00
|
|
|
void ProgressDialog::_cancel_pressed() {
|
|
|
|
cancelled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressDialog::_bind_methods() {
|
|
|
|
ClassDB::bind_method("_cancel_pressed", &ProgressDialog::_cancel_pressed);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
ProgressDialog::ProgressDialog() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
main = memnew(VBoxContainer);
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(main);
|
2017-09-22 00:12:33 +02:00
|
|
|
main->set_anchors_and_margins_preset(Control::PRESET_WIDE);
|
2014-02-10 02:10:30 +01:00
|
|
|
set_exclusive(true);
|
2017-03-05 16:44:50 +01:00
|
|
|
last_progress_tick = 0;
|
|
|
|
singleton = this;
|
2017-12-14 12:59:46 +01:00
|
|
|
cancel_hb = memnew(HBoxContainer);
|
|
|
|
main->add_child(cancel_hb);
|
|
|
|
cancel_hb->hide();
|
|
|
|
cancel = memnew(Button);
|
|
|
|
cancel_hb->add_spacer();
|
|
|
|
cancel_hb->add_child(cancel);
|
|
|
|
cancel->set_text(TTR("Cancel"));
|
|
|
|
cancel_hb->add_spacer();
|
|
|
|
cancel->connect("pressed", this, "_cancel_pressed");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|