Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 6878b28c92a10928c7ac54eafe4ceea5a899d608 (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
/*
   entitywidget.cc
   This file is part of the Project::OSiRiON world editor 
   and is distributed under the terms and conditions of 
   the GNU General Public License version 2
*/

#include <entitywidget.h>

#include <QPainter>
#include <QMouseEvent>
#include <QDebug>

#include <cmath>

namespace editor
{

EntityWidget::EntityWidget(QWidget *parent) : QWidget(parent)
{
	is_selected = false;
	is_dragging = false;
}

EntityWidget::EntityWidget(EntityWidget &entity_widget, QWidget *parent) : QWidget(parent)
{
	is_selected = false;
	is_dragging = false;
	
	assign(entity_widget);
}

void EntityWidget::set_selected(const bool selected)
{
	is_selected = selected;
	update();
}

void EntityWidget::assign(EntityWidget &other)
{
	EntityProperties properties = *(other.properties());
	entitywidget_entityproperties.assign(properties);
}

void EntityWidget::paintEvent(QPaintEvent *event)
{	
	QPen pen(Qt::black, 1, Qt::SolidLine);
	QPainter painter(this);
	
	if (is_selected) {
		pen.setColor(Qt::red);
		painter.setPen(pen);
		
		painter.setClipping(false);
	} else {
		painter.setClipping(true);
	}
	
	painter.setPen(pen);
	painter.drawEllipse(0, 0, width() - 1 , height() - 1);
	
	float r = width() / 2;
	if (r < 12)
		r = 12;

	/*	
	float dx = r * sin(entitywidget_entityproperties.yaw());
	float dy = r * cos(entitywidget_entityproperties.yaw());
	
	if (!is_selected) {
		pen.setColor(Qt::yellow);
	}
	painter.setPen(pen);
	
	painter.drawLine(width() /2, height() /2, width() /2 - dx, height() /2 - dy);
	*/
}

void EntityWidget::mousePressEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton) {
		//qDebug() << "clicked entity " << name();
		event->accept();
		if (is_selected) {
			is_dragging = true;
			emit dragStart();
		}
		emit clicked(this);
	} else {
		event->ignore();
		is_dragging = false;
	}
}

void EntityWidget::mouseReleaseEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton) {
		if (is_dragging)
			emit dragStop();
		is_dragging = false;
	}
}

void EntityWidget::mouseMoveEvent(QMouseEvent *event)
{
	if (is_dragging) {
		emit dragMove(this, event->pos().x(), event->pos().y());
	}
}

}