summaryrefslogtreecommitdiff
path: root/src/tasks/TaskWindow.cpp
blob: c705d36ececd7db424c9c0dae9daa1d5f9026310 (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
/*
 * TaskWindow.cpp
 *
 *  Created on: Apr 6, 2012
 *      Author: Simon
 */

#include "interface/Label.h"
#include "TaskWindow.h"
#include "Task.h"

TaskWindow::TaskWindow(std::string title_, Task * task_, bool closeOnDone):
	task(task_),
	title(title_),
	ui::Window(ui::Point(-1, -1), ui::Point(300, 200)),
	progress(0),
	done(false),
	closeOnDone(closeOnDone)
{

	ui::Label * tempLabel = new ui::Label(ui::Point(3, 3), ui::Point(Size.X-6, 16), title);
	AddComponent(tempLabel);

	statusLabel = new ui::Label(ui::Point(3, 19), ui::Point(Size.X-6, 16), "");
	AddComponent(statusLabel);

	ui::Engine::Ref().ShowWindow(this);

	task->SetTaskListener(this);
	task->Start();
}

void TaskWindow::NotifyStatus(Task * task)
{
	statusLabel->SetText(task->GetStatus());
}

void TaskWindow::NotifyDone(Task * task)
{
	if(closeOnDone)
		Exit();
}

void TaskWindow::Exit()
{
	if(ui::Engine::Ref().GetWindow()==this)
	{
		ui::Engine::Ref().CloseWindow();
		delete this;
	}
}

void TaskWindow::NotifyProgress(Task * task)
{
	progress = task->GetProgress();
}

void TaskWindow::OnTick(float dt)
{
	intermediatePos += 1.0f*dt;
	if(intermediatePos>100.0f)
		intermediatePos = 0.0f;
	task->Poll();
}

void TaskWindow::OnDraw()
{
	Graphics * g = ui::Engine::Ref().g;
	g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3);
	g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255);

	g->drawrect(Position.X + 20, Position.Y + 36, Size.X-40, 24, 255, 255, 255, 255);

	if(progress!=-1)
	{
		float size = float(Size.X-40)*(float(progress)/100.0f); // TIL...
		g->fillrect(Position.X + 20, Position.Y + 36, size, 24, 255, 255, 255, 255);
	} else {
		int size = 40, rsize = 0;
		float position = float(Size.X-40)*(intermediatePos/100.0f);
		if(position + size > Size.X-40)
		{
			size = (Size.X-40)-position;
			rsize = 40-size;
		}
		g->fillrect(Position.X + 20 + position, Position.Y + 36, size, 24, 255, 255, 255, 255);
		if(rsize)
		{
			g->fillrect(Position.X + 20, Position.Y + 36, rsize, 24, 255, 255, 255, 255);
		}
	}
}

TaskWindow::~TaskWindow() {
	delete task;
}