summaryrefslogtreecommitdiff
path: root/tools/editor/progress_dialog.cpp
blob: 5c667564fe6919bc1d6a5df181295c3e56d0f5ce (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*************************************************************************/
/*  progress_dialog.cpp                                                  */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2017 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.                */
/*************************************************************************/
#include "progress_dialog.h"
#include "main/main.h"
#include "message_queue.h"
#include "os/os.h"
#include "editor_scale.h"
void BackgroundProgress::_add_task(const String& p_task,const String& p_label, int p_steps) {

	_THREAD_SAFE_METHOD_
	ERR_FAIL_COND(tasks.has(p_task));
	Task t;
	t.hb = memnew( HBoxContainer );
	Label *l=memnew( Label );
	l->set_text(p_label+" ");
	t.hb->add_child(l);
	t.progress = memnew( ProgressBar );
	t.progress->set_max(p_steps);
	t.progress->set_val(p_steps);
	Control *ec = memnew( Control );
	ec->set_h_size_flags(SIZE_EXPAND_FILL);
	ec->set_v_size_flags(SIZE_EXPAND_FILL);
	t.progress->set_area_as_parent_rect();
	ec->add_child(t.progress);
	ec->set_custom_minimum_size(Size2(80,5)*EDSCALE);
	t.hb->add_child(ec);

	add_child(t.hb);

	tasks[p_task]=t;

}

void BackgroundProgress::_update() {

	_THREAD_SAFE_METHOD_

	for (Map<String,int>::Element *E=updates.front();E;E=E->next()) {

		if (tasks.has(E->key())) {
			_task_step(E->key(),E->get());
		}
	}

	updates.clear();



}


void BackgroundProgress::_task_step(const String& p_task, int p_step){

	_THREAD_SAFE_METHOD_

	ERR_FAIL_COND(!tasks.has(p_task));

	Task &t=tasks[p_task];
	if (p_step<0)
		t.progress->set_val(t.progress->get_val()+1);
	else
		t.progress->set_val(p_step);

}
void BackgroundProgress::_end_task(const String& p_task){

	_THREAD_SAFE_METHOD_

	ERR_FAIL_COND(!tasks.has(p_task));
	Task &t=tasks[p_task];

	memdelete(t.hb);
	tasks.erase(p_task);
}

void BackgroundProgress::_bind_methods(){

	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);

}

void BackgroundProgress::add_task(const String& p_task,const String& p_label, int p_steps){

	MessageQueue::get_singleton()->push_call(this,"_add_task",p_task,p_label,p_steps);
}
void BackgroundProgress::task_step(const String& p_task, int p_step){

	//this code is weird, but it prevents deadlock.
	bool no_updates;
	{
		_THREAD_SAFE_METHOD_
		no_updates=updates.empty();
	}

	if (no_updates)
		MessageQueue::get_singleton()->push_call(this,"_update");

	{
		_THREAD_SAFE_METHOD_
		updates[p_task]=p_step;
	}

}

void BackgroundProgress::end_task(const String& p_task){

	MessageQueue::get_singleton()->push_call(this,"_end_task",p_task);

}


////////////////////////////////////////////////


ProgressDialog *ProgressDialog::singleton=NULL;

void ProgressDialog::_notification(int p_what) {

	switch(p_what) {

		case NOTIFICATION_DRAW: {

			Ref<StyleBox> style = get_stylebox("panel","PopupMenu");
			draw_style_box(style,Rect2(Point2(),get_size()));

		} break;
	}
}


void ProgressDialog::_popup() {

	Size2 ms = main->get_combined_minimum_size();
	ms.width = MAX(500*EDSCALE,ms.width);


	Ref<StyleBox> style = get_stylebox("panel","PopupMenu");
	ms+=style->get_minimum_size();
	for(int i=0;i<4;i++) {
		main->set_margin(Margin(i),style->get_margin(Margin(i)));
	}

	popup_centered(ms);

}

void ProgressDialog::add_task(const String& p_task,const String& p_label,int p_steps) {

	ERR_FAIL_COND(tasks.has(p_task));
	Task t;
	t.vb = memnew( VBoxContainer );
	VBoxContainer *vb2 = memnew( VBoxContainer );
	t.vb->add_margin_child(p_label,vb2);
	t.progress = memnew( ProgressBar );
	t.progress->set_max(p_steps);
	t.progress->set_val(p_steps);
	vb2->add_child(t.progress);
	t.state=memnew( Label );
	t.state->set_clip_text(true);
	vb2->add_child(t.state);
	main->add_child(t.vb);

	tasks[p_task]=t;
	_popup();

}

void ProgressDialog::task_step(const String& p_task, const String& p_state, int p_step,bool p_force_redraw){

	ERR_FAIL_COND(!tasks.has(p_task));

	if (!p_force_redraw) {
		uint64_t tus = OS::get_singleton()->get_ticks_usec();
		if (tus-last_progress_tick < 50000) //50ms
			return;
	}

	Task &t=tasks[p_task];
	if (p_step<0)
		t.progress->set_val(t.progress->get_val()+1);
	else
		t.progress->set_val(p_step);

	t.state->set_text(p_state);
	last_progress_tick=OS::get_singleton()->get_ticks_usec();
	Main::iteration(); // this will not work on a lot of platforms, so it's only meant for the editor

}


void ProgressDialog::end_task(const String& p_task){

	ERR_FAIL_COND(!tasks.has(p_task));
	Task &t=tasks[p_task];

	memdelete(t.vb);
	tasks.erase(p_task);

	if (tasks.empty())
		hide();
	else
		_popup();
}



ProgressDialog::ProgressDialog() {

	main = memnew( VBoxContainer );
	add_child(main);
	main->set_area_as_parent_rect();
	set_exclusive(true);
	last_progress_tick=0;
	singleton=this;
}