2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* progress_dialog.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/os.h"
|
2023-04-20 15:13:21 +02:00
|
|
|
#include "editor/editor_interface.h"
|
2023-01-11 09:13:21 +01:00
|
|
|
#include "editor/editor_node.h"
|
2024-01-15 13:14:55 +01:00
|
|
|
#include "editor/themes/editor_scale.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "main/main.h"
|
2020-03-03 14:36:29 +01:00
|
|
|
#include "servers/display_server.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_
|
2019-09-25 10:28:50 +02:00
|
|
|
ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists.");
|
2018-10-24 22:20:05 +02:00
|
|
|
BackgroundProgress::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);
|
2022-03-19 01:02:57 +01:00
|
|
|
t.progress->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
|
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_
|
|
|
|
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<String, int> &E : updates) {
|
|
|
|
if (tasks.has(E.key)) {
|
|
|
|
_task_step(E.key, E.value);
|
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];
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_step < 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
t.progress->set_value(t.progress->get_value() + 1);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-01-04 05:16:14 +01:00
|
|
|
t.progress->set_value(p_step);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02: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::add_task(const String &p_task, const String &p_label, int p_steps) {
|
2023-12-18 15:46:56 +01:00
|
|
|
callable_mp(this, &BackgroundProgress::_add_task).call_deferred(p_task, p_label, p_steps);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02: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.
|
2018-10-24 22:20:05 +02:00
|
|
|
bool no_updates = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
{
|
|
|
|
_THREAD_SAFE_METHOD_
|
2020-12-15 13:04:21 +01:00
|
|
|
no_updates = updates.is_empty();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (no_updates) {
|
2023-12-18 15:46:56 +01:00
|
|
|
callable_mp(this, &BackgroundProgress::_update).call_deferred();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
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) {
|
2023-12-18 15:46:56 +01:00
|
|
|
callable_mp(this, &BackgroundProgress::_end_task).call_deferred(p_task);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
ProgressDialog *ProgressDialog::singleton = nullptr;
|
2016-03-12 14:44:12 +01:00
|
|
|
|
2024-06-07 08:26:12 +02:00
|
|
|
void ProgressDialog::_update_ui() {
|
|
|
|
// Run main loop for two frames.
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
DisplayServer::get_singleton()->process_events();
|
|
|
|
Main::iteration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
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
|
|
|
|
2024-05-14 15:50:53 +02:00
|
|
|
Ref<StyleBox> style = main->get_theme_stylebox(SceneStringName(panel), SNAME("PopupMenu"));
|
2017-03-05 16:44:50 +01:00
|
|
|
ms += style->get_minimum_size();
|
2023-05-29 17:00:09 +02:00
|
|
|
|
2020-12-22 17:24:29 +01:00
|
|
|
main->set_offset(SIDE_LEFT, style->get_margin(SIDE_LEFT));
|
|
|
|
main->set_offset(SIDE_RIGHT, -style->get_margin(SIDE_RIGHT));
|
|
|
|
main->set_offset(SIDE_TOP, style->get_margin(SIDE_TOP));
|
|
|
|
main->set_offset(SIDE_BOTTOM, -style->get_margin(SIDE_BOTTOM));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2024-06-07 08:26:12 +02:00
|
|
|
if (is_inside_tree()) {
|
|
|
|
Rect2i adjust = _popup_adjust_rect();
|
|
|
|
if (adjust != Rect2i()) {
|
|
|
|
set_position(adjust.position);
|
|
|
|
set_size(adjust.size);
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-13 22:05:02 +02:00
|
|
|
for (Window *window : host_windows) {
|
|
|
|
if (window->has_focus()) {
|
|
|
|
popup_exclusive_centered(window, ms);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No host window found, use main window.
|
2023-05-29 17:00:09 +02:00
|
|
|
EditorInterface::get_singleton()->popup_dialog_centered(this, ms);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2019-01-14 14:59:28 +01:00
|
|
|
if (MessageQueue::get_singleton()->is_flushing()) {
|
|
|
|
ERR_PRINT("Do not use progress dialog (task) while flushing the message queue or using call_deferred()!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-25 10:28:50 +02:00
|
|
|
ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists.");
|
2018-10-24 22:20:05 +02:00
|
|
|
ProgressDialog::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();
|
|
|
|
}
|
2022-04-10 20:52:55 +02:00
|
|
|
cancel_hb->move_to_front();
|
2023-01-21 12:25:29 +01:00
|
|
|
canceled = 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();
|
|
|
|
}
|
2024-06-07 08:26:12 +02:00
|
|
|
_update_ui();
|
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) {
|
2023-01-21 12:25:29 +01:00
|
|
|
ERR_FAIL_COND_V(!tasks.has(p_task), canceled);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2024-07-04 21:30:18 +02:00
|
|
|
Task &t = tasks[p_task];
|
2015-12-21 13:05:49 +01:00
|
|
|
if (!p_force_redraw) {
|
|
|
|
uint64_t tus = OS::get_singleton()->get_ticks_usec();
|
2024-07-04 21:30:18 +02:00
|
|
|
if (tus - t.last_progress_tick < 200000) { //200ms
|
2023-01-21 12:25:29 +01:00
|
|
|
return canceled;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-12-21 13:05:49 +01:00
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_step < 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
t.progress->set_value(t.progress->get_value() + 1);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-01-04 05:16:14 +01:00
|
|
|
t.progress->set_value(p_step);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
t.state->set_text(p_state);
|
2024-07-04 21:30:18 +02:00
|
|
|
t.last_progress_tick = OS::get_singleton()->get_ticks_usec();
|
2024-06-07 08:26:12 +02:00
|
|
|
_update_ui();
|
2019-04-27 14:06:16 +02:00
|
|
|
|
2023-01-21 12:25:29 +01:00
|
|
|
return canceled;
|
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);
|
|
|
|
|
2020-12-15 13:04:21 +01:00
|
|
|
if (tasks.is_empty()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
hide();
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
_popup();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2023-10-13 22:05:02 +02:00
|
|
|
void ProgressDialog::add_host_window(Window *p_window) {
|
|
|
|
ERR_FAIL_NULL(p_window);
|
|
|
|
host_windows.push_back(p_window);
|
|
|
|
}
|
|
|
|
|
2017-12-14 12:59:46 +01:00
|
|
|
void ProgressDialog::_cancel_pressed() {
|
2023-01-21 12:25:29 +01:00
|
|
|
canceled = true;
|
2017-12-14 12:59:46 +01:00
|
|
|
}
|
|
|
|
|
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);
|
2022-03-19 01:02:57 +01:00
|
|
|
main->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
|
2014-02-10 02:10:30 +01:00
|
|
|
set_exclusive(true);
|
2023-01-31 08:56:46 +01:00
|
|
|
set_flag(Window::FLAG_POPUP, false);
|
2017-03-05 16:44:50 +01:00
|
|
|
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();
|
2024-05-14 09:40:21 +02:00
|
|
|
cancel->connect(SceneStringName(pressed), callable_mp(this, &ProgressDialog::_cancel_pressed));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|